166 lines
5.0 KiB
TypeScript
166 lines
5.0 KiB
TypeScript
"use client"
|
|
|
|
import { Plus } from "lucide-react"
|
|
import { Button, Heading } from "@modules/common/components/ui"
|
|
import { useActionState, useEffect, useState } from "react"
|
|
|
|
import { addCustomerAddress } from "@lib/data/customer"
|
|
import useToggleState from "@lib/hooks/use-toggle-state"
|
|
import { HttpTypes } from "@medusajs/types"
|
|
import CountrySelect from "@modules/checkout/components/country-select"
|
|
import { SubmitButton } from "@modules/checkout/components/submit-button"
|
|
import Input from "@modules/common/components/input"
|
|
import Modal from "@modules/common/components/modal"
|
|
|
|
const AddAddress = ({
|
|
region,
|
|
}: {
|
|
region: HttpTypes.StoreRegion
|
|
addresses: HttpTypes.StoreCustomerAddress[]
|
|
}) => {
|
|
const [successState, setSuccessState] = useState(false)
|
|
const { state, open, close: closeModal } = useToggleState(false)
|
|
|
|
const [formState, formAction] = useActionState(addCustomerAddress, {
|
|
success: false,
|
|
error: null,
|
|
} as { success: boolean; error: string | null })
|
|
|
|
const close = () => {
|
|
setSuccessState(false)
|
|
closeModal()
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (successState) {
|
|
close()
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [successState])
|
|
|
|
useEffect(() => {
|
|
if (formState.success) {
|
|
setSuccessState(true)
|
|
}
|
|
}, [formState])
|
|
|
|
return (
|
|
<>
|
|
<button
|
|
className="group flex h-full min-h-[200px] w-full flex-col justify-between rounded-2xl border border-dashed border-border p-6 text-left transition-colors hover:border-primary hover:bg-muted/50"
|
|
onClick={open}
|
|
data-testid="add-address-button"
|
|
>
|
|
<span className="font-display text-xl text-foreground">Nova adresa</span>
|
|
<Plus />
|
|
</button>
|
|
|
|
<Modal isOpen={state} close={close} data-testid="add-address-modal">
|
|
<Modal.Title>
|
|
<Heading className="font-display text-2xl font-normal">Nova adresa</Heading>
|
|
</Modal.Title>
|
|
<form action={formAction}>
|
|
<Modal.Body>
|
|
<div className="flex flex-col gap-y-2">
|
|
<div className="grid grid-cols-2 gap-x-2">
|
|
<Input
|
|
label="Ime"
|
|
name="first_name"
|
|
required
|
|
autoComplete="given-name"
|
|
data-testid="first-name-input"
|
|
/>
|
|
<Input
|
|
label="Prezime"
|
|
name="last_name"
|
|
required
|
|
autoComplete="family-name"
|
|
data-testid="last-name-input"
|
|
/>
|
|
</div>
|
|
<Input
|
|
label="Firma"
|
|
name="company"
|
|
autoComplete="organization"
|
|
data-testid="company-input"
|
|
/>
|
|
<Input
|
|
label="Adresa"
|
|
name="address_1"
|
|
required
|
|
autoComplete="address-line1"
|
|
data-testid="address-1-input"
|
|
/>
|
|
<Input
|
|
label="Stan, sprat i sl."
|
|
name="address_2"
|
|
autoComplete="address-line2"
|
|
data-testid="address-2-input"
|
|
/>
|
|
<div className="grid grid-cols-[144px_1fr] gap-x-2">
|
|
<Input
|
|
label="Poštanski broj"
|
|
name="postal_code"
|
|
required
|
|
autoComplete="postal-code"
|
|
data-testid="postal-code-input"
|
|
/>
|
|
<Input
|
|
label="Grad"
|
|
name="city"
|
|
required
|
|
autoComplete="locality"
|
|
data-testid="city-input"
|
|
/>
|
|
</div>
|
|
<Input
|
|
label="Kanton / entitet"
|
|
name="province"
|
|
autoComplete="address-level1"
|
|
data-testid="state-input"
|
|
/>
|
|
<CountrySelect
|
|
region={region}
|
|
name="country_code"
|
|
required
|
|
autoComplete="country"
|
|
data-testid="country-select"
|
|
/>
|
|
<Input
|
|
label="Telefon"
|
|
name="phone"
|
|
autoComplete="phone"
|
|
data-testid="phone-input"
|
|
/>
|
|
</div>
|
|
{formState.error && (
|
|
<div
|
|
className="text-rose-500 text-sm py-2"
|
|
data-testid="address-error"
|
|
>
|
|
{formState.error}
|
|
</div>
|
|
)}
|
|
</Modal.Body>
|
|
<Modal.Footer>
|
|
<div className="flex gap-3 mt-6">
|
|
<Button
|
|
type="reset"
|
|
variant="secondary"
|
|
onClick={close}
|
|
className="h-10"
|
|
data-testid="cancel-button"
|
|
>
|
|
Odustani
|
|
</Button>
|
|
<SubmitButton data-testid="save-button">Save</SubmitButton>
|
|
</div>
|
|
</Modal.Footer>
|
|
</form>
|
|
</Modal>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default AddAddress
|