-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathFirestore.js
More file actions
180 lines (155 loc) · 4.34 KB
/
Copy pathFirestore.js
File metadata and controls
180 lines (155 loc) · 4.34 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
180
import 'firebase/performance';
import React, { useState, SuspenseList, useTransition } from 'react';
import {
AuthCheck,
SuspenseWithPerf,
useFirestoreCollectionData,
useFirestoreCollectionDataOnce,
useFirestoreDocData,
useFirestoreDocDataOnce,
useFirestore
} from 'reactfire';
const Counter = props => {
const firestore = useFirestore;
const serverIncrement = firestore.FieldValue.increment;
const ref = firestore().doc('count/counter');
const increment = amountToIncrement => {
ref.update({
value: serverIncrement(amountToIncrement)
});
};
const { value } = useFirestoreDocData(ref);
return (
<>
<button onClick={() => increment(-1)}>-</button>
<span> {value} </span>
<button onClick={() => increment(1)}>+</button>
</>
);
};
const StaticValue = props => {
const firestore = useFirestore();
const ref = firestore.doc('count/counter');
const { value } = useFirestoreDocDataOnce(ref);
return <span>{value}</span>;
};
const AnimalEntry = ({ saveAnimal }) => {
const [text, setText] = useState('');
const [disabled, setDisabled] = useState(false);
const onSave = () => {
setDisabled(true);
saveAnimal(text).then(() => {
setText('');
setDisabled(false);
});
};
return (
<>
<input
value={text}
disabled={disabled}
placeholder="Iguana"
onChange={({ target }) => setText(target.value)}
/>
<button onClick={onSave} disabled={disabled || text.length < 3}>
Add new animal
</button>
</>
);
};
const List = ({ query, removeAnimal }) => {
const animals = useFirestoreCollectionData(query, { idField: 'id' });
return (
<ul>
{animals.map(animal => (
<li key={animal.id}>
{animal.commonName}{' '}
<button onClick={() => removeAnimal(animal.id)}>X</button>
</li>
))}
</ul>
);
};
const StaticList = () => {
const ref = useFirestore()
.collection('animals')
.orderBy('commonName', 'asc');
const animals = useFirestoreCollectionDataOnce(ref, { idField: 'id' });
return (
<ul>
{animals.map(animal => (
<li key={animal.id}>{animal.commonName}</li>
))}
</ul>
);
};
const FavoriteAnimals = props => {
const firestore = useFirestore();
const baseRef = firestore.collection('animals');
const [isAscending, setIsAscending] = useState(true);
const query = baseRef.orderBy('commonName', isAscending ? 'asc' : 'desc');
const [startTransition, isPending] = useTransition({
timeoutMs: 1000
});
const toggleSort = () => {
startTransition(() => {
setIsAscending(!isAscending);
});
};
const addNewAnimal = commonName =>
baseRef.add({
commonName
});
const removeAnimal = id => baseRef.doc(id).delete();
return (
<>
<AnimalEntry saveAnimal={addNewAnimal} />
<br />
<button onClick={toggleSort} disabled={isPending}>
Sort {isAscending ? '^' : 'v'}
</button>
<React.Suspense fallback="loading...">
<List query={query} removeAnimal={removeAnimal} />
</React.Suspense>
</>
);
};
const SuspenseWrapper = props => {
return (
<SuspenseWithPerf fallback="loading..." traceId="firestore-demo-root">
<AuthCheck fallback="sign in to use Firestore">
<SuspenseList revealOrder="together">
<h3>Sample Doc Listener</h3>
<SuspenseWithPerf
fallback="connecting to Firestore..."
traceId="firestore-demo-doc"
>
<Counter />
</SuspenseWithPerf>
<h3>Sample One-time Get</h3>
<SuspenseWithPerf
fallback="connecting to Firestore..."
traceId="firestore-demo-doc"
>
<StaticValue />
</SuspenseWithPerf>
<h3>Sample Collection Listener</h3>
<SuspenseWithPerf
fallback="connecting to Firestore..."
traceId="firestore-demo-collection"
>
<FavoriteAnimals />
</SuspenseWithPerf>
<h3>Sample One-time Get Collection</h3>
<SuspenseWithPerf
fallback="connecting to Firestore..."
traceId="firestore-demo-collection"
>
<StaticList />
</SuspenseWithPerf>
</SuspenseList>
</AuthCheck>
</SuspenseWithPerf>
);
};
export default SuspenseWrapper;