Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rachel2 #800

Open
wants to merge 3 commits into
base: rachel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/backend/src/modules/course/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getCourse,
getCourses,
} from "./controller";
import { IntermediateCourse, formatComment } from "./formatter";
import { IntermediateCourse } from "./formatter";
import { CourseModule } from "./generated-types/module-types";

const resolvers: CourseModule.Resolvers = {
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "serve -s dist -p 3000"
},
"dependencies": {
"@apollo/client": "3.12.7",
"@apollo/client": "^3.12.7",
"@floating-ui/dom": "^1.6.13",
"@mapbox/mapbox-gl-directions": "^4.3.1",
"@radix-ui/react-checkbox": "^1.1.3",
Expand Down
8 changes: 8 additions & 0 deletions apps/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const Class = {
Grades: lazy(() => import("@/components/Class/Grades")),
Overview: lazy(() => import("@/components/Class/Overview")),
Sections: lazy(() => import("@/components/Class/Sections")),
// added comment
Comments: lazy(() => import("@/components/Class/Comments")),
};

const Course = {
Expand All @@ -30,6 +32,7 @@ const Course = {
Grades: lazy(() => import("@/components/Course/Grades")),
Overview: lazy(() => import("@/components/Course/Overview")),
Classes: lazy(() => import("@/components/Course/Classes")),
Comments: lazy(() => import("@/components/Class/Comments")),
};

const Catalog = lazy(() => import("@/app/Catalog"));
Expand Down Expand Up @@ -130,6 +133,7 @@ const router = createBrowserRouter([
element: <Course.Grades />,
path: "grades",
},

{
path: "*",
loader: () => redirect("."),
Expand All @@ -156,6 +160,10 @@ const router = createBrowserRouter([
element: <Class.Grades />,
path: "grades",
},
{
element: <Course.Comments />,
path: "comments",
},
{
path: "*",
loader: () => redirect("."),
Expand Down
38 changes: 38 additions & 0 deletions apps/frontend/src/components/Class/Comments/Comments.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.root {
--foreground-color: #000; /* Default for light mode */
padding: 24px 0;
font-size: 14px;
display: flex;
flex-direction: column;
gap: 8px;
.input {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
}

.button {
padding: 8px 12px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.2s ease-in-out;
}

.button:hover {
background-color: #0056b3;
}

.li {
color: var(--foreground-color);
}
}

[data-theme="dark"] {
--foreground-color: #fff; /* Dark mode */
}
80 changes: 80 additions & 0 deletions apps/frontend/src/components/Class/Comments/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { useEffect, useState } from "react";

import { useAddComment } from "@/hooks/api/classes/useAddComment";
import { useGetComments } from "@/hooks/api/classes/useGetComments";
import useClass from "@/hooks/useClass";

import styles from "./Comments.module.scss";

export default function Comments() {
const { class: _class } = useClass();
const [value, setValue] = useState("");

// Fetch comments for the class
const { data: comments } = useGetComments(
_class.subject,
_class.courseNumber
);

// Mutation for adding a new comment
const [addComment] = useAddComment();

useEffect(() => {
if (comments) {
console.log("Fetched Comments:", comments);
}
}, [comments]);

const userEmail = "[email protected]"; // Replace with actual user email

const handleSubmit = async () => {
if (value.trim() !== "") {
try {
await addComment(_class.subject, _class.courseNumber, userEmail, value); // Pass arguments directly
setValue(""); // Clear input after submission
} catch (error) {
console.error("Error submitting comment:", error);
}
}
};

return (
<div className={styles.root}>
<label htmlFor="text-input">Make a new comment:</label>
<input
id="text-input"
type="text"
value={value}
onChange={(e) => setValue(e.target.value)}
className={styles.input}
placeholder="Say anything..."
/>
<button onClick={handleSubmit} className={styles.button}>
Submit
</button>

{comments && comments.length > 0 ? (
<ul>
{comments.map((comment, index) => (
<li key={index} className={styles.li}>
<p>
<strong>{comment.userEmail}</strong>: {comment.content}
</p>
<p
style={{
fontSize: "12px",
color: "gray",
marginBottom: "12px",
}}
>
{new Date(comment.createdAt).toLocaleString()}
</p>
</li>
))}
</ul>
) : (
<p>No comments yet.</p>
)}
</div>
);
}
1 change: 1 addition & 0 deletions apps/frontend/src/components/Class/Overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import styles from "./Overview.module.scss";

export default function Overview() {
const { class: _class } = useClass();
console.log(_class);

return (
<div className={styles.root}>
Expand Down
18 changes: 18 additions & 0 deletions apps/frontend/src/components/Class/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ const Enrollment = lazy(() => import("./Enrollment"));
const Grades = lazy(() => import("./Grades"));
const Overview = lazy(() => import("./Overview"));
const Sections = lazy(() => import("./Sections"));
const Comments = lazy(() => import("./Comments"));

interface BodyProps {
children: ReactNode;
dialog?: boolean;
}

function Body({ children, dialog }: BodyProps) {
console.log(children);
return (
<Suspense
fallback={
Expand Down Expand Up @@ -136,6 +138,7 @@ export default function Class({
onClose,
dialog,
}: ClassProps) {
console.log("DIALOG", dialog);
const { pins, addPin, removePin } = usePins();
const location = useLocation();

Expand Down Expand Up @@ -405,6 +408,11 @@ export default function Class({
<Tabs.Trigger value="grades" asChild>
<MenuItem>Grades</MenuItem>
</Tabs.Trigger>

{/* Created a new Comments MenuItem */}
<Tabs.Trigger value="comments" asChild>
<MenuItem>Comments</MenuItem>
</Tabs.Trigger>
</Tabs.List>
) : (
<div className={styles.menu}>
Expand All @@ -428,6 +436,12 @@ export default function Class({
<MenuItem active={isActive}>Grades</MenuItem>
)}
</NavLink>
{/* Make a link */}
<NavLink to={{ ...location, pathname: "comments" }}>
{({ isActive }) => (
<MenuItem active={isActive}>Comments</MenuItem>
)}
</NavLink>
</div>
)}
</Container>
Expand All @@ -451,6 +465,10 @@ export default function Class({
<Tabs.Content value="grades" asChild>
<Grades />
</Tabs.Content>
{/* Add Comments component to body */}
<Tabs.Content value="comments" asChild>
<Comments />
</Tabs.Content>
</Body>
</ClassContext>
</Root>
Expand Down
46 changes: 46 additions & 0 deletions apps/frontend/src/hooks/api/classes/useAddComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useCallback } from "react";

import { MutationHookOptions, useMutation } from "@apollo/client";

import { ADD_COMMENT, CreateCommentResponse } from "@/lib/api";

export const useAddComment = () => {
const mutation = useMutation<CreateCommentResponse>(ADD_COMMENT, {
update(cache, { data }) {
const newComment = data?.addComment;

if (!newComment) return;

cache.modify({
fields: {
comments(existingComments = []) {
return [...existingComments, newComment];
},
},
});
},
});

const addComment = useCallback(
async (
subject: string,
number: string,
userEmail: string,
content: string,
options?: Omit<MutationHookOptions<CreateCommentResponse>, "variables">
) => {
const mutate = mutation[0];

return await mutate({
...options,
variables: { subject, number, userEmail, content },
});
},
[mutation]
);

return [addComment, mutation[1]] as [
mutate: typeof addComment,
result: (typeof mutation)[1],
];
};
24 changes: 24 additions & 0 deletions apps/frontend/src/hooks/api/classes/useGetComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { QueryHookOptions, useQuery } from "@apollo/client";

import { GET_COMMENTS, getCommentsResponse } from "@/lib/api";

export const useGetComments = (
subject: string,
number: string,
userEmail?: string | null,
options?: Omit<QueryHookOptions<getCommentsResponse>, "variables">
) => {
const query = useQuery<getCommentsResponse>(GET_COMMENTS, {
...options,
variables: {
subject,
number,
userEmail: userEmail || undefined,
},
});

return {
...query,
data: query.data?.comments, // Return the comments array
};
};
53 changes: 53 additions & 0 deletions apps/frontend/src/lib/api/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,56 @@ export const GET_CATALOG = gql`
}
}
`;

export interface Comment {
subject: string;
number: string;
userEmail: string;
content: string;
createdAt: string;
}
export interface getCommentsResponse {
comments: Comment[];
}

export const GET_COMMENTS = gql`
query GetComments(
$subject: String!
$number: CourseNumber!
$userEmail: String
) {
comments(subject: $subject, number: $number, userEmail: $userEmail) {
subject
number
userEmail
content
createdAt
}
}
`;

export interface CreateCommentResponse {
addComment: Comment;
}

export const ADD_COMMENT = gql`
mutation addComment(
$subject: String!
$number: CourseNumber!
$userEmail: String!
$content: String!
) {
addComment(
subject: $subject
number: $number
userEmail: $userEmail
content: $content
) {
subject
number
userEmail
content
createdAt
}
}
`;
1 change: 1 addition & 0 deletions apps/frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default defineConfig({
server: {
host: true,
port: 3000,
allowedHosts: ["localhost", "frontend"],
},
resolve: {
alias: {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.