([]);
+ const [createOrderMutation, { loading: mutateLoading }] = useMutation(CreateOrderDocument, {
+ variables: { customerId: selectedUser, orderLines: order },
+ });
+ const { showToast } = useToast();
const reset = () => {
setOrder([]);
setSelectedUser("");
};
- const addToOrder = (id: string) => {
+ const addToOrder = (productId: string) => {
let added = false;
const newOrder = order.map((orderLine: OrderLine) => {
- if (orderLine.id === id) {
+ if (orderLine.productId === productId) {
added = true;
return { ...orderLine, amount: orderLine.amount + 1 };
}
return orderLine;
});
if (!added) {
- newOrder.push({ id, amount: 1 });
+ newOrder.push({ productId, amount: 1 });
}
setOrder(newOrder);
};
@@ -54,6 +60,22 @@ export const Register: React.FC = () => {
return Loading...
;
}
+ const onSubmit = async () => {
+ let res;
+ try {
+ res = await createOrderMutation();
+ } catch (e) {
+ showToast("Whoops");
+ console.log("woops");
+ return;
+ }
+ const { data } = res;
+ if (data?.createOrder?.__typename === "CreateOrderSuccess") {
+ showToast("Woop");
+ return;
+ }
+ showToast("booo");
+ };
return (
@@ -86,8 +108,8 @@ export const Register: React.FC = () => {
key={product.id}
product={product}
onProduct={() => addToOrder(product.id)}
- amount={order.find((orderLine: OrderLine) => orderLine.id === product.id)?.amount || 0}
- active={order.some((orderLine: OrderLine) => orderLine.id === product.id)}
+ amount={order.find((orderLine: OrderLine) => orderLine.productId === product.id)?.amount || 0}
+ active={order.some((orderLine: OrderLine) => orderLine.productId === product.id)}
/>
))}
@@ -96,7 +118,7 @@ export const Register: React.FC = () => {
Totalt: {total}
-