ordering-ui-react-native 0.21.88-release → 0.21.89-release
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import React, { useState, createContext, useEffect, useContext } from 'react';
|
|
2
|
+
import { useApi, useSession, useEvent, useWebsocket } from 'ordering-components/native'
|
|
3
|
+
import { useNetInfo } from '@react-native-community/netinfo';
|
|
4
|
+
|
|
5
|
+
import { _retrieveStoreData, _setStoreData, _removeStoreData } from '../../providers/StoreUtil'
|
|
6
|
+
|
|
7
|
+
type State = {
|
|
8
|
+
isNetConnected: boolean | null
|
|
9
|
+
isCombinedTabs: boolean | null
|
|
10
|
+
canSaveChangesOffline: boolean | null
|
|
11
|
+
actions: { [key: string]: any }
|
|
12
|
+
orders: { [key: string]: any } | null
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type Functions = {
|
|
16
|
+
applyOffAction: (changes: any) => any
|
|
17
|
+
registerOffOrder: (order: any) => any
|
|
18
|
+
setState: React.Dispatch<React.SetStateAction<State>>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const defaultState = {
|
|
22
|
+
isNetConnected: null,
|
|
23
|
+
isCombinedTabs: null,
|
|
24
|
+
canSaveChangesOffline: false,
|
|
25
|
+
actions: {},
|
|
26
|
+
orders: null,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export const OfflineActionsContext = createContext<[State, Functions]>([
|
|
31
|
+
defaultState,
|
|
32
|
+
{
|
|
33
|
+
applyOffAction: () => {},
|
|
34
|
+
registerOffOrder: () => {},
|
|
35
|
+
setState: () => {}
|
|
36
|
+
}
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
export const OfflineActionsProvider = (props: any) => {
|
|
40
|
+
const netInfo = useNetInfo()
|
|
41
|
+
const [ordering] = useApi()
|
|
42
|
+
const [{ token }] = useSession()
|
|
43
|
+
const [events] = useEvent()
|
|
44
|
+
const socket = useWebsocket()
|
|
45
|
+
|
|
46
|
+
const [state, setState] = useState<State>({
|
|
47
|
+
isNetConnected: netInfo.isConnected,
|
|
48
|
+
isCombinedTabs: false,
|
|
49
|
+
canSaveChangesOffline: false,
|
|
50
|
+
actions: [],
|
|
51
|
+
orders: null
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const getStatusById = (id: number) => {
|
|
55
|
+
if (!id && id !== 0) return
|
|
56
|
+
const active = [0, 3, 4, 7, 8, 9, 13, 14, 18, 19, 20, 21]
|
|
57
|
+
const pending = [0, 13]
|
|
58
|
+
const inProgress = [3, 4, 7, 8, 9, 14, 18, 19, 20, 21]
|
|
59
|
+
const completed = [1, 11, 15]
|
|
60
|
+
|
|
61
|
+
const status = pending.includes(id)
|
|
62
|
+
? 'pending'
|
|
63
|
+
: inProgress.includes(id)
|
|
64
|
+
? 'inProgress'
|
|
65
|
+
: completed.includes(id)
|
|
66
|
+
? 'completed'
|
|
67
|
+
: 'cancelled'
|
|
68
|
+
|
|
69
|
+
const combinedStatus = active.includes(id)
|
|
70
|
+
? 'active'
|
|
71
|
+
: completed.includes(id)
|
|
72
|
+
? 'completed'
|
|
73
|
+
: 'cancelled'
|
|
74
|
+
|
|
75
|
+
return state.isCombinedTabs ? combinedStatus : status
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const applyOffAction = async (changes: any) => {
|
|
79
|
+
if (state.canSaveChangesOffline === false) return false
|
|
80
|
+
|
|
81
|
+
let _actions: any = state.actions?.[changes?.data?.orderId] ?? []
|
|
82
|
+
|
|
83
|
+
if (state.actions?.[changes?.data?.orderId]) {
|
|
84
|
+
_actions.push(changes)
|
|
85
|
+
} else {
|
|
86
|
+
_actions = [changes]
|
|
87
|
+
}
|
|
88
|
+
const actions = { ...state.actions, [changes?.data?.orderId]: _actions }
|
|
89
|
+
|
|
90
|
+
setState(state => ({ ...state, actions }))
|
|
91
|
+
await _setStoreData('offline_actions_array', actions)
|
|
92
|
+
return true
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const registerOffOrder = async (order: any) => {
|
|
96
|
+
if (!order) return
|
|
97
|
+
|
|
98
|
+
const oldStatusString = getStatusById(order?.oldStatus)
|
|
99
|
+
const newStatusString = getStatusById(order?.status)
|
|
100
|
+
|
|
101
|
+
const orderStatuses: any = [oldStatusString]
|
|
102
|
+
oldStatusString !== newStatusString && orderStatuses.push(newStatusString)
|
|
103
|
+
|
|
104
|
+
const ordersStoraged: any = {}
|
|
105
|
+
for (const status of orderStatuses) {
|
|
106
|
+
ordersStoraged[status] = await _retrieveStoreData(`${status}_orders`)
|
|
107
|
+
|
|
108
|
+
if (ordersStoraged[status]) {
|
|
109
|
+
if (orderStatuses.length === 1) {
|
|
110
|
+
ordersStoraged[status] = [
|
|
111
|
+
...ordersStoraged[status].filter((_order: any) => _order.id !== order.id),
|
|
112
|
+
order
|
|
113
|
+
].sort((a: any, b: any) => b.id - a.id)
|
|
114
|
+
} else if (orderStatuses.length === 2) {
|
|
115
|
+
if (status === oldStatusString) {
|
|
116
|
+
ordersStoraged[status] = ordersStoraged[status]
|
|
117
|
+
.filter((_order: any) => _order.id !== order.id)
|
|
118
|
+
.sort((a: any, b: any) => b.id - a.id)
|
|
119
|
+
}
|
|
120
|
+
if (status === newStatusString) {
|
|
121
|
+
ordersStoraged[status] = [
|
|
122
|
+
...ordersStoraged[status],
|
|
123
|
+
order
|
|
124
|
+
].sort((a: any, b: any) => b.id - a.id)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
await _setStoreData(`${status}_orders`, ordersStoraged[status]);
|
|
128
|
+
} else {
|
|
129
|
+
ordersStoraged[status] = [order]
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (Object.keys(ordersStoraged).length) {
|
|
133
|
+
setState(state => ({ ...state, orders: ordersStoraged }))
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const syncChanges = async (changes: any) => {
|
|
138
|
+
const ordersIdUpdated: any = []
|
|
139
|
+
|
|
140
|
+
Object.keys(changes).forEach(async (orderId) => {
|
|
141
|
+
const arr = changes[orderId]
|
|
142
|
+
|
|
143
|
+
const [lastChange, ...restOfChanges] = arr.reverse();
|
|
144
|
+
|
|
145
|
+
if (restOfChanges.length > 0) {
|
|
146
|
+
const ordersString = restOfChanges
|
|
147
|
+
.map((obj: any) => (
|
|
148
|
+
Object.entries(obj?.data?.body).map(([clave, valor]) => `${clave}: '${valor}'`).join(', ')
|
|
149
|
+
))
|
|
150
|
+
.join(', ')
|
|
151
|
+
handleSendMessage({ message: ordersString, orderId })
|
|
152
|
+
}
|
|
153
|
+
const id = await updateOrderStatus({ orderId, body: lastChange?.data?.body })
|
|
154
|
+
id && ordersIdUpdated.push(id);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
ordersIdUpdated.length && events.emit('offline_order_updated', ordersIdUpdated)
|
|
158
|
+
await _removeStoreData('offline_actions_array');
|
|
159
|
+
setState(state => ({ ...state, actions: [] }));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const actionsFromStorage = async (isConnected: boolean) => {
|
|
163
|
+
setState(state => ({ ...state, isNetConnected: isConnected }))
|
|
164
|
+
const storedActions = await _retrieveStoreData('offline_actions_array');
|
|
165
|
+
|
|
166
|
+
if (isConnected && Object.keys(storedActions)?.length) {
|
|
167
|
+
syncChanges(storedActions)
|
|
168
|
+
return
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
Object.keys(storedActions)?.length && setState(state => ({ ...state, actions: storedActions }));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
useEffect(() => {
|
|
175
|
+
if (netInfo.isConnected === null || state.canSaveChangesOffline === false) return
|
|
176
|
+
actionsFromStorage(netInfo.isConnected)
|
|
177
|
+
}, [netInfo.isConnected])
|
|
178
|
+
|
|
179
|
+
const functions: any = {
|
|
180
|
+
applyOffAction,
|
|
181
|
+
registerOffOrder,
|
|
182
|
+
setState
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const updateOrderStatus = async (offlineData: any) => {
|
|
186
|
+
try {
|
|
187
|
+
const { content: { result: order, error } } = await ordering
|
|
188
|
+
.setAccessToken(token)
|
|
189
|
+
.orders(offlineData?.orderId)
|
|
190
|
+
.save(offlineData?.body)
|
|
191
|
+
|
|
192
|
+
return error ? null : order?.id
|
|
193
|
+
} catch {
|
|
194
|
+
return null
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const handleSendMessage = async (offlineData: any) => {
|
|
199
|
+
try {
|
|
200
|
+
const _canRead = [0, 2, 3, 4]
|
|
201
|
+
const body = {
|
|
202
|
+
comment: offlineData?.message,
|
|
203
|
+
type: 2,
|
|
204
|
+
can_see: _canRead.join(',')
|
|
205
|
+
}
|
|
206
|
+
const response = await fetch(`${ordering.root}/orders/${offlineData?.orderId}/messages`, {
|
|
207
|
+
method: 'POST',
|
|
208
|
+
headers: {
|
|
209
|
+
'Content-Type': 'application/json',
|
|
210
|
+
Authorization: `Bearer ${token}`,
|
|
211
|
+
'X-App-X': ordering.appId,
|
|
212
|
+
'X-Socket-Id-X': socket?.getId()
|
|
213
|
+
},
|
|
214
|
+
body: JSON.stringify(body)
|
|
215
|
+
})
|
|
216
|
+
const { error, result } = await response.json()
|
|
217
|
+
} catch {
|
|
218
|
+
return null
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const eventsDictiorary: any = {
|
|
223
|
+
evt_off_change_order_status: updateOrderStatus
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<OfflineActionsContext.Provider value={[{ ...state, isNetConnected: netInfo.isConnected }, functions]}>
|
|
228
|
+
{props.children}
|
|
229
|
+
</OfflineActionsContext.Provider>
|
|
230
|
+
);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
export const useOfflineActions = () => {
|
|
234
|
+
const actionsManager = useContext(OfflineActionsContext)
|
|
235
|
+
return actionsManager || [defaultState, {}]
|
|
236
|
+
}
|