update: adjustment on alerts and adjustment on the detail form

This commit is contained in:
falendikategar 2024-09-26 11:25:02 +07:00
parent cd32ac33c7
commit b71025fd4b
2 changed files with 50 additions and 54 deletions

View File

@ -5,7 +5,6 @@ import {
Button, Button,
Flex, Flex,
ActionIcon, ActionIcon,
Select,
ScrollArea, ScrollArea,
TextInput, TextInput,
NumberInput, NumberInput,
@ -18,13 +17,12 @@ import { useEffect } from "react";
import { notifications } from "@mantine/notifications"; import { notifications } from "@mantine/notifications";
import FormResponseError from "@/errors/FormResponseError"; import FormResponseError from "@/errors/FormResponseError";
import createInputComponents from "@/utils/createInputComponents"; import createInputComponents from "@/utils/createInputComponents";
import stringToColorHex from "@/utils/stringToColorHex";
import { import {
createQuestion, createQuestion,
getQuestionByIdQueryOptions, getQuestionByIdQueryOptions,
updateQuestion, updateQuestion,
fetchAspects, // Import fetchAspects query fetchAspects,
fetchSubAspects, // Import fetchSubAspects query fetchSubAspects,
} from "../queries/questionQueries"; } from "../queries/questionQueries";
/** /**
@ -39,8 +37,8 @@ interface Option {
} }
interface CreateQuestionPayload { interface CreateQuestionPayload {
id?: string; // Make id optional id?: string;
subAspectId: string; // Ensure this matches the correct ID type subAspectId: string;
question: string; question: string;
needFile: boolean; needFile: boolean;
options: Option[]; // Array of options (text and score) options: Option[]; // Array of options (text and score)
@ -48,7 +46,7 @@ interface CreateQuestionPayload {
interface UpdateQuestionPayload { interface UpdateQuestionPayload {
id: string; // The ID of the question to update id: string; // The ID of the question to update
subAspectId: string; // Ensure this matches the correct ID type subAspectId: string;
question: string; question: string;
needFile: boolean; needFile: boolean;
options?: Option[]; // Optional array of options (text and score) options?: Option[]; // Optional array of options (text and score)
@ -120,9 +118,10 @@ export default function QuestionFormModal() {
form.setErrors({}); form.setErrors({});
}, [questionQuery.data]); }, [questionQuery.data]);
// Define possible actions, depending on the action, it can be one or the other
interface MutationOptions { interface MutationOptions {
action: "edit" | "create"; // Define possible actions action: "edit" | "create";
data: CreateQuestionPayload | UpdateQuestionPayload; // Depending on the action, it can be one or the other data: CreateQuestionPayload | UpdateQuestionPayload;
} }
interface MutationResponse { interface MutationResponse {
@ -138,26 +137,6 @@ export default function QuestionFormModal() {
return await createQuestion(options.data as CreateQuestionPayload); return await createQuestion(options.data as CreateQuestionPayload);
} }
}, },
onSuccess: (data) => {
// You can use data.message here if you want to display success messages
notifications.show({
message: data.message,
color: "green",
});
},
onError: (error) => {
if (error instanceof FormResponseError) {
form.setErrors(error.formErrors);
return;
}
if (error instanceof Error) {
notifications.show({
message: error.message,
color: "red",
});
}
},
}); });
const handleSubmit = async (values: CreateQuestionPayload) => { const handleSubmit = async (values: CreateQuestionPayload) => {
@ -179,13 +158,13 @@ export default function QuestionFormModal() {
if (formType === "create") { if (formType === "create") {
await mutation.mutateAsync({ action: "create", data: payload }); await mutation.mutateAsync({ action: "create", data: payload });
notifications.show({ notifications.show({
message: "Question created successfully!", message: "Data pertanyaan berhasil dibuat!",
color: "green", color: "green",
}); });
} else { } else {
await mutation.mutateAsync({ action: "edit", data: payload }); await mutation.mutateAsync({ action: "edit", data: payload });
notifications.show({ notifications.show({
message: "Question updated successfully!", message: "Data pertanyaan berhasil diperbarui!",
color: "green", color: "green",
}); });
} }
@ -225,29 +204,43 @@ export default function QuestionFormModal() {
disableAll: mutation.isPending, disableAll: mutation.isPending,
readonlyAll: formType === "detail", readonlyAll: formType === "detail",
inputs: [ inputs: [
{ formType === "detail"
? {
type: "text",
label: "Nama Aspek",
readOnly: true,
value: aspectsQuery.data?.find(aspect => aspect.id === form.values.aspectId)?.name || "",
}
: {
type: "select", type: "select",
label: "Nama Aspek", label: "Nama Aspek",
data: aspectsQuery.data?.map((aspect) => ({ data: aspectsQuery.data?.map((aspect) => ({
value: aspect.id, value: aspect.id,
label: aspect.name, label: aspect.name,
})) || [], })) || [],
disabled: mutation.isPending || formType === "detail", disabled: mutation.isPending,
...form.getInputProps("aspectId"), ...form.getInputProps("aspectId"),
}, },
{ formType === "detail"
? {
type: "text",
label: "Nama Sub Aspek",
readOnly: true,
value: filteredSubAspects.find(subAspect => subAspect.id === form.values.subAspectId)?.name || "",
}
: {
type: "select", type: "select",
label: "Nama Sub Aspek", label: "Nama Sub Aspek",
data: filteredSubAspects.map((subAspect) => ({ data: filteredSubAspects.map((subAspect) => ({
value: subAspect.id, value: subAspect.id,
label: subAspect.name, label: subAspect.name,
})), })),
disabled: mutation.isPending || formType === "detail", disabled: mutation.isPending,
...form.getInputProps("subAspectId"), ...form.getInputProps("subAspectId"),
}, },
{ {
type: "textarea", type: "textarea",
label: "Teks Pertanyaan", label: "Pertanyaan",
...form.getInputProps("question"), ...form.getInputProps("question"),
}, },
formType === "detail" formType === "detail"
@ -260,6 +253,7 @@ export default function QuestionFormModal() {
: { : {
type: "checkbox", type: "checkbox",
label: "Dibutuhkan Upload File?", label: "Dibutuhkan Upload File?",
checked: form.values.needFile,
...form.getInputProps("needFile"), ...form.getInputProps("needFile"),
}, },
], ],
@ -271,13 +265,15 @@ export default function QuestionFormModal() {
<Group key={index} mb="sm"> <Group key={index} mb="sm">
<TextInput <TextInput
label={`Jawaban ${index + 1}`} label={`Jawaban ${index + 1}`}
placeholder="Teks Jawaban" placeholder="Jawaban"
readOnly={formType === "detail"}
{...form.getInputProps(`options.${index}.text`)} {...form.getInputProps(`options.${index}.text`)}
required required
/> />
<NumberInput <NumberInput
label="Skor" label="Skor"
placeholder="Skor" placeholder="Skor"
readOnly={formType === "detail"}
{...form.getInputProps(`options.${index}.score`)} {...form.getInputProps(`options.${index}.score`)}
required required
/> />

View File

@ -3,7 +3,7 @@ import PageTemplate from "@/components/PageTemplate";
import { createLazyFileRoute } from "@tanstack/react-router"; import { createLazyFileRoute } from "@tanstack/react-router";
import ExtractQueryDataType from "@/types/ExtractQueryDataType"; import ExtractQueryDataType from "@/types/ExtractQueryDataType";
import { createColumnHelper } from "@tanstack/react-table"; import { createColumnHelper } from "@tanstack/react-table";
import { Badge, Flex } from "@mantine/core"; import { Flex } from "@mantine/core";
import createActionButtons from "@/utils/createActionButton"; import createActionButtons from "@/utils/createActionButton";
import { TbEye, TbPencil, TbTrash } from "react-icons/tb"; import { TbEye, TbPencil, TbTrash } from "react-icons/tb";
import QuestionDeleteModal from "@/modules/questionsManagement/modals/QuestionDeleteModal"; import QuestionDeleteModal from "@/modules/questionsManagement/modals/QuestionDeleteModal";