From 6469b8f195c3160fc0a8a4df3871fd870d0d0ca0 Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <84702959+andrii-balitskyi@users.noreply.github.com> Date: Wed, 16 Oct 2024 18:28:39 +0200 Subject: [PATCH] Add Submit Order Button and Dialog (#78) * Add CreateOrderDialog * Add submit order button to editor nav bar * Format --- src/components/EditorNav.tsx | 11 ++ .../dialogs/create-order-dialog.tsx | 146 ++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 src/components/dialogs/create-order-dialog.tsx diff --git a/src/components/EditorNav.tsx b/src/components/EditorNav.tsx index 013945b8..2d008cc2 100644 --- a/src/components/EditorNav.tsx +++ b/src/components/EditorNav.tsx @@ -39,6 +39,7 @@ import { SnippetLink } from "./SnippetLink" import { useGlobalStore } from "@/hooks/use-global-store" import { useRenameSnippetDialog } from "./dialogs/rename-snippet-dialog" import { useConfirmDeleteSnippetDialog } from "./dialogs/confirm-delete-snippet-dialog" +import { useCreateOrderDialog } from "./dialogs/create-order-dialog" import { AnyCircuitElement } from "circuit-json" export default function EditorNav({ @@ -68,6 +69,8 @@ export default function EditorNav({ useRenameSnippetDialog() const { Dialog: DeleteDialog, openDialog: openDeleteDialog } = useConfirmDeleteSnippetDialog() + const { Dialog: CreateOrderDialog, openDialog: openCreateOrderDialog } = + useCreateOrderDialog() return ( @@ -180,6 +183,13 @@ export default function EditorNav({ + openCreateOrderDialog()} + > + + Submit Order + openDeleteDialog()} @@ -254,6 +264,7 @@ export default function EditorNav({ snippetId={snippet?.snippet_id ?? ""} snippetName={snippet?.unscoped_name ?? ""} /> + ) } diff --git a/src/components/dialogs/create-order-dialog.tsx b/src/components/dialogs/create-order-dialog.tsx new file mode 100644 index 00000000..e0c1a431 --- /dev/null +++ b/src/components/dialogs/create-order-dialog.tsx @@ -0,0 +1,146 @@ +import { Dialog, DialogContent, DialogHeader, DialogTitle } from "../ui/dialog" +import { Button } from "../ui/button" +import { Checkbox } from "../ui/checkbox" +import { useState, useEffect } from "react" +import { createUseDialog } from "./create-use-dialog" +import { useAxios } from "@/hooks/use-axios" +import { useToast } from "@/hooks/use-toast" +import { useQueryClient } from "react-query" + +export const CreateOrderDialog = ({ + open, + onOpenChange, +}: { + open: boolean + onOpenChange: (open: boolean) => void +}) => { + const axios = useAxios() + const { toast } = useToast() + const qc = useQueryClient() + const [pending, setPending] = useState(false) + const [checkpoints, setCheckpoints] = useState({ + shipping: false, + errors: false, + parts: false, + }) + + useEffect(() => { + if (open) { + validateCheckpoints() + } + }, [open]) + + const validateCheckpoints = async () => { + try { + // Placeholder: Check if shipping information is in profile + const hasShippingInfo = await checkShippingInfo() + + // Placeholder: Check if PCB has no errors + const hasNoErrors = await checkPCBErrors() + + // Placeholder: Check if all parts are available at PCB fab + const allPartsAvailable = await checkPartsAvailability() + + setCheckpoints({ + shipping: hasShippingInfo, + errors: hasNoErrors, + parts: allPartsAvailable, + }) + } catch (error) { + console.error("Error validating checkpoints:", error) + } + } + + const checkShippingInfo = async () => { + // Placeholder: Implement actual check for shipping info + return true + } + + const checkPCBErrors = async () => { + // Placeholder: Implement actual check for PCB errors + return true + } + + const checkPartsAvailability = async () => { + // Placeholder: Implement actual check for parts availability + return true + } + + const handleSubmit = async () => { + try { + setPending(true) + // TODO: Implement order submission logic + onOpenChange(false) + setPending(false) + toast({ + title: "Order submitted", + description: "Your order has been successfully submitted.", + }) + qc.invalidateQueries({ queryKey: ["orders"] }) + } catch (error) { + console.error("Error submitting order:", error) + toast({ + title: "Error", + description: "Failed to submit the order. Please try again.", + variant: "destructive", + }) + } finally { + setPending(false) + } + } + + return ( + + + + Create Order + + + Order the circuit board fully assembled from a PCB fabricator + + + + + + Shipping Information in Profile + + + + + + PCB Has No Errors + + + + + + All parts available at PCB fabricator + + + + + onOpenChange(false)}> + Cancel + + + {pending ? "Submitting..." : "Submit Order"} + + + + + ) +} + +export const useCreateOrderDialog = createUseDialog(CreateOrderDialog)
+ Order the circuit board fully assembled from a PCB fabricator +