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