forked from adrianhajdin/uber
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
60 lines (54 loc) · 1.63 KB
/
index.ts
File metadata and controls
60 lines (54 loc) · 1.63 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
import { create } from "zustand";
import { DriverStore, LocationStore, MarkerData } from "@/types/type";
export const useLocationStore = create<LocationStore>((set) => ({
userLatitude: null,
userLongitude: null,
userAddress: null,
destinationLatitude: null,
destinationLongitude: null,
destinationAddress: null,
setUserLocation: ({
latitude,
longitude,
address,
}: {
latitude: number;
longitude: number;
address: string;
}) => {
set(() => ({
userLatitude: latitude,
userLongitude: longitude,
userAddress: address,
}));
// if driver is selected and now new location is set, clear the selected driver
const { selectedDriver, clearSelectedDriver } = useDriverStore.getState();
if (selectedDriver) clearSelectedDriver();
},
setDestinationLocation: ({
latitude,
longitude,
address,
}: {
latitude: number;
longitude: number;
address: string;
}) => {
set(() => ({
destinationLatitude: latitude,
destinationLongitude: longitude,
destinationAddress: address,
}));
// if driver is selected and now new location is set, clear the selected driver
const { selectedDriver, clearSelectedDriver } = useDriverStore.getState();
if (selectedDriver) clearSelectedDriver();
},
}));
export const useDriverStore = create<DriverStore>((set) => ({
drivers: [] as MarkerData[],
selectedDriver: null,
setSelectedDriver: (driverId: number) =>
set(() => ({ selectedDriver: driverId })),
setDrivers: (drivers: MarkerData[]) => set(() => ({ drivers })),
clearSelectedDriver: () => set(() => ({ selectedDriver: null })),
}));