-
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.
Merge pull request #2 from ruishanteo/leaderboard
Added leaderboard
- Loading branch information
Showing
9 changed files
with
89 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ const Landing = () => { | |
fontSize: 70, | ||
}} | ||
> | ||
WEBNAME | ||
EDUNOW | ||
</Typography> | ||
|
||
<Typography | ||
|
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 |
---|---|---|
@@ -1,72 +1,57 @@ | ||
import { createSlice } from "@reduxjs/toolkit"; | ||
|
||
import { db } from "hooks/Firebase"; | ||
import { | ||
addDoc, | ||
collection, | ||
doc, | ||
getDoc, | ||
getDocs, | ||
query, | ||
where, | ||
} from "firebase/firestore"; | ||
import { useAuth } from "hooks/FirebaseHooks"; | ||
import { collection, doc, getDoc, getDocs, setDoc } from "firebase/firestore"; | ||
|
||
const usersSlice = createSlice({ | ||
name: "users", | ||
initialState: { | ||
users: [], | ||
currentUser: {}, | ||
currentScore: 0, | ||
}, | ||
reducers: { | ||
saveUsers: (state, action) => { | ||
state.users = action.payload; | ||
}, | ||
saveCurrentUser: (state, action) => { | ||
state.currentUser = action.payload; | ||
state.currentScore = action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
export function fetchCurrentUser() { | ||
return async (dispatch, getState) => { | ||
const user = useAuth(); | ||
const response = await getDoc(collection(db, "users")); | ||
const userData = { ...response.data(), id: doc.id }; | ||
return userData; | ||
}; | ||
} | ||
|
||
export async function fetchUsers(dispatch, getState) { | ||
const response = await getDocs(collection(db, "users")); | ||
const users = response.docs.map((doc) => ({ ...doc.data(), id: doc.id })); | ||
dispatch(usersSlice.actions.saveUsers(users)); | ||
} | ||
|
||
export function fetchCurrentUser(user) { | ||
return async (dispatch, getState) => { | ||
const response = await getDoc(doc(db, "users", user.uid)); | ||
const userData = { ...response.data(), id: doc.id }; | ||
dispatch(usersSlice.actions.saveCurrentUser(userData.score)); | ||
}; | ||
} | ||
|
||
export function fetchUser(userId) { | ||
return async (dispatch, getState) => { | ||
const response = await getDoc(collection(db, "users", userId)); | ||
const response = await getDoc(doc(db, "users", userId)); | ||
const user = { ...response.data(), id: doc.id }; | ||
return user; | ||
}; | ||
} | ||
|
||
export async function saveUser(dispatch, getState) { | ||
addDoc(collection(db, "users"), getState().currentUser); | ||
} | ||
|
||
export function saveCurrentUser(user) { | ||
export function updateCurrentUser(user, score) { | ||
return async (dispatch, getState) => { | ||
const docs = await getDocs( | ||
query(collection(db, "users"), where("uid", "==", user.uid)) | ||
); | ||
await addDoc(collection(db, "users"), { | ||
uid: user.uid, | ||
email: user.email, | ||
await fetchCurrentUser(user); | ||
const oldScore = getState().users.currentScore; | ||
await setDoc(doc(db, "users", user.uid), { | ||
displayName: user.displayName, | ||
score: score + (oldScore ? oldScore : 0), | ||
}); | ||
dispatch(fetchCurrentUser(user)); | ||
}; | ||
} | ||
|
||
export const { goNext, goToQuestion, saveQuestionsToStore } = | ||
usersSlice.actions; | ||
export const questionsReducer = usersSlice.reducer; | ||
export const { saveUsers, saveCurrentUser } = usersSlice.actions; | ||
export const usersReducers = usersSlice.reducer; |