-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π¨ Refactor(#47): μ μ²μ μμ νμ΄μ§ 리ν©ν°λ§
- Loading branch information
Showing
2 changed files
with
82 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useRouter } from "next/navigation"; | ||
import { useToast } from "@/components/ui/use-toast"; | ||
import { useSuspenseQuery } from "@tanstack/react-query"; | ||
import editApplication from "@/services/application/editApplication"; | ||
import getApplicationDetails from "@/services/application/getApplicationDetails"; | ||
import Button, { buttonSize } from "@/components/_common/Button"; | ||
import { getApplicationDetailsKey } from "@/constants/queryKeys"; | ||
|
||
interface EditButtonsProps { | ||
survey: { | ||
question: string; | ||
answer: string; | ||
}[]; | ||
applicationId: string; | ||
steadyId: string; | ||
} | ||
|
||
const EditButtons = ({ survey, applicationId, steadyId }: EditButtonsProps) => { | ||
const { toast } = useToast(); | ||
const router = useRouter(); | ||
const { refetch: applicationRefetch } = useSuspenseQuery({ | ||
queryKey: getApplicationDetailsKey(applicationId), | ||
queryFn: () => getApplicationDetails(applicationId), | ||
}); | ||
|
||
const handleClickEdit = async () => { | ||
const checkInvalidAnswers = survey.some((item) => !item.answer.length); | ||
if (checkInvalidAnswers) { | ||
toast({ | ||
description: "μ λ ₯λμ§ μμ νλͺ©μ΄ μμ΅λλ€.", | ||
variant: "red", | ||
}); | ||
} else { | ||
try { | ||
await editApplication(applicationId, { | ||
answers: survey.map((item) => item.answer), | ||
}); | ||
toast({ | ||
description: "μ€ν λ μ μ²μ μμ μ±κ³΅!", | ||
variant: "green", | ||
}); | ||
await applicationRefetch(); | ||
router.replace(`/steady/detail/${steadyId}`); | ||
} catch (error) { | ||
toast({ | ||
description: "μ€ν λ μ μ²μ μμ μ€ν¨!", | ||
variant: "red", | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button | ||
className={`${buttonSize.sm} bg-st-white`} | ||
onClick={() => router.back()} | ||
> | ||
μ·¨μ | ||
</Button> | ||
<Button | ||
onClick={handleClickEdit} | ||
className={`${buttonSize.sm} bg-st-primary text-st-white`} | ||
> | ||
μμ μλ£ | ||
</Button> | ||
</> | ||
); | ||
}; | ||
|
||
export default EditButtons; |