Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/ahmedbechirmezhoud/CSTC into…
Browse files Browse the repository at this point in the history
… dev
  • Loading branch information
ahmedbechirmezhoud committed Mar 17, 2022
2 parents 4e1249c + bdf2006 commit 904eebe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
4 changes: 3 additions & 1 deletion screens/VoteScreen/VoteScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import BlueButton from "../../components/BlueButton/BlueButton";

import { CheckBox } from 'react-native-elements';
import { getParticipantList, voteForParticipant } from "../../services/vote/userFuncs";
import { CurrentUser } from "../../utils/user";


export default function VoteScreen(){

const { dispatchInfo } = useContext(InfoContext);
const [universities, setUniversities] = useState([]);
const [choosed, setchoosed] = useState(null);
const [voted, setVoted] = useState(null);
const [voted, setVoted] = useState(CurrentUser.votedFor);
const [loading, setLoading] = useState(false);

useEffect(()=>{
console.log(CurrentUser.votedFor)
getParticipantList()
.then((arr)=>{
setUniversities(arr);
Expand Down
8 changes: 5 additions & 3 deletions services/firestore/userFuncs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ErrorCodes } from '../../const/errorCodes';
import { USER_PATH, PHONE_EMAIL_PATH } from './../../const/firestorePaths';
import { CurrentUser, userData } from '../../utils/user';
import { registerForPushNotificationsAsync } from '../Notification';
import { ref, get } from 'firebase/database';
import { ref, get, goOnline, goOffline } from 'firebase/database';
import { signOut } from '../auth/loginService';


Expand Down Expand Up @@ -79,11 +79,13 @@ export async function getCurrentUserData(){
}
}

goOnline(rtdb);
let userPath = ref(rtdb, USER_PATH + auth.currentUser.uid);
let vote = (await get(userPath));

goOffline(rtdb);

if(!vote.exists()) return {...data, votedFor: null, phone: phone.phone, email: email};
return {data, votedFor: vote.votedFor, phone: phone.phone, email: email};
return {data, votedFor: vote.toJSON().votedFor, phone: phone.phone, email: email};
}

export async function readDataFromPath(path){
Expand Down
20 changes: 14 additions & 6 deletions services/vote/userFuncs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FirebaseError } from 'firebase/app';
import { increment, ref, update, get } from 'firebase/database';
import { increment, ref, update, get, goOffline, goOnline } from 'firebase/database';
import { rtdb, auth } from '../../configInit';
import { ErrorCodes } from '../../const/errorCodes';
import {USER_PATH, PART_PATH} from './../../const/firestorePaths';
Expand Down Expand Up @@ -29,20 +29,28 @@ export async function voteForParticipant(pID){
CurrentUser.votedFor = pID;
obj[USER_PATH+auth.currentUser.uid].votedFor = CurrentUser.votedFor;

goOnline(rtdb);
await update(ref(rtdb), obj)
.catch((err) => {
console.log(err)
console.log(err);
goOffline(rtdb);
throw new FirebaseError(ErrorCodes.VOTE_ERROR[0], ErrorCodes.VOTE_ERROR[1])
})
});
goOffline(rtdb);
}

export async function getParticipantList(){
if(!auth.currentUser) throw new FirebaseError(ErrorCodes.NOT_LOGGED_IN[0], ErrorCodes.NOT_LOGGED_IN[1]);

goOnline(rtdb);
let partPath = ref(rtdb, PART_PATH);
let data = await get(partPath).catch(()=>{
throw new FirebaseError(ErrorCodes.UNABLE_TO_FETCH_TEAMS[0], ErrorCodes.UNABLE_TO_FETCH_TEAMS[1]);
});
let data = await get(partPath)
.catch(()=>{
goOffline(rtdb);
throw new FirebaseError(ErrorCodes.UNABLE_TO_FETCH_TEAMS[0], ErrorCodes.UNABLE_TO_FETCH_TEAMS[1]);
});
goOffline(rtdb);
console.log(data);
let JSONObj = data.toJSON();

if(JSONObj){
Expand Down

0 comments on commit 904eebe

Please sign in to comment.