From afabcd057f621dd685bf2dffa8699173ddb03126 Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Mon, 16 Aug 2021 15:48:07 -0700 Subject: [PATCH] Show quantity correctly Signed-off-by: James Ketrenos --- whisky-business/App.tsx | 77 +++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/whisky-business/App.tsx b/whisky-business/App.tsx index b37ba05..6b5e777 100644 --- a/whisky-business/App.tsx +++ b/whisky-business/App.tsx @@ -14,20 +14,22 @@ const Whisky = (props : { active?: boolean, whisky: any, style: any }) => { const locations: any[] = []; - whisky.inventories.forEach((item : { location: any, quantity: number, updated: string }) => { - quantity += item.quantity; - const updated = moment(item.updated, 'YYYY-MM-DD'); - if (time < updated) { - time = updated; - } - locations.push( - - {item.location.address} - {item.location.phone} - {item.quantity} - - ) - }); + if (whisky.inventories) { + whisky.inventories.forEach((item : { location: any, quantity: number, updated: string }) => { + quantity += item.quantity; + const updated = moment(item.updated, 'YYYY-MM-DD'); + if (time < updated) { + time = updated; + } + locations.push( + + {item.location.address} + {item.location.phone} + {item.quantity} + + ) + }); + } const date = (time.unix() == 0) ? "" : time.format("YYYY-MM-DD"); return ( @@ -36,7 +38,7 @@ const Whisky = (props : { active?: boolean, whisky: any, style: any }) => { {whisky.code} {whisky.description} {date} - {quantity} + {whisky.quantity} { props.active && { locations } @@ -85,6 +87,7 @@ const whiskyStyles = StyleSheet.create({ export default function App() { const [whiskies, setWhiskies] = useState(null), [search, setSearch] = useState(""), + [lastSearch, setLastSearch] = useState(""), [activeWhisky, setActiveWhisky] = useState(""); const onPress = (code : string) => { @@ -92,7 +95,8 @@ export default function App() { }; const items = whiskies ? whiskies - .filter((item : any) => item.size == 750 && item.inventories.length) + .filter((item : any) => item.size == 750) + .sort((a : any, b : any) => a.description.localeCompare(b.description)) .map((whisky : any) => { const active = whisky.code == activeWhisky; return ( @@ -127,40 +131,31 @@ export default function App() { } }`; - client.request(query) - .then(data => { - setWhiskies(data.Whiskies); - }) - .catch (error => { - console.error(error); - }); - const submitSearch = () => { - const query = gql` { - Whiskies { + if (search.trim() == "") { + setWhiskies([]); + return; + } + + if (lastSearch == search) { + return; + } + + const _query = gql` { + Whiskies(code:"${search}") { code description price size lastSeen - inventories { - location { - code - address - city - phone - longitude - latitude - } - quantity - updated - } + quantity + updated } }`; - console.log(search); - - client.request(query) + setLastSearch(search); + + client.request(_query) .then(data => { setWhiskies(data.Whiskies); })