-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
326 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
createContext, ReactNode, useCallback, useContext, useMemo, | ||
} from 'react'; | ||
import { | ||
collection, deleteDoc, doc, setDoc, | ||
} from 'firebase/firestore'; | ||
import { useCollection } from 'react-firebase-hooks/firestore'; | ||
import { httpsCallable } from 'firebase/functions'; | ||
import { useAuth } from './AuthProvider'; | ||
import { db, functions } from './Firebase'; | ||
|
||
interface Token { | ||
value: string; | ||
expires: Date; | ||
} | ||
|
||
interface Table { | ||
name: string; | ||
token?: Token; | ||
} | ||
|
||
interface TableState { | ||
tables: Table[]; | ||
addTable: (name: string) => Promise<void>; | ||
removeTable: (name: string) => Promise<void>; | ||
generateToken: (name: string) => Promise<void>; | ||
} | ||
|
||
const TableContext = createContext<TableState | undefined>(undefined); | ||
|
||
export function useTables() { | ||
const value = useContext(TableContext); | ||
if (value === undefined) throw new Error('Expected tables context value to be set.'); | ||
return value; | ||
} | ||
|
||
interface TableProviderProps { | ||
children: ReactNode; | ||
} | ||
export default function TableProvider({ children }: TableProviderProps) { | ||
const { clubId } = useAuth(); | ||
|
||
const tablesRef = clubId ? collection(db, 'clubs', clubId, 'tables') : null; | ||
const [values] = useCollection(tablesRef); | ||
// useEffect(() => { if (error) throw error; }, [error]); | ||
|
||
const addTable = useCallback(async (name: string) => { | ||
if (!clubId) throw Error("Club's id not given. Cannot add a table."); | ||
|
||
const ref = doc(db, 'clubs', clubId, 'tables', name); | ||
await setDoc(ref, {}); | ||
}, [clubId]); | ||
|
||
const removeTable = useCallback(async (name: string) => { | ||
if (!clubId) throw Error("Club's id not given. Cannot remove a table."); | ||
|
||
const ref = doc(db, 'clubs', clubId, 'tables', name); | ||
await deleteDoc(ref); | ||
}, [clubId]); | ||
|
||
const generateToken = useCallback(async (name: string) => { | ||
const createToken = httpsCallable(functions, 'table-createtoken'); | ||
await createToken({ tableId: name }); | ||
}, []); | ||
|
||
const value = useMemo(() => ({ | ||
tables: values ? values.docs.map((d) => ({ name: d.id, ...d.data() })) : [], | ||
addTable, | ||
removeTable, | ||
generateToken, | ||
}), [values, addTable, removeTable, generateToken]); | ||
|
||
return ( | ||
<TableContext.Provider value={value}> | ||
{children} | ||
</TableContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.