Show quantity correctly

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2021-08-16 15:48:07 -07:00
parent 056b388520
commit afabcd057f

View File

@ -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(
<View key={item.location.code} style={[styles.horizontal,styles.container]}>
<Text style={whiskyStyles.address}>{item.location.address}</Text>
<Text style={whiskyStyles.phone}>{item.location.phone}</Text>
<Text style={whiskyStyles.quantity}>{item.quantity}</Text>
</View>
)
});
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(
<View key={item.location.code} style={[styles.horizontal,styles.container]}>
<Text style={whiskyStyles.address}>{item.location.address}</Text>
<Text style={whiskyStyles.phone}>{item.location.phone}</Text>
<Text style={whiskyStyles.quantity}>{item.quantity}</Text>
</View>
)
});
}
const date = (time.unix() == 0) ? "" : time.format("YYYY-MM-DD");
return (
@ -36,7 +38,7 @@ const Whisky = (props : { active?: boolean, whisky: any, style: any }) => {
<View style={whiskyStyles.code}><Text>{whisky.code}</Text></View>
<View style={whiskyStyles.description}><Text>{whisky.description}</Text></View>
<View style={whiskyStyles.date}><Text>{date}</Text></View>
<View style={whiskyStyles.quantity}><Text>{quantity}</Text></View>
<View style={whiskyStyles.quantity}><Text>{whisky.quantity}</Text></View>
</View>
{ props.active && <View style={styles.vertical}>
{ locations }
@ -85,6 +87,7 @@ const whiskyStyles = StyleSheet.create({
export default function App() {
const [whiskies, setWhiskies] = useState<any>(null),
[search, setSearch] = useState<string>(""),
[lastSearch, setLastSearch] = useState<string>(""),
[activeWhisky, setActiveWhisky] = useState<string>("");
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);
})