Skip to content

Commit

Permalink
better notification error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerFisher committed Jan 10, 2025
1 parent 4cb54ed commit f8cb125
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
15 changes: 5 additions & 10 deletions app/components/forms/NotificationGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ const NotificationGroup = ({
});
},
}}
errors={fields.name.errors}
/>
<Text as="label" size="3" htmlFor="format">
<strong>Delivery format</strong>
Expand All @@ -187,9 +186,6 @@ const NotificationGroup = ({
<RadioGroup.Item value="email">Email</RadioGroup.Item>
<RadioGroup.Item value="rss">RSS</RadioGroup.Item>
</RadioGroup.Root>
{fields.format.errors && (
<ErrorCallout error={fields.format.errors[0]} />
)}
<Box my="4">
<Text as="label" size="3">
<strong>Filters</strong>
Expand Down Expand Up @@ -233,11 +229,6 @@ const NotificationGroup = ({
</Box>
</Card>
</Box>
{fields.queries.errors && (
<Box mb="4">
<ErrorCallout error={fields.queries.errors[0]} />
</Box>
)}
{(testFetcher.data || testFetcher.data === 0) && (
<Box width="100%" my="4">
<strong>
Expand Down Expand Up @@ -289,7 +280,11 @@ const NotificationGroup = ({
lastResult?.initialValue.id === group.id && (
<Box mt="4">
<Text as="p">
<strong>Your notification settings have been saved.</strong>
{lastResult?.status === "success" ? (
<strong>Your notification settings have been saved.</strong>
) : (
<Text>Error: this notification group already exists</Text>
)}
</Text>
</Box>
)}
Expand Down
7 changes: 5 additions & 2 deletions app/routes/notifications/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const action = async ({ request }: Route.ActionArgs) => {
code: z.ZodIssueCode.custom,
message: "At least one query item is required",
});
return;
}

const existingGroups = await db.query.notificationGroup.findMany({
Expand All @@ -69,27 +70,29 @@ export const action = async ({ request }: Route.ActionArgs) => {
for (const group of existingGroups) {
if (group.name === data.name && group.id !== data.id) {
ctx.addIssue({
path: ["name"],
code: z.ZodIssueCode.custom,
message: "A group with this name already exists",
});
return;
}

if (
JSON.stringify(group.query) === JSON.stringify(data.queries) &&
group.id !== data.id
) {
ctx.addIssue({
path: ["queries"],
code: z.ZodIssueCode.custom,
message: "A group with these queries already exists",
});
return;
}
}
}),
async: true,
});

console.log(submission.status);

if (submission.status !== "success") {
return data(
{ result: submission.reply() },
Expand Down

0 comments on commit f8cb125

Please sign in to comment.