Skip to content

Commit c862bed

Browse files
authored
Merge pull request #248 from CS3219-AY2425S1/persist-language-change
Persist language changes
2 parents c93470e + 533b3c4 commit c862bed

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

backend/collaboration/src/routes/collaborationRoutes.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,33 @@ router.post(
182182
}
183183
);
184184

185+
// Update a session language by ID
186+
router.post(
187+
"/:id/updateLanguage",
188+
async (req: Request, res: Response) => {
189+
const { id } = req.params;
190+
191+
const updateSession: Partial<TSession> = {};
192+
193+
updateSession.language = req.body.language;
194+
195+
try {
196+
const session = await Session.findOne({ collabid: id }).exec();
197+
if (!session) {
198+
return res.status(404).json({ message: "Session not found" });
199+
}
200+
201+
const updatedSession = await Session.findOneAndUpdate(
202+
{ collabid: id },
203+
{ $set: updateSession }
204+
);
205+
res.status(200).json(updatedSession);
206+
} catch (error) {
207+
return res.status(500).send("Internal server error");
208+
}
209+
}
210+
);
211+
185212
// Delete a session by ID
186213
router.delete(
187214
"/:id",

frontend/src/api/collaboration.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ export const updateSession = async (
7272
return resp.json();
7373
};
7474

75+
export const updateSessionLanguage = async (
76+
collabid: string,
77+
language: string
78+
): Promise<SessionFull> => {
79+
const url = `${COLLABORATION_SERVICE}/${collabid}/updateLanguage`;
80+
const resp = await fetch(url, {
81+
method: "POST",
82+
headers: {
83+
"Content-Type": "application/json",
84+
},
85+
body: JSON.stringify({
86+
language: language
87+
}),
88+
});
89+
90+
if (!resp.ok) {
91+
throw new Error("Failed to update the session");
92+
}
93+
94+
return resp.json();
95+
};
96+
7597
export const deleteSessionById = async (
7698
collabid: string
7799
): Promise<SessionFull> => {

frontend/src/app/(user)/dashboard/components/DashboardDataTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const columns: ColumnDef<TCombinedSession>[] = [
6666
},
6767
{
6868
accessorKey: "language",
69-
header: () => <Cell>Language</Cell>,
69+
header: () => <Cell>Matched Language</Cell>,
7070
cell: ({ row }) => {
7171
const language = row.getValue("language") as string;
7272
return (

frontend/src/app/collaboration/components/editor.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ function Collaboration({ room, language, code, setLanguage }: Readonly<Props>) {
208208
theme={editorTheme}
209209
setTheme={setEditorTheme}
210210
saving={saving}
211+
room={room}
211212
peerOnline={peerOnline}
212213
/>
213214
<div className="w-full h-[1px] mx-auto my-2"></div>

frontend/src/app/collaboration/components/toolbar.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import MoonLoader from "react-spinners/MoonLoader";
77
import React from 'react';
88
import Select, { OptionProps } from 'react-select';
99
import { CSSObject } from '@emotion/react';
10+
import { updateSessionLanguage } from "@/api/collaboration";
1011

1112
type Props = {
1213
editor: editor.IStandaloneCodeEditor;
@@ -16,6 +17,7 @@ type Props = {
1617
peerOnline: boolean;
1718
theme: string;
1819
setTheme: (theme: string) => void;
20+
room: string;
1921
};
2022

2123
type LanguageOption = {
@@ -28,7 +30,7 @@ type ThemeOption = {
2830
label: string;
2931
}
3032

31-
export function Toolbar({ editor, language, saving, setLanguage, peerOnline, theme, setTheme }: Readonly<Props>) {
33+
export function Toolbar({ editor, language, saving, setLanguage, peerOnline, theme, setTheme, room }: Readonly<Props>) {
3234
const languages: LanguageOption[] = [
3335
{ value: 'javascript', label: 'JavaScript' },
3436
{ value: 'python', label: 'Python' },
@@ -37,6 +39,7 @@ export function Toolbar({ editor, language, saving, setLanguage, peerOnline, the
3739
const handleLanguageChange = (selectedOption: LanguageOption | null) => {
3840
if (selectedOption) {
3941
setLanguage(selectedOption.value);
42+
updateSessionLanguage(room, selectedOption.value);
4043
}
4144
};
4245

0 commit comments

Comments
 (0)