-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorders.tsx
217 lines (197 loc) · 7.56 KB
/
orders.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
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
import { Layout } from "@/screens/partials/layout.tsx";
import useApi, { SettingsData } from "@/api/db/use.api.ts";
import { Order as OrderModel, OrderStatus } from "@/api/model/order.ts";
import { Tables } from "@/api/db/tables.ts";
import { useEffect, useState } from "react";
import { useDB } from "@/api/db/db.ts";
import { Order } from "@/components/orders/order";
import ScrollContainer from "react-indiana-drag-scroll";
import { Floor } from "@/api/model/floor.ts";
import { ReactSelect } from "@/components/common/input/custom.react.select.tsx";
import { User } from "@/api/model/user.ts";
import { useAtom } from "jotai/index";
import { appSettings } from "@/store/jotai.ts";
import { OrderType } from "@/api/model/order_type.ts";
import { DatePicker } from "@/components/common/react-aria/datepicker.tsx";
import { getLocalTimeZone, today } from '@internationalized/date';
import { DateValue } from "react-aria-components";
import { Button } from "@/components/common/input/button.tsx";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBars, faTableColumns } from "@fortawesome/free-solid-svg-icons";
export const Orders = () => {
const db = useDB();
const [liveQuery, setLiveQuery] = useState(null);
const [params, setParams] = useAtom(appSettings);
const [date, setDate] = useState<DateValue>(today(getLocalTimeZone()));
const [view, setView] = useState<'row' | 'column'>('row');
const {
data: orders,
fetchData: fetchOrders,
addFilter: addOrderFilter,
resetFilters: resetOrdersFilters
} = useApi<SettingsData<OrderModel>>(Tables.orders, [], ['created_at asc'], 0, 99999, ['items', 'items.item', 'item.item.modifiers', 'table', 'user', 'order_type', 'customer', 'discount', 'tax', 'payments', 'payments.order_payment', 'extras', 'extras.order_extras']);
const {
data: floors,
} = useApi<SettingsData<Floor>>(Tables.floors, [], ['priority asc'], 0, 99999);
const {
data: users,
} = useApi<SettingsData<User>>(Tables.users, [], [], 0, 99999);
const {
data: orderTypes,
} = useApi<SettingsData<OrderType>>(Tables.order_types, [], [], 0, 99999);
const runLiveQuery = async () => {
const result = await db.live(Tables.orders, function () {
fetchOrders();
});
setLiveQuery(result);
}
useEffect(() => {
runLiveQuery().then();
return () => {
db.db.kill(liveQuery).then(() => console.log('live query killed'));
}
}, []);
useEffect(() => {
resetOrdersFilters();
const floorFilters = [];
const userFilters = [];
const orderTypeFilters = [];
const statusFilters = [];
params.ordersFilters.floors.forEach(floor => {
floorFilters.push(`floor = ${floor.value}`);
});
if( floorFilters.length > 0 ) {
addOrderFilter(`(${floorFilters.join(' or ')})`);
}
params.ordersFilters.users.forEach(user => {
userFilters.push(`user = ${user.value}`);
});
if( userFilters.length > 0 ) {
addOrderFilter(`(${userFilters.join(' or ')})`);
}
params.ordersFilters.statuses.forEach(status => {
statusFilters.push(`status = "${status.value}"`);
});
if( statusFilters.length > 0 ) {
addOrderFilter(`(${statusFilters.join(' or ')})`);
}
params.ordersFilters.orderTypes.forEach(order_type => {
orderTypeFilters.push(`order_type = ${order_type.value}`);
});
if( orderTypeFilters.length > 0 ) {
addOrderFilter(`(${orderTypeFilters.join(' or ')})`);
}
if( date ) {
addOrderFilter(`time::format(created_at, "%Y-%m-%d") = "${date?.toString()}"`);
}
}, [params.ordersFilters, date]);
return (
<Layout containerClassName="overflow-hidden">
<div className="flex gap-5 p-3 flex-col">
<div className="h-[60px] flex-0 rounded-xl bg-white flex items-center px-3 gap-3">
<div className="min-w-[200px]">
<ReactSelect
options={[OrderStatus["In Progress"], OrderStatus.Paid, OrderStatus.Completed].map(item => ({
label: item,
value: item
}))}
isMulti
placeholder="Select order status"
value={params.ordersFilters.statuses}
onChange={(value: any) => {
setParams(prev => ({
...prev,
ordersFilters: {
...prev.ordersFilters,
statuses: value,
}
}))
}}
/>
</div>
<div className="min-w-[200px]">
<ReactSelect
options={orderTypes?.data.map(item => ({
label: item.name,
value: item.id
}))}
isMulti
placeholder="Select order types"
value={params.ordersFilters.orderTypes}
onChange={(value: any) => {
setParams(prev => ({
...prev,
ordersFilters: {
...prev.ordersFilters,
orderTypes: value,
}
}))
}}
/>
</div>
<div className="min-w-[200px]">
<ReactSelect
options={floors?.data?.map(item => ({
label: item.name,
value: item.id
}))}
isMulti
placeholder="Select floors"
value={params.ordersFilters.floors}
onChange={(value: any) => {
setParams(prev => ({
...prev,
ordersFilters: {
...prev.ordersFilters,
floors: value,
}
}))
}}
/>
</div>
<div className="min-w-[200px]">
<ReactSelect
options={users?.data?.map(item => ({
label: item.first_name + ' ' + item.last_name,
value: item.id
}))}
isMulti
placeholder="Select users"
value={params.ordersFilters.users}
onChange={(value: any) => {
setParams(prev => ({
...prev,
ordersFilters: {
...prev.ordersFilters,
users: value,
}
}))
}}
/>
</div>
<div>
<DatePicker value={date} onChange={setDate} maxValue={today(getLocalTimeZone())} isClearable/>
</div>
<div className="input-group flex-1 justify-end">
<Button iconButton variant="primary" onClick={() => setView('row')} active={view === 'row'}>
<FontAwesomeIcon icon={faTableColumns}/>
</Button>
<Button iconButton variant="primary" onClick={() => setView('column')} active={view === 'column'}>
<FontAwesomeIcon icon={faBars}/>
</Button>
</div>
</div>
<ScrollContainer className="h-[calc(100vh_-_190px)]">
<div className="flex-1 rounded-xl flex gap-3 flex-row">
{orders?.data?.map(item => (
<div className="w-[400px] flex-shrink-0" key={item.id}>
<Order order={item}/>
</div>
))}
</div>
</ScrollContainer>
<div className="h-[60px] flex-0 rounded-xl bg-white flex items-center px-3 gap-3"></div>
</div>
</Layout>
);
}