Skip to content

Commit 930741f

Browse files
committed
Configure the service functions and integrate edit question
1 parent 2f3a6e3 commit 930741f

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
Question,
3333
CreateQuestion,
3434
NewQuestion,
35+
EditQuestion,
3536
} from "./services/question";
3637
import {
3738
CategoriesOption,
@@ -137,6 +138,22 @@ export default function Home() {
137138
}
138139
};
139140

141+
const handleEditQuestion = async (
142+
values: NewQuestion,
143+
index: number,
144+
docRefId: string
145+
) => {
146+
try {
147+
const editedQuestion = await EditQuestion(values, docRefId);
148+
// Reset form or update UI as needed
149+
handleModalClose(index);
150+
editForm.resetFields();
151+
success("Problem Updated!");
152+
} catch (err: any) {
153+
error(err.message);
154+
}
155+
};
156+
140157
const handleCreateQuestion = async (values: NewQuestion) => {
141158
try {
142159
const createdQuestion = await CreateQuestion(values);
@@ -278,7 +295,7 @@ export default function Home() {
278295
{...layout}
279296
form={editForm}
280297
onFinish={(values) => {
281-
// handleEditQuestion(values); TODO!!! (SEAN) follow the create concept and display error and success values
298+
handleEditQuestion(values, index, question.docRefId);
282299
}}
283300
>
284301
<Form.Item

apps/question-service/src/app/services/question.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,29 @@ export const CreateQuestion = async (
108108
}
109109
};
110110

111-
// Update single question (TODO: Sean)
111+
export const EditQuestion = async (
112+
question: NewQuestion,
113+
docRefId: string
114+
): Promise<Question> => {
115+
const response = await fetch(
116+
`${process.env.NEXT_PUBLIC_API_URL}questions/${docRefId}`,
117+
{
118+
method: "PUT",
119+
headers: {
120+
"Content-Type": "application/json",
121+
},
122+
body: JSON.stringify(question),
123+
}
124+
);
125+
126+
if (response.status === 200) {
127+
return response.json();
128+
} else {
129+
throw new Error(
130+
`Error updating question: ${response.status} ${response.statusText}`
131+
);
132+
}
133+
};
112134

113135
// Delete single question (TODO: Ryan)
114136
export async function DeleteQuestion(docRef: String): Promise<void> {

0 commit comments

Comments
 (0)