import { StatusBar } from 'expo-status-bar';
import React, { useState } from 'react';
import { StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native';
import { request, GraphQLClient, gql } from 'graphql-request';
import moment from 'moment';
import { buildExecutionContext } from 'graphql/execution/execute';
const client = new GraphQLClient("http://localhost:4000/graphql", { headers: {} });
const Whisky = (props : { active?: boolean, whisky: any, style: any }) => {
const whisky = props.whisky;
let quantity = 0,
time = moment.unix(0);
const locations: any[] = [];
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 (
{whisky.code}
{whisky.description}
{date}
{whisky.quantity}
{ props.active &&
{ locations }
}
);
};
const textStyle = {
paddingTop: 5,
paddingRight: 10,
paddingBottom: 5,
paddingLeft: 10
};
const whiskyStyles = StyleSheet.create({
container: {
flex: 1,
minHeight: 25,
},
code: {
...textStyle
},
description: {
flexGrow: 10,
...textStyle
},
date: {
...textStyle
},
address: {
flex: 1,
flexGrow: 1,
...textStyle
},
phone: {
...textStyle
},
quantity: {
flexShrink: 10,
minWidth: 50,
...textStyle
}
});
export default function App() {
const [whiskies, setWhiskies] = useState(null),
[search, setSearch] = useState(""),
[lastSearch, setLastSearch] = useState(""),
[activeWhisky, setActiveWhisky] = useState("");
const onPress = (code : string) => {
setActiveWhisky(code == activeWhisky ? "" : code);
};
const items = whiskies ? whiskies
.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 (
onPress(whisky.code)}>
);
}) : [];
const query = gql` {
Whiskies {
code
description
price
size
lastSeen
inventories {
location {
code
address
city
phone
longitude
latitude
}
quantity
updated
}
}
}`;
const submitSearch = () => {
if (search.trim() == "") {
setWhiskies([]);
return;
}
if (lastSearch == search) {
return;
}
const _query = gql` {
Whiskies(code:"${search}") {
code
description
price
size
lastSeen
quantity
updated
}
}`;
setLastSearch(search);
client.request(_query)
.then(data => {
setWhiskies(data.Whiskies);
})
.catch (error => {
console.error(error);
});
};
const keyPress = (event : KeyboardEvent) => {
if (event.code == "\n") {
submitSearch();
}
};
return (
keyPress(event)}
onChange={(value) => setSearch(value.target.value)}>
{ items }
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
horizontal: {
flex: 1,
display: "flex",
flexGrow: 1,
flexDirection: "row",
},
vertical: {
flex: 1,
display: "flex",
flexGrow: 1,
flexDirection: "column",
},
whisky: {
width: 500,
borderWidth: 1,
borderColor: "black",
margin: 2
}
});