-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathFirestore.tsx
153 lines (135 loc) · 4.8 KB
/
Firestore.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
import * as React from 'react';
import { useState, useTransition } from 'react';
import {
FirestoreProvider,
SuspenseWithPerf,
useFirestore,
useFirestoreCollectionData,
useFirestoreDocData,
useFirestoreDocDataOnce,
useInitFirestore,
} from 'reactfire';
import { WideButton } from '../display/Button';
import { CardSection } from '../display/Card';
import { LoadingSpinner } from '../display/LoadingSpinner';
import { AuthWrapper } from './Auth';
import { initializeFirestore, doc, collection, enableIndexedDbPersistence, increment, updateDoc, orderBy, query, addDoc, deleteDoc } from 'firebase/firestore';
const Counter = () => {
const firestore = useFirestore();
const ref = doc(firestore, 'count', 'counter');
const incrementCounter = (amountToIncrement) => {
updateDoc(ref, {
value: increment(amountToIncrement),
});
};
const { data: count } = useFirestoreDocData(ref);
return (
<>
<button onClick={() => incrementCounter(-1)}>-</button>
<span> {(count as any).value} </span>
<button onClick={() => incrementCounter(1)}>+</button>
</>
);
};
const StaticValue = () => {
const firestore = useFirestore();
const ref = doc(firestore, 'count/counter');
const { data } = useFirestoreDocDataOnce(ref);
return <span>{(data as any).value}</span>;
};
const List = ({ query, removeAnimal }) => {
const { data: animals } = useFirestoreCollectionData(query, { idField: 'id' });
return (
<>
<div className="h-20 overflow-x-scroll shadow-inner m-2 border border-black">
<ul>
{(animals as Array<{ id: string; commonName: string }>).map((animal) => (
<li key={animal.id}>
{animal.commonName} <button onClick={() => removeAnimal(animal.id)}>X</button>
</li>
))}
</ul>
</div>
<ul>
{Array.from(
(animals as any[]).reduce((animalCountMap, animal) => {
const currentCount = animalCountMap.get(animal.commonName) ?? 0;
return animalCountMap.set(animal.commonName, currentCount + 1);
}, new Map<string, number>())
).map((animalStat) => {
const [animalName, animalCount] = (animalStat as [string, number]);
return (
<li key={animalName}>
{animalName}: {animalCount}
</li>
);
})}
</ul>
</>
);
};
const FavoriteAnimals = (props) => {
const firestore = useFirestore();
const animalsCollection = collection(firestore, 'animals');
const [isAscending, setIsAscending] = useState(true);
const animalsQuery = query(animalsCollection, orderBy('commonName', isAscending ? 'asc' : 'desc'));
const [isPending, startTransition] = useTransition();
const toggleSort = () => {
startTransition(() => {
setIsAscending(!isAscending);
});
};
const addAnimal = () => {
const possibleAnimals = ['Dog', 'Cat', 'Iguana', 'Zebra'];
const selectedAnimal = possibleAnimals[Math.floor(Math.random() * possibleAnimals.length)];
addDoc(animalsCollection, { commonName: selectedAnimal });
};
const removeAnimal = (id) => deleteDoc(doc(animalsCollection, id));
return (
<>
<WideButton label="Sort" onClick={toggleSort} />
<React.Suspense fallback="loading...">
<List query={animalsQuery} removeAnimal={removeAnimal} />
</React.Suspense>
<WideButton
label="Add Animal"
onClick={() => {
addAnimal();
}}
/>
</>
);
};
function FirestoreWrapper({ children }) {
const { data: firestoreInstance } = useInitFirestore(async (firebaseApp) => {
const db = initializeFirestore(firebaseApp, {});
await enableIndexedDbPersistence(db);
return db;
});
return <FirestoreProvider sdk={firestoreInstance}>{children}</FirestoreProvider>;
}
export const Firestore = (props) => {
return (
<SuspenseWithPerf fallback={<LoadingSpinner />} traceId="firestore-demo-root">
<FirestoreWrapper>
<AuthWrapper fallback={<span>sign in to use Firestore</span>}>
<CardSection title="Get/Set document value">
<SuspenseWithPerf fallback="connecting to Firestore..." traceId="firestore-demo-doc">
<Counter />
</SuspenseWithPerf>
</CardSection>
<CardSection title="Fetch data once">
<SuspenseWithPerf fallback="connecting to Firestore..." traceId="firestore-demo-doc">
<StaticValue />
</SuspenseWithPerf>
</CardSection>
<CardSection title="Work with lists of data">
<SuspenseWithPerf fallback="connecting to Firestore..." traceId="firestore-demo-collection">
<FavoriteAnimals />
</SuspenseWithPerf>
</CardSection>
</AuthWrapper>
</FirestoreWrapper>
</SuspenseWithPerf>
);
};