-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocationsCard.tsx
More file actions
179 lines (161 loc) · 6.81 KB
/
LocationsCard.tsx
File metadata and controls
179 lines (161 loc) · 6.81 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
"use client";
import { getCompanyLocations } from "@/api/company";
import { createLocation, deleteLocation, updateLocationAddress } from "@/api/location";
import LocationEditor from "@/components/LocationEditor";
import Loading from "@/components/loading";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { CreateLocationRequest, Location, UpdateLocationRequest } from "@/types/location";
import { useMutation, useQuery } from "@tanstack/react-query";
import { useEffect, useState } from "react";
import { IoAddCircleOutline } from "react-icons/io5";
export default function LocationsCard({
locationSelected,
onLocationSelect,
}: {
locationSelected?: Location["id"] | null;
onLocationSelect?: (locationId: Location["id"]) => void;
}) {
const [locationInfo, setLocationInfo] = useState<(CreateLocationRequest | UpdateLocationRequest)[]>([]);
const [editingLocationIndex, setEditingLocationIndex] = useState<number | null>(null);
const [saveError, setSaveError] = useState<string | null>(null);
const { data: locationsQuery, isPending: locationPending } = useQuery({
queryKey: ["locations"],
queryFn: getCompanyLocations,
});
const { mutate: updateLocationsMutate } = useMutation({
mutationFn: (location: UpdateLocationRequest) => updateLocationAddress(location),
onSuccess: () => {
setSaveError(null);
setEditingLocationIndex(null);
},
onError: (error: Error) => {
if (error.message.includes("postalCode")) {
setSaveError("Error updating location. Please check postal code details and try again.");
} else {
const errorMessage = error.message || "Error updating location. Check required fields and try again";
setSaveError(errorMessage);
}
},
});
const { mutate: createLocationMutate } = useMutation({
mutationFn: (location: CreateLocationRequest) => createLocation(location),
onSuccess: () => {
setSaveError(null);
setEditingLocationIndex(null);
},
onError: (error: Error) => {
if (error.message.includes("postalCode")) {
setSaveError("Error creating location. Please check postal code details and try again.");
} else {
const errorMessage = error.message || "Error creating location. Check required fields and try again";
setSaveError(errorMessage);
}
},
});
const { mutate: deleteLocationMutate } = useMutation({
mutationFn: (locationId: string) => deleteLocation(locationId),
onSuccess: () => {
setSaveError(null);
setEditingLocationIndex(null);
},
onError: (_error: Error) => {
const errorMessage = _error.message || "Error removing location. Check required fields and try again";
setSaveError(errorMessage);
},
});
const updateLocation = (index: number, location: CreateLocationRequest | UpdateLocationRequest) => {
const newLocations = [...locationInfo];
newLocations[index] = location;
setLocationInfo(newLocations);
};
const removeLocation = (index: number) => {
const location = locationInfo[index];
if ("id" in location && location.id) {
deleteLocationMutate(location.id);
}
setLocationInfo((prev) => prev.filter((_, i) => i !== index));
setEditingLocationIndex(null);
};
const addLocation = () => {
setLocationInfo([
...locationInfo,
{
alias: "",
streetAddress: "",
city: "",
stateProvince: "",
postalCode: "",
country: "",
},
]);
setEditingLocationIndex(locationInfo.length);
};
const handleSave = () => {
if (editingLocationIndex === null) return;
const location = locationInfo[editingLocationIndex];
if (
!location.alias ||
!location.streetAddress ||
!location.city ||
!location.stateProvince ||
!location.postalCode ||
!location.country
) {
setSaveError("Please fill in all required fields before saving.");
return;
}
if ("id" in location) {
updateLocationsMutate(location as UpdateLocationRequest);
} else {
createLocationMutate(location as CreateLocationRequest);
}
};
useEffect(() => {
if (locationsQuery) {
setLocationInfo(locationsQuery);
}
}, [locationsQuery]);
return (
<Card className="p-[28px] flex gap-[12px] border-none shadow-none">
<p className="font-bold text-[20px]">Locations</p>
{locationPending ? (
<Loading lines={2} />
) : (
<div>
<div className="grid grid-cols-2 gap-x-[38px] gap-y-[16px]">
{locationInfo.map((location, index) => (
<div key={index}>
<LocationEditor
location={location}
isSelected={"id" in location && locationSelected === location.id}
onClick={
onLocationSelect && "id" in location
? () => onLocationSelect(location.id)
: undefined
}
setLocation={(loc) => updateLocation(index, loc)}
removeLocation={() => removeLocation(index)}
isExpanded={editingLocationIndex === index}
onExpand={() =>
editingLocationIndex === index
? setEditingLocationIndex(null)
: setEditingLocationIndex(index)
}
onCollapse={() => handleSave()}
saveError={saveError}
/>
</div>
))}
</div>
<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={addLocation}
>
<IoAddCircleOutline /> Add a location
</Button>
</div>
)}
</Card>
);
}