Skip to content

Commit

Permalink
[all] Create editorials tab, made changes to query for retrieving edi…
Browse files Browse the repository at this point in the history
…torials
  • Loading branch information
MananGandhi1810 committed Oct 31, 2024
1 parent 13ab4ea commit 2b14255
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 6 deletions.
11 changes: 7 additions & 4 deletions backend/handlers/editorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ const getEditorials = async (req, res) => {
problemStatementId,
hidden: false,
},
select: {
id: true,
title: true,
createdAt: true,
include: {
user: {
select: {
id: true,
name: true,
},
},
},
});
res.json({
Expand Down
129 changes: 129 additions & 0 deletions frontend/src/components/custom/Editorials.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import React, { useCallback, useEffect, useState } from "react";
import { ScrollArea } from "../ui/scroll-area";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "../ui/table";
import { ArrowLeft, Edit } from "lucide-react";
import Markdown from "react-markdown";
import { Button } from "../ui/button";

function Editorials({
editorials,
selectedEditorialId,
setSelectedEditorialId,
}) {
if (editorials.length == 0) {
return (
<div className="flex h-full-w-nav-w-tab w-full justify-center items-center flex-col gap-2">
No Editorials Found
</div>
);
}

const formatter = new Intl.DateTimeFormat("en-IN", {
day: "2-digit",
month: "2-digit",
year: "numeric",
});
const [selectedEditorial, setSelectedEditorial] = useState(null);

useEffect(() => {
setSelectedEditorial(
editorials.find((editorial) => editorial.id == selectedEditorialId),
);
}, [selectedEditorialId]);

if (selectedEditorialId != "") {
console.log(selectedEditorial);
if (selectedEditorial == undefined) {
return (
<div className="flex h-full-w-nav-w-tab w-full justify-center items-center flex-col gap-2">
Could not load editorial
<div
className="flex flex-row gap-2 cursor-pointer hover:underline pt-2 w-fit"
onClick={() => {
setSelectedEditorialId("");
setSelectedEditorial(null);
}}
>
<ArrowLeft /> View Other Editorials
</div>
</div>
);
}

return (
<div className="flex h-full-w-nav-w-tab w-full flex-col">
<ScrollArea className="h-full-w-nav-w-tab flex flex-col p-6 py-0 overflow-auto">
<div
className="flex flex-row gap-2 cursor-pointer hover:underline pt-2 w-fit"
onClick={() => setSelectedEditorialId("")}
>
<ArrowLeft /> View Other Editorials
</div>
<p className="text-2xl font-bold pt-4">
{selectedEditorial.title}
</p>
<Markdown>{selectedEditorial.content}</Markdown>
</ScrollArea>
</div>
);
}

return (
<div className="flex h-full-w-nav-w-tab w-full flex-col">
<ScrollArea className="h-full-w-nav-w-tab flex flex-col p-6 py-0 overflow-auto">
<Button
variant="outline"
className="py-2 flex flex-row gap-2 m-2"
>
<Edit />
Write an editorial
</Button>
<Table>
<TableHeader>
<TableRow>
<TableHead>Editorial</TableHead>
<TableHead className="w-32">Written On</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{editorials.map((editorial) => {
const date = new Date(editorial.createdAt);
const formattedDate = formatter.format(date);

return (
<TableRow
key={editorial.id}
className="group hover:cursor-pointer"
onClick={() =>
setSelectedEditorialId(
`${editorial.id}`,
)
}
>
<TableCell className="overflow-ellipsis overflow-hidden w-full flex flex-col items-start justify-items-center">
<p className="text-lg group-hover:underline">
{editorial.title}
</p>
<p className="text-md">
By {editorial.user.name}
</p>
</TableCell>
<TableCell>{formattedDate}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</ScrollArea>
</div>
);
}

export default Editorials;
42 changes: 40 additions & 2 deletions frontend/src/pages/Code.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@/components/ui/resizable.jsx";
import { Button } from "@/components/ui/button.jsx";
import Editor from "@monaco-editor/react";
import { Loader2, Play, Send, Upload } from "lucide-react";
import { Loader2, Play, Upload } from "lucide-react";
import axios from "axios";
import AuthContext from "@/context/auth-provider.jsx";
import {
Expand Down Expand Up @@ -40,8 +40,8 @@ import {
TooltipTrigger,
} from "@/components/ui/tooltip";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Textarea } from "@/components/ui/textarea";
import AIChat from "@/components/custom/AIChat";
import Editorials from "@/components/custom/Editorials";

function Code() {
const problemStatement = useLoaderData();
Expand Down Expand Up @@ -88,6 +88,12 @@ function Code() {
const [aiInput, setAiInput] = useState("");
const [currentResponse, setCurrentResponse] = useState("");
const [tabValue, setTabValue] = useState("testcases");
const [editorials, setEditorials] = useState([]);
const [selectedEditorialId, setSelectedEditorialId] = useState("");

useEffect(() => {
getEditorials();
}, []);

useEffect(() => {
if (!Object.values(supportedLanguages).includes(language)) {
Expand Down Expand Up @@ -325,6 +331,21 @@ function Code() {
} catch (e) {}
};

const getEditorials = async () => {
const res = await axios
.get(`${process.env.SERVER_URL}/editorial/${id}`, {
headers: {
Authorization: `Bearer ${user.token}`,
},
validateStatus: false,
})
.then((res) => res.data);
if (!res.success) {
return;
}
setEditorials(res.data.editorials);
};

return (
<div className="w-screen h-full-w-nav">
<AlertDialog open={showDialog} onOpenChange={setShowDialog}>
Expand Down Expand Up @@ -363,6 +384,12 @@ function Code() {
<TabsTrigger value="ai" className="m-0.5">
Ask AI
</TabsTrigger>
<TabsTrigger
value="editorials"
className="m-0.5"
>
Editorials
</TabsTrigger>
</TabsList>
<TabsContent value="problem-statement">
<ScrollArea className="flex h-full w-full flex-col gap-5 pb-14">
Expand All @@ -380,6 +407,17 @@ function Code() {
setAiInput={setAiInput}
/>
</TabsContent>
<TabsContent value="editorials">
<Editorials
editorials={editorials}
selectedEditorialId={
selectedEditorialId
}
setSelectedEditorialId={
setSelectedEditorialId
}
/>
</TabsContent>
</Tabs>
</ResizablePanel>
</ResizablePanelGroup>
Expand Down

0 comments on commit 2b14255

Please sign in to comment.