import { Fulfillment } from "@medusajs/medusa" import { useAdminCreateClaimShipment, useAdminCreateShipment, useAdminCreateSwapShipment, } from "medusa-react" import React, { useState } from "react" import { Controller, useFieldArray, useForm, useWatch } from "react-hook-form" import { useTranslation } from "react-i18next" import Button from "../../../../components/fundamentals/button" import CheckIcon from "../../../../components/fundamentals/icons/check-icon" import IconTooltip from "../../../../components/molecules/icon-tooltip" import Input from "../../../../components/molecules/input" import Modal from "../../../../components/molecules/modal" import useNotification from "../../../../hooks/use-notification" import { getErrorMessage } from "../../../../utils/error-messages" type MarkShippedModalProps = { orderId: string fulfillment: Fulfillment handleCancel: () => void } type MarkShippedFormData = { tracking_numbers: { value: string | undefined }[] } const MarkShippedModal: React.FC = ({ orderId, fulfillment, handleCancel, }) => { const { t } = useTranslation() const { control, handleSubmit } = useForm({ defaultValues: { tracking_numbers: [{ value: "" }], }, shouldUnregister: true, }) const [noNotis, setNoNotis] = useState(false) const { fields, append: appendTracking, remove: removeTracking, } = useFieldArray({ control, name: "tracking_numbers", }) const watchedFields = useWatch({ control, name: "tracking_numbers", defaultValue: [], }) const markOrderShipped = useAdminCreateShipment(orderId) const markSwapShipped = useAdminCreateSwapShipment(orderId) const markClaimShipped = useAdminCreateClaimShipment(orderId) const isSubmitting = markOrderShipped.isLoading || markSwapShipped.isLoading || markClaimShipped.isLoading const notification = useNotification() const onSubmit = (data: MarkShippedFormData) => { const resourceId = fulfillment.claim_order_id || fulfillment.swap_id || fulfillment.order_id const [type] = resourceId.split("_") const tracking_numbers = data.tracking_numbers .map((tn) => tn?.value) .filter(Boolean) as string[] type actionType = | typeof markOrderShipped | typeof markSwapShipped | typeof markClaimShipped let action: actionType = markOrderShipped let successText = t( "mark-shipped-successfully-marked-order-as-shipped", "Successfully marked order as shipped" ) let requestObj switch (type) { case "swap": action = markSwapShipped requestObj = { fulfillment_id: fulfillment.id, swap_id: resourceId, tracking_numbers, no_notification: noNotis, } successText = t( "mark-shipped-successfully-marked-swap-as-shipped", "Successfully marked swap as shipped" ) break case "claim": action = markClaimShipped requestObj = { fulfillment_id: fulfillment.id, claim_id: resourceId, tracking_numbers, } successText = t( "mark-shipped-successfully-marked-claim-as-shipped", "Successfully marked claim as shipped" ) break default: requestObj = { fulfillment_id: fulfillment.id, tracking_numbers, no_notification: noNotis, } break } action.mutate(requestObj, { onSuccess: () => { notification( t("mark-shipped-success", "Success"), successText, "success" ) handleCancel() }, onError: (err) => notification( t("mark-shipped-error", "Error"), getErrorMessage(err), "error" ), }) } return (
{ console.log(errors) })} > {t( "mark-shipped-mark-fulfillment-shipped", "Mark Fulfillment Shipped" )}
{t("mark-shipped-tracking", "Tracking")}
{fields.map((tn, index) => ( { return ( removeTracking(index)} /> ) }} /> ))}
setNoNotis(!noNotis)} >
{!noNotis && }
{t("mark-shipped-send-notifications", "Send notifications")}
) } export default MarkShippedModal