Skip to content

Commit

Permalink
Fix up saving multi condition form
Browse files Browse the repository at this point in the history
  • Loading branch information
elie222 committed Dec 21, 2024
1 parent 3ecbf70 commit c037166
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
19 changes: 10 additions & 9 deletions apps/web/app/(app)/automation/RuleForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
SectionDescription,
TypographyH3,
} from "@/components/Typography";
import { ActionType, type CategoryFilterType, RuleType } from "@prisma/client";
import { ActionType, CategoryFilterType, RuleType } from "@prisma/client";
import { createRuleAction, updateRuleAction } from "@/utils/actions/rule";
import {
type CreateRuleBody,
Expand Down Expand Up @@ -163,8 +163,6 @@ export function RuleForm({ rule }: { rule: CreateRuleBody & { id?: string } }) {
return unusedCondition;
}, [conditions]);

console.log("errors", errors);

// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
useEffect(() => {
trigger("conditions");
Expand Down Expand Up @@ -314,7 +312,7 @@ export function RuleForm({ rule }: { rule: CreateRuleBody & { id?: string } }) {
<>
<div className="flex items-center gap-4">
<RadioGroup
defaultValue="include"
defaultValue={CategoryFilterType.INCLUDE}
value={
watch(`conditions.${index}.categoryFilterType`) ||
undefined
Expand All @@ -328,11 +326,17 @@ export function RuleForm({ rule }: { rule: CreateRuleBody & { id?: string } }) {
className="flex gap-6"
>
<div className="flex items-center space-x-2">
<RadioGroupItem value="include" id="include" />
<RadioGroupItem
value={CategoryFilterType.INCLUDE}
id="include"
/>
<Label name="include" label="Match" />
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="exclude" id="exclude" />
<RadioGroupItem
value={CategoryFilterType.EXCLUDE}
id="exclude"
/>
<Label name="exclude" label="Skip" />
</div>
</RadioGroup>
Expand Down Expand Up @@ -682,9 +686,6 @@ function GroupsTab(props: {
{...props.registerProps}
// TODO: fix this
// error={props.errors.groupId}
// error={
// errors.conditions?.[index]?.type as FieldError | undefined
// }
/>
</div>
)}
Expand Down
6 changes: 4 additions & 2 deletions apps/web/utils/actions/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export const createRuleAction = withActionInstrumentation(
const rule = await prisma.rule.create({
data: {
name: body.name || "",
instructions: body.instructions || "",
automate: body.automate ?? undefined,
runOnThreads: body.runOnThreads ?? undefined,
actions: body.actions
Expand All @@ -58,6 +57,8 @@ export const createRuleAction = withActionInstrumentation(
}
: undefined,
userId: session.user.id,
// conditions
instructions: conditions.instructions || null,
from: conditions.from || null,
to: conditions.to || null,
subject: conditions.subject || null,
Expand Down Expand Up @@ -119,10 +120,11 @@ export const updateRuleAction = withActionInstrumentation(
prisma.rule.update({
where: { id: body.id, userId: session.user.id },
data: {
instructions: body.instructions || "",
automate: body.automate ?? undefined,
runOnThreads: body.runOnThreads ?? undefined,
name: body.name || undefined,
// conditions
instructions: conditions.instructions || null,
from: conditions.from || null,
to: conditions.to || null,
subject: conditions.subject || null,
Expand Down
21 changes: 9 additions & 12 deletions apps/web/utils/actions/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,34 @@ export const zodRuleType = z.enum([
]);

const zodAiCondition = z.object({
type: z.literal(RuleType.AI),
instructions: z.string(),
instructions: z.string().nullish(),
});

const zodGroupCondition = z.object({
type: z.literal(RuleType.GROUP),
groupId: z.string(),
groupId: z.string().nullish(),
});

const zodStaticCondition = z.object({
type: z.literal(RuleType.STATIC),
to: z.string().nullish(),
from: z.string().nullish(),
subject: z.string().nullish(),
body: z.string().nullish(),
});

const zodCategoryCondition = z.object({
type: z.literal(RuleType.CATEGORY),
categoryFilterType: z
.enum([CategoryFilterType.INCLUDE, CategoryFilterType.EXCLUDE])
.nullish(),
categoryFilters: z.array(z.string()).nullish(),
});

const zodCondition = z.union([
zodAiCondition,
zodGroupCondition,
zodStaticCondition,
zodCategoryCondition,
]);
const zodCondition = z.object({
type: zodRuleType,
...zodAiCondition.shape,
...zodGroupCondition.shape,
...zodStaticCondition.shape,
...zodCategoryCondition.shape,
});
export type ZodCondition = z.infer<typeof zodCondition>;

export const createRuleBody = z.object({
Expand Down
2 changes: 1 addition & 1 deletion apps/web/utils/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const staticEmptyCondition = {

const categoryEmptyCondition = {
type: RuleType.CATEGORY,
categoryFilterType: CategoryFilterType.INCLUDE,
categoryFilterType: null,
categoryFilters: null,
};

Expand Down

0 comments on commit c037166

Please sign in to comment.