-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scenario): update scenario name and description (#361)
- Loading branch information
Showing
10 changed files
with
232 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,5 +20,10 @@ | |
"typescript": "5.3.3", | ||
"vite-tsconfig-paths": "^4.3.1", | ||
"vitest": "1.2.1" | ||
}, | ||
"pnpm": { | ||
"patchedDependencies": { | ||
"[email protected]": "patches/[email protected]" | ||
} | ||
} | ||
} |
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
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
149 changes: 149 additions & 0 deletions
149
packages/app-builder/src/routes/ressources+/scenarios+/update.tsx
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,149 @@ | ||
import { FormError } from '@app-builder/components/Form/FormError'; | ||
import { FormField } from '@app-builder/components/Form/FormField'; | ||
import { FormInput } from '@app-builder/components/Form/FormInput'; | ||
import { FormLabel } from '@app-builder/components/Form/FormLabel'; | ||
import { setToastMessage } from '@app-builder/components/MarbleToaster'; | ||
import { serverServices } from '@app-builder/services/init.server'; | ||
import { getRoute } from '@app-builder/utils/routes'; | ||
import { fromUUID } from '@app-builder/utils/short-uuid'; | ||
import { conform, useForm } from '@conform-to/react'; | ||
import { getFieldsetConstraint, parse } from '@conform-to/zod'; | ||
import { type ActionFunctionArgs, json } from '@remix-run/node'; | ||
import { useFetcher, useNavigation } from '@remix-run/react'; | ||
import { useEffect, useId, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { redirectBack } from 'remix-utils/redirect-back'; | ||
import { Button, Modal } from 'ui-design-system'; | ||
import { z } from 'zod'; | ||
|
||
const updateScenarioFormSchema = z.object({ | ||
scenarioId: z.string().uuid(), | ||
name: z.string().min(1), | ||
description: z.string().nullable().default(null), | ||
}); | ||
|
||
type UpdateScenarioForm = z.infer<typeof updateScenarioFormSchema>; | ||
|
||
export async function action({ request }: ActionFunctionArgs) { | ||
const { | ||
authService, | ||
toastSessionService: { getSession, commitSession }, | ||
} = serverServices; | ||
const { scenario } = await authService.isAuthenticated(request, { | ||
failureRedirect: getRoute('/sign-in'), | ||
}); | ||
const formData = await request.formData(); | ||
const submission = parse(formData, { schema: updateScenarioFormSchema }); | ||
|
||
if (submission.intent !== 'submit' || !submission.value) { | ||
return json(submission); | ||
} | ||
|
||
try { | ||
await scenario.updateScenario(submission.value); | ||
return redirectBack(request, { | ||
fallback: getRoute('/scenarios/:scenarioId', { | ||
scenarioId: fromUUID(submission.value.scenarioId), | ||
}), | ||
}); | ||
} catch (error) { | ||
const session = await getSession(request); | ||
setToastMessage(session, { | ||
type: 'error', | ||
messageKey: 'common:errors.unknown', | ||
}); | ||
return json(submission, { | ||
headers: { 'Set-Cookie': await commitSession(session) }, | ||
}); | ||
} | ||
} | ||
|
||
export function UpdateScenario({ | ||
children, | ||
defaultValue, | ||
}: { | ||
children: React.ReactNode; | ||
defaultValue: UpdateScenarioForm; | ||
}) { | ||
const [open, setOpen] = useState(false); | ||
|
||
const navigation = useNavigation(); | ||
useEffect(() => { | ||
if (navigation.state === 'loading') { | ||
setOpen(false); | ||
} | ||
}, [navigation.state]); | ||
|
||
return ( | ||
<Modal.Root open={open} onOpenChange={setOpen}> | ||
<Modal.Trigger asChild>{children}</Modal.Trigger> | ||
<Modal.Content> | ||
<UpdateScenarioContent defaultValue={defaultValue} /> | ||
</Modal.Content> | ||
</Modal.Root> | ||
); | ||
} | ||
|
||
function UpdateScenarioContent({ | ||
defaultValue, | ||
}: { | ||
defaultValue: UpdateScenarioForm; | ||
}) { | ||
const { t } = useTranslation(['scenarios', 'common']); | ||
const fetcher = useFetcher<typeof action>(); | ||
console.log(defaultValue); | ||
const formId = useId(); | ||
const [form, { name, description, scenarioId }] = useForm({ | ||
id: formId, | ||
defaultValue, | ||
lastSubmission: fetcher.data, | ||
constraint: getFieldsetConstraint(updateScenarioFormSchema), | ||
// onValidate({ formData }) { | ||
// return parse(formData, { | ||
// schema: updateScenarioFormSchema, | ||
// }); | ||
// }, | ||
}); | ||
|
||
return ( | ||
<fetcher.Form | ||
method="PATCH" | ||
action={getRoute('/ressources/scenarios/update')} | ||
{...form.props} | ||
> | ||
<Modal.Title>{t('scenarios:create_scenario.title')}</Modal.Title> | ||
<div className="flex flex-col gap-6 p-6"> | ||
<input {...conform.input(scenarioId, { type: 'hidden' })} /> | ||
<FormField config={name} className="group flex w-full flex-col gap-2"> | ||
<FormLabel>{t('scenarios:create_scenario.name')}</FormLabel> | ||
<FormInput | ||
type="text" | ||
placeholder={t('scenarios:create_scenario.name_placeholder')} | ||
/> | ||
<FormError /> | ||
</FormField> | ||
<FormField | ||
config={description} | ||
className="group flex w-full flex-col gap-2" | ||
> | ||
<FormLabel>{t('scenarios:create_scenario.description')}</FormLabel> | ||
<FormInput | ||
type="text" | ||
placeholder={t('scenarios:create_scenario.description_placeholder')} | ||
/> | ||
<FormError /> | ||
</FormField> | ||
<div className="flex flex-1 flex-row gap-2"> | ||
<Modal.Close asChild> | ||
<Button className="flex-1" variant="secondary"> | ||
{t('common:cancel')} | ||
</Button> | ||
</Modal.Close> | ||
<Button className="flex-1" variant="primary" type="submit"> | ||
{t('common:save')} | ||
</Button> | ||
</div> | ||
</div> | ||
</fetcher.Form> | ||
); | ||
} |
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
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,15 @@ | ||
diff --git a/build/server/redirect-back.d.ts b/build/server/redirect-back.d.ts | ||
index b96ca64aaf202b4103099dc4299e09310566f341..9b8b85276fe07283f8e2947ca752b67128a1e827 100644 | ||
--- a/build/server/redirect-back.d.ts | ||
+++ b/build/server/redirect-back.d.ts | ||
@@ -1,3 +1,4 @@ | ||
+import { TypedResponse } from "@remix-run/node"; | ||
/** | ||
* Create a new Response with a redirect set to the URL the user was before. | ||
* It uses the Referer header to detect the previous URL. It asks for a fallback | ||
@@ -14,4 +15,4 @@ | ||
*/ | ||
export declare function redirectBack(request: Request, { fallback, ...init }: ResponseInit & { | ||
fallback: string; | ||
-}): Response; | ||
+}): TypedResponse<never>; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.