-
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.
Merge pull request #123 from mblajek/fz-58-facility-create
FZ-58 Create facility flow
- Loading branch information
Showing
7 changed files
with
142 additions
and
3 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
37 changes: 37 additions & 0 deletions
37
resources/js/features/facility-edit/FacilityCreateForm.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,37 @@ | ||
import {createMutation} from "@tanstack/solid-query"; | ||
import {useLangFunc} from "components/utils"; | ||
import {Admin, System} from "data-access/memo-api"; | ||
import {VoidComponent} from "solid-js"; | ||
import toast from "solid-toast"; | ||
import {FacilityForm, FacilityFormOutput} from "./FacilityForm"; | ||
|
||
interface Props { | ||
onSuccess?: () => void; | ||
onCancel?: () => void; | ||
} | ||
|
||
export const FacilityCreateForm: VoidComponent<Props> = (props) => { | ||
const t = useLangFunc(); | ||
const adminInvalidator = Admin.useInvalidator(); | ||
const systemInvalidator = System.useInvalidator(); | ||
const facilityMutation = createMutation(() => ({ | ||
mutationFn: Admin.createFacility, | ||
meta: {isFormSubmit: true}, | ||
})); | ||
|
||
async function createFacility(values: FacilityFormOutput) { | ||
await facilityMutation.mutateAsync({ | ||
name: values.name, | ||
url: values.url, | ||
}); | ||
adminInvalidator.facilities(); | ||
systemInvalidator.facilities(); | ||
toast.success(t("forms.facility_create.success")); | ||
props.onSuccess?.(); | ||
} | ||
|
||
return <FacilityForm id="facility_create" onSubmit={createFacility} onCancel={props.onCancel} />; | ||
}; | ||
|
||
// For lazy loading | ||
export default FacilityCreateForm; |
26 changes: 26 additions & 0 deletions
26
resources/js/features/facility-edit/FacilityCreateModal.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,26 @@ | ||
import {useLangFunc} from "components/utils"; | ||
import {VoidComponent, createSignal, lazy} from "solid-js"; | ||
import {Modal as ModalComponent, MODAL_STYLE_PRESETS} from "components/ui"; | ||
|
||
const FacilityCreateForm = lazy(() => import("features/facility-edit/FacilityCreateForm")); | ||
|
||
const [modalOpen, setModalOpen] = createSignal(false); | ||
|
||
export const FacilityCreateModal: VoidComponent = () => { | ||
const t = useLangFunc(); | ||
return ( | ||
<ModalComponent | ||
title={t("forms.facility_create.formName")} | ||
open={modalOpen()} | ||
closeOn={["escapeKey", "closeButton"]} | ||
onClose={() => setModalOpen(false)} | ||
style={MODAL_STYLE_PRESETS.medium} | ||
> | ||
<FacilityCreateForm onSuccess={() => setModalOpen(false)} onCancel={() => setModalOpen(false)} /> | ||
</ModalComponent> | ||
); | ||
}; | ||
|
||
export function showFacilityCreateModal() { | ||
setModalOpen(true); | ||
} |
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,55 @@ | ||
import {FormConfigWithoutTransformFn} from "@felte/core"; | ||
import {FelteForm, FelteSubmit} from "components/felte-form"; | ||
import {TextField, getTrimInputHandler, trimInput} from "components/ui"; | ||
import {VoidComponent, createComputed, splitProps} from "solid-js"; | ||
import {z} from "zod"; | ||
|
||
/** Produces best effort suggestion for the url, e.g. "My Facility Name" --> "my-facility-name" */ | ||
function getUrlSuggestion(name: string) { | ||
const url = trimInput(name).toLowerCase().replaceAll(" ", "-"); | ||
// Remove diacritics, especially for polish characters. | ||
// https://stackoverflow.com/a/37511463/1832228 | ||
return url.normalize("NFD").replace(/\p{Diacritic}/gu, "").replace('ł', 'l'); | ||
} | ||
|
||
const getSchema = () => | ||
z.object({ | ||
name: z.string(), | ||
url: z.string(), | ||
}); | ||
|
||
export type FacilityFormInput = z.input<ReturnType<typeof getSchema>>; | ||
export type FacilityFormOutput = z.output<ReturnType<typeof getSchema>>; | ||
|
||
type FormProps = FormConfigWithoutTransformFn<FacilityFormInput>; | ||
type MyProps = { | ||
onCancel?: () => void; | ||
id: string; | ||
}; | ||
type Props = FormProps & MyProps; | ||
|
||
export const FacilityForm: VoidComponent<Props> = (props) => { | ||
const [localProps, formProps]: [MyProps, FormProps] = splitProps(props, ["id", "onCancel"]); | ||
return ( | ||
<FelteForm id={localProps.id} schema={getSchema()} {...formProps} class="flex flex-col gap-4"> | ||
{(form) => { | ||
createComputed((lastSuggestion: string|undefined) => { | ||
const suggestion = getUrlSuggestion(form.data("name") || ''); | ||
if (form.data("url") === lastSuggestion) { | ||
form.setFields("url", suggestion); | ||
} | ||
return suggestion; | ||
}); | ||
return ( | ||
<> | ||
<div class="flex flex-col gap-1"> | ||
<TextField name="name" type="text" autocomplete="off" onBlur={getTrimInputHandler()} /> | ||
<TextField name="url" type="text" autocomplete="off" onBlur={getTrimInputHandler()} /> | ||
</div> | ||
<FelteSubmit cancel={localProps.onCancel} /> | ||
</> | ||
); | ||
}} | ||
</FelteForm> | ||
); | ||
}; |
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