add address
This commit is contained in:
@@ -19,7 +19,7 @@ dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
|||||||
@router.get('/<org_id>/address')
|
@router.get('/<org_id>/address')
|
||||||
def address(org_id: str):
|
def address(org_id: str):
|
||||||
return dyn.collection.get_item(
|
return dyn.collection.get_item(
|
||||||
KeyPair(org_id, 'ADDRESS'),
|
KeyPair(org_id, 'METADATA#ADDRESS'),
|
||||||
raise_on_error=False,
|
raise_on_error=False,
|
||||||
default={},
|
default={},
|
||||||
)
|
)
|
||||||
@@ -44,7 +44,7 @@ class Address(BaseModel):
|
|||||||
@router.post('/<org_id>/address')
|
@router.post('/<org_id>/address')
|
||||||
def update(org_id: str, address: Annotated[Address, Body(embed=True)]):
|
def update(org_id: str, address: Annotated[Address, Body(embed=True)]):
|
||||||
dyn.collection.put_item(
|
dyn.collection.put_item(
|
||||||
key=KeyPair(org_id, 'ADDRESS'),
|
key=KeyPair(org_id, 'METADATA#ADDRESS'),
|
||||||
updated_at=now(),
|
updated_at=now(),
|
||||||
**address.model_dump(exclude_none=True),
|
**address.model_dump(exclude_none=True),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import { initials } from '@repo/ui/lib/utils'
|
|||||||
import { Link } from 'react-router'
|
import { Link } from 'react-router'
|
||||||
|
|
||||||
import type { Workspace, WorkspaceContextProps } from '@/middleware/workspace'
|
import type { Workspace, WorkspaceContextProps } from '@/middleware/workspace'
|
||||||
|
import type { Address } from '@/routes/_.$orgid.enrollments.buy/review'
|
||||||
|
|
||||||
type Subscription = {
|
type Subscription = {
|
||||||
billing_day: number
|
billing_day: number
|
||||||
@@ -39,6 +40,7 @@ type Subscription = {
|
|||||||
const WorkspaceContext = createContext<
|
const WorkspaceContext = createContext<
|
||||||
| (WorkspaceContextProps & {
|
| (WorkspaceContextProps & {
|
||||||
subscription: Subscription | null
|
subscription: Subscription | null
|
||||||
|
address: Address | null
|
||||||
})
|
})
|
||||||
| null
|
| null
|
||||||
>(null)
|
>(null)
|
||||||
@@ -57,11 +59,13 @@ export function WorkspaceProvider({
|
|||||||
activeWorkspace,
|
activeWorkspace,
|
||||||
workspaces,
|
workspaces,
|
||||||
subscription,
|
subscription,
|
||||||
|
address,
|
||||||
children
|
children
|
||||||
}: {
|
}: {
|
||||||
activeWorkspace: Workspace
|
activeWorkspace: Workspace
|
||||||
workspaces: Workspace[]
|
workspaces: Workspace[]
|
||||||
subscription?: Subscription
|
subscription?: Subscription
|
||||||
|
address?: Address
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
@@ -69,6 +73,7 @@ export function WorkspaceProvider({
|
|||||||
value={{
|
value={{
|
||||||
activeWorkspace,
|
activeWorkspace,
|
||||||
workspaces,
|
workspaces,
|
||||||
|
address: address && Object.keys(address).length > 0 ? address : null,
|
||||||
subscription:
|
subscription:
|
||||||
subscription && Object.keys(subscription).length > 0
|
subscription && Object.keys(subscription).length > 0
|
||||||
? subscription
|
? subscription
|
||||||
@@ -106,6 +111,7 @@ export function WorkspaceSwitcher() {
|
|||||||
{subscription && (
|
{subscription && (
|
||||||
<BadgeCheckIcon className="fill-blue-500 absolute size-3 -top-1 -right-1" />
|
<BadgeCheckIcon className="fill-blue-500 absolute size-3 -top-1 -right-1" />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{initials(activeWorkspace.name)}
|
{initials(activeWorkspace.name)}
|
||||||
</div>
|
</div>
|
||||||
<div className="grid flex-1 text-left text-sm leading-tight">
|
<div className="grid flex-1 text-left text-sm leading-tight">
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ export function Discount({ onChange, ...props }: DiscountProps) {
|
|||||||
resolver: zodResolver(formSchema)
|
resolver: zodResolver(formSchema)
|
||||||
})
|
})
|
||||||
const [open, { toggle, set }] = useToggle()
|
const [open, { toggle, set }] = useToggle()
|
||||||
|
const { handleSubmit, control, formState, setError, reset } = form
|
||||||
const { runAsync } = useRequest(
|
const { runAsync } = useRequest(
|
||||||
async (coupon) => {
|
async (coupon) => {
|
||||||
return await fetch(`/~/api/coupons/${coupon}`, {
|
return await fetch(`/~/api/coupons/${coupon}`, {
|
||||||
@@ -54,7 +55,6 @@ export function Discount({ onChange, ...props }: DiscountProps) {
|
|||||||
},
|
},
|
||||||
{ manual: true }
|
{ manual: true }
|
||||||
)
|
)
|
||||||
const { handleSubmit, control, formState, setError, reset } = form
|
|
||||||
|
|
||||||
const onSubmit = async (data: Schema) => {
|
const onSubmit = async (data: Schema) => {
|
||||||
const r = await runAsync(data.coupon)
|
const r = await runAsync(data.coupon)
|
||||||
@@ -62,6 +62,7 @@ export function Discount({ onChange, ...props }: DiscountProps) {
|
|||||||
if (!r.ok) {
|
if (!r.ok) {
|
||||||
return setError('coupon', { message: 'Cupom inválido' })
|
return setError('coupon', { message: 'Cupom inválido' })
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
sk: code,
|
sk: code,
|
||||||
discount_amount: amount,
|
discount_amount: amount,
|
||||||
|
|||||||
@@ -33,8 +33,6 @@ import { Currency } from '@repo/ui/components/currency'
|
|||||||
|
|
||||||
import { useWizard } from '@/components/wizard'
|
import { useWizard } from '@/components/wizard'
|
||||||
import { isName } from '../_.$orgid.users.add/data'
|
import { isName } from '../_.$orgid.users.add/data'
|
||||||
import type { PaymentMethod } from '@repo/ui/routes/orders/data'
|
|
||||||
import type { WizardState } from './store'
|
|
||||||
import { applyDiscount } from './discount'
|
import { applyDiscount } from './discount'
|
||||||
import { useWizardStore } from './store'
|
import { useWizardStore } from './store'
|
||||||
|
|
||||||
@@ -86,7 +84,8 @@ export type CreditCard = z.infer<typeof creditCard>
|
|||||||
|
|
||||||
export function Payment({}) {
|
export function Payment({}) {
|
||||||
const wizard = useWizard()
|
const wizard = useWizard()
|
||||||
const { update, ...state } = useWizardStore()
|
const { update, summary, ...state } = useWizardStore()
|
||||||
|
const { total } = summary()
|
||||||
const { control, handleSubmit } = useForm<Schema>({
|
const { control, handleSubmit } = useForm<Schema>({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
payment_method: state.payment_method,
|
payment_method: state.payment_method,
|
||||||
@@ -96,17 +95,6 @@ export function Payment({}) {
|
|||||||
resolver: zodResolver(formSchema)
|
resolver: zodResolver(formSchema)
|
||||||
})
|
})
|
||||||
const paymentMethod = useWatch({ control, name: 'payment_method' })
|
const paymentMethod = useWatch({ control, name: 'payment_method' })
|
||||||
const subtotal = state.items.reduce(
|
|
||||||
(acc, { course, quantity }) =>
|
|
||||||
acc +
|
|
||||||
(course?.unit_price || 0) *
|
|
||||||
(Number.isFinite(quantity) && quantity > 0 ? quantity : 1),
|
|
||||||
0
|
|
||||||
)
|
|
||||||
const discount = state.coupon
|
|
||||||
? applyDiscount(subtotal, state.coupon.amount, state.coupon.type) * -1
|
|
||||||
: 0
|
|
||||||
const total = subtotal > 0 ? subtotal + discount : 0
|
|
||||||
|
|
||||||
const onSubmit = async ({ payment_method, ...data }: Schema) => {
|
const onSubmit = async ({ payment_method, ...data }: Schema) => {
|
||||||
if (payment_method === 'CREDIT_CARD') {
|
if (payment_method === 'CREDIT_CARD') {
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ import {
|
|||||||
import { paymentMethods } from '@repo/ui/routes/orders/data'
|
import { paymentMethods } from '@repo/ui/routes/orders/data'
|
||||||
|
|
||||||
import { useWizard } from '@/components/wizard'
|
import { useWizard } from '@/components/wizard'
|
||||||
import { type WizardState } from './store'
|
|
||||||
import {
|
import {
|
||||||
Field,
|
Field,
|
||||||
FieldDescription,
|
FieldDescription,
|
||||||
@@ -59,6 +58,14 @@ import {
|
|||||||
} from '@repo/ui/components/ui/input-group'
|
} from '@repo/ui/components/ui/input-group'
|
||||||
|
|
||||||
import { useWizardStore } from './store'
|
import { useWizardStore } from './store'
|
||||||
|
import { useParams } from 'react-router'
|
||||||
|
import {
|
||||||
|
Empty,
|
||||||
|
EmptyContent,
|
||||||
|
EmptyDescription,
|
||||||
|
EmptyHeader,
|
||||||
|
EmptyTitle
|
||||||
|
} from '@repo/ui/components/ui/empty'
|
||||||
|
|
||||||
type ReviewProps = {
|
type ReviewProps = {
|
||||||
onSubmit: () => void | Promise<void>
|
onSubmit: () => void | Promise<void>
|
||||||
@@ -66,7 +73,7 @@ type ReviewProps = {
|
|||||||
|
|
||||||
export function Review({ onSubmit }: ReviewProps) {
|
export function Review({ onSubmit }: ReviewProps) {
|
||||||
const wizard = useWizard()
|
const wizard = useWizard()
|
||||||
const { items, summary } = useWizardStore()
|
const { items, summary, address } = useWizardStore()
|
||||||
const { subtotal, discount, interest_amount, total } = summary()
|
const { subtotal, discount, interest_amount, total } = summary()
|
||||||
const [loading, { set }] = useToggle()
|
const [loading, { set }] = useToggle()
|
||||||
|
|
||||||
@@ -165,7 +172,10 @@ export function Review({ onSubmit }: ReviewProps) {
|
|||||||
Voltar
|
Voltar
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button type="submit" disabled={loading}>
|
<Button
|
||||||
|
type="submit"
|
||||||
|
disabled={loading || !formSchema.safeParse(address).success}
|
||||||
|
>
|
||||||
{loading && <Spinner />}
|
{loading && <Spinner />}
|
||||||
Pagar <Currency>{total}</Currency>
|
Pagar <Currency>{total}</Currency>
|
||||||
</Button>
|
</Button>
|
||||||
@@ -183,10 +193,10 @@ export function Summary() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<ItemGroup className="grid lg:grid-cols-2 gap-4">
|
<ItemGroup className="grid lg:grid-cols-2 gap-4">
|
||||||
|
{address ? (
|
||||||
<Item variant="outline" className="items-start">
|
<Item variant="outline" className="items-start">
|
||||||
<ItemContent>
|
<ItemContent>
|
||||||
<ItemTitle>Endereço de cobrança</ItemTitle>
|
<ItemTitle>Endereço de cobrança</ItemTitle>
|
||||||
{address && (
|
|
||||||
<ul className="text-muted-foreground text-sm leading-normal font-normal text-balance">
|
<ul className="text-muted-foreground text-sm leading-normal font-normal text-balance">
|
||||||
{address?.address1}
|
{address?.address1}
|
||||||
{address?.address2 ? <>, {address?.address2}</> : null}
|
{address?.address2 ? <>, {address?.address2}</> : null}
|
||||||
@@ -197,12 +207,38 @@ export function Summary() {
|
|||||||
<br />
|
<br />
|
||||||
{formatCEP(address?.postcode)}
|
{formatCEP(address?.postcode)}
|
||||||
</ul>
|
</ul>
|
||||||
)}
|
|
||||||
</ItemContent>
|
</ItemContent>
|
||||||
<ItemActions>
|
<ItemActions>
|
||||||
<AddressDialog />
|
<AddressDialog>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
type="button"
|
||||||
|
className="text-muted-foreground cursor-pointer"
|
||||||
|
size="icon-sm"
|
||||||
|
>
|
||||||
|
<PencilIcon />
|
||||||
|
<span className="sr-only">Editar endereço</span>
|
||||||
|
</Button>
|
||||||
|
</AddressDialog>
|
||||||
</ItemActions>
|
</ItemActions>
|
||||||
</Item>
|
</Item>
|
||||||
|
) : (
|
||||||
|
<Empty className="border border-dashed md:p-6 gap-4">
|
||||||
|
<EmptyHeader>
|
||||||
|
<EmptyTitle>Nenhum endereço ainda</EmptyTitle>
|
||||||
|
<EmptyDescription>
|
||||||
|
Você ainda não cadastrou nenhum endereço.
|
||||||
|
</EmptyDescription>
|
||||||
|
</EmptyHeader>
|
||||||
|
<EmptyContent>
|
||||||
|
<AddressDialog>
|
||||||
|
<Button size="sm" variant="outline" className="cursor-pointer">
|
||||||
|
Cadastrar endereço
|
||||||
|
</Button>
|
||||||
|
</AddressDialog>
|
||||||
|
</EmptyContent>
|
||||||
|
</Empty>
|
||||||
|
)}
|
||||||
|
|
||||||
<Item variant="outline" className="items-start">
|
<Item variant="outline" className="items-start">
|
||||||
<ItemContent>
|
<ItemContent>
|
||||||
@@ -241,32 +277,36 @@ const formSchema = z.object({
|
|||||||
|
|
||||||
export type Address = z.infer<typeof formSchema>
|
export type Address = z.infer<typeof formSchema>
|
||||||
|
|
||||||
export function AddressDialog() {
|
export function AddressDialog({ children }) {
|
||||||
const [open, { toggle, set: setOpen }] = useToggle()
|
|
||||||
const { update, address } = useWizardStore()
|
const { update, address } = useWizardStore()
|
||||||
const { handleSubmit, control, reset } = useForm({
|
const { orgid } = useParams()
|
||||||
|
const { runAsync } = useRequest(
|
||||||
|
async (address: Address) => {
|
||||||
|
await fetch(`/~/api/orgs/${orgid}/address`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ address }),
|
||||||
|
headers: new Headers({ 'Content-Type': 'application/json' })
|
||||||
|
})
|
||||||
|
},
|
||||||
|
{
|
||||||
|
manual: true
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const { handleSubmit, control, reset, formState } = useForm({
|
||||||
defaultValues: address,
|
defaultValues: address,
|
||||||
resolver: zodResolver(formSchema)
|
resolver: zodResolver(formSchema)
|
||||||
})
|
})
|
||||||
|
const [open, { toggle, set: setOpen }] = useToggle(address === undefined)
|
||||||
|
|
||||||
const onSubmit = async (address: Address) => {
|
const onSubmit = async (address: Address) => {
|
||||||
|
await runAsync(address)
|
||||||
setOpen(false)
|
setOpen(false)
|
||||||
update({ address })
|
update({ address })
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} onOpenChange={toggle}>
|
<Dialog open={open} onOpenChange={toggle}>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>{children}</DialogTrigger>
|
||||||
<Button
|
|
||||||
variant="ghost"
|
|
||||||
type="button"
|
|
||||||
className="text-muted-foreground cursor-pointer"
|
|
||||||
size="icon-sm"
|
|
||||||
>
|
|
||||||
<PencilIcon />
|
|
||||||
<span className="sr-only">Editar endereço</span>
|
|
||||||
</Button>
|
|
||||||
</DialogTrigger>
|
|
||||||
<DialogContent className="sm:max-w-[425px]">
|
<DialogContent className="sm:max-w-[425px]">
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>Editar endereço</DialogTitle>
|
<DialogTitle>Editar endereço</DialogTitle>
|
||||||
@@ -400,7 +440,9 @@ export function AddressDialog() {
|
|||||||
</Button>
|
</Button>
|
||||||
</DialogClose>
|
</DialogClose>
|
||||||
|
|
||||||
<Button type="submit">Atualizar endereço</Button>
|
<Button type="submit" disabled={formState.isSubmitting}>
|
||||||
|
{formState.isSubmitting && <Spinner />} Atualizar endereço
|
||||||
|
</Button>
|
||||||
</DialogFooter>
|
</DialogFooter>
|
||||||
</form>
|
</form>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
|
|||||||
@@ -34,8 +34,9 @@ import { Payment } from './payment'
|
|||||||
import { Assigned } from './assigned'
|
import { Assigned } from './assigned'
|
||||||
import { Review } from './review'
|
import { Review } from './review'
|
||||||
|
|
||||||
import { useWizardStore, type WizardState } from './store'
|
import { useWizardStore } from './store'
|
||||||
import { useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
|
import { useWorksapce } from '@/components/workspace-switcher'
|
||||||
|
|
||||||
export function meta({}: Route.MetaArgs) {
|
export function meta({}: Route.MetaArgs) {
|
||||||
return [{ title: 'Comprar matrículas' }]
|
return [{ title: 'Comprar matrículas' }]
|
||||||
@@ -65,7 +66,9 @@ export default function Route({
|
|||||||
}: Route.ComponentProps) {
|
}: Route.ComponentProps) {
|
||||||
const fetcher = useFetcher()
|
const fetcher = useFetcher()
|
||||||
const [mounted, setMounted] = useState(false)
|
const [mounted, setMounted] = useState(false)
|
||||||
const { index, kind, setIndex, setKind, reset, ...state } = useWizardStore()
|
const { address } = useWorksapce()
|
||||||
|
const { index, kind, setIndex, setKind, reset, update, ...state } =
|
||||||
|
useWizardStore()
|
||||||
|
|
||||||
const onSubmit = async () => {
|
const onSubmit = async () => {
|
||||||
await fetcher.submit(JSON.stringify(state), {
|
await fetcher.submit(JSON.stringify(state), {
|
||||||
@@ -79,6 +82,12 @@ export default function Route({
|
|||||||
setMounted(true)
|
setMounted(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (address) {
|
||||||
|
update({ address })
|
||||||
|
}
|
||||||
|
}, [address])
|
||||||
|
|
||||||
if (!mounted) {
|
if (!mounted) {
|
||||||
return <Skeleton />
|
return <Skeleton />
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,14 +64,9 @@ export function shouldRevalidate({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function Route({ loaderData }: Route.ComponentProps) {
|
export default function Route({ loaderData }: Route.ComponentProps) {
|
||||||
const {
|
const { user, activeWorkspace, workspaces, sidebar_state } = loaderData
|
||||||
user,
|
const subscription = use(loaderData.subscription)
|
||||||
activeWorkspace,
|
const address = use(loaderData.address)
|
||||||
workspaces,
|
|
||||||
sidebar_state,
|
|
||||||
subscription: subscription_
|
|
||||||
} = loaderData
|
|
||||||
const subscription = use(subscription_)
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (typeof window !== 'undefined' && window.rybbit) {
|
if (typeof window !== 'undefined' && window.rybbit) {
|
||||||
@@ -88,6 +83,7 @@ export default function Route({ loaderData }: Route.ComponentProps) {
|
|||||||
activeWorkspace={activeWorkspace}
|
activeWorkspace={activeWorkspace}
|
||||||
workspaces={workspaces}
|
workspaces={workspaces}
|
||||||
subscription={subscription}
|
subscription={subscription}
|
||||||
|
address={address}
|
||||||
>
|
>
|
||||||
<SidebarProvider defaultOpen={sidebar_state === 'true'} className="flex">
|
<SidebarProvider defaultOpen={sidebar_state === 'true'} className="flex">
|
||||||
<AppSidebar />
|
<AppSidebar />
|
||||||
|
|||||||
Reference in New Issue
Block a user