-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompany.tsx
More file actions
265 lines (250 loc) · 11.1 KB
/
company.tsx
File metadata and controls
265 lines (250 loc) · 11.1 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { setCompanyMetadata } from "@/actions/auth";
import { createCompany } from "@/api/company";
import { createLocationBulk } from "@/api/location";
import LocationEditor from "@/components/LocationEditor";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { type Company, CompanyTypesEnum, CreateCompanyRequest, businessTypes } from "@/types/company";
import { CreateLocationBulkRequest, CreateLocationRequest } from "@/types/location";
import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from "@/components/ui/select";
import { useMutation } from "@tanstack/react-query";
import React, { useState } from "react";
import { Spinner } from "@/components/ui/spinner";
import { Card } from "@/components/ui/card";
import { IoAddCircleOutline } from "react-icons/io5";
interface CompanyInfoProps {
handleNext: () => void;
}
export default function Company({ handleNext: incrementNext }: CompanyInfoProps) {
const [companyError, setCompanyError] = useState<string | null>(null);
const [locError, setLocError] = useState<string | null>(null);
const [companyPayload, setCompanyPayload] = useState<CreateCompanyRequest>({
name: "",
businessOwnerFullName: "",
companyType: "LLC",
alternateEmail: "",
});
const [locationPayloads, setLocationPayloads] = useState<CreateLocationBulkRequest>([
{
alias: "",
streetAddress: "",
city: "",
stateProvince: "",
postalCode: "",
country: "",
},
]);
const [editingLocationIndex, setEditingLocationIndex] = useState<number | null>(0);
const updateLocation = (index: number, location: CreateLocationRequest) => {
const newLocations = [...locationPayloads];
newLocations[index] = location;
setLocationPayloads(newLocations);
};
const addLocation = () => {
setLocationPayloads([
...locationPayloads,
{
alias: "",
streetAddress: "",
city: "",
stateProvince: "",
postalCode: "",
country: "",
},
]);
setEditingLocationIndex(locationPayloads.length);
};
const removeLocation = (index: number) => {
setLocationPayloads((prev) => prev.filter((_, i) => i !== index));
setEditingLocationIndex(null);
};
const { isPending: isLocationPending, mutate: mutateLocation } = useMutation({
mutationFn: (payload: CreateLocationBulkRequest) => createLocationBulk(payload),
onSuccess: () => {
incrementNext();
},
onError: (_error: Error) => {
const errorMessage = _error.message || "Error creating locations. Check required fields and try again";
setLocError(errorMessage);
},
});
const {
isPending,
error: companyMutateError,
mutate,
} = useMutation<Company, Error, CreateCompanyRequest>({
mutationFn: (payload: CreateCompanyRequest) => createCompany(payload),
onError: (error: Error) => {
console.error("Error creating company:", error);
},
onSuccess: async (data: Company) => {
await setCompanyMetadata(data.id);
mutateLocation(locationPayloads);
},
});
const locationInProgress = () =>
locationPayloads.some((location) => {
return (
location.alias === "" ||
location.streetAddress === "" ||
location.city === "" ||
location.stateProvince === "" ||
location.postalCode === "" ||
location.country === ""
);
});
const handleAddLocation = () => {
if (locationInProgress()) {
setLocError("Please complete the current location before adding a new one.");
return;
}
setLocError(null);
addLocation();
};
const handleNext = () => {
if (
companyPayload.name === "" ||
companyPayload.businessOwnerFullName === "" /* || companyPayload.businessType === "" */
) {
setCompanyError("Please fill in all required fields.");
return;
}
if (locationPayloads.length === 0) {
setLocError("Please add at least one location.");
return;
}
if (locationInProgress()) {
setLocError("Please complete all location fields.");
return;
}
setEditingLocationIndex(null);
setCompanyError(null);
setLocError(null);
mutate(companyPayload);
};
return (
<Card className="w-full px-[163px] py-[127px]">
<div className="flex justify-center w-[492px]">
<label className="block text-[30px] text-black font-bold my-[30px]"> Business Information </label>
</div>
<div className="w-full flex flex-col items-center">
<div className="w-full flex gap-[16px]">
<div className="flex flex-col gap-[8px] w-full">
<Label htmlFor="name" className="text-[16px]">
Business Owner<span className="text-red-500 text-[16px]">*</span>
</Label>
<Input
id="name"
name="name"
type="text"
required
className="px-[28px] py-[16px] h-[45px] rounded-[10px] placeholder:text-gray-400 placeholder:text-[16px] bg-transparent text-[16px]"
onChange={(e) =>
setCompanyPayload({ ...companyPayload, businessOwnerFullName: e.target.value })
}
/>
</div>
<div className="flex flex-col gap-[8px] w-full pb-[16px]">
<Label htmlFor="businessType" className="text-[16px]">
Business Type<span className="text-red-500 text-[16px]">*</span>
</Label>
<Select
defaultValue={companyPayload.companyType}
onValueChange={(value: CompanyTypesEnum) => {
setCompanyPayload({ ...companyPayload, companyType: value });
}}
>
<SelectTrigger
id="businessType"
style={{
height: "45px",
width: "100%",
padding: "16px 28px",
fontSize: "16px",
borderRadius: "10px",
backgroundColor: "transparent",
}}
>
<SelectValue />
</SelectTrigger>
<SelectContent>
{businessTypes.map((type) => (
<SelectItem key={type} value={type} className="text-[16px]">
{type}
</SelectItem>
))}
</SelectContent>
</Select>
{/* add ^ onChange={(e) => setCompanyPayload({ ...companyPayload, businessType: e.target.value })} */}
</div>
</div>
<div className="flex flex-col gap-[8px] w-full mb-[30px]">
<Label htmlFor="owner" className="text-[16px]">
Business Name<span className="text-red-500 text-[16px]">*</span>
</Label>
<Input
id="owner"
name="owner"
type="text"
required
className="px-[28px] py-[16px] h-[45px] rounded-[10px] placeholder:text-gray-400 placeholder:text-[16px] bg-transparent text-[16px]"
onChange={(e) => setCompanyPayload({ ...companyPayload, name: e.target.value })}
/>
</div>
<div className="flex flex-col gap-[8px] w-full mb-[30px]">
<Label htmlFor="owner" className="text-[16px]">
Secondary Email (Optional)
</Label>
<Input
id="owner"
name="owner"
type="text"
className="px-[28px] py-[16px] h-[45px] rounded-[10px] placeholder:text-gray-400 placeholder:text-[16px] bg-transparent text-[16px]"
onChange={(e) => setCompanyPayload({ ...companyPayload, alternateEmail: e.target.value })}
/>
<div className="text-xs text-gray-600">
FEMA disaster notifications will additionally be sent to this email.
</div>
</div>
{locationPayloads.map((location, index) => (
<LocationEditor
key={index}
location={location}
setLocation={(loc) => updateLocation(index, loc as CreateLocationRequest)}
removeLocation={() => removeLocation(index)}
isExpanded={editingLocationIndex === index}
onExpand={() =>
editingLocationIndex === index
? setEditingLocationIndex(null)
: setEditingLocationIndex(index)
}
onCollapse={() => setEditingLocationIndex(null)}
/>
))}
<Button
className="w-[196px] flex items-center text-[16px] h-[34px] self-start px-[12px] py-[4px] underline bg-slate hover:text-gray-600"
onClick={handleAddLocation}
>
<IoAddCircleOutline /> Add a location
</Button>
</div>
<div className="w-full flex flex-col gap-2 items-center">
{(locError || companyError || companyMutateError) && (
<p className="text-red-500 text-sm mt-[-16px]">
{companyError || locError || companyMutateError?.message}{" "}
</p>
)}
<Button
type="button"
onClick={handleNext}
disabled={isPending || isLocationPending}
className="max-h-[45px] w-fit bg-[var(--fuchsia)] text-white px-[20px] py-[12px] text-[16px]"
>
{isPending || isLocationPending ? <Spinner /> : <></>}
Next
</Button>
</div>
</Card>
);
}