@@ -30,7 +30,7 @@ import { TextLink } from "~/components/primitives/TextLink";
3030import { TimezoneList } from "~/components/scheduled/timezones" ;
3131import { useEnvironment } from "~/hooks/useEnvironment" ;
3232import { useSearchParams } from "~/hooks/useSearchParam" ;
33- import { useParams , Form , useActionData , useFetcher } from "@remix-run/react" ;
33+ import { useParams , Form , useActionData , useFetcher , useSubmit } from "@remix-run/react" ;
3434import {
3535 redirectBackWithErrorMessage ,
3636 redirectWithErrorMessage ,
@@ -67,6 +67,8 @@ import { DeleteTaskRunTemplateData, RunTemplateData } from "~/v3/taskRunTemplate
6767import { Dialog , DialogContent , DialogHeader , DialogTrigger } from "~/components/primitives/Dialog" ;
6868import { DialogClose , DialogDescription } from "@radix-ui/react-dialog" ;
6969import { FormButtons } from "~/components/primitives/FormButtons" ;
70+ import { toast } from "sonner" ;
71+ import { ToastUI } from "~/components/primitives/Toast" ;
7072
7173export const loader = async ( { request, params } : LoaderFunctionArgs ) => {
7274 const userId = await requireUserId ( request ) ;
@@ -129,7 +131,7 @@ export const action: ActionFunction = async ({ request, params }) => {
129131 if ( formAction === "create-template" ) {
130132 const runTemplateData = parse ( formData , { schema : RunTemplateData } ) ;
131133 if ( ! runTemplateData . value ) {
132- return json ( runTemplateData ) ;
134+ return json ( runTemplateData . error ) ;
133135 }
134136
135137 const templateService = new TaskRunTemplateService ( ) ;
@@ -1471,22 +1473,31 @@ function CreateTemplateModal({
14711473 getCurrentPayload : ( ) => string ;
14721474 getCurrentMetadata : ( ) => string ;
14731475} ) {
1474- const lastSubmission = useActionData < typeof action > ( ) ;
1475-
1476- const fetcher = useFetcher ( ) ;
1476+ const submit = useSubmit ( ) ;
14771477 const [ isModalOpen , setIsModalOpen ] = useState ( false ) ;
14781478
1479+ const lastSubmission = useActionData < typeof action > ( ) ;
1480+
14791481 useEffect ( ( ) => {
14801482 if (
1481- fetcher . state === "idle" &&
1482- fetcher . data &&
1483- typeof fetcher . data === "object" &&
1484- "success" in fetcher . data &&
1485- fetcher . data . success
1483+ lastSubmission &&
1484+ typeof lastSubmission === "object" &&
1485+ "formAction" in lastSubmission &&
1486+ "success" in lastSubmission &&
1487+ lastSubmission . formAction === "create-template" &&
1488+ lastSubmission . success === true
14861489 ) {
14871490 setIsModalOpen ( false ) ;
1491+ toast . custom (
1492+ ( t ) => (
1493+ < ToastUI variant = "success" message = "Template created successfully" t = { t as string } />
1494+ ) ,
1495+ {
1496+ duration : 2000 ,
1497+ }
1498+ ) ;
14881499 }
1489- } , [ fetcher . state , fetcher . data ] ) ;
1500+ } , [ lastSubmission ] ) ;
14901501
14911502 const [
14921503 form ,
@@ -1512,20 +1523,13 @@ function CreateTemplateModal({
15121523 } ,
15131524 ] = useForm ( {
15141525 id : "save-template" ,
1515- lastSubmission :
1516- lastSubmission &&
1517- typeof lastSubmission === "object" &&
1518- "formAction" in lastSubmission &&
1519- lastSubmission . formAction === "create-template"
1520- ? ( lastSubmission as any )
1521- : undefined ,
15221526 onSubmit ( event , { formData } ) {
15231527 event . preventDefault ( ) ;
15241528
15251529 formData . set ( payload . name , getCurrentPayload ( ) ) ;
15261530 formData . set ( metadata . name , getCurrentMetadata ( ) ) ;
15271531
1528- fetcher . submit ( formData , { method : "POST" } ) ;
1532+ submit ( formData , { method : "POST" } ) ;
15291533 } ,
15301534 onValidate ( { formData } ) {
15311535 return parse ( formData , { schema : RunTemplateData } ) ;
0 commit comments