rename vars and add validation

This commit is contained in:
Josh Hawkins 2024-12-08 12:27:12 -06:00
parent 2fac0dd1ad
commit 1ced240956
2 changed files with 120 additions and 108 deletions

View File

@ -82,7 +82,7 @@ export default function ZoneEditPane({
} }
}, [polygon, config]); }, [polygon, config]);
const [topWidth, bottomWidth, leftDepth, rightDepth] = useMemo(() => { const [lineA, lineB, lineC, lineD] = useMemo(() => {
const distances = const distances =
polygon?.camera && polygon?.camera &&
polygon?.name && polygon?.name &&
@ -93,7 +93,8 @@ export default function ZoneEditPane({
: [0, 0, 0, 0]; : [0, 0, 0, 0];
}, [polygon, config]); }, [polygon, config]);
const formSchema = z.object({ const formSchema = z
.object({
name: z name: z
.string() .string()
.min(2, { .min(2, {
@ -152,35 +153,47 @@ export default function ZoneEditPane({
review_alerts: z.boolean().default(false).optional(), review_alerts: z.boolean().default(false).optional(),
review_detections: z.boolean().default(false).optional(), review_detections: z.boolean().default(false).optional(),
speedEstimation: z.boolean().default(false), speedEstimation: z.boolean().default(false),
topWidth: z.coerce lineA: z.coerce
.number() .number()
.min(0.1, { .min(0.1, {
message: "Distance must be greater than or equal to 0.1", message: "Distance must be greater than or equal to 0.1",
}) })
.optional() .optional()
.or(z.literal("")), .or(z.literal("")),
bottomWidth: z.coerce lineB: z.coerce
.number() .number()
.min(0.1, { .min(0.1, {
message: "Distance must be greater than or equal to 0.1", message: "Distance must be greater than or equal to 0.1",
}) })
.optional() .optional()
.or(z.literal("")), .or(z.literal("")),
leftDepth: z.coerce lineC: z.coerce
.number() .number()
.min(0.1, { .min(0.1, {
message: "Distance must be greater than or equal to 0.1", message: "Distance must be greater than or equal to 0.1",
}) })
.optional() .optional()
.or(z.literal("")), .or(z.literal("")),
rightDepth: z.coerce lineD: z.coerce
.number() .number()
.min(0.1, { .min(0.1, {
message: "Distance must be greater than or equal to 0.1", message: "Distance must be greater than or equal to 0.1",
}) })
.optional() .optional()
.or(z.literal("")), .or(z.literal("")),
}); })
.refine(
(data) => {
if (data.speedEstimation) {
return !!data.lineA && !!data.lineB && !!data.lineC && !!data.lineD;
}
return true;
},
{
message: "All distance fields must be filled to use speed estimation.",
path: ["speedEstimation"],
},
);
const form = useForm<z.infer<typeof formSchema>>({ const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema), resolver: zodResolver(formSchema),
@ -197,11 +210,11 @@ export default function ZoneEditPane({
config?.cameras[polygon.camera]?.zones[polygon.name]?.loitering_time, config?.cameras[polygon.camera]?.zones[polygon.name]?.loitering_time,
isFinished: polygon?.isFinished ?? false, isFinished: polygon?.isFinished ?? false,
objects: polygon?.objects ?? [], objects: polygon?.objects ?? [],
speedEstimation: !!(topWidth || bottomWidth || leftDepth || rightDepth), speedEstimation: !!(lineA || lineB || lineC || lineD),
topWidth, lineA,
bottomWidth, lineB,
leftDepth, lineC,
rightDepth, lineD,
}, },
}); });
@ -213,10 +226,10 @@ export default function ZoneEditPane({
loitering_time, loitering_time,
objects: form_objects, objects: form_objects,
speedEstimation, speedEstimation,
topWidth, lineA,
bottomWidth, lineB,
leftDepth, lineC,
rightDepth, lineD,
}: ZoneFormValuesType, // values submitted via the form }: ZoneFormValuesType, // values submitted via the form
objects: string[], objects: string[],
) => { ) => {
@ -314,9 +327,7 @@ export default function ZoneEditPane({
} }
let distancesQuery = ""; let distancesQuery = "";
const distances = [topWidth, bottomWidth, leftDepth, rightDepth].join( const distances = [lineA, lineB, lineC, lineD].join(",");
",",
);
if (speedEstimation) { if (speedEstimation) {
distancesQuery = `&cameras.${polygon?.camera}.zones.${zoneName}.distances=${distances}`; distancesQuery = `&cameras.${polygon?.camera}.zones.${zoneName}.distances=${distances}`;
} else { } else {
@ -543,7 +554,7 @@ export default function ZoneEditPane({
polygons[activePolygonIndex].points.length !== 4 polygons[activePolygonIndex].points.length !== 4
) { ) {
toast.error( toast.error(
"Zones with speed estimation must have exactly 4 points", "Zones with speed estimation must have exactly 4 points.",
); );
return; return;
} }
@ -557,6 +568,7 @@ export default function ZoneEditPane({
Enable speed estimation for objects in this zone. The zone Enable speed estimation for objects in this zone. The zone
must have exactly 4 points. must have exactly 4 points.
</FormDescription> </FormDescription>
<FormMessage />
</FormItem> </FormItem>
)} )}
/> />
@ -568,7 +580,7 @@ export default function ZoneEditPane({
<> <>
<FormField <FormField
control={form.control} control={form.control}
name="topWidth" name="lineA"
render={({ field }) => ( render={({ field }) => (
<FormItem> <FormItem>
<FormLabel>Line A distance</FormLabel> <FormLabel>Line A distance</FormLabel>
@ -584,7 +596,7 @@ export default function ZoneEditPane({
/> />
<FormField <FormField
control={form.control} control={form.control}
name="bottomWidth" name="lineB"
render={({ field }) => ( render={({ field }) => (
<FormItem> <FormItem>
<FormLabel>Line B distance</FormLabel> <FormLabel>Line B distance</FormLabel>
@ -600,7 +612,7 @@ export default function ZoneEditPane({
/> />
<FormField <FormField
control={form.control} control={form.control}
name="leftDepth" name="lineC"
render={({ field }) => ( render={({ field }) => (
<FormItem> <FormItem>
<FormLabel>Line C distance</FormLabel> <FormLabel>Line C distance</FormLabel>
@ -616,7 +628,7 @@ export default function ZoneEditPane({
/> />
<FormField <FormField
control={form.control} control={form.control}
name="rightDepth" name="lineD"
render={({ field }) => ( render={({ field }) => (
<FormItem> <FormItem>
<FormLabel>Line D distance</FormLabel> <FormLabel>Line D distance</FormLabel>

View File

@ -20,10 +20,10 @@ export type ZoneFormValuesType = {
isFinished: boolean; isFinished: boolean;
objects: string[]; objects: string[];
speedEstimation: boolean; speedEstimation: boolean;
topWidth: number; lineA: number;
bottomWidth: number; lineB: number;
leftDepth: number; lineC: number;
rightDepth: number; lineD: number;
}; };
export type ObjectMaskFormValuesType = { export type ObjectMaskFormValuesType = {