Skip to content

Commit

Permalink
bug, reset state when form is submitted
Browse files Browse the repository at this point in the history
  • Loading branch information
sinamics committed Jul 26, 2023
1 parent 61798cf commit 8c2e2fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/components/modules/networkRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import { type RoutesEntity } from "~/types/network";
import { type ChangeEvent, useState } from "react";
import { type ErrorData } from "~/types/errorHandling";

const initialRouteInput = {
target: "",
via: "",
};

export const NettworkRoutes = () => {
const [showRouteInput, setShowRouteInput] = useState<boolean>(false);
const [routeInput, setRouteInput] = useState<RoutesEntity>();
const [routeInput, setRouteInput] = useState<RoutesEntity>(initialRouteInput);

const { query } = useRouter();
const {
Expand Down Expand Up @@ -64,6 +69,7 @@ export const NettworkRoutes = () => {
onSuccess: () => {
void refecthNetworkById();
setShowRouteInput(false);
setRouteInput(initialRouteInput);
},
}
);
Expand Down
18 changes: 12 additions & 6 deletions src/server/api/routers/networkRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@ function isValidDomain(domain: string): boolean {
return domainRegex.test(domain);
}
const RouteSchema = z.object({
target: z.string().refine(isValidCIDR, {
message: "Destination IP must be a valid CIDR notation!",
}),
target: z
.string()
.optional()
.refine((val) => val !== undefined && isValidCIDR(val), {
message: "Destination IP must be a valid CIDR notation!",
}),
via: z
.union([
z.string().refine(isValidIP, {
message: "Via IP must be a valid IP address!",
}),
z
.string()
.optional()
.refine((val) => !val || isValidIP(val), {
message: "Via IP must be a valid IP address!",
}),
z.null(),
])
.optional(),
Expand Down

0 comments on commit 8c2e2fb

Please sign in to comment.