Skip to content

Commit cadf15f

Browse files
committed
Create edit modal for question
1 parent 5614be9 commit cadf15f

File tree

1 file changed

+132
-3
lines changed

1 file changed

+132
-3
lines changed

apps/question-service/src/app/page.tsx

Lines changed: 132 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
} from "../utils/SelectOptions";
4141
import Link from "next/link";
4242
import TextArea from "antd/es/input/TextArea";
43+
import { title } from "process";
4344

4445
/**
4546
* defines the State of the page whe a user is deleing an object. Has 3 general states:
@@ -106,6 +107,36 @@ export default function Home() {
106107
const [form] = Form.useForm();
107108
const [isNewProblemModalOpen, setIsNewProblemModelOpen] = useState(false);
108109

110+
// States for Edit Existing Problem Modal
111+
const [editForm] = Form.useForm();
112+
const [isEditModalOpen, setIsEditModalOpen] = useState<boolean[] | undefined>(
113+
undefined
114+
);
115+
116+
const handleEditClick = (index: number, question: Question) => {
117+
// Open the modal for the specific question
118+
const updatedModals =
119+
isEditModalOpen && isEditModalOpen.map((_, idx) => idx === index);
120+
setIsEditModalOpen(updatedModals); // Only the selected question's modal is open
121+
122+
// Set the form value
123+
editForm.setFieldsValue({
124+
title: question.title,
125+
description: question.description,
126+
complexity: question.complexity,
127+
categories: question.categories,
128+
});
129+
};
130+
131+
// Function to handle modal close
132+
const handleModalClose = (index: number) => {
133+
if (isEditModalOpen) {
134+
const updatedModals = [...isEditModalOpen];
135+
updatedModals[index] = false; // Close the specific modal
136+
setIsEditModalOpen(updatedModals);
137+
}
138+
};
139+
109140
const handleCreateQuestion = async (values: NewQuestion) => {
110141
try {
111142
const createdQuestion = await CreateQuestion(values);
@@ -162,6 +193,7 @@ export default function Home() {
162193
setCurrentPage(data.currentPage);
163194
setLimit(data.limit);
164195
setIsLoading(false);
196+
setIsEditModalOpen(Array(data.questions.length).fill(false));
165197
}
166198

167199
useEffect(() => {
@@ -231,11 +263,108 @@ export default function Home() {
231263
title: "Actions",
232264
key: "actions",
233265
dataIndex: "id",
234-
render: (_: number, question: Question) => (
266+
render: (_: number, question: Question, index: number) => (
235267
<div>
236268
{/* TODO (Sean): Include Logic to handle retrieving of editable data here and display in a modal component */}
237-
238-
<Button className="edit-button" icon={<EditOutlined />}></Button>
269+
<Modal
270+
title="Edit Problem"
271+
open={isEditModalOpen && isEditModalOpen[index]}
272+
onCancel={() => handleModalClose(index)}
273+
footer={null}
274+
width={600}
275+
>
276+
<Form
277+
name="edit-form"
278+
{...layout}
279+
form={editForm}
280+
onFinish={(values) => {
281+
// handleEditQuestion(values); TODO!!! (SEAN) follow the create concept and display error and success values
282+
}}
283+
>
284+
<Form.Item
285+
name="title"
286+
label="Title"
287+
rules={[
288+
{
289+
required: true,
290+
message: "Please enter question title!",
291+
},
292+
]}
293+
>
294+
<Input name="title" />
295+
</Form.Item>
296+
<Form.Item
297+
name="description"
298+
label="Description"
299+
rules={[
300+
{
301+
required: true,
302+
message: "Please enter question description!",
303+
},
304+
]}
305+
>
306+
<TextArea name="description" />
307+
</Form.Item>
308+
<Form.Item
309+
name="complexity"
310+
label="Complexity"
311+
rules={[
312+
{
313+
required: true,
314+
message: "Please select a complexity!",
315+
},
316+
]}
317+
>
318+
<Select
319+
options={[
320+
{
321+
label: "Easy",
322+
value: "easy",
323+
},
324+
{
325+
label: "Medium",
326+
value: "medium",
327+
},
328+
{
329+
label: "Hard",
330+
value: "hard",
331+
},
332+
]}
333+
onChange={(value) => form.setFieldValue("complexity", value)}
334+
allowClear
335+
/>
336+
</Form.Item>
337+
<Form.Item
338+
name="categories"
339+
label="Categories"
340+
rules={[
341+
{
342+
required: true,
343+
message: "Please select the relevant categories!",
344+
},
345+
]}
346+
>
347+
<Select
348+
mode="multiple"
349+
options={CategoriesOption}
350+
onChange={(value) => form.setFieldValue("categories", value)}
351+
allowClear
352+
/>
353+
</Form.Item>
354+
<Form.Item
355+
style={{ display: "flex", justifyContent: "flex-end" }}
356+
>
357+
<Button type="primary" htmlType="submit">
358+
Save
359+
</Button>
360+
</Form.Item>
361+
</Form>
362+
</Modal>
363+
<Button
364+
className="edit-button"
365+
icon={<EditOutlined />}
366+
onClick={() => handleEditClick(index, question)}
367+
></Button>
239368
{/* TODO (Ryan): Include Pop-up confirmation for delete when clicked and link to delete API --> can also explore success notification or look into react-toast*/}
240369
<Button
241370
className="delete-button"

0 commit comments

Comments
 (0)