Skip to content

Commit

Permalink
feat: add saved requests and nearby request
Browse files Browse the repository at this point in the history
  • Loading branch information
geeekgod committed Oct 10, 2022
1 parent 0c10c6d commit 30d2488
Show file tree
Hide file tree
Showing 7 changed files with 577 additions and 16 deletions.
103 changes: 100 additions & 3 deletions app/context/DataContext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import AsyncStorage from "@react-native-async-storage/async-storage";
import * as SplashScreen from 'expo-splash-screen';
import { AuthContext } from "../AuthContext";
import bloodLineApi from "../../api";
import * as Location from 'expo-location';

SplashScreen.preventAutoHideAsync();

Expand All @@ -23,7 +24,13 @@ const DataContextProvider = ({ children }) => {
const [appIsReady, setAppIsReady] = useState(false)
const [profile, setProfile] = useState({});
const [requests, setRequests] = useState([])

const [savedRequests, setSavedRequests] = useState([]);
const [nearByRequests, setNearByRequests] = useState([])


const [location, setLocation] = useState(null);

console.log("nearByRequests", nearByRequests, location);
const getRequest = (token) => {
bloodLineApi.get('/request', {
headers: {
Expand All @@ -41,16 +48,66 @@ const DataContextProvider = ({ children }) => {
})
}

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

const getNearByRequests = (token) => {
console.log(2, location !== null, location);
if (location !== null) {
console.log(1);
bloodLineApi.post('/request/near', {
long: location.coords.longitude,
lat: location.coords.latitude,
headers: {
Authorization: token
}
}).then((res) => {
console.log(res.data);
if (res.data.success) {
setNearByRequests(res.data.data)
storeData("@nearByRequest", res.data.data)
}
}).catch((err) => {
console.log(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));
const requestsN = await AsyncStorage.getItem("@request");
const savedRequestN = await AsyncStorage.getItem("@savedRequest");
const nearByRequest = await AsyncStorage.getItem("@nearByRequest");
setProfile(JSON.parse(profile) ? JSON.parse(profile) : {});
setRequests(JSON.parse(requestsN) ? JSON.parse(requestsN) : [])
setSavedRequests(JSON.parse(savedRequestN) ? JSON.parse(savedRequestN) : [])
setNearByRequests(JSON.parse(nearByRequest) ? JSON.parse(nearByRequest) : [])
setTimeout(() => {
if (accessTokenN) {
getRequest(JSON.parse(accessTokenN));
getSavedRequest(JSON.parse(accessTokenN))
getNearByRequests(JSON.parse(accessTokenN))
}
}, 500)
} catch (err) {
Expand Down Expand Up @@ -79,14 +136,54 @@ const DataContextProvider = ({ children }) => {
};


// To get location

useEffect(() => {
(async () => {

let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}

let location = await Location.getCurrentPositionAsync({});
setLocation(location);
setTimeout(() => setLocation(null))
})();
}, []);

const getLocation = async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}

let location = await Location.getCurrentPositionAsync({});
setLocation(location);
}


if (!appIsReady) {
return null
}

return (
<DataContext.Provider
onLayout={onLayoutRootView}
value={{ profile, storeProfile, requests, getRequest }}
value={{
profile,
storeProfile,
requests,
getRequest,
getSavedRequest,
savedRequests,
location,
getLocation,
getNearByRequests,
nearByRequests
}}
>
{children}
</DataContext.Provider>
Expand Down
25 changes: 24 additions & 1 deletion app/navigation/DrawerNavigator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Home from '../../screens/Home';
import PostRequest from '../../screens/PostRequest';
import { Ionicons } from "@expo/vector-icons";
import CustomDrawerHeader from '../../components/CustomDrawerHeader';
import SavedRequests from '../../screens/SavedRequests';
import NearByRequests from '../../screens/NearByRequests';

const Drawer = createDrawerNavigator();

Expand Down Expand Up @@ -33,13 +35,34 @@ const DrawerNavigator = () => {
>
<Drawer.Screen name="Home"
options={{
header: (props) => <CustomDrawerHeader {...props} title={'Home'}/>,
header: (props) => <CustomDrawerHeader {...props} title={'Home'} />,
drawerLabel: "Home",
drawerIcon: ({ color }) => (
<Ionicons name="home-outline" size={25} color={color} />
),
}}
component={Home} />

<Drawer.Screen name="SavedRequest"
options={{
header: (props) => <CustomDrawerHeader {...props} title={'Saved Requests'} />,
drawerLabel: "Saved Requests",
drawerIcon: ({ color }) => (
<Ionicons name="bookmark" size={25} color={color} />
),
}}
component={SavedRequests} />

<Drawer.Screen name="NearByRequest"
options={{
header: (props) => <CustomDrawerHeader {...props} title={'Near By Request'} />,
drawerLabel: "Near By Request",
drawerIcon: ({ color }) => (
<Ionicons name="location" size={25} color={color} />
),
}}
component={NearByRequests} />

<Drawer.Screen name="PostRequest"
options={{
header: (props) => <CustomDrawerHeader {...props} title={'Post Request'} />,
Expand Down
66 changes: 58 additions & 8 deletions app/screens/Home/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import React, { useContext, useEffect, useState } from 'react';
import { StyleSheet, View } from 'react-native';
import { StyleSheet, TouchableOpacity, View } from 'react-native';
import { heightScreen, widthScreen } from '../../utils/layout';
import { Box, Button, CloseIcon, HStack, Divider, Icon, IconButton, Text, VStack, FlatList } from 'native-base';
import { Box, Button, HStack, Divider, Icon, IconButton, Text, VStack, FlatList } from 'native-base';
import { DataContext } from '../../context/DataContext';
import * as Linking from 'expo-linking'
import Empty from "../../assets/svg/home/home.svg";
import { AuthContext } from '../../context/AuthContext';
import { Ionicons } from '@expo/vector-icons';
import bloodLineApi from '../../api';



const Home = () => {


const { requests, getRequest } = useContext(DataContext);
const { accessToken } = useContext(AuthContext)
const { requests, getRequest, getSavedRequest, savedRequests } = useContext(DataContext);
const { accessToken, getUser } = useContext(AuthContext)

const [refreshing, setRefreshing] = useState(false);

const loadRequest = async () => {
return new Promise(() => {
getRequest(accessToken)
getSavedRequest(accessToken)
setRefreshing(false)
})
}
Expand All @@ -29,6 +32,23 @@ const Home = () => {
await loadRequest()
}

const saveUnSave = (id) => {
const headers = { headers: { "Authorization": accessToken } }
bloodLineApi.get(`/request/save?requestId=${id}`, {
headers, withCredentials: true
}).then((res) => {
if (res.data.success) {
getSavedRequest(accessToken)
getRequest(accessToken)
getUser(accessToken);
}
}).catch((err) => {
if (err.response) {
console.log(err.response.data);
}
})
}

return (
<View style={styles.container}>
<Box pt='4' pb='4'>
Expand All @@ -44,18 +64,31 @@ const Home = () => {
refreshing={refreshing}
onRefresh={onRefresh}
contentContainerStyle={{ paddingVertical: 15 }} data={requests} renderItem={({ item }) => {
const isSaved = savedRequests.length > 0 && savedRequests.findIndex((e) => e._id === item._id) !== -1
return (
<Box border="1" mt='3' mb='3' borderRadius="xl"
w='full'
position='relative'
borderColor='muted.300' borderWidth='1'>
<IconButton
zIndex='100'
variant='unstyled'
position='absolute'
rounded='full'
right='0'
_icon={{
color: 'primary.100',
as: Ionicons,
name: isSaved ? "bookmark" : "bookmark-outline",
size: "xl",
}}
onPress={() => saveUnSave(item._id)}
/>
<VStack space="2">
<Text fontFamily='body' fontWeight='600' fontSize='lg'
px="4" pt="4">
{item.name}
</Text>
<Box px="4">
{item.email}
</Box>
<Box px="4">
{item.address}
</Box>
Expand All @@ -65,6 +98,24 @@ const Home = () => {
<Box px="4">
{item.pin}
</Box>
<TouchableOpacity
onPress={() => Linking.openURL(`mailto:${item.email}`)}
style={{ marginVertical: 5 }}
activeOpacity={0.6}
>
<Box px="4" >
{`Mail to ${item.email}`}
</Box>
</TouchableOpacity>
<TouchableOpacity
onPress={() => Linking.openURL(`tel:+91${item.phone}`)}
style={{ marginVertical: 5 }}
activeOpacity={0.6}
>
<Box px="4" >
{`Contact on ${item.phone}`}
</Box>
</TouchableOpacity>
<Box px='4' pb="4">
<Button
size='md'
Expand Down Expand Up @@ -103,7 +154,6 @@ const styles = StyleSheet.create({
width: widthScreen,
backgroundColor: "#fff",
paddingHorizontal: heightScreen > 800 ? 32 : 26,
// justifyContent: "center",
alignItems: "center",
position: "relative",
},
Expand Down
Loading

0 comments on commit 30d2488

Please sign in to comment.