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..ceab81a --- /dev/null +++ b/src/mock.js @@ -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)) +} \ 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 &&