Skip to content

Commit 585559b

Browse files
author
Thiago Lopes
committed
Moved ProductCardProps to types and refact DetailsScreen
1 parent de2c7c4 commit 585559b

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

src/components/ProductCard.tsx

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
import { useMemo, useState } from 'react'
2-
import {
3-
StyleSheet,
4-
TouchableOpacity,
5-
View,
6-
Text,
7-
ImageProps,
8-
Image
9-
} from 'react-native'
2+
import { StyleSheet, TouchableOpacity, View, Text, Image } from 'react-native'
103
import { useNavigation } from '@react-navigation/native'
114
import { NativeStackNavigationProp } from '@react-navigation/native-stack'
125
import Icon from 'react-native-vector-icons/FontAwesome6'
136
import { colors } from '@Theme/colors'
14-
import { Product } from '@Data/types'
7+
import { ProductCardProps } from '@Data/types'
158
import StarsRating from '@ComponentsStarsRating'
169
import LikeButton from '@ComponentsLikeButton'
1710
import formatUSD from '@Utils/formatPrice'
1811
import BlobBackground from '@Assets/brand/backgroundBlob.svg'
1912

20-
export interface ProductCardProps
21-
extends Omit<Product, 'image_link' | 'colors' | 'average_rating'> {
22-
imgUrl: ImageProps['source']
23-
productColors: Product['colors']
24-
averageRating: Product['average_rating']
25-
}
26-
2713
const ProductCard = ({
2814
id,
2915
brand,

src/data/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import { ImageProps } from 'react-native'
2+
3+
export interface ProductCardProps
4+
extends Omit<Product, 'image_link' | 'colors' | 'average_rating'> {
5+
imgUrl: ImageProps['source']
6+
productColors: Product['colors']
7+
averageRating: Product['average_rating']
8+
}
9+
110
export interface Product {
211
index: number
312
id: string

src/screens/DetailScreen.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,12 @@ interface DetailScreenProps {
2727
export default function DetailScreen({ route, navigation }: DetailScreenProps) {
2828
const { index: productIndex } = route?.params || { index: 0 }
2929
const product = allProducts[productIndex]
30-
const {
31-
colors: productColors,
32-
image_link: imgUrl,
33-
average_rating: averageRating
34-
} = product
3530

3631
const [liked, setLiked] = useState(false)
37-
const [selectedColor, setSelectedColor] = useState(productColors[0].color)
32+
const [selectedColor, setSelectedColor] = useState(product.colors[0].color)
3833

3934
useEffect(() => {
40-
setSelectedColor(productColors[0].color)
35+
setSelectedColor(product.colors[0].color)
4136
}, [productIndex])
4237

4338
function addToCart() {
@@ -70,14 +65,14 @@ export default function DetailScreen({ route, navigation }: DetailScreenProps) {
7065
<AppLayout fullHeight>
7166
<View style={styles.productVisual}>
7267
<BlobBackground
73-
fill={colors[productColors[0].blobBg]}
68+
fill={colors[product.colors[0].blobBg]}
7469
style={[
7570
styles.productBlob,
7671
{ transform: [{ scaleX: productIndex % 2 === 0 ? 1 : -1 }] }
7772
]}
7873
/>
7974
<Image
80-
source={imgUrl}
75+
source={product.image_link}
8176
style={[
8277
styles.productImg,
8378
{ maxWidth: product.brand === 'Beats' ? 138 : 200 }
@@ -111,13 +106,15 @@ export default function DetailScreen({ route, navigation }: DetailScreenProps) {
111106

112107
<View style={styles.productInfo}>
113108
<View style={styles.starsContainer}>
114-
<StarsRating {...{ averageRating, size: 12 }} />
109+
<StarsRating
110+
{...{ averageRating: product.average_rating, size: 12 }}
111+
/>
115112
</View>
116113

117114
<Text style={styles.productName}>{product.name}</Text>
118115

119116
<Text style={styles.productPrice}>
120-
{formatUSD(productColors[0].price)}
117+
{formatUSD(product.colors[0].price)}
121118
</Text>
122119
</View>
123120

@@ -126,7 +123,7 @@ export default function DetailScreen({ route, navigation }: DetailScreenProps) {
126123
<Text style={styles.label}>Colors</Text>
127124

128125
<View style={styles.colorsContainer}>
129-
{productColors.map((item: ProductColor, index) => (
126+
{product.colors.map((item: ProductColor, index) => (
130127
<TouchableOpacity
131128
key={index}
132129
hitSlop={{ top: 10, right: 10, bottom: 10, left: 10 }}

0 commit comments

Comments
 (0)