Skip to content

Commit e36413b

Browse files
committed
refactor: ListWithFetch.tsx logics enhancement
1 parent 423f4ee commit e36413b

5 files changed

Lines changed: 471 additions & 52 deletions

File tree

__tests__/ListWithFetch.test.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ test('displays images from the server', async () => {
1515
expect(loadingSpinner).not.toBeUndefined()
1616

1717
//load images from server
18-
const images = await findAllByLabelText(/flavor/i)
19-
expect(images).toHaveLength(2)
18+
const userContainers = await findAllByLabelText(/user-container/i)
19+
expect(userContainers).toHaveLength(10)
2020

2121
//loading spinner no longer shows
2222
expect(queryByLabelText(/loader/i)).toBeNull()
@@ -25,15 +25,12 @@ test('displays images from the server', async () => {
2525
expect(queryByLabelText(/alert/i)).toBeNull()
2626
})
2727

28-
test('displays error upon error esponse from server', async () => {
28+
test('displays error upon error response from server', async () => {
2929
server.resetHandlers(
30-
rest.get(
31-
'https://4ec38857-2800-4f07-838e-535a78cf7d51.mock.pstmn.io/flavors',
32-
(res, req, ctx) => {
33-
// @ts-ignore
34-
res(ctx.status(500))
35-
},
36-
),
30+
rest.get('https://random-data-api.com/api/v2/users', (res, req, ctx) => {
31+
// @ts-ignore
32+
res(ctx.status(500))
33+
}),
3734
)
3835
render(<ListWithFetch />)
3936
const {findByLabelText, getByLabelText, getByText, queryByLabelText} = screen

ios/Podfile.lock

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,12 @@ PODS:
272272
- React-jsinspector (0.66.0)
273273
- React-logger (0.66.0):
274274
- glog
275-
- react-native-safe-area-context (3.4.1):
275+
- react-native-safe-area-context (4.3.3):
276+
- RCT-Folly
277+
- RCTRequired
278+
- RCTTypeSafety
276279
- React-Core
280+
- ReactCommon/turbomodule/core
277281
- react-native-video (5.2.0):
278282
- React-Core
279283
- react-native-video/Video (= 5.2.0)
@@ -348,7 +352,7 @@ PODS:
348352
- React-Core
349353
- RNCMaskedView (0.1.11):
350354
- React
351-
- RNGestureHandler (1.10.3):
355+
- RNGestureHandler (2.6.0):
352356
- React-Core
353357
- RNScreens (3.17.0):
354358
- React-Core
@@ -540,7 +544,7 @@ SPEC CHECKSUMS:
540544
React-jsiexecutor: 6a05173dc0142abc582bd4edd2d23146b8cc218a
541545
React-jsinspector: be95ad424ba9f7b817aff22732eb9b1b810a000a
542546
React-logger: 9a9cd87d4ea681ae929b32ef580638ff1b50fb24
543-
react-native-safe-area-context: 9e40fb181dac02619414ba1294d6c2a807056ab9
547+
react-native-safe-area-context: b456e1c40ec86f5593d58b275bd0e9603169daca
544548
react-native-video: a4c2635d0802f983594b7057e1bce8f442f0ad28
545549
React-perflogger: 1f554c2b684e2f484f9edcdfdaeedab039fbaca8
546550
React-RCTActionSheet: 610d5a5d71ab4808734782c8bca6a12ec3563672
@@ -556,7 +560,7 @@ SPEC CHECKSUMS:
556560
ReactCommon: eb059748e842a1a86025ebbd4ac9d99e74492f88
557561
RNCAsyncStorage: b03032fdbdb725bea0bd9e5ec5a7272865ae7398
558562
RNCMaskedView: 0e1bc4bfa8365eba5fbbb71e07fbdc0555249489
559-
RNGestureHandler: a479ebd5ed4221a810967000735517df0d2db211
563+
RNGestureHandler: 920eb17f5b1e15dae6e5ed1904045f8f90e0b11e
560564
RNScreens: 0df01424e9e0ed7827200d6ed1087ddd06c493f9
561565
Yoga: c11abbf5809216c91fcd62f5571078b83d9b6720
562566
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

src/components/ListWithFetch.tsx

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import React, {useEffect, useState} from 'react'
2-
import {ActivityIndicator, Image, StyleSheet, Text, View} from 'react-native'
2+
import {
3+
ActivityIndicator,
4+
Image,
5+
ScrollView,
6+
StyleSheet,
7+
Text,
8+
View,
9+
} from 'react-native'
310
import {Colors} from 'react-native/Libraries/NewAppScreen'
411
import axios from 'axios'
512

13+
const AVATAR_SIZE = 68
14+
615
export default () => {
716
const [flavorsData, setFlavorsData] = useState([])
817
const [loading, setLoading] = useState(false)
@@ -13,7 +22,7 @@ export default () => {
1322
setLoading(true)
1423
try {
1524
const response = await axios.get(
16-
'https://4ec38857-2800-4f07-838e-535a78cf7d51.mock.pstmn.io/flavors',
25+
'https://random-data-api.com/api/v2/users?size=10',
1726
)
1827
// @ts-ignore
1928
setFlavorsData(response.data)
@@ -28,9 +37,8 @@ export default () => {
2837
}, [])
2938

3039
return (
31-
<View style={styles.body}>
32-
<Text>The Ice Cream Shoppe</Text>
33-
<Text>Today's Flavors</Text>
40+
<ScrollView>
41+
<Text>The Funky Users DB</Text>
3442
{loading && (
3543
<ActivityIndicator
3644
color={'#000'}
@@ -43,17 +51,27 @@ export default () => {
4351
<Text>Error oopsie!</Text>
4452
</View>
4553
)}
46-
{flavorsData.map(({name, image}) => (
47-
<View key={name} style={styles.flavorContainer}>
48-
<Image
49-
source={{uri: image}}
50-
style={styles.image}
51-
accessibilityLabel={`${name}-flavor`}
52-
/>
53-
<Text>{name}</Text>
54-
</View>
55-
))}
56-
</View>
54+
{flavorsData.map(
55+
({first_name, last_name, uid, avatar, email, phone_number}) => (
56+
<View
57+
key={uid}
58+
style={styles.userContainer}
59+
accessibilityLabel={`${uid}-user-container`}>
60+
>
61+
<View style={styles.avatarWrapper}>
62+
<Image source={{uri: avatar}} style={styles.image} />
63+
</View>
64+
<View style={styles.userInfoContainer}>
65+
<Text>
66+
{first_name} {last_name}
67+
</Text>
68+
<Text>{email}</Text>
69+
<Text>{phone_number}</Text>
70+
</View>
71+
</View>
72+
),
73+
)}
74+
</ScrollView>
5775
)
5876
}
5977

@@ -64,6 +82,18 @@ const styles = StyleSheet.create({
6482
flex: 1,
6583
},
6684
errorContainer: {backgroundColor: '#C63939', padding: 16, borderRadius: 6},
67-
flavorContainer: {alignItems: 'center', margin: 8},
68-
image: {width: 100, height: 100},
85+
userContainer: {
86+
alignItems: 'center',
87+
flexDirection: 'row',
88+
padding: 16,
89+
marginBottom: 8,
90+
flex: 1,
91+
},
92+
avatarWrapper: {
93+
backgroundColor: 'rgba(88,186,224,0.65)',
94+
padding: 16,
95+
borderRadius: AVATAR_SIZE,
96+
},
97+
userInfoContainer: {flex: 1, marginLeft: 16},
98+
image: {height: AVATAR_SIZE, width: AVATAR_SIZE},
6999
})

0 commit comments

Comments
 (0)