Skip to content

Commit

Permalink
feat: #80 with nice typing
Browse files Browse the repository at this point in the history
  • Loading branch information
nobrayner committed Mar 25, 2021
1 parent d620261 commit 9fc9b41
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 162 deletions.
27 changes: 16 additions & 11 deletions src/routes/raids/ViewRaid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import styled from '@emotion/styled'
import UserStatBlock from '#components/userStatBlock'
import Emoji from '#components/emoji'
import LoadingSpinner from '#components/loadingSpinner'
import useDocument from '#utils/useDocument'
import { RAID_STATS } from '#utils/firestoreCollections'
import useFirestoreQuery, { to } from '#utils/useFirestoreQuery'

const userStatSorts: {
[key: string]: (a: UserStats, b: UserStats) => number
Expand All @@ -19,12 +20,20 @@ const userStatSortNames = Object.keys(userStatSorts)

const ViewRaid = () => {
const { raidId } = useParams<{ raidId: string }>()
const documentData = useDocument<ViewRaidData>('raid-stats', raidId)
const documentQuery = useFirestoreQuery((firestore) =>
firestore.collection(RAID_STATS).withConverter(to<ViewRaidData>()).doc(),
)

const [currentSort, setCurrentSort] = useState(userStatSortNames[0])

if (documentData.state === 'success') {
const data = documentData.data
if (documentQuery.status === 'success') {
const data = documentQuery.data

if (!data)
return (
<p>Couldn`t find that Raid - did you fall into the wrong dungeon?</p>
)

return (
<$StatsView>
<$Header>
Expand Down Expand Up @@ -84,14 +93,10 @@ const ViewRaid = () => {
</$StatContainer>
</$StatsView>
)
} else if (documentData.state === 'error') {
return <p>{JSON.stringify(documentData.error)}</p>
} else if (documentQuery.status === 'error') {
return <p>{JSON.stringify(documentQuery.error)}</p>
} else {
return documentData.state === 'loading' ? (
<LoadingSpinner />
) : (
<p>Couldn`t find that Raid - did you fall into the wrong dungeon?</p>
)
return <LoadingSpinner />
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/routes/raids/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import React from 'react'

import { Link } from 'react-router-dom'
import LoadingSpinner from '#components/loadingSpinner'
import useCollection from '#utils/useCollection'
import useFirestoreQuery, { to } from '#utils/useFirestoreQuery'
import { RAID_STATS } from '#utils/firestoreCollections'

const AllRaids = () => {
const collectionData = useCollection<ViewRaidData>('raid-stats')
const collectionQuery = useFirestoreQuery((firestore) =>
firestore.collection(RAID_STATS).withConverter(to<ViewRaidData>()),
)

if (collectionData.state === 'success') {
const data = collectionData.data
if (collectionQuery.status === 'success') {
const data = collectionQuery.data
return (
<>
<h1>Raids</h1>
Expand Down Expand Up @@ -40,7 +43,7 @@ const AllRaids = () => {
)
}

return collectionData.state === 'loading' ? (
return collectionQuery.status === 'loading' ? (
<LoadingSpinner />
) : (
// Theoretically this can/should never be hit... But a fun message nonetheless
Expand Down
1 change: 1 addition & 0 deletions src/utils/firestoreCollections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const RAID_STATS = 'raid-stats'
52 changes: 0 additions & 52 deletions src/utils/useCollection/index.ts

This file was deleted.

51 changes: 0 additions & 51 deletions src/utils/useDocument/index.ts

This file was deleted.

25 changes: 0 additions & 25 deletions src/utils/useFirestore/index.ts

This file was deleted.

Loading

0 comments on commit 9fc9b41

Please sign in to comment.