From ee4fe8e314e63e3f3cc48bb7e2e78ebd20a5a1fd Mon Sep 17 00:00:00 2001 From: jinxiaozhi Date: Mon, 7 Aug 2023 20:07:09 +0800 Subject: [PATCH 1/3] add the mock data to project --- package.json | 3 +- src/BookStore.jsx | 10 +- src/mock.js | 92 + src/screens/BookDetailsScreen.jsx | 53 +- src/screens/BookSearchScreen.jsx | 14 +- yarn.lock | 11708 ++++++++++++++++++++++++++++ 6 files changed, 11827 insertions(+), 53 deletions(-) create mode 100644 src/mock.js create mode 100644 yarn.lock diff --git a/package.json b/package.json index 1f27dff..4634cbf 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ }, "dependencies": { "@expo/vector-icons": "^13.0.0", + "@expo/webpack-config": "^0.17.2", "@react-native-async-storage/async-storage": "~1.17.3", "@react-native-masked-view/masked-view": "0.2.8", "@react-navigation/native": "^6.0.10", @@ -24,7 +25,7 @@ "lottie-react-native": "5.1.4", "react": "18.1.0", "react-dom": "18.1.0", - "react-native": "0.70.5", + "react-native": "0.70.8", "react-native-gesture-handler": "~2.8.0", "react-native-modalize": "^2.0.13", "react-native-reanimated": "~2.12.0", diff --git a/src/BookStore.jsx b/src/BookStore.jsx index 1e02891..f985dae 100644 --- a/src/BookStore.jsx +++ b/src/BookStore.jsx @@ -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({ @@ -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() { diff --git a/src/mock.js b/src/mock.js new file mode 100644 index 0000000..83081e7 --- /dev/null +++ b/src/mock.js @@ -0,0 +1,92 @@ +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', + 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', + 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', + 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', + 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', + 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', + about: 'dwqnjkdwnqwjk 1223', + } + }, +] + +export default async function setMockData(){ + return await AsyncStorage.setItem('@lists', JSON.stringify(mockData)) +} \ No newline at end of file diff --git a/src/screens/BookDetailsScreen.jsx b/src/screens/BookDetailsScreen.jsx index 466168e..4e238b3 100644 --- a/src/screens/BookDetailsScreen.jsx +++ b/src/screens/BookDetailsScreen.jsx @@ -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 = { @@ -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 ( @@ -263,7 +238,7 @@ function BookDetailsScreen({ navigation, route }) { > {ios &&