Browse Source

Adding api response error handling

master
GabrielTrettel 3 years ago
parent
commit
5bb8bc5f5d
  1. 1
      src/app/navigation/AuthNavigator.js
  2. 60
      src/app/screens/PasswordRecoveryChangePswdScreen.js
  3. 3
      src/app/screens/PasswordRecoveryScreen.js

1
src/app/navigation/AuthNavigator.js

@ -36,6 +36,7 @@ const AuthNavigator = () => (
<Stack.Screen
name="PasswordRecoveryChangePswd"
component={PasswordRecoveryChangePswd}
initialParams={{ authToken : "" }}
options={{ headerTitle: "" }}
/>

60
src/app/screens/PasswordRecoveryChangePswdScreen.js

@ -1,4 +1,4 @@
import React from "react";
import React, { useState } from "react";
import { StyleSheet, View, Text } from "react-native";
import * as Yup from "yup";
@ -6,6 +6,7 @@ import { Form, SubmitButton } from "../components/forms";
import colors from "../config/colors";
import { dimensions } from "../config/dimensions";
import PasswordFormField from "../components/forms/PasswordFormField";
import ConfirmationModal from "../components/ConfirmationModal";
const validationSchema = Yup.object().shape({
password: Yup.string()
@ -18,13 +19,58 @@ const validationSchema = Yup.object().shape({
.matches(/[a-zA-Z]/, "A senha só pode conter letras"),
});
export default function PasswordRecoveryChangePswd() {
export default function PasswordRecoveryChangePswd({ route }) {
const authToken = route.params.authToken;
console.log(authToken);
const [confirmatioModalData, setConfirmatioModalData] = useState({
show: false,
message: "",
});
const comparePassword = (password, confirmPassword) => {
return password !== confirmPassword;
};
const handleApiReturn = (apiReturn) => {
console.log(apiReturn);
switch (apiReturn) {
case 200:
setConfirmatioModalData({
message: "Senha alterada com sucesso",
show: true,
});
break;
case 404:
setConfirmatioModalData({
message: "Número de telefone inválido",
show: true,
});
break;
case 500:
case 403:
console.log("EXPIRED TOKEN");
break;
default:
setConfirmatioModalData({
message: "Algo deu errado, tente novamente mais tarde",
show: true,
});
break;
}
};
return (
<View>
<ConfirmationModal
show={confirmatioModalData.show}
description={confirmatioModalData.message}
confirmationLabel="OK"
onConfirm={() =>
setConfirmatioModalData({ ...confirmatioModalData, show: false })
}
/>
<Form
validationSchema={validationSchema}
initialValues={{
@ -32,17 +78,21 @@ export default function PasswordRecoveryChangePswd() {
confirmPassword: "",
}}
onSubmit={(form, actions) => {
const psw_match = comparePassword(
const psw_not_match = comparePassword(
form.password,
form.confirmPassword
);
if (psw_match) {
if (psw_not_match) {
actions.setFieldError(
"confirmPassword",
"As senhas não correspondem"
);
} else {
// FIXME: This should be replaced by the proper api call in future
const apiReturnTest = 403;
handleApiReturn(apiReturnTest);
}
console.log("redefinir senha -> atualizar senha -> confirmar");
}}
>
<View style={{ padding: 16 }}>

3
src/app/screens/PasswordRecoveryScreen.js

@ -98,11 +98,12 @@ export default function PasswordRecovery({ navigation }) {
onSubmit={() => {
// FIXME: This should be replaced by the proper api call in future
const apiReturnTest = 200;
const apiReturnTokenTest = "API EXAMPLE TOKEN API";
switch (apiReturnTest) {
case 200:
console.log("ir pra proxima tela....");
navigation.navigate("PasswordRecoveryChangePswd")
navigation.navigate("PasswordRecoveryChangePswd", { authToken : apiReturnTokenTest })
break;
case 404:
setConfirmatioModalData({

Loading…
Cancel
Save