-
Notifications
You must be signed in to change notification settings - Fork 732
/
Copy pathColdEmailSettings.tsx
128 lines (118 loc) · 3.53 KB
/
ColdEmailSettings.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"use client";
import { useCallback, useMemo } from "react";
import { Controller, type SubmitHandler, useForm } from "react-hook-form";
import { LoadingContent } from "@/components/LoadingContent";
import { toastError, toastSuccess } from "@/components/Toast";
import { zodResolver } from "@hookform/resolvers/zod";
import { ColdEmailSetting } from "@/generated/prisma";
import { isActionError } from "@/utils/error";
import { Button } from "@/components/ui/button";
import {
type UpdateColdEmailSettingsBody,
updateColdEmailSettingsBody,
} from "@/utils/actions/cold-email.validation";
import { updateColdEmailSettingsAction } from "@/utils/actions/cold-email";
import { ColdEmailPromptForm } from "@/app/(app)/cold-email-blocker/ColdEmailPromptForm";
import { RadioGroup } from "@/components/RadioGroup";
import { useUser } from "@/hooks/useUser";
export function ColdEmailSettings() {
const { data, isLoading, error, mutate } = useUser();
return (
<LoadingContent loading={isLoading} error={error}>
{data && (
<div className="space-y-10">
<ColdEmailForm coldEmailBlocker={data.coldEmailBlocker} />
<ColdEmailPromptForm
coldEmailPrompt={data.coldEmailPrompt}
onSuccess={mutate}
/>
</div>
)}
</LoadingContent>
);
}
export function ColdEmailForm({
coldEmailBlocker,
buttonText,
onSuccess,
}: {
coldEmailBlocker?: ColdEmailSetting | null;
buttonText?: string;
onSuccess?: () => void;
}) {
const {
control,
handleSubmit,
formState: { errors, isSubmitting },
} = useForm<UpdateColdEmailSettingsBody>({
resolver: zodResolver(updateColdEmailSettingsBody),
defaultValues: {
coldEmailBlocker: coldEmailBlocker || ColdEmailSetting.DISABLED,
},
});
const onSubmit: SubmitHandler<UpdateColdEmailSettingsBody> = useCallback(
async (data) => {
const result = await updateColdEmailSettingsAction(data);
if (isActionError(result)) {
toastError({
description: "There was an error updating the settings.",
});
} else {
toastSuccess({ description: "Settings updated!" });
onSuccess?.();
}
},
[onSuccess],
);
const onSubmitForm = handleSubmit(onSubmit);
const options: {
value: ColdEmailSetting;
label: string;
description: string;
}[] = useMemo(
() => [
{
value: ColdEmailSetting.ARCHIVE_AND_READ_AND_LABEL,
label: "Archive, Mark Read & Label",
description: "Archive cold emails, mark them as read, and label them",
},
{
value: ColdEmailSetting.ARCHIVE_AND_LABEL,
label: "Archive & Label",
description: "Archive cold emails and label them",
},
{
value: ColdEmailSetting.LABEL,
label: "Label Only",
description: "Label cold emails, but keep them in my inbox",
},
{
value: ColdEmailSetting.DISABLED,
label: "Turn Off",
description: "Disable cold email blocker",
},
],
[],
);
return (
<form onSubmit={onSubmitForm} className="max-w-lg">
<Controller
name="coldEmailBlocker"
control={control}
render={({ field }) => (
<RadioGroup
label="How should we handle cold emails?"
options={options}
{...field}
error={errors.coldEmailBlocker}
/>
)}
/>
<div className="mt-2">
<Button type="submit" loading={isSubmitting}>
{buttonText || "Save"}
</Button>
</div>
</form>
);
}