()
| 31 | }; |
| 32 | |
| 33 | const AnimalsList = () => { |
| 34 | const database = useDatabase(); |
| 35 | const animalsRef = ref(database, 'animals'); |
| 36 | const animalsQuery = query(animalsRef, orderByChild('commonName')); |
| 37 | const { status, data: animals } = useDatabaseListData<{commonName: string, id: string}>(animalsQuery, { |
| 38 | idField: 'id' |
| 39 | }); |
| 40 | |
| 41 | const addAnimal = () => { |
| 42 | const possibleAnimals = ['Dog', 'Cat', 'Iguana', 'Zebra']; |
| 43 | const selectedAnimal = possibleAnimals[Math.floor(Math.random() * possibleAnimals.length)]; |
| 44 | const newRef = push(animalsRef); |
| 45 | set(newRef, { commonName: selectedAnimal }); |
| 46 | }; |
| 47 | |
| 48 | if (status === 'loading') { |
| 49 | return <LoadingSpinner />; |
| 50 | } else if (!animals) { |
| 51 | return <span>no animals found</span> |
| 52 | } |
| 53 | |
| 54 | return ( |
| 55 | <> |
| 56 | <div className="h-20 overflow-x-scroll shadow-inner m-2 border border-black"> |
| 57 | <ul> |
| 58 | {animals.map(animal => ( |
| 59 | <li key={animal.id as string}>{animal.commonName as string}</li> |
| 60 | ))} |
| 61 | </ul> |
| 62 | </div> |
| 63 | <ul> |
| 64 | {Array.from( |
| 65 | animals.reduce((animalCountMap, animal) => { |
| 66 | const currentCount = animalCountMap.get(animal.commonName) ?? 0; |
| 67 | return animalCountMap.set(animal.commonName, currentCount + 1); |
| 68 | }, new Map<string, number>()) |
| 69 | ).map((animalStat: [string, number]) => { |
| 70 | const [animalName, animalCount] = animalStat; |
| 71 | return ( |
| 72 | <li key={animalName}> |
| 73 | {animalName}: {animalCount} |
| 74 | </li> |
| 75 | ); |
| 76 | })} |
| 77 | </ul> |
| 78 | <WideButton |
| 79 | label="Add Animal" |
| 80 | onClick={() => { |
| 81 | addAnimal(); |
| 82 | }} |
| 83 | /> |
| 84 | </> |
| 85 | ); |
| 86 | }; |
| 87 | |
| 88 | export const RealtimeDatabase = () => { |
| 89 | const firebaseApp = useFirebaseApp(); |
nothing calls this directly
no test coverage detected