import { useMutation, useQuery } from "@apollo/client/react"; import React, { useState } from "react"; import { Link, useNavigate } from "react-router-dom"; import styles from "./Inventory.module.css"; import { GQLError } from "../../components/GQLError"; import { useToast } from "../../hooks/useToast"; import { CreateInventoryTransactionDocument, InventoryProductsDocument } from "../../types"; const LOGIN_URL = `${import.meta.env.VITE_API_URL}/accounts/login/?next=/inventory/add`; const InventoryAdd = () => { const navigate = useNavigate(); const { showToast } = useToast(); const { data, loading, error } = useQuery(InventoryProductsDocument); const [createInventoryTransaction, { loading: saving }] = useMutation(CreateInventoryTransactionDocument); const [productId, setProductId] = useState(""); const [amount, setAmount] = useState(""); const [unitPrice, setUnitPrice] = useState(""); const [comment, setComment] = useState(""); const products = data?.allProducts || []; if (loading) return
Loading...
; if (error) return ; const onSubmit = async (event: React.FormEvent) => { event.preventDefault(); try { const result = await createInventoryTransaction({ variables: { data: { productId, amount: Number(amount), unitPrice, comment, }, }, }); const response = result.data?.createInventoryTransaction; if (response?.__typename === "CreateInventoryTransactionSuccess") { showToast(`Registrerte ${amount} stk ${response.inventoryTransaction.product.name}.`); navigate("/inventory"); } else if (response?.__typename === "FormErrors") { showToast(response.message, "danger"); } else { showToast("Kunne ikke registrere varetransaksjonen.", "danger"); } } catch (mutationError) { showToast( mutationError instanceof Error ? mutationError.message : "Kunne ikke registrere varetransaksjonen.", "danger", ); } }; return (

Registrer varetransaksjon

Du må være innlogget for å registrere varer. Logg inn.