-
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.
Fixed context bug with GatherWordsProvided added it too I guess
- Loading branch information
Showing
5 changed files
with
156 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { | ||
createContext, | ||
useState, | ||
useContext, | ||
useEffect, | ||
ReactNode, | ||
} from "react"; | ||
import { WordInput } from "@/components/StoryEngine"; | ||
import { | ||
setDoc, | ||
doc, | ||
query, | ||
collection, | ||
where, | ||
onSnapshot, | ||
} from "firebase/firestore"; | ||
import { db } from "../firebase"; | ||
import { useAuth } from "../contexts/AuthContext"; | ||
import { useParams } from "react-router-dom"; | ||
|
||
// Define the context interface | ||
interface GatherWordsContextType { | ||
contextWords: WordInput[]; | ||
setContextWords: React.Dispatch<React.SetStateAction<WordInput[]>>; | ||
} | ||
|
||
// Create the context | ||
const initialContextValue: GatherWordsContextType = { | ||
contextWords: [], | ||
setContextWords: () => {}, // Placeholder function | ||
}; | ||
const GatherWordsContext = | ||
createContext<GatherWordsContextType>(initialContextValue); | ||
|
||
interface Props { | ||
children: ReactNode; | ||
} | ||
|
||
// Create a provider component | ||
export const GatherWordsProvider = ({ children }: Props) => { | ||
const [contextWords, setContextWords] = useState<WordInput[]>([]); | ||
const { currentUser } = useAuth(); | ||
const params = useParams(); | ||
useEffect(() => { | ||
console.log("contextWords changed"); | ||
console.table(contextWords); | ||
}, [contextWords]); | ||
useEffect(() => { | ||
if (!currentUser) { | ||
console.log( | ||
"You must be logged in to play a game or at least have a valid session." | ||
); | ||
return; | ||
} | ||
const uid = currentUser.uid; | ||
const joinRoom = () => { | ||
const ref = `rooms/${params.gameId}/users/${uid}`; | ||
console.log(ref); | ||
setDoc(doc(db, ref), { | ||
uid: uid, | ||
timeJoined: new Date().getTime(), | ||
status: "active", | ||
}); | ||
}; | ||
const watchWords = () => { | ||
const ref = `rooms/${params.gameId}/words`; | ||
const q = query(collection(db, ref), where("user", "==", uid)); | ||
const unsubscribe = onSnapshot(q, (querySnapshot) => { | ||
const wordSnaps: WordInput[] = []; | ||
querySnapshot.forEach((doc) => { | ||
const data = doc.data(); | ||
wordSnaps.push({ | ||
word: data.word, | ||
partOfSpeech: data.partOfSpeech, | ||
refPath: doc.ref.path, | ||
index: data.index, | ||
status: data.status, | ||
user: data.user, | ||
}); | ||
}); | ||
console.log("ContextWOrds"); | ||
console.table(contextWords); | ||
setContextWords((prev) => { | ||
console.log(prev); | ||
console.log(wordSnaps); | ||
return wordSnaps; | ||
}); | ||
}); | ||
|
||
return unsubscribe; | ||
}; | ||
|
||
joinRoom(); | ||
const unsubscribe = watchWords(); | ||
return unsubscribe; | ||
}, [currentUser]); | ||
const value = { | ||
contextWords: contextWords, | ||
setContextWords: setContextWords, | ||
}; | ||
return ( | ||
<GatherWordsContext.Provider value={value}> | ||
{children} | ||
</GatherWordsContext.Provider> | ||
); | ||
}; | ||
|
||
// Custom hook to use the context | ||
export const useGatherWords = () => { | ||
const context = useContext(GatherWordsContext); | ||
if (!context) { | ||
throw new Error("useGatherWords must be used within a GatherWordsProvider"); | ||
} | ||
return context; | ||
}; | ||
export default GatherWordsContext; |
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