Skip to content

Commit

Permalink
Add state actions
Browse files Browse the repository at this point in the history
  • Loading branch information
IzuEneh committed Jul 16, 2023
1 parent 2e48bed commit 4439500
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
24 changes: 21 additions & 3 deletions src/Modules/Common/api/SavedState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type State = {

type Action = {
type: string;
data: any;
data: SavedRestaurant | { id: string };
};

const initialState = {
Expand Down Expand Up @@ -93,10 +93,28 @@ const useSavedRestaurantsDispatch = () => {
function savedRestaurantReducer(state: State, action: Action): State {
switch (action.type) {
case "add": {
return state;
const newRestaurant = action.data;
if (
(newRestaurant as SavedRestaurant).name == undefined ||
state.saved.some((item) => item.id == newRestaurant.id)
) {
return state;
}

const saved = [...state.saved, newRestaurant as SavedRestaurant];
return {
...state,
saved,
};
}
case "remove": {
return state;
const { id } = action.data;

const saved = state.saved.filter((item) => item.id !== id);
return {
...state,
saved,
};
}
default:
return state;
Expand Down
5 changes: 3 additions & 2 deletions src/Modules/Details/BottomBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import { Ionicons } from "@expo/vector-icons";
import { AntDesign } from "@expo/vector-icons";

type Props = {
onSave: () => void;
coordinates: { latitude: string; longitude: string };
phone: string;
style?: ViewStyle;
};

function BottomBar({ coordinates, phone, style }: Props) {
function BottomBar({ coordinates, phone, style, onSave }: Props) {
const handleNavigate = async () => {
const { latitude, longitude } = coordinates;
const url = `https://www.google.com/maps/dir/?api=1&destination=${latitude},${longitude}`;
Expand Down Expand Up @@ -53,7 +54,7 @@ function BottomBar({ coordinates, phone, style }: Props) {
</TouchableOpacity>
<TouchableOpacity
style={[styles.callButton, styles.iconButton]}
onPress={() => {}}
onPress={onSave}
>
<AntDesign name="hearto" size={24} color="#ED4337" />
<Text style={styles.callButtonText}>Save</Text>
Expand Down
13 changes: 13 additions & 0 deletions src/Modules/Details/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import Reviews from "./Reviews";
import BusinessHeader from "./BusinessHeader";
import ImageSlider from "./ImageSlider";
import { useRestaurant } from "Modules/Common/hooks/";
import { useSavedRestaurantsDispatch } from "Modules/Common/api/SavedState";

type NavProp = NativeStackScreenProps<RootStackParamList, "Details">;

function Details({ route }: NavProp) {
const { id } = route.params;
const business = useRestaurant(id);
const dispatch = useSavedRestaurantsDispatch();

if (!business) {
return (
Expand All @@ -36,6 +38,17 @@ function Details({ route }: NavProp) {
coordinates={business.coordinates}
phone={business.phone}
style={styles.bottomBar}
onSave={() => {
dispatch({
type: "add",
data: {
categories: business.categories,
id: business.id,
image_url: business.image_url,
name: business.name,
},
});
}}
/>
</>
);
Expand Down
1 change: 1 addition & 0 deletions src/Modules/Drawer/ListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const styles = StyleSheet.create({
container: {
paddingHorizontal: 16,
paddingTop: 48,
paddingBottom: 100,
},
separator: {
borderColor: "#94a3b8",
Expand Down

0 comments on commit 4439500

Please sign in to comment.