Skip to content

Commit 1be2880

Browse files
author
Thiago Lopes
committed
Create & added getTotalCarPrice
1 parent a98c5fc commit 1be2880

File tree

4 files changed

+77
-23
lines changed

4 files changed

+77
-23
lines changed

src/context/GlobalState.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const GlobalContext = createContext<GlobalStateContextProps>({
1919
addToCart: () => {},
2020
increaseQty: () => {},
2121
decreaseQty: () => {},
22+
getTotalCartPrice: () => 0,
2223
deleteAllFromCart: () => {}
2324
})
2425

@@ -32,6 +33,11 @@ export const GlobalProvider = ({ children }: GlobalStateProps) => {
3233
const isOnFavorite = (id: string) => {
3334
return state.favorites.some((product) => product.id === id)
3435
}
36+
const getTotalCartPrice = () => {
37+
return state.cart.reduce((acc, product) => {
38+
return acc + product.colors[0].price * product.colors[0].quantity
39+
}, 0)
40+
}
3541
const toggleFavorite = (product: Product) => {
3642
dispatch({
3743
type: ActionTypes.TOGGLE_FAVORITE,
@@ -80,6 +86,7 @@ export const GlobalProvider = ({ children }: GlobalStateProps) => {
8086
addToCart,
8187
increaseQty,
8288
decreaseQty,
89+
getTotalCartPrice,
8390
deleteAllFromCart
8491
}}
8592
>

src/data/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface GlobalStateContextProps extends AppState {
1010
addToCart: (product: Product) => void
1111
increaseQty: (id: string, color: string) => void
1212
decreaseQty: (id: string, color: string) => void
13+
getTotalCartPrice: () => number
1314
deleteAllFromCart: () => void
1415
}
1516
export interface AppState {

src/screens/CartScreen.tsx

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ import { colors } from '@Theme/colors'
1111
import AppLayout from '@ComponentsAppLayout'
1212
import ShoppingCard from '@ComponentsShoppingCard'
1313
import { GlobalContext } from '@Context/GlobalState'
14+
import formatPrice from '@Utils/formatPrice'
1415

1516
export default function CartScreen() {
16-
const { cart, deleteAllFromCart, increaseQty, decreaseQty } =
17-
useContext(GlobalContext)
17+
const {
18+
cart,
19+
deleteAllFromCart,
20+
increaseQty,
21+
decreaseQty,
22+
getTotalCartPrice
23+
} = useContext(GlobalContext)
24+
25+
function buyNow() {
26+
console.log(`buy now`)
27+
}
1828

1929
return (
2030
<AppLayout fullHeight>
@@ -34,26 +44,46 @@ export default function CartScreen() {
3444
</TouchableOpacity>
3545

3646
{cart.length ? (
37-
<ScrollView
38-
contentContainerStyle={styles.itemsScroll}
39-
showsVerticalScrollIndicator={false}
40-
>
41-
{cart.map((product) => (
42-
<ShoppingCard
43-
{...product}
44-
{...{
45-
key: product.id + product.colors[0].color,
46-
imgUrl: product.image_link,
47-
productColors: product.colors,
48-
averageRating: product.average_rating,
49-
increaseQty: () =>
50-
increaseQty(product.id, product.colors[0].color),
51-
decreaseQty: () =>
52-
decreaseQty(product.id, product.colors[0].color)
53-
}}
54-
/>
55-
))}
56-
</ScrollView>
47+
<>
48+
<ScrollView
49+
contentContainerStyle={styles.itemsScroll}
50+
showsVerticalScrollIndicator={false}
51+
>
52+
{cart.map((product) => (
53+
<ShoppingCard
54+
{...product}
55+
{...{
56+
key: product.id + product.colors[0].color,
57+
imgUrl: product.image_link,
58+
productColors: product.colors,
59+
averageRating: product.average_rating,
60+
increaseQty: () =>
61+
increaseQty(product.id, product.colors[0].color),
62+
decreaseQty: () =>
63+
decreaseQty(product.id, product.colors[0].color)
64+
}}
65+
/>
66+
))}
67+
</ScrollView>
68+
69+
<TouchableOpacity
70+
onPress={buyNow}
71+
disabled={!cart.length}
72+
activeOpacity={0.6}
73+
style={[
74+
styles.buyNowBtn,
75+
{
76+
backgroundColor: cart.length
77+
? colors.primaryColor
78+
: colors.blackCardBg
79+
}
80+
]}
81+
>
82+
<Text style={styles.buyNowLabel}>
83+
BUY NOW: {formatPrice(getTotalCartPrice())}
84+
</Text>
85+
</TouchableOpacity>
86+
</>
5787
) : (
5888
<View style={styles.emptyCart}>
5989
<Icon
@@ -82,7 +112,22 @@ const styles = StyleSheet.create({
82112
paddingVertical: 12,
83113
marginBottom: 20
84114
},
115+
buyNowBtn: {
116+
position: 'absolute',
117+
bottom: 0,
118+
width: '85%',
119+
alignSelf: 'center',
120+
borderRadius: 20,
121+
paddingVertical: 12,
122+
marginBottom: 10
123+
},
85124
deleteAllLabel: {
125+
color: colors.whiteColor,
126+
fontFamily: 'Poppins_400Regular',
127+
textAlign: 'center'
128+
},
129+
buyNowLabel: {
130+
fontFamily: 'Poppins_600SemiBold',
86131
color: colors.whiteColor,
87132
textAlign: 'center'
88133
},
@@ -105,7 +150,7 @@ const styles = StyleSheet.create({
105150
itemsScroll: {
106151
rowGap: 20,
107152
width: '100%',
108-
paddingBottom: 20,
153+
paddingBottom: 80,
109154
paddingHorizontal: 35,
110155
alignSelf: 'center'
111156
}

src/screens/FavoriteScreen.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const styles = StyleSheet.create({
8383
},
8484
deleteAllLabel: {
8585
color: colors.whiteColor,
86+
fontFamily: 'Poppins_400Regular',
8687
textAlign: 'center'
8788
},
8889
emptyFavorites: {

0 commit comments

Comments
 (0)