From d223595f7457c8a34d79d45b5d866fef1173ede2 Mon Sep 17 00:00:00 2001 From: abiyasa05 Date: Tue, 29 Oct 2024 15:28:26 +0700 Subject: [PATCH] update: add the text input to disable after submit --- .../modals/AspectFormModal.tsx | 89 ++++++++++--------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/apps/frontend/src/modules/aspectManagement/modals/AspectFormModal.tsx b/apps/frontend/src/modules/aspectManagement/modals/AspectFormModal.tsx index 1079c67..56116aa 100644 --- a/apps/frontend/src/modules/aspectManagement/modals/AspectFormModal.tsx +++ b/apps/frontend/src/modules/aspectManagement/modals/AspectFormModal.tsx @@ -4,7 +4,7 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { getRouteApi } from "@tanstack/react-router"; import { createAspect, updateAspect, getAspectByIdQueryOptions } from "../queries/aspectQueries"; import { TbDeviceFloppy } from "react-icons/tb"; -import { useEffect } from "react"; +import { useEffect, useState } from "react"; import { notifications } from "@mantine/notifications"; import FormResponseError from "@/errors/FormResponseError"; import { createId } from "@paralleldrive/cuid2"; @@ -91,16 +91,22 @@ export default function AspectFormModal() { subAspects?: string; }; + const [isSubmitting, setIsSubmitting] = useState(false); + const handleSubmit = async (values: typeof form.values) => { try { + // Start submit process + setIsSubmitting(true); + // Name field validation if (values.name.trim() === "") { form.setErrors({ name: "Nama aspek harus diisi" }); + setIsSubmitting(false); return; } - + let payload: CreateAspectPayload | EditAspectPayload; - + if (formType === "create") { payload = { name: values.name, @@ -114,7 +120,6 @@ export default function AspectFormModal() { }; await createAspect(payload); } else if (formType === "edit") { - // Add validation for aspect name here payload = { id: values.id, name: values.name, @@ -132,17 +137,17 @@ export default function AspectFormModal() { }; await updateAspect(payload); } - + queryClient.invalidateQueries({ queryKey: ["management-aspect"] }); - + notifications.show({ message: `Aspek ${formType === "create" ? "berhasil dibuat" : "berhasil diedit"}`, }); - + navigate({ search: {} }); } catch (error) { console.error("Error during submit:", error); - + if (error instanceof Error && error.message === "Aspect name already exists") { notifications.show({ message: "Nama aspek sudah ada. Silakan gunakan nama lain.", @@ -154,8 +159,10 @@ export default function AspectFormModal() { color: "red", }); } + } finally { + setIsSubmitting(false); } - }; + }; return ( -
handleSubmit(values))}> + - + {form.values.subAspects.map((field, index) => ( - { - const newSubAspects = [...form.values.subAspects]; - newSubAspects[index] = { ...newSubAspects[index], name: event.target.value }; - form.setValues({ subAspects: newSubAspects }); - }} - disabled={formType === "detail"} - style={{ flex: 1 }} - /> - {formType === "detail" && ( - Jumlah Soal: {field.questionCount} - )} - {formType !== "detail" && ( - - )} - + disabled={formType === "detail" || isSubmitting} + style={{ flex: 1 }} + /> + {formType === "detail" ? ( + Jumlah Soal: {field.questionCount} + ) : ( + + )} + ))} {formType !== "detail" && ( @@ -221,13 +226,13 @@ export default function AspectFormModal() { Tambah Sub Aspek )} - + {/* Buttons */} @@ -236,7 +241,7 @@ export default function AspectFormModal() { variant="filled" leftSection={} type="submit" - loading={mutation.isPending} + loading={isSubmitting} > Simpan @@ -244,5 +249,5 @@ export default function AspectFormModal() {
- ); + ); } \ No newline at end of file