Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
fix: edit reservation unit validation
Browse files Browse the repository at this point in the history
- allow switching draft <-> published without modification
- don't allow empty html tags without any text
- disallow negative minPerson / maxPerson numbers
  • Loading branch information
joonatank committed Dec 5, 2023
1 parent aad9d66 commit b5b855c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 46 deletions.
1 change: 1 addition & 0 deletions apps/admin-ui/src/i18n/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ const translations: ITranslations = {
"Max persons must be greater than min persons": [
"Maksimi henkilömäärä pitää olla suurempi kuin minimi henkilömäärä",
],
"Number must be greater than or equal to 0": ["Pitää olla vähintään 0"],
},
},
level: {
Expand Down
31 changes: 7 additions & 24 deletions apps/admin-ui/src/spa/ReservationUnit/edit/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { addDays, format } from "date-fns";
import { z } from "zod";
import { setTimeOnDate } from "@/component/reservations/utils";
import { checkLengthWithoutHtml } from "@/schemas";

export const PaymentTypes = ["ONLINE", "INVOICE", "ON_SITE"] as const;

Expand Down Expand Up @@ -160,8 +161,8 @@ export const ReservationUnitEditSchema = z
bufferTimeAfter: z.number(),
bufferTimeBefore: z.number(),
maxReservationsPerUser: z.number().nullable(),
maxPersons: z.number().nullable(),
minPersons: z.number().nullable(),
maxPersons: z.number().min(0).nullable(),
minPersons: z.number().min(0).nullable(),
maxReservationDuration: z.number().nullable(),
minReservationDuration: z.number().nullable(),
pk: z.number(),
Expand Down Expand Up @@ -330,27 +331,9 @@ export const ReservationUnitEditSchema = z
path: ["nameSv"],
});
}
if (v.descriptionEn === "") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Required",
path: ["descriptionEn"],
});
}
if (v.descriptionSv === "") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Required",
path: ["descriptionSv"],
});
}
if (v.descriptionFi === "") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Required",
path: ["descriptionFi"],
});
}
checkLengthWithoutHtml(v.descriptionEn, ctx, "descriptionEn", 1);
checkLengthWithoutHtml(v.descriptionFi, ctx, "descriptionFi", 1);
checkLengthWithoutHtml(v.descriptionSv, ctx, "descriptionSv", 1);

if (v.maxPersons && v.minPersons) {
if (v.maxPersons < v.minPersons) {
Expand Down Expand Up @@ -573,7 +556,7 @@ export const convertReservationUnit = (
paymentTypes: filterNonNullable(data?.paymentTypes?.map((pt) => pt?.code)),
pricings: convertPricingList(filterNonNullable(data?.pricings)),
images: filterNonNullable(data?.images).map((i) => convertImage(i)),
isDraft: data?.isArchived ?? false,
isDraft: data?.isDraft ?? false,
isArchived: data?.isArchived ?? false,
hasFuturePricing:
data?.pricings?.some(
Expand Down
49 changes: 27 additions & 22 deletions apps/admin-ui/src/spa/ReservationUnit/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -785,13 +785,13 @@ function BasicSection({
/>
<CustomNumberInput
name="maxPersons"
min={undefined}
min={0}
max={maxPersons}
form={form}
/>
<CustomNumberInput
name="minPersons"
min={undefined}
min={0}
max={watch("maxPersons") || 1}
form={form}
/>
Expand Down Expand Up @@ -1868,7 +1868,7 @@ const ReservationUnitEditor = ({

// ----------------------------- Constants ---------------------------------
const { getValues, setValue, watch, formState, handleSubmit } = form;
const { isDirty, isSubmitting } = formState;
const { isDirty: hasChanges, isSubmitting: isSaving } = formState;

const paymentTermsOptions = makeTermsOptions(
parametersData,
Expand Down Expand Up @@ -2017,13 +2017,31 @@ const ReservationUnitEditor = ({
}
};

const isSaving = isSubmitting;
const hasChanges = isDirty;
const handleBack = () => {
if (hasChanges) {
setModalContent(
<DiscardChangesDialog
onAccept={() => {
setModalContent(null);
history(-1);
}}
onClose={() => setModalContent(null)}
/>,
true
);
} else {
history(-1);
}
};

const previewDisabled =
isSaving ||
!reservationUnit?.pk ||
!reservationUnit?.uuid ||
!previewUrlPrefix;
const draftEnabled = hasChanges || !watch("isDraft");
const publishEnabled = hasChanges || watch("isDraft");
const archiveEnabled = watch("pk") !== 0 && !watch("isArchived");

return (
<form onSubmit={handleSubmit(onSubmit)} noValidate>
Expand Down Expand Up @@ -2069,9 +2087,7 @@ const ReservationUnitEditor = ({
<ArchiveButton
onClick={handleArchiveButtonClick}
variant="secondary"
disabled={
isSaving || getValues("pk") === 0 || getValues("isArchived")
}
disabled={isSaving || !archiveEnabled}
theme="black"
>
{t("ReservationUnitEditor.archive")}
Expand All @@ -2084,18 +2100,7 @@ const ReservationUnitEditor = ({
disabled={isSaving}
variant="supplementary"
iconLeft={<IconArrowLeft />}
onClick={() =>
setModalContent(
<DiscardChangesDialog
onAccept={() => {
setModalContent(null);
history(-1);
}}
onClose={() => setModalContent(null)}
/>,
true
)
}
onClick={handleBack}
>
{t("common.prev")}
</WhiteButton>
Expand All @@ -2116,7 +2121,7 @@ const ReservationUnitEditor = ({
<WhiteButton
size="small"
variant="secondary"
disabled={isSaving || !hasChanges}
disabled={isSaving || !draftEnabled}
isLoading={isSaving && watch("isDraft")}
type="button"
loadingText={t("ReservationUnitEditor.saving")}
Expand All @@ -2126,7 +2131,7 @@ const ReservationUnitEditor = ({
</WhiteButton>
<WhiteButton
variant="primary"
disabled={isSaving || !hasChanges}
disabled={isSaving || !publishEnabled}
isLoading={isSaving && !watch("isDraft")}
loadingText={t("ReservationUnitEditor.saving")}
type="button"
Expand Down

0 comments on commit b5b855c

Please sign in to comment.