Skip to content

Commit

Permalink
Add functionalities to forms
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedbechirmezhoud committed Mar 16, 2022
1 parent d4d2fea commit d46777a
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 35 deletions.
32 changes: 19 additions & 13 deletions screens/ForgotPwdScreen/ForgotPwdScreen.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { View, StyleSheet, TextInput,Alert, Image, Text, Dimensions } from "react-native";
import React, { useState } from "react";
import React, { useContext, useState } from "react";
import Background from "../../components/Background/Background";
import BlueButton from "../../components/BlueButton/BlueButton";
import { Card } from 'react-native-elements';
import { resetUserPassword } from "../../services/account/accountService";
import { InfoContext } from "../../Context/InfoContext";

export default ChangeEmail = () => {

const [emailInput, setEmailInputInput] = useState("");
const [validEmail, setValidEmail] = useState(true);

const { dispatchInfo } = useContext(InfoContext);
const emailInputHandler = (textInput) => {
setEmailInputInput(textInput);
(String(textInput)
Expand All @@ -20,17 +22,21 @@ export default ChangeEmail = () => {

const confirmButtonHandler = () => {
if(validEmail){
Alert.alert(
"Done!",
"an Email has been sentd",
[
{
text: "OK",
style: "cancel",
},
]
);

resetUserPassword(emailInput)
.then(() => {
Alert.alert(
"Done!",
"a Verification Email has been sent to " + emailInput,
[
{
text: "OK",
style: "cancel",
},
]
);}
)
.catch((e) => dispatchInfo({ payload : { error : e } }) )

}
};

Expand Down
2 changes: 1 addition & 1 deletion screens/LoginPageScreen/LoginPageScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default LoginPageScreen = () => {
/>
<TextInput
style={styles.inputBox}
placeholder={"E-mail"}
placeholder={"E-mail or phone"}
placeholderTextColor='#507686'
keyboardType='email-address'
onChangeText={emailInputHandler}
Expand Down
32 changes: 20 additions & 12 deletions screens/Settings/ChangeEmail/ChangeEmail.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { View, StyleSheet, TextInput,Alert, Image, Text, Dimensions } from "react-native";
import React, { useState } from "react";
import React, { useContext, useState } from "react";
import Background from "../../../components/Background/Background";
import BlueButton from "../../../components/BlueButton/BlueButton";
import { Card } from 'react-native-elements';
import PasswordInput from "../../../components/PasswordInput";
import { changeUserEmail } from "../../../services/account/accountService";
import { InfoContext } from "../../../Context/InfoContext";

export default ChangeEmail = () => {

const [emailInput, setEmailInputInput] = useState("");
const [passwordInput, setPassowrdInput] = useState("");
const [validEmail, setValidEmail] = useState(true);
const { dispatchInfo } = useContext(InfoContext);

const emailInputHandler = (textInput) => {
setEmailInputInput(textInput);
(String(textInput)
Expand All @@ -20,17 +24,21 @@ export default ChangeEmail = () => {
};

const confirmButtonHandler = () => {
if(validEmail){
Alert.alert(
"Done!",
"Your Email has changed",
[
{
text: "OK",
style: "cancel",
},
]
);
if(validEmail&& passwordInput.length >= 8 ){
changeUserEmail(passwordInput, emailInput)
.then(() => {
Alert.alert(
"Done!",
"Your Email has changed",
[
{
text: "OK",
style: "cancel",
},
]
);
}).catch( e => dispatchInfo({ payload : {error: e} }) )

}
};

Expand Down
15 changes: 11 additions & 4 deletions screens/Settings/ChangePwd/ChangePwd.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import { StyleSheet, Alert, Image, Text, Dimensions } from "react-native";
import React, { useState } from "react";
import React, { useContext, useState } from "react";
import Background from "../../../components/Background/Background";
import BlueButton from "../../../components/BlueButton/BlueButton";
import { Card } from 'react-native-elements';
import PasswordInput from "../../../components/PasswordInput";
import { updateUserPassword } from "../../../services/account/accountService";
import { InfoContext } from "../../../Context/InfoContext";

export default ChangePwd = () => {

const [passwordInput, setPassowrdInput] = useState("");
const [currPass, setCurrPass ] = useState('');
const [confPass, setConfPass ] = useState('');
const { dispatchInfo } = useContext(InfoContext);

isValid = (password) => password.length >= 8

const confirmButtonHandler = () => {
if(isValid(passwordInput) && isValid(currPass) && isValid(confPass) ){
updateUserPassword(passwordInput)
Alert.alert(
let done = false;
try{
done = updateUserPassword(currPass, passwordInput);
}catch(error){
dispatchInfo({ payload : {error} });
}
done && Alert.alert(
"Done!",
"Your password has changed",
"Your password has been changed",
[
{
text: "OK",
Expand Down
19 changes: 18 additions & 1 deletion screens/VoteScreen/VoteScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { InfoContext } from "../../Context/InfoContext";
import BlueButton from "../../components/BlueButton/BlueButton";

import { CheckBox, Icon } from 'react-native-elements';
import { voteForParticipant } from "../../services/vote/userFuncs";


export default function VoteScreen(){
Expand All @@ -17,6 +18,22 @@ export default function VoteScreen(){
const [universities, setUniversities] = useState(["INSAT", "ESPRIT", "MSU"]);
const [choosed, setchoosed] = useState(null);
const [voted, setVoted] = useState(null);
const [loading, setLoading] = useState(false);

const handleSubmit = () => {
setLoading(true);
voteForParticipant(choosed)
.then(() => {
setVoted(choosed);
setLoading(false);
})
.catch(error => {
dispatchInfo({ payload : { error } });
console.log(error);
setLoading(false);
} )

}

return(
<Background>
Expand All @@ -41,7 +58,7 @@ export default function VoteScreen(){
checked={choosed === university}
onPress={() => setchoosed(university)}
/>))}
<BlueButton text="Vote" style={{ alignSelf: "center", marginTop: Dimensions.get("screen").height/10 }} buttonHandler={() => setVoted(choosed)} />
<BlueButton text="Vote" disabled={loading} style={{ alignSelf: "center", marginTop: Dimensions.get("screen").height/10 }} buttonHandler={handleSubmit} />
</>))}
</Card>
</Background>
Expand Down
6 changes: 4 additions & 2 deletions services/auth/loginService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
getCurrentUserData,
initCurrentUser,
isCurrentUserInited,
phoneToEmail
phoneToEmail,
updatePathValues
} from '../firestore/userFuncs';
import { CurrentUser } from '../../utils/user';
import { ErrorCodes } from '../../const/errorCodes';
Expand All @@ -26,7 +27,7 @@ import { updateNotificationToken } from '../account/accountService';
import { errorHandler } from '../exceptionHandler';
import { isPhoneNumber, isValidPhoneNumber } from '../../utils/verification/phoneNumber';
import { isValidEmail } from '../../utils/verification/emailAddress';

import { USER_PATH } from "../../const/firestorePaths";

/**
* Logins a user using an identifier & a password.
Expand Down Expand Up @@ -114,6 +115,7 @@ export async function signinWithEmail(email, password) {
* @public
*/
export async function signOut(){
updatePathValues(USER_PATH + auth.currentUser.uid, { notificationToken: null } );
await firebaseSignOut(auth).catch(errorHandler);
CurrentUser.logout();
return true;
Expand Down
7 changes: 5 additions & 2 deletions services/vote/userFuncs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import { CurrentUser } from '../../utils/user';
*/
export async function voteForParticipant(pID){
if(!auth.currentUser) throw new FirebaseError(ErrorCodes.NOT_LOGGED_IN[0], ErrorCodes.NOT_LOGGED_IN[1]);
(CurrentUser.votedFor === undefined) && (CurrentUser.votedFor = null);
if(CurrentUser.votedFor === pID) return;

let refNewPart = ref(rtdb, PART_PATH+pID);
let userPath = ref(rtdb, USER_PATH+auth.currentUser.uid);

// Transaction : Do all or nothing
await runTransaction(rtdb, ()=>{

await runTransaction(refNewPart, async () => {
if(CurrentUser.votedFor != null){ // User is changing vote
let refOldPart = ref(rtdb, PART_PATH+CurrentUser.votedFor);
await update(refOldPart, {
Expand All @@ -39,6 +41,7 @@ export async function voteForParticipant(pID){
votedFor: CurrentUser.votedFor
})
}).catch((err) => {
console.log(err)
throw new FirebaseError(ErrorCodes.VOTE_ERROR[0], ErrorCodes.VOTE_ERROR[1])
})

Expand Down

0 comments on commit d46777a

Please sign in to comment.