Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to mock some data #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/BookStore.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import { proxy, useSnapshot } from 'valtio';
import loadMockData from './mock.js'

// global state
const state = proxy({
Expand All @@ -12,7 +13,14 @@ async function loadBooks() {
const data = json ? JSON.parse(json) : [];
state.books = data;
}
loadBooks();


loadMockData().then(()=>{
setTimeout(()=>{
loadBooks();
},100)
})


// save books to async storage
async function saveBooks() {
Expand Down
98 changes: 98 additions & 0 deletions src/mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import AsyncStorage from '@react-native-async-storage/async-storage';

const mockData = [
{
bookId: 1,
status: 'Reading',
numPages: 150,
avgRating: 4,
bookTitleBare: 'Bare title',
imageUrl:'https://www.texanerin.com/content/uploads/2019/06/nobake-chocolate-cookies-1-650x975.jpg',
description: 'this is a good book',
relatedIds: [2,3],
author: {
name: 'David King',
image_url: 'https://th.bing.com/th/id/OIP.p0a8fUSQQAd99wh0AZdPJAHaEo?w=155&h=182&c=7&r=0&o=5&pid=1.7',
about: 'dwqnjkdwnqwjk 1223',
},
},
{
bookId: 2,
status: 'Reading',
bookTitleBare: 'Bare title',
numPages: 350,
avgRating: 4,
imageUrl:'https://th.bing.com/th/id/OIP.EYdT7l9cizAp4u7dRlA8ywHaD5?w=307&h=180&c=7&r=0&o=5&pid=1.7',
description: 'this is a not bad book',
relatedIds: [1,2],
author: {
name: 'David King father',
image_url: 'https://th.bing.com/th/id/OIP.p0a8fUSQQAd99wh0AZdPJAHaEo?w=155&h=182&c=7&r=0&o=5&pid=1.7',
about: 'dwqnjkdwnqwjk 1223',
}
},
{
bookId: 3,
status: 'Reading',
bookTitleBare: 'Bare title',
numPages: 50,
avgRating: 5,
imageUrl:'https://th.bing.com/th/id/OIP.9-1-IWqS-RiC8QCiWBuaxgHaEo?w=223&h=180&c=7&r=0&o=5&pid=1.7',
description: 'That is all right',
relatedIds: [1,2],
author: {
name: 'Tom',
image_url: 'https://th.bing.com/th/id/OIP.X550xngH_XGXcgSogi1XLAHaEK?w=304&h=180&c=7&r=0&o=5&pid=1.7',
about: 'dwqnjkdwnqwjk 1223',
}
},
{
bookId: 4,
status: 'Reading',
bookTitleBare: 'Bare title',
numPages: 150,
avgRating: 5,
imageUrl:'https://th.bing.com/th/id/OIP.ej6xO43EOubmidv01efXbgHaDx?w=319&h=178&c=7&r=0&o=5&pid=1.7',
description: 'nothing is written',
relatedIds: [],
author: {
name: 'Jerry',
image_url: 'https://th.bing.com/th/id/OIP.X550xngH_XGXcgSogi1XLAHaEK?w=304&h=180&c=7&r=0&o=5&pid=1.7',
about: 'dwqnjkdwnqwjk 1223',
}
},
{
bookId: 5,
status: 'Reading',
bookTitleBare: 'Bare title',
numPages: 150,
avgRating: 5,
imageUrl:'https://th.bing.com/th/id/OIP.il4Nq4ViBsylDuVRqRjYxwHaEo?w=287&h=180&c=7&r=0&o=5&pid=1.7',
description: 'where are you?',
relatedIds: [],
author: {
name: 'Kenin',
image_url: 'https://th.bing.com/th/id/OIP.9Hc_BKrM_ONJ0wcxJ-liAAHaEK?w=278&h=180&c=7&r=0&o=5&pid=1.7',
about: 'dwqnjkdwnqwjk 1223',
}
},
{
bookId: 6,
status: 'Completed',
bookTitleBare: 'Test title',
numPages: 220,
avgRating: 3,
imageUrl:'https://th.bing.com/th/id/OIP.il4Nq4ViBsylDuVRqRjYxwHaEo?w=287&h=180&c=7&r=0&o=5&pid=1.7',
description: 'where are you?',
relatedIds: [],
author: {
name: 'Kenin',
image_url: 'https://th.bing.com/th/id/OIP.9Hc_BKrM_ONJ0wcxJ-liAAHaEK?w=278&h=180&c=7&r=0&o=5&pid=1.7',
about: 'dwqnjkdwnqwjk 1223',
}
},
]

export default async function setMockData(){
return await AsyncStorage.setItem('@lists', JSON.stringify(mockData))
}
53 changes: 14 additions & 39 deletions src/screens/BookDetailsScreen.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,42 +110,19 @@ function BookDetailsScreen({ navigation, route }) {
},
});

// Find book in list
const item = bookList.find((b) => b.bookId === book.bookId);

// Load book details
useEffect(() => {
// Related Books
axios.get(`https://www.goodreads.com/book/auto_complete?format=json&q=${book.author.name}`)
.then((resp) => {
const bks = resp.data.filter((bk, i, arr) => {
arr[i].imageUrl = bk.imageUrl.replace(/_..../, '_SY475_');
return (book.bookId !== bk.bookId);
});
setRelated(bks);
})
.catch((error) => {
Console.log('Failed to related books:', error);
});

// Book details
axios.get(`https://www.goodreads.com/book/show/${book.bookId}.xml?key=Bi8vh08utrMY3HAqM9rkWA`)
.then((resp) => {
const data = parser.parse(resp.data);
setFullBook(data?.GoodreadsResponse?.book);
})
.catch((error) => {
Console.log('Failed to get book details:', error);
});

// Author details
axios.get(`https://www.goodreads.com/author/show.xml?key=Bi8vh08utrMY3HAqM9rkWA&id=${book.author.id}`)
.then((resp) => {
const data = parser.parse(resp.data);
setAuthor(data?.GoodreadsResponse?.author);
loaded.value = withTiming(1);
})
.catch((error) => {
Console.log('Failed to get author details:', error);
});
}, [book]);
Promise.resolve(item).then(bookContent=>{
setFullBook(bookContent)
setAuthor(bookContent.author||{});
const relatedBooks = bookList.filter(el=>bookContent.relatedIds&&bookContent.relatedIds.includes(el.bookId))
setRelated(relatedBooks)
loaded.value = withTiming(1);
})
},[book]);

// Screen anims
const anims = {
Expand Down Expand Up @@ -246,8 +223,6 @@ function BookDetailsScreen({ navigation, route }) {
},
};

// Find book in list
const item = bookList.find((b) => b.bookId === book.bookId);

// Render book details
return (
Expand All @@ -263,7 +238,7 @@ function BookDetailsScreen({ navigation, route }) {
>
<Animated.View style={anims.screen}>
{ios && <StatusBar hidden={useIsFocused()} animated />}
<BookHeader scrollY={scrollY} book={book} />
<BookHeader scrollY={scrollY} book={item} />
<AntDesign size={27} name="close" onPress={goBack} style={styles.closeIcon} />

<Animated.View style={anims.scrollView}>
Expand All @@ -275,11 +250,11 @@ function BookDetailsScreen({ navigation, route }) {
<View style={styles.detailsBox}>
<View style={styles.detailsRow}>
<Text center size={13}>RATING</Text>
<Text bold style={styles.subDetails}>{book.avgRating}</Text>
<Text bold style={styles.subDetails}>{item.avgRating}</Text>
</View>
<View style={[styles.detailsRow, styles.detailsRowBorder]}>
<Text center size={13}>PAGES</Text>
<Text bold style={styles.subDetails}>{book.numPages}</Text>
<Text bold style={styles.subDetails}>{item.numPages}</Text>
</View>
<Pressable onPress={openSheet} style={styles.detailsRow}>
<Text center size={13}>STATUS</Text>
Expand Down
14 changes: 2 additions & 12 deletions src/screens/BookSearchScreen.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,10 @@ function BookSearchScreen({ navigation }) {
Haptics.selectionAsync();
};

// search query
useEffect(() => {
if (query.length > 0) {
axios.get(`https://www.goodreads.com/book/auto_complete?format=json&q=${query}`)
.then((resp) => {
const bks = resp.data.map((book) => ({
...book,
imageUrl: book.imageUrl.replace(/_..../, '_SY475_'),
}));
setBooks(bks);
})
.catch((error) => {
Alert.alert('Failed to get books', error);
});
const matchList = bookList.filter(item=>item.bookTitleBare.includes(query)||item.description.includes(query)||item.author.name.includes(query))
setBooks(matchList)
}
}, [query]);

Expand Down
Loading