-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFacebookIntegrationDetail.tsx
More file actions
186 lines (177 loc) · 5.44 KB
/
FacebookIntegrationDetail.tsx
File metadata and controls
186 lines (177 loc) · 5.44 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { Cell } from '@tanstack/table-core';
import { FacebookIntegrationFormSheet } from './FacebookIntegrationForm';
import { IIntegrationDetail } from '@/integrations/types/Integration';
import {
Button,
Dialog,
Separator,
Sheet,
Spinner,
Form,
Input,
toast,
} from 'erxes-ui';
import { IconEdit } from '@tabler/icons-react';
import { useIntegrationDetail } from '@/integrations/hooks/useIntegrationDetail';
import { z } from 'zod';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { useEffect, useState } from 'react';
import { SelectBrand } from 'ui-modules';
import { SelectChannel } from '@/inbox/channel/components/SelectChannel';
import { useIntegrationEdit } from '@/integrations/hooks/useIntegrationEdit';
import { FACEBOOK_INTEGRATION_SCHEMA } from '@/integrations/facebook/constants/FbMessengerSchema';
export const FacebookIntegrationDetail = ({ isPost }: { isPost?: boolean }) => {
return (
<div>
<FacebookIntegrationFormSheet isPost={isPost} />
</div>
);
};
export const FacebookIntegrationActions = ({
cell,
}: {
cell: Cell<IIntegrationDetail, unknown>;
}) => {
return <FacebookIntegrationEditSheet id={cell.row.original._id} />;
};
export const FacebookIntegrationEditSheet = ({ id }: { id: string }) => {
const [open, setOpen] = useState(false);
return (
<Dialog open={open} onOpenChange={setOpen}>
<Dialog.Trigger asChild>
<Button variant="outline" size="icon">
<IconEdit />
</Button>
</Dialog.Trigger>
<Dialog.Content className="p-0 gap-0 border-0 shadow-lg">
<FacebookIntegrationEditForm id={id} setOpen={setOpen} />
</Dialog.Content>
</Dialog>
);
};
export const FacebookIntegrationEditForm = ({
id,
setOpen,
}: {
id: string;
setOpen: (open: boolean) => void;
}) => {
const { loading, integrationDetail } = useIntegrationDetail({
integrationId: id,
});
const { editIntegration, loading: editLoading } = useIntegrationEdit();
const form = useForm<z.infer<typeof FACEBOOK_INTEGRATION_SCHEMA>>({
resolver: zodResolver(FACEBOOK_INTEGRATION_SCHEMA),
});
useEffect(() => {
if (integrationDetail) {
form.reset(integrationDetail);
}
}, [integrationDetail, form]);
const onSubmit = (data: z.infer<typeof FACEBOOK_INTEGRATION_SCHEMA>) => {
editIntegration({
variables: {
_id: id,
...data,
},
onCompleted: () => {
setOpen(false);
toast({ title: 'Integration updated' });
},
onError: (error) => {
toast({ title: error.message, variant: 'destructive' });
},
});
};
if (loading) return <Spinner className="p-20" />;
return (
<>
<Dialog.Header className="flex-row items-center justify-between space-y-0 px-4 py-3">
<Dialog.Title>{integrationDetail?.name}</Dialog.Title>
<Sheet.Close />
</Dialog.Header>
<Separator />
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit, (error) => {
console.log(error);
})}
>
<div className="p-6 pb-8 space-y-6">
<Form.Field
name="facebookPage"
render={({ field }) => {
const facebookPageName = field.value?.[0] ?? null;
return (
<Form.Item>
<Form.Label>Page Name</Form.Label>
<Form.Control>
<Input
name={field.name}
value={facebookPageName?.name || ''}
disabled
readOnly
/>
</Form.Control>
</Form.Item>
);
}}
/>
<Form.Field
name="name"
render={({ field }) => (
<Form.Item>
<Form.Label>Name</Form.Label>
<Form.Control>
<Input {...field} />
</Form.Control>
</Form.Item>
)}
/>
<Form.Field
name="brandId"
render={({ field }) => (
<Form.Item>
<Form.Label>Brand</Form.Label>
<SelectBrand.FormItem
value={field.value}
onValueChange={field.onChange}
/>
<Form.Message />
</Form.Item>
)}
/>
<Form.Field
name="channels"
render={({ field }) => {
return (
<Form.Item>
<Form.Label>Channels</Form.Label>
<SelectChannel.FormItem
value={field.value}
onValueChange={field.onChange}
mode="multiple"
/>
<Form.Message />
</Form.Item>
);
}}
/>
</div>
<Separator />
<Dialog.Footer className="flex justify-end py-4 px-6">
<Dialog.Close asChild>
<Button disabled={loading || editLoading} variant="ghost">
Close
</Button>
</Dialog.Close>
<Button type="submit" disabled={loading || editLoading}>
Save
</Button>
</Dialog.Footer>
</form>
</Form>
</>
);
};