-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders-list.tsx
154 lines (130 loc) · 3.85 KB
/
orders-list.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
/* eslint-disable no-console */
import { Suspense, useEffect } from 'react'
import { FlatList } from 'react-native'
import SplashScreen from 'react-native-splash-screen'
import { ErrorBoundary } from '@components/fun/base-bucket/bucket-error-boundary'
import { deleteOrderBucket, favoritesBucket, Order, orders2Bucket, ordersBucket, selectedBucket } from '@components/fun/orders-store'
import { Flex } from '@ds/base/flex'
import { Pressable } from '@ds/base/pressable'
import { Row } from '@ds/base/row'
import { Text } from '@ds/base/text'
import { borderRadius, useMemoizedTheme } from '@theme'
const userId = 'julien3'
const OrderItem = ({ order }: { order: Order }) => {
const { theme: { colors } } = useMemoizedTheme()
const selected = selectedBucket.useIsSelectedOrder(order.id)
const favorited = favoritesBucket.useSelector(state => state[order.id])
const select = (id: string) => () =>
selectedBucket.selectOrder(id)
const favorite = (id: string) => () => {
const old = favoritesBucket.get()
const oldVal = old[id] ?? false
favoritesBucket.set({ ...old, [id]: !oldVal })
}
const remove = (id: string) => async () => {
try {
await deleteOrderBucket(id).mutate({
id: 'hello'
})
} catch (err) {
console.log(JSON.stringify(err, null, 2))
}
}
return (
<Pressable
backgroundColor={colors.blue}
borderRadius={borderRadius.XS}
justify='space-between'
mb='small'
onPress={select(order.id)}
ph='small'
pv='micro'
row
>
<Text color={'#fff'}>
{`${selected ? '[X] - ' : ''} ${order.name} ${favorited ? '- Loved' : ''}`}
</Text>
<Row>
<Pressable onPress={favorite(order.id)}>
<Text color={colors.lightGrey}>Love</Text>
</Pressable>
<Pressable onPress={remove(order.id)}>
<Text color={colors.red}>Delete</Text>
</Pressable>
</Row>
</Pressable>
)
}
const OrdersList = () => {
orders2Bucket(userId).useQueryTrigger()
const orders2 = orders2Bucket(userId).useValue()
const { data: orders, loading, loadingMore, loadMore, refetch } = ordersBucket(userId).useQuery()
console.log('orders', orders.length)
console.log('orders2', orders2.length)
const renderOrder = (item: Order, index: number) =>
<OrderItem key={index} order={item} />
const fun = async () =>
await refetch()
const onEndReached = async () =>
await loadMore()
return (
<>
<FlatList
ListFooterComponent={loadingMore ? <Text>Loading more...</Text> : null}
data={orders}
keyExtractor={(item) => item.id}
onEndReached={onEndReached}
renderItem={({ index, item }) => renderOrder(item, index)}
/>
<Row justify='flex-end'>
<Pressable onPress={fun}>
<Text>{loading ? 'Reloading...' : 'Refetch'}</Text>
</Pressable>
</Row>
</>
)
}
const Total = () => {
const total = ordersBucket(userId).useOrdersCount()
return (
<Text mb='medium'>
{`Orders ${Number(total).toFixed(0)}`}
</Text>
)
}
const DeleteButton = () => {
const count = selectedBucket.useSelectedCount()
const deleteAll = async () =>
selectedBucket.deleteAllSelected(userId)
if (count === 0) {
return (null)
}
return (
<Pressable onPress={deleteAll}>
<Text>
{`Delete ${Number(count)} items`}
</Text>
</Pressable>
)
}
export const OrdersScreen = () => {
const isHydrated = ordersBucket(userId).useIsHydrated()
useEffect(() => {
SplashScreen.hide()
}, [])
return isHydrated
? (
<Flex p='medium'>
<Total />
<ErrorBoundary>
<Suspense fallback={<Text>Loading...</Text>}>
<OrdersList />
</Suspense>
</ErrorBoundary>
<DeleteButton />
</Flex>
)
: (
<Text center mt='large'>Hydrating...</Text>
)
}