Skip to content

Commit

Permalink
feat: add post request integration with API
Browse files Browse the repository at this point in the history
  • Loading branch information
geeekgod committed Oct 6, 2022
1 parent 6197c67 commit aee45fa
Show file tree
Hide file tree
Showing 4 changed files with 313 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/context/AuthContext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const AuthContextProvider = ({ children }) => {
if (userN && accessTokenN) {
getUser(JSON.parse(accessTokenN));
}
}, 1000)
}, 500)
} catch (err) {
console.log(err);
} finally {
Expand Down
27 changes: 26 additions & 1 deletion app/context/DataContext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { createContext, useCallback, useContext, useEffect, useState } fr
import AsyncStorage from "@react-native-async-storage/async-storage";
import * as SplashScreen from 'expo-splash-screen';
import { AuthContext } from "../AuthContext";
import bloodLineApi from "../../api";

SplashScreen.preventAutoHideAsync();

Expand All @@ -21,13 +22,37 @@ const DataContextProvider = ({ children }) => {

const [appIsReady, setAppIsReady] = useState(false)
const [profile, setProfile] = useState({});
const [requests, setRequests] = useState([])

const getRequest = (token) => {
bloodLineApi.get('/request', {
headers: {
Authorization: token
}
}).then((res) => {
if (res.data.success) {
setRequests(res.data.data)
storeData("@request", res.data.data)
}
}).catch((err) => {
if (err.response.status === 401 && err.response.data.message === "Not Authorized") {
logout()
}
})
}

useEffect(() => {
const firstLoad = async () => {
try {
setAppIsReady(false)
const profile = await AsyncStorage.getItem("@profile");
const accessTokenN = await AsyncStorage.getItem("@accessToken");
setProfile(JSON.parse(profile));
setTimeout(() => {
if (accessTokenN) {
getRequest(JSON.parse(accessTokenN));
}
}, 500)
} catch (err) {
console.log(err);
} finally {
Expand Down Expand Up @@ -61,7 +86,7 @@ const DataContextProvider = ({ children }) => {
return (
<DataContext.Provider
onLayout={onLayoutRootView}
value={{ profile, storeProfile }}
value={{ profile, storeProfile, requests, getRequest }}
>
{children}
</DataContext.Provider>
Expand Down
14 changes: 7 additions & 7 deletions app/screens/CreateProfile/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useContext, useEffect, useState } from 'react';
import { StyleSheet, View } from 'react-native';
import { heightScreen, widthScreen } from '../../utils/layout';
import { Button, Center, Select, CheckIcon, FormControl, HStack, Icon, IconButton, Image, Input, Stack, Text, VStack } from 'native-base';
import { Button, Center, Select, FormControl, HStack, Icon, IconButton, Image, Input, Stack, Text, VStack } from 'native-base';
import { AuthContext } from '../../context/AuthContext';
import { Fontisto, MaterialIcons, FontAwesome5, AntDesign } from "@expo/vector-icons";
import bloodLineApi from '../../api';
Expand Down Expand Up @@ -39,7 +39,7 @@ const bloodGroups = [
value: "AB+ve"
},
{
name: "Type O Negative",
name: "Type AB Negative",
value: "AB-ve"
},
]
Expand All @@ -63,32 +63,32 @@ const CreateProfile = () => {
const [bloodGroupErr, setBloodGroupErr] = useState("")

useEffect(() => {
if (phone !== "") {
if (phone.trim() !== "") {
setPhoneErr("Phone number is required")
} else {
setPhoneErr("")
}

if (phone.length < 10 || phone.length > 10) {
if (phone.trim().length < 10 || phone.trim().length > 10) {
setPhoneErr("Phone number is invalid")
} else {
setPhoneErr("")
}

if (city !== "") {
if (city.trim() !== "") {
setCityErr("City name is required")
} else {
setCityErr("")
}

if (city.length < 3) {
if (city.trim().length < 3) {
setCityErr("City name is too short")
}
else {
setCityErr("")
}

if (pin.length < 5 && pin !== "") {
if (pin.trim().length < 5 && pin !== "") {
setPinErr("Pin code is invalid")
}
else {
Expand Down
Loading

0 comments on commit aee45fa

Please sign in to comment.