@thorprovider/storefront 0.7.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -2
- package/assets/templates/AGENTS.md +14 -2
- package/assets/templates/app/app/[[...slug]]/page.tsx +9 -2
- package/assets/templates/app/lib/__STOREFRONT__/account-address.ts +129 -0
- package/assets/templates/app/lib/__STOREFRONT__/account-lifecycle.client.tsx +2 -2
- package/assets/templates/app/lib/__STOREFRONT__/account-private.core.ts +143 -0
- package/assets/templates/app/lib/__STOREFRONT__/account-profile.ts +220 -0
- package/assets/templates/app/lib/__STOREFRONT__/actions.ts +677 -80
- package/assets/templates/app/lib/__STOREFRONT__/addresses-port.ts +107 -0
- package/assets/templates/app/lib/__STOREFRONT__/commerce.ts +477 -38
- package/assets/templates/app/lib/__STOREFRONT__/confirmation.core.ts +240 -0
- package/assets/templates/app/lib/__STOREFRONT__/customer-profile-port.ts +103 -0
- package/assets/templates/app/lib/__STOREFRONT__/fulfillment-evidence-port.ts +31 -0
- package/assets/templates/app/lib/__STOREFRONT__/handlers.core.ts +21 -4
- package/assets/templates/app/lib/__STOREFRONT__/mappers.ts +6 -2
- package/assets/templates/app/lib/__STOREFRONT__/order-detail-mapper.ts +642 -0
- package/assets/templates/app/lib/__STOREFRONT__/order-detail-port.ts +25 -0
- package/assets/templates/app/lib/__STOREFRONT__/orders-port.ts +30 -0
- package/assets/templates/app/lib/__STOREFRONT__/ports.ts +28 -0
- package/assets/templates/app/lib/__STOREFRONT__/projection-ownership.client.tsx +4 -4
- package/assets/templates/app/lib/__STOREFRONT__/session.ts +24 -6
- package/assets/templates/app/lib/__STOREFRONT__/state.core.ts +21 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/address-book-advanced.client.tsx +29 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/checkout-commerce-v1.client.tsx +19 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/checkout-commerce-v1.tsx +16 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/customer-profile.client.tsx +27 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/index.ts +4 -1
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/mega-menu-universal.client.tsx +19 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/mega-menu-universal.tsx +16 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/schema.address-book-advanced.ts +437 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/schema.checkout-commerce-v1.ts +398 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/schema.customer-profile.ts +138 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/schema.mega-menu-universal.ts +76 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/schema.universal-banner.ts +217 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/universal-banner.client.tsx +24 -0
- package/assets/templates/app/lib/__STOREFRONT__/views/sections/universal-banner.tsx +16 -0
- package/assets/templates/features/checkout/handlers.ts +24 -4
- package/assets/templates/features/customer-addresses/handlers.ts +11 -0
- package/assets/templates/features/customer-addresses/state.ts +33 -0
- package/assets/templates/features/customer-auth/state.ts +10 -0
- package/assets/templates/features/customer-profile/handlers.ts +11 -0
- package/assets/templates/features/customer-profile/state.ts +18 -0
- package/assets/templates/views/account/address-book-advanced.defaults.ts +534 -0
- package/assets/templates/views/account/assembly/assembly.json +36 -0
- package/assets/templates/views/account/assembly/index.skeleton.tsx +277 -0
- package/assets/templates/views/account/assembly/registry.skeleton.tsx +1057 -0
- package/assets/templates/views/account/assembly/slots/addresses/AddressBookAdvanced/binding.json +5 -0
- package/assets/templates/views/account/assembly/slots/addresses/AddressBookAdvanced/catalog.part +4 -0
- package/assets/templates/views/account/assembly/slots/addresses/AddressBookAdvanced/element.part +5 -0
- package/assets/templates/views/account/assembly/slots/addresses/AddressBookAdvanced/registry.part +1 -0
- package/assets/templates/views/account/assembly/slots/addresses/cards/catalog.part +5 -0
- package/assets/templates/views/account/assembly/slots/addresses/cards/element.part +10 -0
- package/assets/templates/views/account/assembly/slots/addresses/cards/registry.part +1 -0
- package/assets/templates/views/account/assembly/slots/login/card/catalog.part +4 -0
- package/assets/templates/views/account/assembly/slots/login/card/element.part +13 -0
- package/assets/templates/views/account/assembly/slots/login/card/registry.part +1 -0
- package/assets/templates/views/account/assembly/slots/orders/OrderHistory/catalog.part +5 -0
- package/assets/templates/views/account/assembly/slots/orders/OrderHistory/element.part +5 -0
- package/assets/templates/views/account/assembly/slots/orders/OrderHistory/registry.part +2 -0
- package/assets/templates/views/account/assembly/slots/orders/OrderHistoryAdvanced/binding.json +5 -0
- package/assets/templates/views/account/assembly/slots/orders/OrderHistoryAdvanced/catalog.part +5 -0
- package/assets/templates/views/account/assembly/slots/orders/OrderHistoryAdvanced/element.part +5 -0
- package/assets/templates/views/account/assembly/slots/orders/OrderHistoryAdvanced/registry.part +1 -0
- package/assets/templates/views/account/assembly/slots/profile/CustomerProfileAdvanced/binding.json +5 -0
- package/assets/templates/views/account/assembly/slots/profile/CustomerProfileAdvanced/catalog.part +4 -0
- package/assets/templates/views/account/assembly/slots/profile/CustomerProfileAdvanced/element.part +5 -0
- package/assets/templates/views/account/assembly/slots/profile/CustomerProfileAdvanced/registry.part +1 -0
- package/assets/templates/views/account/assembly/slots/profile/form/catalog.part +5 -0
- package/assets/templates/views/account/assembly/slots/profile/form/element.part +10 -0
- package/assets/templates/views/account/assembly/slots/profile/form/registry.part +1 -0
- package/assets/templates/views/account/assembly/slots/register/SignUpPage1/catalog.part +4 -0
- package/assets/templates/views/account/assembly/slots/register/SignUpPage1/element.part +13 -0
- package/assets/templates/views/account/assembly/slots/register/SignUpPage1/registry.part +1 -0
- package/assets/templates/views/account/assembly/slots/summary/stack/catalog.part +5 -0
- package/assets/templates/views/account/assembly/slots/summary/stack/element.part +5 -0
- package/assets/templates/views/account/assembly/slots/summary/stack/registry.part +1 -0
- package/assets/templates/views/account/assembly/slots/summary/tabs/catalog.part +5 -0
- package/assets/templates/views/account/assembly/slots/summary/tabs/element.part +5 -0
- package/assets/templates/views/account/assembly/slots/summary/tabs/registry.part +1 -0
- package/assets/templates/views/account/content.ts +326 -0
- package/assets/templates/views/account/customer-profile.defaults.ts +136 -0
- package/assets/templates/views/account/fulfillment-evidence.client.tsx +34 -0
- package/assets/templates/views/account/fulfillment-evidence.defaults.ts +145 -0
- package/assets/templates/views/account/fulfillment-evidence.schema.ts +15 -0
- package/assets/templates/views/account/index.tsx +259 -38
- package/assets/templates/views/account/order-detail.client.tsx +35 -0
- package/assets/templates/views/account/order-detail.defaults.ts +457 -0
- package/assets/templates/views/account/orders-advanced.client.tsx +77 -0
- package/assets/templates/views/account/orders-advanced.defaults.ts +230 -0
- package/assets/templates/views/account/registry.tsx +935 -420
- package/assets/templates/views/account/schema.ts +50 -0
- package/assets/templates/views/account/sections-data.ts +2 -0
- package/assets/templates/views/cart/content.ts +1 -0
- package/assets/templates/views/category/assembly/index.skeleton.tsx +1 -0
- package/assets/templates/views/category/assembly/slots/filter/CategoryListing/element.part +1 -0
- package/assets/templates/views/category/content.ts +2 -0
- package/assets/templates/views/category/index.tsx +2 -0
- package/assets/templates/views/category/sections-data.ts +2 -0
- package/assets/templates/views/checkout/checkout-commerce-hosted.tsx +1089 -0
- package/assets/templates/views/checkout/content.ts +51 -0
- package/assets/templates/views/checkout/index.tsx +1 -1
- package/assets/templates/views/checkout/props.ts +10 -2
- package/assets/templates/views/checkout/registry.tsx +104 -42
- package/assets/templates/views/checkout/schema.ts +76 -1
- package/assets/templates/views/home/assembly/assembly.json +23 -2
- package/assets/templates/views/home/assembly/index.skeleton.tsx +4 -1
- package/assets/templates/views/home/assembly/registry.skeleton.tsx +1 -0
- package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/binding.json +5 -0
- package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/catalog.part +4 -0
- package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/element.part +4 -0
- package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/registry.part +10 -0
- package/assets/templates/views/home/content.ts +247 -1
- package/assets/templates/views/home/data.ts +10 -1
- package/assets/templates/views/home/index.tsx +8 -6
- package/assets/templates/views/home/props.ts +7 -1
- package/assets/templates/views/home/registry.tsx +11 -28
- package/assets/templates/views/home/schema.ts +163 -0
- package/assets/templates/views/home/sections-data.ts +2 -0
- package/assets/templates/views/product/content.ts +1 -0
- package/assets/templates/views/static/content.ts +1 -0
- package/bin/storefront.js +356 -27
- package/package.json +2 -1
- package/recipes/adapter-surface.json +159 -0
- package/recipes/archetypes.json +43 -4
- package/recipes/catalog.json +147 -2
- package/recipes/feature.schema.json +9 -0
- package/recipes/features.json +72 -1
- package/recipes/manifest.schema.json +5 -0
- package/recipes/recipe.schema.json +13 -1
- package/recipes/recipes.json +37 -7
- package/recipes/section.schema.json +112 -0
- package/recipes/sections.json +281 -4
- package/recipes/validate.mjs +83 -1
- package/scripts/adapter-surface.mjs +41 -0
- package/scripts/agent-blocks.mjs +700 -0
- package/scripts/block-validator.mjs +45 -0
- package/scripts/candidate.mjs +56 -14
- package/scripts/composers.mjs +7 -2
- package/scripts/default-sections.mjs +78 -0
- package/scripts/file-classes.mjs +18 -0
- package/scripts/generate-adapter-surface.mjs +148 -0
- package/scripts/operations.mjs +100 -0
- package/scripts/reconcile.mjs +67 -39
- package/scripts/section-configure.mjs +316 -0
- package/scripts/sync-assets.mjs +1 -0
- package/scripts/view-assembly.mjs +101 -14
- package/skills/sf-feature/SKILL.md +19 -0
- package/skills/sf-view-gen/SKILL.md +73 -7
- package/skills/sf-view-gen/assets/recipes/archetypes.json +43 -4
- package/skills/sf-view-gen/assets/recipes/catalog.json +147 -2
- package/skills/sf-view-gen/assets/recipes/features.json +72 -1
- package/skills/sf-view-gen/assets/recipes/recipes.json +37 -7
- package/skills/sf-view-gen/assets/recipes/section.schema.json +112 -0
- package/skills/sf-view-gen/assets/recipes/sections.json +281 -4
- package/skills/sf-view-gen/assets/templates/views/sections/checkout-commerce-v1.client.tsx +19 -0
- package/skills/sf-view-gen/assets/templates/views/sections/checkout-commerce-v1.tsx +16 -0
- package/skills/sf-view-gen/assets/templates/views/sections/mega-menu-universal.client.tsx +19 -0
- package/skills/sf-view-gen/assets/templates/views/sections/mega-menu-universal.tsx +16 -0
- package/skills/sf-view-gen/assets/templates/views/sections/schema.checkout-commerce-v1.ts +398 -0
- package/skills/sf-view-gen/assets/templates/views/sections/schema.mega-menu-universal.ts +76 -0
- package/skills/sf-view-gen/assets/templates/views/sections/schema.universal-banner.ts +217 -0
- package/skills/sf-view-gen/assets/templates/views/sections/universal-banner.client.tsx +24 -0
- package/skills/sf-view-gen/assets/templates/views/sections/universal-banner.tsx +16 -0
- package/skills/sf-view-gen/scripts/validate.mjs +83 -1
|
@@ -10,34 +10,482 @@
|
|
|
10
10
|
// passed straight from the block callback to the Server Action and is NEVER
|
|
11
11
|
// written to the json-render state store, so no password reaches client state,
|
|
12
12
|
// HTML, or logs. Only the non-secret result (customer/error) is stored.
|
|
13
|
+
//
|
|
14
|
+
// The account modules (profile, addresses) are installed as separate features.
|
|
15
|
+
// Their presence flag lives in the feature's state slice, so an uninstalled
|
|
16
|
+
// module has no flag, no navigation entry, and performs no backend read. The
|
|
17
|
+
// frame owns the shape (stack/tabs) and the module navigation; tab visibility is
|
|
18
|
+
// runtime state so the element tree stays static and assemblable.
|
|
13
19
|
|
|
14
20
|
import { useCallback, useEffect, useRef } from 'react'
|
|
15
21
|
import type { ReactNode } from 'react'
|
|
22
|
+
import { usePathname, useRouter } from 'next/navigation'
|
|
16
23
|
import { Button } from '@thorprovider/blocks/ui/button'
|
|
17
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
AccountFrame as AccountFrameBlock,
|
|
26
|
+
AddressBook as AddressBookBlock,
|
|
27
|
+
AddressForm as AddressFormBlock,
|
|
28
|
+
CustomerAccountOverview as AccountOverviewBlock,
|
|
29
|
+
CustomerAccountPlaceholder as ModulePlaceholderBlock,
|
|
30
|
+
CustomerAccountV1 as AccountShellBlock,
|
|
31
|
+
LoginPage as LoginPageBlock,
|
|
32
|
+
OrderHistory as OrderHistoryBlock,
|
|
33
|
+
ProfileForm as ProfileFormBlock,
|
|
34
|
+
SignUpPage1 as SignUpBlock,
|
|
35
|
+
} from '@thorprovider/blocks'
|
|
18
36
|
import type {
|
|
37
|
+
AddressBookLabels,
|
|
38
|
+
CustomerAccountOverviewProps,
|
|
39
|
+
CustomerAccountPlaceholderProps,
|
|
40
|
+
CustomerAccountV1Props,
|
|
19
41
|
OrderHistoryLabels,
|
|
20
42
|
OrderHistoryOrder,
|
|
21
43
|
OrderHistoryStatusTone,
|
|
22
44
|
} from '@thorprovider/blocks'
|
|
23
|
-
import
|
|
45
|
+
import { addressBookAdvancedRegistry as addressBookAdvancedRegistry } from '../sections/address-book-advanced.client'
|
|
46
|
+
import { customerProfileRegistry as customerProfileRegistry } from '../sections/customer-profile.client'
|
|
47
|
+
import { AccountOrdersAdvancedPanel as AccountOrdersAdvancedPanel } from './orders-advanced.client'
|
|
24
48
|
import { useStateStore } from '@json-render/react'
|
|
25
49
|
import type { Customer } from '@thorprovider/types'
|
|
26
|
-
import {
|
|
27
|
-
|
|
50
|
+
import {
|
|
51
|
+
createAccountAddressAction,
|
|
52
|
+
deleteAccountAddressAction,
|
|
53
|
+
loadAccountAddressesAction,
|
|
54
|
+
loadOrdersPageAction,
|
|
55
|
+
setDefaultAccountAddressAction,
|
|
56
|
+
updateAccountAddressAction,
|
|
57
|
+
updateAccountProfileAction,
|
|
58
|
+
} from '../../actions'
|
|
59
|
+
import { ACCOUNT_ORDERS_DEFAULT_PAGE_SIZE, accountIdentityKey, buildAccountProfileFields, isAccountModuleVisible } from '../../account-private.core'
|
|
28
60
|
import { AccountBoundary } from '../../account-boundary.client'
|
|
29
61
|
import { useAccountLifecycle } from '../../account-lifecycle.client'
|
|
30
|
-
import type { AccountOrderView } from '../../state.core'
|
|
62
|
+
import type { AccountAddressView, AccountOrderView } from '../../state.core'
|
|
31
63
|
import type { ViewRegistry } from '../types'
|
|
64
|
+
import { accountContent } from './content'
|
|
65
|
+
import { AccountOrderDetailPanel } from './order-detail.client'
|
|
66
|
+
import { AccountFulfillmentEvidencePanel } from './fulfillment-evidence.client'
|
|
32
67
|
import {
|
|
68
|
+
accountAddressPropsSchema,
|
|
69
|
+
accountFramePropsSchema,
|
|
33
70
|
accountLoginPropsSchema,
|
|
34
71
|
accountOrdersPropsSchema,
|
|
72
|
+
accountProfilePropsSchema,
|
|
35
73
|
accountRegisterPropsSchema,
|
|
36
74
|
} from './schema'
|
|
37
75
|
|
|
38
76
|
/** Bounded page size for the account order-history window. */
|
|
39
77
|
const ORDERS_PAGE_SIZE = ACCOUNT_ORDERS_DEFAULT_PAGE_SIZE
|
|
40
78
|
|
|
79
|
+
type PanelProps = {
|
|
80
|
+
props: Record<string, unknown>
|
|
81
|
+
emit: (event: string) => void
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
type AccountState = Record<string, unknown>
|
|
85
|
+
|
|
86
|
+
// The installed account modules, in render order. `orders` is part of the auth
|
|
87
|
+
// host; `profile` and `addresses` exist only when their feature is installed
|
|
88
|
+
// (the presence flag comes from the feature state slice).
|
|
89
|
+
function installedModules(state: AccountState): { id: string; label: string }[] {
|
|
90
|
+
const modules = [{ id: 'orders', label: accountContent.modules.orders }]
|
|
91
|
+
if (state.accountProfileAvailable === true) {
|
|
92
|
+
modules.push({ id: 'profile', label: accountContent.modules.profile })
|
|
93
|
+
}
|
|
94
|
+
if (state.accountAddressesAvailable === true) {
|
|
95
|
+
modules.push({ id: 'addresses', label: accountContent.modules.addresses })
|
|
96
|
+
}
|
|
97
|
+
return modules
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const SHELL_OVERVIEW_ID = 'overview'
|
|
101
|
+
const SHELL_FAVORITES_ID = 'lists'
|
|
102
|
+
|
|
103
|
+
// `views/account/content.ts` is user-owned and is never rewritten by sync. An
|
|
104
|
+
// existing store therefore has no shell keys when this registry lands; the
|
|
105
|
+
// defaults keep the cutover from crashing and can be overridden per store.
|
|
106
|
+
const SHELL_DEFAULTS = {
|
|
107
|
+
shell: {
|
|
108
|
+
loading: 'Cargando tu cuenta…',
|
|
109
|
+
retry: 'Reintentar',
|
|
110
|
+
reference: 'Referencia: {reference}',
|
|
111
|
+
backToAccount: 'Mi cuenta',
|
|
112
|
+
errorTitle: 'No pudimos cargar tu cuenta',
|
|
113
|
+
errorBody: 'Vuelve a intentarlo en unos segundos.',
|
|
114
|
+
header: {
|
|
115
|
+
greeting: 'Hola, {name}',
|
|
116
|
+
signOut: 'Cerrar sesión',
|
|
117
|
+
verified: 'Correo verificado',
|
|
118
|
+
memberSince: 'Cliente desde {date}',
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
overview: {
|
|
122
|
+
sections: 'Secciones de tu cuenta',
|
|
123
|
+
},
|
|
124
|
+
placeholder: {
|
|
125
|
+
badge: 'Próximamente',
|
|
126
|
+
upcoming: 'Qué encontrarás aquí',
|
|
127
|
+
},
|
|
128
|
+
shellModules: {
|
|
129
|
+
overview: {
|
|
130
|
+
label: 'Mi cuenta',
|
|
131
|
+
description: 'Todas las secciones de tu cuenta en un solo lugar.',
|
|
132
|
+
},
|
|
133
|
+
orders: {
|
|
134
|
+
label: 'Mis pedidos',
|
|
135
|
+
description: 'Consulta tus compras, entregas y devoluciones.',
|
|
136
|
+
},
|
|
137
|
+
profile: {
|
|
138
|
+
label: 'Mi perfil',
|
|
139
|
+
description: 'Administra tus datos personales y de contacto.',
|
|
140
|
+
},
|
|
141
|
+
addresses: {
|
|
142
|
+
label: 'Direcciones',
|
|
143
|
+
description: 'Administra tus direcciones de envío y facturación.',
|
|
144
|
+
},
|
|
145
|
+
favorites: {
|
|
146
|
+
label: 'Listas de compras',
|
|
147
|
+
description: 'Guarda productos para comprarlos después.',
|
|
148
|
+
planned: [
|
|
149
|
+
'Listas con nombre y productos guardados',
|
|
150
|
+
'Mover productos a la cesta',
|
|
151
|
+
'Listas compartidas cuando la tienda lo permita',
|
|
152
|
+
],
|
|
153
|
+
note: 'Se activa cuando las listas de compras están habilitadas.',
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
shellCards: {
|
|
157
|
+
orders: {
|
|
158
|
+
description: 'Consulta tus compras, entregas y devoluciones.',
|
|
159
|
+
},
|
|
160
|
+
profile: {
|
|
161
|
+
description: 'Administra tus datos personales y de contacto.',
|
|
162
|
+
},
|
|
163
|
+
addresses: {
|
|
164
|
+
description: 'Administra tus direcciones de envío y facturación.',
|
|
165
|
+
},
|
|
166
|
+
favorites: {
|
|
167
|
+
description: 'Guarda productos para comprarlos después.',
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
type ShellContent = typeof SHELL_DEFAULTS
|
|
173
|
+
|
|
174
|
+
const accountContentCompat = accountContent as typeof accountContent & Partial<ShellContent>
|
|
175
|
+
const shellContent = {
|
|
176
|
+
shell: {
|
|
177
|
+
...SHELL_DEFAULTS.shell,
|
|
178
|
+
...(accountContentCompat.shell ?? {}),
|
|
179
|
+
header: {
|
|
180
|
+
...SHELL_DEFAULTS.shell.header,
|
|
181
|
+
...(accountContentCompat.shell?.header ?? {}),
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
overview: {
|
|
185
|
+
...SHELL_DEFAULTS.overview,
|
|
186
|
+
...(accountContentCompat.overview ?? {}),
|
|
187
|
+
},
|
|
188
|
+
placeholder: {
|
|
189
|
+
...SHELL_DEFAULTS.placeholder,
|
|
190
|
+
...(accountContentCompat.placeholder ?? {}),
|
|
191
|
+
},
|
|
192
|
+
shellModules: {
|
|
193
|
+
overview: {
|
|
194
|
+
...SHELL_DEFAULTS.shellModules.overview,
|
|
195
|
+
...(accountContentCompat.shellModules?.overview ?? {}),
|
|
196
|
+
},
|
|
197
|
+
orders: {
|
|
198
|
+
...SHELL_DEFAULTS.shellModules.orders,
|
|
199
|
+
...(accountContentCompat.shellModules?.orders ?? {}),
|
|
200
|
+
},
|
|
201
|
+
profile: {
|
|
202
|
+
...SHELL_DEFAULTS.shellModules.profile,
|
|
203
|
+
...(accountContentCompat.shellModules?.profile ?? {}),
|
|
204
|
+
},
|
|
205
|
+
addresses: {
|
|
206
|
+
...SHELL_DEFAULTS.shellModules.addresses,
|
|
207
|
+
...(accountContentCompat.shellModules?.addresses ?? {}),
|
|
208
|
+
},
|
|
209
|
+
favorites: {
|
|
210
|
+
...SHELL_DEFAULTS.shellModules.favorites,
|
|
211
|
+
...(accountContentCompat.shellModules?.favorites ?? {}),
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
shellCards: {
|
|
215
|
+
orders: {
|
|
216
|
+
...SHELL_DEFAULTS.shellCards.orders,
|
|
217
|
+
...(accountContentCompat.shellCards?.orders ?? {}),
|
|
218
|
+
},
|
|
219
|
+
profile: {
|
|
220
|
+
...SHELL_DEFAULTS.shellCards.profile,
|
|
221
|
+
...(accountContentCompat.shellCards?.profile ?? {}),
|
|
222
|
+
},
|
|
223
|
+
addresses: {
|
|
224
|
+
...SHELL_DEFAULTS.shellCards.addresses,
|
|
225
|
+
...(accountContentCompat.shellCards?.addresses ?? {}),
|
|
226
|
+
},
|
|
227
|
+
favorites: {
|
|
228
|
+
...SHELL_DEFAULTS.shellCards.favorites,
|
|
229
|
+
...(accountContentCompat.shellCards?.favorites ?? {}),
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function shellCapabilities(state: AccountState): CustomerAccountV1Props['context']['capabilities'] {
|
|
235
|
+
return {
|
|
236
|
+
orderHistory: state.accountAuthenticated === true,
|
|
237
|
+
profile: state.accountProfileAvailable === true,
|
|
238
|
+
addressBook: state.accountAddressesAvailable === true,
|
|
239
|
+
// The planner entry is explicit: the card opens a documented placeholder,
|
|
240
|
+
// never a fake favorites list.
|
|
241
|
+
shoppingLists: true,
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function shellModules(): CustomerAccountV1Props['context']['modules'] {
|
|
246
|
+
return [
|
|
247
|
+
{
|
|
248
|
+
id: SHELL_OVERVIEW_ID,
|
|
249
|
+
label: shellContent.shellModules.overview.label,
|
|
250
|
+
status: 'ready',
|
|
251
|
+
description: shellContent.shellModules.overview.description,
|
|
252
|
+
planned: [],
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
id: 'orders',
|
|
256
|
+
label: shellContent.shellModules.orders.label,
|
|
257
|
+
status: 'ready',
|
|
258
|
+
capability: 'orderHistory',
|
|
259
|
+
description: shellContent.shellModules.orders.description,
|
|
260
|
+
planned: [],
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
id: 'profile',
|
|
264
|
+
label: shellContent.shellModules.profile.label,
|
|
265
|
+
status: 'ready',
|
|
266
|
+
capability: 'profile',
|
|
267
|
+
description: shellContent.shellModules.profile.description,
|
|
268
|
+
planned: [],
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
id: 'addresses',
|
|
272
|
+
label: shellContent.shellModules.addresses.label,
|
|
273
|
+
status: 'ready',
|
|
274
|
+
capability: 'addressBook',
|
|
275
|
+
description: shellContent.shellModules.addresses.description,
|
|
276
|
+
planned: [],
|
|
277
|
+
},
|
|
278
|
+
{
|
|
279
|
+
id: SHELL_FAVORITES_ID,
|
|
280
|
+
label: shellContent.shellModules.favorites.label,
|
|
281
|
+
status: 'planned',
|
|
282
|
+
capability: 'shoppingLists',
|
|
283
|
+
description: shellContent.shellModules.favorites.description,
|
|
284
|
+
planned: shellContent.shellModules.favorites.planned,
|
|
285
|
+
...(shellContent.shellModules.favorites.note === undefined
|
|
286
|
+
? {}
|
|
287
|
+
: { note: shellContent.shellModules.favorites.note }),
|
|
288
|
+
},
|
|
289
|
+
]
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function shellCards(): CustomerAccountOverviewProps['model']['cards'] {
|
|
293
|
+
return [
|
|
294
|
+
{
|
|
295
|
+
id: 'orders',
|
|
296
|
+
title: shellContent.shellModules.orders.label,
|
|
297
|
+
description: shellContent.shellCards.orders.description,
|
|
298
|
+
icon: 'orders',
|
|
299
|
+
capability: 'orderHistory',
|
|
300
|
+
order: 1,
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
id: 'profile',
|
|
304
|
+
title: shellContent.shellModules.profile.label,
|
|
305
|
+
description: shellContent.shellCards.profile.description,
|
|
306
|
+
icon: 'profile',
|
|
307
|
+
capability: 'profile',
|
|
308
|
+
order: 2,
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
id: 'addresses',
|
|
312
|
+
title: shellContent.shellModules.addresses.label,
|
|
313
|
+
description: shellContent.shellCards.addresses.description,
|
|
314
|
+
icon: 'addresses',
|
|
315
|
+
capability: 'addressBook',
|
|
316
|
+
order: 3,
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
id: SHELL_FAVORITES_ID,
|
|
320
|
+
title: shellContent.shellModules.favorites.label,
|
|
321
|
+
description: shellContent.shellCards.favorites.description,
|
|
322
|
+
icon: 'lists',
|
|
323
|
+
capability: 'shoppingLists',
|
|
324
|
+
order: 4,
|
|
325
|
+
},
|
|
326
|
+
]
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function shellModuleRoute(moduleId: string): string {
|
|
330
|
+
switch (moduleId) {
|
|
331
|
+
case 'orders':
|
|
332
|
+
return '/account/orders'
|
|
333
|
+
case 'profile':
|
|
334
|
+
return '/account/profile'
|
|
335
|
+
case 'addresses':
|
|
336
|
+
return '/account/addresses'
|
|
337
|
+
case SHELL_FAVORITES_ID:
|
|
338
|
+
return '/account/favorites'
|
|
339
|
+
default:
|
|
340
|
+
return '/account'
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function shellActiveModule(pathname: string): string {
|
|
345
|
+
if (pathname === '/account/orders' || pathname.startsWith('/account/orders/')) {
|
|
346
|
+
return 'orders'
|
|
347
|
+
}
|
|
348
|
+
if (pathname === '/account/profile') return 'profile'
|
|
349
|
+
if (pathname === '/account/addresses') return 'addresses'
|
|
350
|
+
if (pathname === '/account/favorites') return SHELL_FAVORITES_ID
|
|
351
|
+
return SHELL_OVERVIEW_ID
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function shellCustomer(customer: Customer): CustomerAccountV1Props['context']['customer'] {
|
|
355
|
+
const name =
|
|
356
|
+
[customer.firstName, customer.lastName].filter(Boolean).join(' ') || customer.email
|
|
357
|
+
const initials =
|
|
358
|
+
name
|
|
359
|
+
.split(/\s+/)
|
|
360
|
+
.filter(Boolean)
|
|
361
|
+
.slice(0, 2)
|
|
362
|
+
.map((part) => part[0])
|
|
363
|
+
.join('')
|
|
364
|
+
.toUpperCase() || customer.email.slice(0, 2).toUpperCase()
|
|
365
|
+
return {
|
|
366
|
+
name,
|
|
367
|
+
email: customer.email,
|
|
368
|
+
initials,
|
|
369
|
+
...(customer.phone ? { phone: customer.phone } : {}),
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function shellState(state: AccountState): CustomerAccountV1Props['state'] {
|
|
374
|
+
if (state.accountReconciling === true) return { status: 'loading' }
|
|
375
|
+
if (state.accountResolution === 'unavailable') {
|
|
376
|
+
return {
|
|
377
|
+
status: 'error',
|
|
378
|
+
title: shellContent.shell.errorTitle,
|
|
379
|
+
message: shellContent.shell.errorBody,
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return { status: 'ready' }
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
type ShellPanelProps = PanelProps & { children?: ReactNode }
|
|
386
|
+
|
|
387
|
+
function AccountShellPanel({ children }: ShellPanelProps) {
|
|
388
|
+
const { state } = useStateStore()
|
|
389
|
+
const pathname = usePathname()
|
|
390
|
+
const router = useRouter()
|
|
391
|
+
const lifecycle = useAccountLifecycle()
|
|
392
|
+
const customer = (state.accountCustomer ?? null) as Customer | null
|
|
393
|
+
const wrapperRef = useRef<HTMLDivElement | null>(null)
|
|
394
|
+
|
|
395
|
+
// A route change remounts the shell, so its own module-change focus effect
|
|
396
|
+
// cannot run. Focus the shell content region after a module route renders;
|
|
397
|
+
// the overview route restores focus to its originating card instead.
|
|
398
|
+
useEffect(() => {
|
|
399
|
+
if (pathname === '/account') return
|
|
400
|
+
const frame = requestAnimationFrame(() => {
|
|
401
|
+
wrapperRef.current
|
|
402
|
+
?.querySelector<HTMLElement>('[role="region"][tabindex="-1"]')
|
|
403
|
+
?.focus()
|
|
404
|
+
})
|
|
405
|
+
return () => cancelAnimationFrame(frame)
|
|
406
|
+
}, [pathname])
|
|
407
|
+
|
|
408
|
+
if (!state.accountAuthenticated || !customer) return null
|
|
409
|
+
|
|
410
|
+
return (
|
|
411
|
+
<div
|
|
412
|
+
ref={wrapperRef}
|
|
413
|
+
className="mx-auto w-full max-w-7xl px-4 py-8 sm:px-6 lg:px-8"
|
|
414
|
+
>
|
|
415
|
+
<AccountShellBlock
|
|
416
|
+
context={{
|
|
417
|
+
customer: shellCustomer(customer),
|
|
418
|
+
capabilities: shellCapabilities(state),
|
|
419
|
+
modules: shellModules(),
|
|
420
|
+
activeModuleId: shellActiveModule(pathname),
|
|
421
|
+
locale: 'es-ES',
|
|
422
|
+
currency: 'EUR',
|
|
423
|
+
}}
|
|
424
|
+
labels={shellContent.shell}
|
|
425
|
+
state={shellState(state)}
|
|
426
|
+
lang="es"
|
|
427
|
+
onModuleSelect={(moduleId) => router.push(shellModuleRoute(moduleId))}
|
|
428
|
+
onSignOut={() => void lifecycle.logout()}
|
|
429
|
+
onRetry={() => void lifecycle.retryResolution()}
|
|
430
|
+
>
|
|
431
|
+
{children}
|
|
432
|
+
</AccountShellBlock>
|
|
433
|
+
</div>
|
|
434
|
+
)
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function AccountOverviewPanel() {
|
|
438
|
+
const { state } = useStateStore()
|
|
439
|
+
const pathname = usePathname()
|
|
440
|
+
const router = useRouter()
|
|
441
|
+
|
|
442
|
+
useEffect(() => {
|
|
443
|
+
if (pathname !== '/account') return
|
|
444
|
+
const originCard = sessionStorage.getItem('account:origin-card')
|
|
445
|
+
if (!originCard) return
|
|
446
|
+
const frame = requestAnimationFrame(() => {
|
|
447
|
+
document.querySelector<HTMLElement>(`[data-module-id="${originCard}"]`)?.focus()
|
|
448
|
+
sessionStorage.removeItem('account:origin-card')
|
|
449
|
+
})
|
|
450
|
+
return () => cancelAnimationFrame(frame)
|
|
451
|
+
}, [pathname])
|
|
452
|
+
|
|
453
|
+
if (!state.accountAuthenticated) return null
|
|
454
|
+
return (
|
|
455
|
+
<AccountOverviewBlock
|
|
456
|
+
title={shellContent.shellModules.overview.label}
|
|
457
|
+
model={{ cards: shellCards(), summaries: {}, notice: null }}
|
|
458
|
+
capabilities={shellCapabilities(state)}
|
|
459
|
+
labels={{ sections: shellContent.overview.sections }}
|
|
460
|
+
onOpenCard={(cardId) => {
|
|
461
|
+
sessionStorage.setItem('account:origin-card', cardId)
|
|
462
|
+
router.push(shellModuleRoute(cardId))
|
|
463
|
+
}}
|
|
464
|
+
/>
|
|
465
|
+
)
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function AccountModulePlaceholderPanel() {
|
|
469
|
+
const { state } = useStateStore()
|
|
470
|
+
if (!state.accountAuthenticated) return null
|
|
471
|
+
const module = shellModules().find((entry) => entry.id === SHELL_FAVORITES_ID)
|
|
472
|
+
if (!module) return null
|
|
473
|
+
const labels: CustomerAccountPlaceholderProps['labels'] = shellContent.placeholder
|
|
474
|
+
return <ModulePlaceholderBlock module={module} labels={labels} />
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Tab visibility is runtime state: the element tree stays static, and only the
|
|
478
|
+
// frame decides which module is current. In the stacked shape every installed
|
|
479
|
+
// module renders in order.
|
|
480
|
+
function useModuleVisible(id: string): boolean {
|
|
481
|
+
const { state } = useStateStore()
|
|
482
|
+
return isAccountModuleVisible(
|
|
483
|
+
typeof state.accountShape === 'string' ? state.accountShape : null,
|
|
484
|
+
typeof state.accountTab === 'string' ? state.accountTab : null,
|
|
485
|
+
id,
|
|
486
|
+
)
|
|
487
|
+
}
|
|
488
|
+
|
|
41
489
|
// Non-secret status surface for the block pages, which expose no error/pending
|
|
42
490
|
// prop of their own. Renders nothing when there is no pending work or error.
|
|
43
491
|
function AuthNotice({ pending, error }: { pending: boolean; error: string | null }) {
|
|
@@ -53,14 +501,9 @@ function AuthNotice({ pending, error }: { pending: boolean; error: string | null
|
|
|
53
501
|
)
|
|
54
502
|
}
|
|
55
503
|
|
|
56
|
-
// The page side of the auth contract. The blocks render only the form:
|
|
57
|
-
//
|
|
58
|
-
//
|
|
59
|
-
// chrome (notice + banner + navbar + footer) and the page — not the block — owns
|
|
60
|
-
// the viewport. This wrapper is the extracted shell, kept so the shopper-visible
|
|
61
|
-
// result of every auth surface is unchanged. It lives in a derived template
|
|
62
|
-
// (CLI-owned) and stays theme-driven: the backdrop uses the same semantic
|
|
63
|
-
// tokens, so dark mode and any preset resolve here too.
|
|
504
|
+
// The page side of the auth contract. The blocks render only the form: the page
|
|
505
|
+
// — not the block — owns the viewport. This wrapper keeps the shopper-visible
|
|
506
|
+
// result of every auth surface unchanged and stays theme-driven.
|
|
64
507
|
function AuthCanvas({ stack = false, children }: { stack?: boolean; children: ReactNode }) {
|
|
65
508
|
return (
|
|
66
509
|
<section
|
|
@@ -75,11 +518,9 @@ function AuthCanvas({ stack = false, children }: { stack?: boolean; children: Re
|
|
|
75
518
|
)
|
|
76
519
|
}
|
|
77
520
|
|
|
78
|
-
// Neutral pending surface shown while a cross-tab/foreground reconciliation is
|
|
79
|
-
//
|
|
80
|
-
//
|
|
81
|
-
// is NOT a signed-out or unavailable state, and the held data is never
|
|
82
|
-
// destroyed (a same-identity re-resolution re-presents it without a refetch).
|
|
521
|
+
// Neutral pending surface shown while a cross-tab/foreground reconciliation is in
|
|
522
|
+
// flight. It replaces the previously held identity/orders so no stale private
|
|
523
|
+
// data is presented as current during the server round-trip.
|
|
83
524
|
function ReconcilingStatus() {
|
|
84
525
|
return (
|
|
85
526
|
<p
|
|
@@ -95,8 +536,7 @@ function ReconcilingStatus() {
|
|
|
95
536
|
|
|
96
537
|
// Backend order status → badge tone. The status model belongs to the host (the
|
|
97
538
|
// block never compares a status string), so this map is the storefront's own
|
|
98
|
-
// vocabulary; an unknown status keeps the neutral tone and its own label
|
|
99
|
-
// state is invented and no order is mislabelled.
|
|
539
|
+
// vocabulary; an unknown status keeps the neutral tone and its own label.
|
|
100
540
|
const ORDER_STATUS_TONES: Record<string, OrderHistoryStatusTone> = {
|
|
101
541
|
pending: 'outline',
|
|
102
542
|
processing: 'secondary',
|
|
@@ -105,10 +545,6 @@ const ORDER_STATUS_TONES: Record<string, OrderHistoryStatusTone> = {
|
|
|
105
545
|
refunded: 'outline',
|
|
106
546
|
}
|
|
107
547
|
|
|
108
|
-
// Map a backend-sourced account order row to the canonical block contract.
|
|
109
|
-
// `id` is the REAL backend order id: it is the React key AND the value the block
|
|
110
|
-
// reports, so selection needs no number→id map. Amounts stay canonical `Money`
|
|
111
|
-
// and an absent image/variant is omitted rather than serialized as ''.
|
|
112
548
|
function toOrderHistoryOrder(order: AccountOrderView): OrderHistoryOrder {
|
|
113
549
|
return {
|
|
114
550
|
id: order.id,
|
|
@@ -130,420 +566,499 @@ function toOrderHistoryOrder(order: AccountOrderView): OrderHistoryOrder {
|
|
|
130
566
|
}
|
|
131
567
|
}
|
|
132
568
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
569
|
+
function AccountLoginPanel({ props, emit }: PanelProps) {
|
|
570
|
+
const { state } = useStateStore()
|
|
571
|
+
const lifecycle = useAccountLifecycle()
|
|
572
|
+
const parsed = accountLoginPropsSchema.parse(props)
|
|
573
|
+
const error = typeof state.accountError === 'string' ? state.accountError : null
|
|
574
|
+
const pending = Boolean(state.accountPending)
|
|
575
|
+
return (
|
|
576
|
+
<>
|
|
577
|
+
<AuthNotice pending={pending} error={error} />
|
|
578
|
+
<AuthCanvas>
|
|
579
|
+
<fieldset disabled={pending} aria-busy={pending} className="contents">
|
|
580
|
+
<LoginPageBlock
|
|
581
|
+
presentation="card"
|
|
582
|
+
logoText={parsed.logoText}
|
|
583
|
+
welcomeTitleLabel={parsed.welcomeTitleLabel}
|
|
584
|
+
welcomeDescriptionLabel={parsed.welcomeDescriptionLabel}
|
|
585
|
+
emailLabel={parsed.emailLabel}
|
|
586
|
+
passwordLabel={parsed.passwordLabel}
|
|
587
|
+
signInLabel={parsed.signInLabel}
|
|
588
|
+
createAccountLabel={parsed.createAccountLabel}
|
|
589
|
+
rememberMeLabel="Recuérdame"
|
|
590
|
+
onSubmit={(data) => {
|
|
591
|
+
void lifecycle.login({ email: data.email, password: data.password })
|
|
592
|
+
}}
|
|
593
|
+
onCreateAccount={() => emit('showRegister')}
|
|
594
|
+
/>
|
|
595
|
+
</fieldset>
|
|
596
|
+
</AuthCanvas>
|
|
597
|
+
</>
|
|
151
598
|
)
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
function AccountRegisterPanel({ props, emit }: PanelProps) {
|
|
602
|
+
const { state } = useStateStore()
|
|
603
|
+
const lifecycle = useAccountLifecycle()
|
|
604
|
+
const parsed = accountRegisterPropsSchema.parse(props)
|
|
605
|
+
const error = typeof state.accountError === 'string' ? state.accountError : null
|
|
606
|
+
const pending = Boolean(state.accountPending)
|
|
152
607
|
return (
|
|
153
|
-
|
|
154
|
-
<
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
</li>
|
|
181
|
-
))}
|
|
182
|
-
</ul>
|
|
183
|
-
<dl className="mt-4 space-y-1 border-t pt-4 text-sm">
|
|
184
|
-
{totals.map((row) => (
|
|
185
|
-
<div key={row.label} className="flex justify-between">
|
|
186
|
-
<dt className="text-muted-foreground">{row.label}</dt>
|
|
187
|
-
<dd>{amount(row.value)}</dd>
|
|
188
|
-
</div>
|
|
189
|
-
))}
|
|
190
|
-
</dl>
|
|
191
|
-
</div>
|
|
192
|
-
</div>
|
|
608
|
+
<>
|
|
609
|
+
<AuthNotice pending={pending} error={error} />
|
|
610
|
+
<AuthCanvas stack>
|
|
611
|
+
<fieldset disabled={pending} aria-busy={pending} className="contents">
|
|
612
|
+
<SignUpBlock
|
|
613
|
+
logoText={parsed.logoText}
|
|
614
|
+
createAccountTitleLabel={parsed.createAccountTitleLabel}
|
|
615
|
+
createAccountDescriptionLabel={parsed.createAccountDescriptionLabel}
|
|
616
|
+
fullNameLabel={parsed.fullNameLabel}
|
|
617
|
+
emailLabel={parsed.emailLabel}
|
|
618
|
+
passwordLabel={parsed.passwordLabel}
|
|
619
|
+
createAccountLabel={parsed.createAccountLabel}
|
|
620
|
+
signInLabel={parsed.signInLabel}
|
|
621
|
+
onSubmit={(data) => {
|
|
622
|
+
const parts = data.fullName.trim().split(/\s+/).filter(Boolean)
|
|
623
|
+
void lifecycle.register({
|
|
624
|
+
email: data.email,
|
|
625
|
+
password: data.password,
|
|
626
|
+
firstName: parts[0] ?? '',
|
|
627
|
+
lastName: parts.slice(1).join(' '),
|
|
628
|
+
})
|
|
629
|
+
}}
|
|
630
|
+
onSignIn={() => emit('showLogin')}
|
|
631
|
+
/>
|
|
632
|
+
</fieldset>
|
|
633
|
+
</AuthCanvas>
|
|
634
|
+
</>
|
|
193
635
|
)
|
|
194
636
|
}
|
|
195
637
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
const parsed = accountLoginPropsSchema.parse(props)
|
|
209
|
-
const error = typeof state.accountError === 'string' ? state.accountError : null
|
|
210
|
-
const pending = Boolean(state.accountPending)
|
|
211
|
-
return (
|
|
212
|
-
<>
|
|
213
|
-
<AuthNotice pending={pending} error={error} />
|
|
214
|
-
{/* The block exposes no pending/disabled prop, so the registry disables
|
|
215
|
-
the whole form natively while a mutation is pending: a disabled
|
|
216
|
-
fieldset freezes the submit control and the inputs without clearing
|
|
217
|
-
them, and `display: contents` leaves the layout untouched. The
|
|
218
|
-
shared lifecycle also drops a same-tick duplicate before React
|
|
219
|
-
re-renders, so no duplicate request is issued in the interim. */}
|
|
220
|
-
<AuthCanvas>
|
|
221
|
-
<fieldset disabled={pending} aria-busy={pending} className="contents">
|
|
222
|
-
<LoginPage
|
|
223
|
-
presentation="card"
|
|
224
|
-
logoText={parsed.logoText}
|
|
225
|
-
welcomeTitleLabel={parsed.welcomeTitleLabel}
|
|
226
|
-
welcomeDescriptionLabel={parsed.welcomeDescriptionLabel}
|
|
227
|
-
emailLabel={parsed.emailLabel}
|
|
228
|
-
passwordLabel={parsed.passwordLabel}
|
|
229
|
-
signInLabel={parsed.signInLabel}
|
|
230
|
-
createAccountLabel={parsed.createAccountLabel}
|
|
231
|
-
rememberMeLabel="Recuérdame"
|
|
232
|
-
// The credentials stay in the block's local state and go straight to
|
|
233
|
-
// the shared lifecycle (which calls the Server Action); they are never
|
|
234
|
-
// written to the store. The lifecycle owns pending/error, the
|
|
235
|
-
// generation guard, duplicate prevention, and the server refresh, so
|
|
236
|
-
// the mounted form is never remounted and its input survives a failure.
|
|
237
|
-
onSubmit={(data) => {
|
|
238
|
-
void lifecycle.login({ email: data.email, password: data.password })
|
|
239
|
-
}}
|
|
240
|
-
onCreateAccount={() => emit('showRegister')}
|
|
241
|
-
/>
|
|
242
|
-
</fieldset>
|
|
243
|
-
</AuthCanvas>
|
|
244
|
-
</>
|
|
245
|
-
)
|
|
246
|
-
},
|
|
247
|
-
AccountRegister: ({ props, emit }) => {
|
|
248
|
-
const { state } = useStateStore()
|
|
249
|
-
const lifecycle = useAccountLifecycle()
|
|
250
|
-
const parsed = accountRegisterPropsSchema.parse(props)
|
|
251
|
-
const error = typeof state.accountError === 'string' ? state.accountError : null
|
|
252
|
-
const pending = Boolean(state.accountPending)
|
|
253
|
-
return (
|
|
254
|
-
<>
|
|
255
|
-
<AuthNotice pending={pending} error={error} />
|
|
256
|
-
{/* Same native pending-disable as the login form (the block exposes no
|
|
257
|
-
pending prop); input values persist across a failed mutation. */}
|
|
258
|
-
<AuthCanvas stack>
|
|
259
|
-
<fieldset disabled={pending} aria-busy={pending} className="contents">
|
|
260
|
-
<SignUpBlock
|
|
261
|
-
logoText={parsed.logoText}
|
|
262
|
-
createAccountTitleLabel={parsed.createAccountTitleLabel}
|
|
263
|
-
createAccountDescriptionLabel={parsed.createAccountDescriptionLabel}
|
|
264
|
-
fullNameLabel={parsed.fullNameLabel}
|
|
265
|
-
emailLabel={parsed.emailLabel}
|
|
266
|
-
passwordLabel={parsed.passwordLabel}
|
|
267
|
-
createAccountLabel={parsed.createAccountLabel}
|
|
268
|
-
signInLabel={parsed.signInLabel}
|
|
269
|
-
onSubmit={(data) => {
|
|
270
|
-
const parts = data.fullName.trim().split(/\s+/).filter(Boolean)
|
|
271
|
-
void lifecycle.register({
|
|
272
|
-
email: data.email,
|
|
273
|
-
password: data.password,
|
|
274
|
-
firstName: parts[0] ?? '',
|
|
275
|
-
lastName: parts.slice(1).join(' '),
|
|
276
|
-
})
|
|
277
|
-
}}
|
|
278
|
-
onSignIn={() => emit('showLogin')}
|
|
279
|
-
/>
|
|
280
|
-
</fieldset>
|
|
281
|
-
</AuthCanvas>
|
|
282
|
-
</>
|
|
283
|
-
)
|
|
284
|
-
},
|
|
285
|
-
AccountPanel: ({ emit }) => {
|
|
286
|
-
const { state } = useStateStore()
|
|
287
|
-
const { reconciling } = useAccountLifecycle()
|
|
288
|
-
// A reconciliation in flight hides the held identity rather than presenting
|
|
289
|
-
// it as current (task 4.2); the panel stays mounted so nothing is destroyed.
|
|
290
|
-
if (reconciling) return <ReconcilingStatus />
|
|
291
|
-
const customer = (state.accountCustomer ?? null) as Customer | null
|
|
292
|
-
const error = typeof state.accountError === 'string' ? state.accountError : null
|
|
293
|
-
const pending = Boolean(state.accountPending)
|
|
294
|
-
const name = customer
|
|
295
|
-
? [customer.firstName, customer.lastName].filter(Boolean).join(' ') || customer.email
|
|
296
|
-
: ''
|
|
297
|
-
return (
|
|
298
|
-
<div className="mx-auto max-w-md px-4 py-16 text-center">
|
|
299
|
-
<h1 className="text-2xl font-bold">Mi cuenta</h1>
|
|
300
|
-
<p className="text-muted-foreground mt-2 text-sm">Has iniciado sesión como</p>
|
|
301
|
-
<p className="mt-1 text-lg font-medium">{name}</p>
|
|
302
|
-
{customer?.email ? <p className="text-muted-foreground text-sm">{customer.email}</p> : null}
|
|
303
|
-
{error ? (
|
|
304
|
-
<p role="alert" className="text-destructive mt-4 text-sm">
|
|
305
|
-
{error}
|
|
306
|
-
</p>
|
|
307
|
-
) : null}
|
|
308
|
-
<Button className="mt-6" disabled={pending} onClick={() => emit('logout')}>
|
|
309
|
-
{pending ? 'Cerrando sesión…' : 'Cerrar sesión'}
|
|
310
|
-
</Button>
|
|
311
|
-
</div>
|
|
312
|
-
)
|
|
313
|
-
},
|
|
314
|
-
/**
|
|
315
|
-
* Signed-in order history, scoped to the CURRENT shopper identity. The first
|
|
316
|
-
* page loads from the cookie-authorized Server Action; later pages load on
|
|
317
|
-
* demand, each scoped to the authenticated customer's channel. Loading is keyed
|
|
318
|
-
* by the shared lifecycle's `identityKey` (customer id, or `anonymous`) plus a
|
|
319
|
-
* per-component generation:
|
|
320
|
-
*
|
|
321
|
-
* - the first-page effect reloads on an identity CHANGE (a boolean flag
|
|
322
|
-
* cannot detect a customer switch) and lazy-loads only on `/account`;
|
|
323
|
-
* - every `loadPage` captures the live key/generation at request time and
|
|
324
|
-
* discards its result if either changed, so a previous customer's rows,
|
|
325
|
-
* count, page, selection, or detail never reappear;
|
|
326
|
-
* - the private STORE slice is cleared by the shared lifecycle write path
|
|
327
|
-
* (`claimAccount`) AT the trigger, not by this view, so it is cleared even
|
|
328
|
-
* when the panel unmounts (logout/expiry/unavailable); this view only
|
|
329
|
-
* HIDES rows whose key is not the current identity until the new
|
|
330
|
-
* identity's page lands (a render guard, never the clear mechanism).
|
|
331
|
-
*
|
|
332
|
-
* The backend's real `count` drives pagination, so a page length is never
|
|
333
|
-
* presented as the total — the block receives one page and renders exactly it.
|
|
334
|
-
* Selection is keyed by the REAL backend order id, which the canonical
|
|
335
|
-
* `OrderHistory` block reports from `onViewOrder(id)`, and the detail is
|
|
336
|
-
* assembled from the fetched row; the selection id survives paging (returning
|
|
337
|
-
* to the order's page reopens it) and is dismissible with the inline close
|
|
338
|
-
* control. A failed page load shows an actionable retry that re-runs the SAME
|
|
339
|
-
* page (never a fabricated empty history). No token is read, stored, or
|
|
340
|
-
* rendered here.
|
|
341
|
-
*/
|
|
342
|
-
AccountOrders: ({ props }) => {
|
|
343
|
-
const { state, set, getSnapshot } = useStateStore()
|
|
344
|
-
const parsed = accountOrdersPropsSchema.parse(props)
|
|
345
|
-
const { identityKey, reconciling } = useAccountLifecycle()
|
|
346
|
-
|
|
347
|
-
// Bumped on every identity change so a response from a previous identity — or
|
|
348
|
-
// a previous visit to the same identity — is discarded even if the key later
|
|
349
|
-
// returns.
|
|
350
|
-
const generationRef = useRef(0)
|
|
351
|
-
// The identity key the currently DISPLAYED rows belong to; null until a load
|
|
352
|
-
// for the current identity succeeds. A render whose key differs hides the
|
|
353
|
-
// rows immediately, so a previous customer's orders never paint after the
|
|
354
|
-
// identity changes, before the shared clear even runs.
|
|
355
|
-
const loadedKeyRef = useRef<string | null>(null)
|
|
356
|
-
// The page of the MOST RECENT request (not the last successful one), so the
|
|
357
|
-
// error-state retry re-runs the exact page the shopper asked for — including
|
|
358
|
-
// a failed next/prev page. Local to the panel: an identity change unmounts it
|
|
359
|
-
// (the `/accountAuthenticated` gate), so it never leaks across shoppers.
|
|
360
|
-
const requestedPageRef = useRef(1)
|
|
361
|
-
|
|
362
|
-
// Read the LIVE identity key from the store so an async response can discard
|
|
363
|
-
// itself even if this component has since re-rendered or unmounted: the store
|
|
364
|
-
// outlives the component, unlike a render closure.
|
|
365
|
-
const currentIdentityKey = useCallback(
|
|
366
|
-
() =>
|
|
367
|
-
accountIdentityKey(
|
|
368
|
-
(getSnapshot().accountCustomer ?? null) as { id?: string | null } | null,
|
|
369
|
-
),
|
|
370
|
-
[getSnapshot],
|
|
371
|
-
)
|
|
638
|
+
/**
|
|
639
|
+
* The account frame: the signed-in identity and the module navigation. It is
|
|
640
|
+
* rendered only for an authenticated shopper (the element visibility gate) and
|
|
641
|
+
* it seeds the shape/tab state the module panels read.
|
|
642
|
+
*/
|
|
643
|
+
function AccountFramePanel({ props }: PanelProps) {
|
|
644
|
+
const { state, set } = useStateStore()
|
|
645
|
+
const parsed = accountFramePropsSchema.parse(props)
|
|
646
|
+
const presentation = parsed.presentation ?? 'stack'
|
|
647
|
+
const customer = (state.accountCustomer ?? null) as Customer | null
|
|
648
|
+
const modules = installedModules(state)
|
|
649
|
+
const activeModule = typeof state.accountTab === 'string' ? state.accountTab : undefined
|
|
372
650
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
set('/
|
|
381
|
-
|
|
382
|
-
.then((result) => {
|
|
383
|
-
if (
|
|
384
|
-
currentIdentityKey() !== capturedKey ||
|
|
385
|
-
generationRef.current !== capturedGeneration
|
|
386
|
-
) {
|
|
387
|
-
return
|
|
388
|
-
}
|
|
389
|
-
set('/accountOrdersPending', false)
|
|
390
|
-
if (result.ok) {
|
|
391
|
-
set('/accountOrders', result.orders)
|
|
392
|
-
set('/accountOrdersCount', result.count)
|
|
393
|
-
set('/accountOrdersPage', page)
|
|
394
|
-
set('/accountOrdersPageSize', result.limit)
|
|
395
|
-
set('/accountOrdersError', null)
|
|
396
|
-
loadedKeyRef.current = capturedKey
|
|
397
|
-
return
|
|
398
|
-
}
|
|
399
|
-
set('/accountOrdersError', result.error)
|
|
400
|
-
})
|
|
401
|
-
.catch((error) => {
|
|
402
|
-
if (
|
|
403
|
-
currentIdentityKey() !== capturedKey ||
|
|
404
|
-
generationRef.current !== capturedGeneration
|
|
405
|
-
) {
|
|
406
|
-
return
|
|
407
|
-
}
|
|
408
|
-
console.error(
|
|
409
|
-
'[storefront/account] loadOrders failed',
|
|
410
|
-
error instanceof Error ? error.message : error,
|
|
411
|
-
)
|
|
412
|
-
set('/accountOrdersPending', false)
|
|
413
|
-
set('/accountOrdersError', parsed.errorLabel ?? 'No se pudieron cargar tus pedidos.')
|
|
414
|
-
})
|
|
651
|
+
useEffect(() => {
|
|
652
|
+
if (presentation === 'tabs') {
|
|
653
|
+
set('/accountShape', 'tabs')
|
|
654
|
+
if (!modules.some((module) => module.id === activeModule)) {
|
|
655
|
+
set('/accountTab', modules[0]?.id ?? 'orders')
|
|
656
|
+
}
|
|
657
|
+
} else {
|
|
658
|
+
set('/accountShape', 'stack')
|
|
659
|
+
set('/accountTab', null)
|
|
415
660
|
}
|
|
661
|
+
}, [presentation, modules.length, activeModule, set])
|
|
662
|
+
|
|
663
|
+
if (!state.accountAuthenticated || !customer) return null
|
|
664
|
+
const displayName =
|
|
665
|
+
[customer.firstName, customer.lastName].filter(Boolean).join(' ') || customer.email
|
|
666
|
+
const labels = parsed.labels ?? {}
|
|
667
|
+
return (
|
|
668
|
+
<div className="mx-auto w-full max-w-7xl px-4 pt-10 sm:px-6 lg:px-8">
|
|
669
|
+
<AccountFrameBlock
|
|
670
|
+
displayName={displayName}
|
|
671
|
+
{...(customer.email ? { email: customer.email } : {})}
|
|
672
|
+
modules={modules}
|
|
673
|
+
{...(activeModule ? { activeModule } : {})}
|
|
674
|
+
presentation={presentation}
|
|
675
|
+
labels={labels}
|
|
676
|
+
onSelectModule={(id) => set('/accountTab', id)}
|
|
677
|
+
/>
|
|
678
|
+
</div>
|
|
679
|
+
)
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
function AccountProfilePanel({ props }: PanelProps) {
|
|
683
|
+
const { state, set } = useStateStore()
|
|
684
|
+
const parsed = accountProfilePropsSchema.parse(props)
|
|
685
|
+
const customer = (state.accountCustomer ?? null) as Customer | null
|
|
686
|
+
const visible = useModuleVisible('profile')
|
|
687
|
+
if (state.accountProfileAvailable !== true || !visible || !customer) return null
|
|
688
|
+
|
|
689
|
+
const pending = Boolean(state.accountProfilePending)
|
|
690
|
+
const error = typeof state.accountProfileError === 'string' ? state.accountProfileError : null
|
|
691
|
+
const emailEditable = state.accountEmailEditable === true
|
|
692
|
+
|
|
693
|
+
return (
|
|
694
|
+
<div className="mx-auto w-full max-w-7xl px-4 py-8 sm:px-6 lg:px-8" id="account-profile">
|
|
695
|
+
<ProfileFormBlock
|
|
696
|
+
fields={buildAccountProfileFields(customer, emailEditable)}
|
|
697
|
+
labels={parsed.labels}
|
|
698
|
+
pending={pending}
|
|
699
|
+
error={error}
|
|
700
|
+
onSubmit={(values) => {
|
|
701
|
+
set('/accountProfilePending', true)
|
|
702
|
+
set('/accountProfileError', null)
|
|
703
|
+
void updateAccountProfileAction({
|
|
704
|
+
firstName: values.firstName,
|
|
705
|
+
lastName: values.lastName,
|
|
706
|
+
phone: values.phone,
|
|
707
|
+
...(emailEditable && values.email !== undefined ? { email: values.email } : {}),
|
|
708
|
+
}).then((result) => {
|
|
709
|
+
set('/accountProfilePending', false)
|
|
710
|
+
if (result.ok) {
|
|
711
|
+
set('/accountCustomer', { ...customer, ...result.customer })
|
|
712
|
+
return
|
|
713
|
+
}
|
|
714
|
+
set('/accountProfileError', result.error)
|
|
715
|
+
})
|
|
716
|
+
}}
|
|
717
|
+
/>
|
|
718
|
+
</div>
|
|
719
|
+
)
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function toAddressEntry(address: AccountAddressView): {
|
|
723
|
+
id: string
|
|
724
|
+
recipientName: string
|
|
725
|
+
lines: string[]
|
|
726
|
+
isDefaultShipping?: boolean
|
|
727
|
+
isDefaultBilling?: boolean
|
|
728
|
+
} {
|
|
729
|
+
const lines = [
|
|
730
|
+
address.address1,
|
|
731
|
+
address.address2,
|
|
732
|
+
[address.city, address.province, address.postalCode].filter(Boolean).join(', '),
|
|
733
|
+
address.countryCode,
|
|
734
|
+
].filter((line): line is string => typeof line === 'string' && line.length > 0)
|
|
735
|
+
return {
|
|
736
|
+
id: address.id,
|
|
737
|
+
recipientName: [address.firstName, address.lastName].filter(Boolean).join(' '),
|
|
738
|
+
lines,
|
|
739
|
+
...(address.isDefaultShipping ? { isDefaultShipping: true } : {}),
|
|
740
|
+
...(address.isDefaultBilling ? { isDefaultBilling: true } : {}),
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
function AccountAddressesPanel({ props }: PanelProps) {
|
|
745
|
+
const { state, set } = useStateStore()
|
|
746
|
+
const parsed = accountAddressPropsSchema.parse(props)
|
|
747
|
+
const { identityKey } = useAccountLifecycle()
|
|
748
|
+
const visible = useModuleVisible('addresses')
|
|
749
|
+
const available = state.accountAddressesAvailable === true
|
|
750
|
+
const loadedKeyRef = useRef<string | null>(null)
|
|
751
|
+
|
|
752
|
+
const addresses = Array.isArray(state.accountAddresses)
|
|
753
|
+
? (state.accountAddresses as AccountAddressView[])
|
|
754
|
+
: []
|
|
755
|
+
const pending = Boolean(state.accountAddressesPending)
|
|
756
|
+
const error = typeof state.accountAddressesError === 'string' ? state.accountAddressesError : null
|
|
757
|
+
const formOpen = Boolean(state.accountAddressFormOpen)
|
|
758
|
+
const editingId = typeof state.accountAddressEditingId === 'string' ? state.accountAddressEditingId : null
|
|
759
|
+
const mutationPending = Boolean(state.accountAddressMutationPending)
|
|
760
|
+
const mutationError =
|
|
761
|
+
typeof state.accountAddressMutationError === 'string' ? state.accountAddressMutationError : null
|
|
762
|
+
const fieldErrors =
|
|
763
|
+
state.accountAddressFieldErrors && typeof state.accountAddressFieldErrors === 'object'
|
|
764
|
+
? (state.accountAddressFieldErrors as Record<string, string>)
|
|
765
|
+
: {}
|
|
416
766
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
// not re-run and the rows/selection survive. A guest/unavailable identity
|
|
426
|
-
// never fetches.
|
|
427
|
-
if (loadedKeyRef.current !== identityKey) {
|
|
428
|
-
generationRef.current += 1
|
|
429
|
-
loadedKeyRef.current = null
|
|
767
|
+
const load = useCallback(() => {
|
|
768
|
+
set('/accountAddressesPending', true)
|
|
769
|
+
set('/accountAddressesError', null)
|
|
770
|
+
void loadAccountAddressesAction().then((result) => {
|
|
771
|
+
set('/accountAddressesPending', false)
|
|
772
|
+
if (result.ok) {
|
|
773
|
+
set('/accountAddresses', result.addresses)
|
|
774
|
+
return
|
|
430
775
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
const count =
|
|
446
|
-
typeof state.accountOrdersCount === 'number' ? state.accountOrdersCount : orders.length
|
|
447
|
-
const page = typeof state.accountOrdersPage === 'number' ? state.accountOrdersPage : 1
|
|
448
|
-
const pageSize =
|
|
449
|
-
typeof state.accountOrdersPageSize === 'number' && state.accountOrdersPageSize > 0
|
|
450
|
-
? state.accountOrdersPageSize
|
|
451
|
-
: ORDERS_PAGE_SIZE
|
|
452
|
-
const selectedId =
|
|
453
|
-
typeof state.accountSelectedOrderId === 'string' ? state.accountSelectedOrderId : null
|
|
454
|
-
// Selection is keyed by the REAL backend order id, never the display number.
|
|
455
|
-
const selected = selectedId ? orders.find((order) => order.id === selectedId) ?? null : null
|
|
456
|
-
// The canonical block's labels, with the legacy per-key copy (still present in
|
|
457
|
-
// user-owned `content.ts` of an older store) folded in front of them.
|
|
458
|
-
const labels: Partial<OrderHistoryLabels> = {
|
|
459
|
-
...(parsed.labels ?? {}),
|
|
460
|
-
...(parsed.titleLabel === undefined ? {} : { title: parsed.titleLabel }),
|
|
461
|
-
...(parsed.descriptionLabel === undefined ? {} : { description: parsed.descriptionLabel }),
|
|
462
|
-
...(parsed.emptyLabel === undefined ? {} : { emptyLabel: parsed.emptyLabel }),
|
|
463
|
-
}
|
|
776
|
+
set('/accountAddressesError', result.error)
|
|
777
|
+
})
|
|
778
|
+
}, [set])
|
|
779
|
+
|
|
780
|
+
useEffect(() => {
|
|
781
|
+
if (!available || !visible) return
|
|
782
|
+
if (loadedKeyRef.current === identityKey) return
|
|
783
|
+
// A new identity reloads its own address book; the previous identity's rows
|
|
784
|
+
// were already cleared by the account claim write path.
|
|
785
|
+
loadedKeyRef.current = identityKey
|
|
786
|
+
load()
|
|
787
|
+
}, [available, visible, identityKey, load])
|
|
788
|
+
|
|
789
|
+
if (!available || !visible) return null
|
|
464
790
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
791
|
+
const editing = editingId ? addresses.find((address) => address.id === editingId) ?? null : null
|
|
792
|
+
const labels = (parsed.labels ?? {}) as Partial<AddressBookLabels>
|
|
793
|
+
const closeForm = () => {
|
|
794
|
+
set('/accountAddressFormOpen', false)
|
|
795
|
+
set('/accountAddressEditingId', null)
|
|
796
|
+
set('/accountAddressMutationError', null)
|
|
797
|
+
set('/accountAddressFieldErrors', {})
|
|
798
|
+
}
|
|
799
|
+
const applyResult = (result: { ok: true; addresses: AccountAddressView[] } | { ok: false; error: string; fieldErrors?: Record<string, string> }) => {
|
|
800
|
+
set('/accountAddressMutationPending', false)
|
|
801
|
+
if (result.ok) {
|
|
802
|
+
set('/accountAddresses', result.addresses)
|
|
803
|
+
closeForm()
|
|
804
|
+
return
|
|
472
805
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
806
|
+
set('/accountAddressMutationError', result.error)
|
|
807
|
+
set('/accountAddressFieldErrors', result.fieldErrors ?? {})
|
|
808
|
+
}
|
|
809
|
+
const submitForm = (values: Record<string, string>) => {
|
|
810
|
+
set('/accountAddressMutationPending', true)
|
|
811
|
+
set('/accountAddressMutationError', null)
|
|
812
|
+
set('/accountAddressFieldErrors', {})
|
|
813
|
+
const input = {
|
|
814
|
+
firstName: values.firstName ?? '',
|
|
815
|
+
lastName: values.lastName ?? '',
|
|
816
|
+
address1: values.address1 ?? '',
|
|
817
|
+
address2: values.address2,
|
|
818
|
+
city: values.city ?? '',
|
|
819
|
+
province: values.province,
|
|
820
|
+
postalCode: values.postalCode,
|
|
821
|
+
countryCode: values.countryCode ?? '',
|
|
822
|
+
phone: values.phone,
|
|
823
|
+
label: values.label,
|
|
483
824
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
825
|
+
const request = editingId
|
|
826
|
+
? updateAccountAddressAction({ ...input, id: editingId })
|
|
827
|
+
: createAccountAddressAction(input)
|
|
828
|
+
void request.then(applyResult)
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
return (
|
|
832
|
+
<div className="mx-auto w-full max-w-7xl px-4 pb-12 sm:px-6 lg:px-8" id="account-addresses">
|
|
833
|
+
{pending ? (
|
|
834
|
+
<p role="status" aria-live="polite" className="text-muted-foreground py-8 text-sm">
|
|
835
|
+
{accountContent.addresses.loadingLabel}
|
|
836
|
+
</p>
|
|
837
|
+
) : error ? (
|
|
838
|
+
<div className="flex flex-col items-start gap-3 py-8">
|
|
491
839
|
<p role="alert" className="text-destructive text-sm">
|
|
492
840
|
{error}
|
|
493
841
|
</p>
|
|
494
|
-
<Button
|
|
495
|
-
|
|
496
|
-
variant="outline"
|
|
497
|
-
size="sm"
|
|
498
|
-
disabled={pending}
|
|
499
|
-
onClick={() => loadPage(requestedPageRef.current)}
|
|
500
|
-
>
|
|
501
|
-
Reintentar
|
|
842
|
+
<Button type="button" variant="outline" size="sm" onClick={load}>
|
|
843
|
+
{accountContent.addresses.retryLabel}
|
|
502
844
|
</Button>
|
|
503
845
|
</div>
|
|
504
|
-
)
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
there is one empty implementation and no fabricated order. Selection
|
|
511
|
-
is reported by backend order id — no number→id map. */}
|
|
512
|
-
<OrderHistory
|
|
513
|
-
orders={orders.map(toOrderHistoryOrder)}
|
|
514
|
-
presentation="cards"
|
|
846
|
+
) : formOpen ? (
|
|
847
|
+
<AddressFormBlock
|
|
848
|
+
fields={accountContent.addressFields.map((field) => ({
|
|
849
|
+
...field,
|
|
850
|
+
value: editing ? (editing as unknown as Record<string, string>)[field.name] ?? '' : '',
|
|
851
|
+
}))}
|
|
515
852
|
labels={labels}
|
|
516
|
-
|
|
853
|
+
pending={mutationPending}
|
|
854
|
+
error={mutationError}
|
|
855
|
+
fieldErrors={fieldErrors}
|
|
856
|
+
onSubmit={submitForm}
|
|
857
|
+
onCancel={closeForm}
|
|
517
858
|
/>
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
859
|
+
) : (
|
|
860
|
+
<AddressBookBlock
|
|
861
|
+
addresses={addresses.map(toAddressEntry)}
|
|
862
|
+
labels={labels}
|
|
863
|
+
onCreate={() => {
|
|
864
|
+
set('/accountAddressEditingId', null)
|
|
865
|
+
set('/accountAddressFormOpen', true)
|
|
866
|
+
}}
|
|
867
|
+
onEdit={(id) => {
|
|
868
|
+
set('/accountAddressEditingId', id)
|
|
869
|
+
set('/accountAddressFormOpen', true)
|
|
870
|
+
}}
|
|
871
|
+
onDelete={(id) => {
|
|
872
|
+
set('/accountAddressMutationPending', true)
|
|
873
|
+
void deleteAccountAddressAction({ id }).then(applyResult)
|
|
874
|
+
}}
|
|
875
|
+
onSetDefaultShipping={(id) => {
|
|
876
|
+
set('/accountAddressMutationPending', true)
|
|
877
|
+
void setDefaultAccountAddressAction({ id, kind: 'shipping' }).then(applyResult)
|
|
878
|
+
}}
|
|
879
|
+
onSetDefaultBilling={(id) => {
|
|
880
|
+
set('/accountAddressMutationPending', true)
|
|
881
|
+
void setDefaultAccountAddressAction({ id, kind: 'billing' }).then(applyResult)
|
|
882
|
+
}}
|
|
883
|
+
/>
|
|
884
|
+
)}
|
|
885
|
+
</div>
|
|
886
|
+
)
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
function AccountOrdersPanel({ props }: PanelProps) {
|
|
890
|
+
const router = useRouter()
|
|
891
|
+
const { state, set, getSnapshot } = useStateStore()
|
|
892
|
+
const parsed = accountOrdersPropsSchema.parse(props)
|
|
893
|
+
const { identityKey, reconciling } = useAccountLifecycle()
|
|
894
|
+
const visible = useModuleVisible('orders')
|
|
895
|
+
|
|
896
|
+
const generationRef = useRef(0)
|
|
897
|
+
const loadedKeyRef = useRef<string | null>(null)
|
|
898
|
+
const requestedPageRef = useRef(1)
|
|
899
|
+
|
|
900
|
+
const currentIdentityKey = useCallback(
|
|
901
|
+
() =>
|
|
902
|
+
accountIdentityKey(
|
|
903
|
+
(getSnapshot().accountCustomer ?? null) as { id?: string | null } | null,
|
|
904
|
+
),
|
|
905
|
+
[getSnapshot],
|
|
906
|
+
)
|
|
907
|
+
|
|
908
|
+
const loadPage = (page: number) => {
|
|
909
|
+
const capturedKey = currentIdentityKey()
|
|
910
|
+
const capturedGeneration = generationRef.current
|
|
911
|
+
requestedPageRef.current = page
|
|
912
|
+
set('/accountOrdersPending', true)
|
|
913
|
+
set('/accountOrdersError', null)
|
|
914
|
+
void loadOrdersPageAction({ page, pageSize: ORDERS_PAGE_SIZE })
|
|
915
|
+
.then((result) => {
|
|
916
|
+
if (
|
|
917
|
+
currentIdentityKey() !== capturedKey ||
|
|
918
|
+
generationRef.current !== capturedGeneration
|
|
919
|
+
) {
|
|
920
|
+
return
|
|
921
|
+
}
|
|
922
|
+
set('/accountOrdersPending', false)
|
|
923
|
+
if (result.ok) {
|
|
924
|
+
set('/accountOrders', result.orders)
|
|
925
|
+
set('/accountOrdersCount', result.count)
|
|
926
|
+
set('/accountOrdersPage', page)
|
|
927
|
+
set('/accountOrdersPageSize', result.limit)
|
|
928
|
+
set('/accountOrdersError', null)
|
|
929
|
+
loadedKeyRef.current = capturedKey
|
|
930
|
+
return
|
|
931
|
+
}
|
|
932
|
+
set('/accountOrdersError', result.error)
|
|
933
|
+
})
|
|
934
|
+
.catch((error) => {
|
|
935
|
+
if (
|
|
936
|
+
currentIdentityKey() !== capturedKey ||
|
|
937
|
+
generationRef.current !== capturedGeneration
|
|
938
|
+
) {
|
|
939
|
+
return
|
|
940
|
+
}
|
|
941
|
+
console.error(
|
|
942
|
+
'[storefront/account] loadOrders failed',
|
|
943
|
+
error instanceof Error ? error.message : error,
|
|
944
|
+
)
|
|
945
|
+
set('/accountOrdersPending', false)
|
|
946
|
+
set('/accountOrdersError', parsed.errorLabel ?? 'No se pudieron cargar tus pedidos.')
|
|
947
|
+
})
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
useEffect(() => {
|
|
951
|
+
if (loadedKeyRef.current !== identityKey) {
|
|
952
|
+
generationRef.current += 1
|
|
953
|
+
loadedKeyRef.current = null
|
|
954
|
+
}
|
|
955
|
+
if (identityKey === 'anonymous') return
|
|
956
|
+
if (loadedKeyRef.current === identityKey) return
|
|
957
|
+
loadPage(1)
|
|
958
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
959
|
+
}, [identityKey])
|
|
960
|
+
|
|
961
|
+
if (!visible) return null
|
|
962
|
+
|
|
963
|
+
const pending = Boolean(state.accountOrdersPending)
|
|
964
|
+
const error = typeof state.accountOrdersError === 'string' ? state.accountOrdersError : null
|
|
965
|
+
const allOrders = Array.isArray(state.accountOrders)
|
|
966
|
+
? (state.accountOrders as AccountOrderView[])
|
|
967
|
+
: []
|
|
968
|
+
const orders = loadedKeyRef.current === identityKey ? allOrders : []
|
|
969
|
+
const count = typeof state.accountOrdersCount === 'number' ? state.accountOrdersCount : orders.length
|
|
970
|
+
const page = typeof state.accountOrdersPage === 'number' ? state.accountOrdersPage : 1
|
|
971
|
+
const pageSize =
|
|
972
|
+
typeof state.accountOrdersPageSize === 'number' && state.accountOrdersPageSize > 0
|
|
973
|
+
? state.accountOrdersPageSize
|
|
974
|
+
: ORDERS_PAGE_SIZE
|
|
975
|
+
const labels: Partial<OrderHistoryLabels> = {
|
|
976
|
+
...(parsed.labels ?? {}),
|
|
977
|
+
...(parsed.titleLabel === undefined ? {} : { title: parsed.titleLabel }),
|
|
978
|
+
...(parsed.descriptionLabel === undefined ? {} : { description: parsed.descriptionLabel }),
|
|
979
|
+
...(parsed.emptyLabel === undefined ? {} : { emptyLabel: parsed.emptyLabel }),
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
if (reconciling) return <ReconcilingStatus />
|
|
983
|
+
if (pending) {
|
|
984
|
+
return (
|
|
985
|
+
<p
|
|
986
|
+
role="status"
|
|
987
|
+
aria-live="polite"
|
|
988
|
+
className="text-muted-foreground mx-auto max-w-7xl px-4 py-8 text-sm"
|
|
989
|
+
>
|
|
990
|
+
{parsed.loadingLabel ?? 'Cargando pedidos…'}
|
|
991
|
+
</p>
|
|
547
992
|
)
|
|
548
|
-
}
|
|
993
|
+
}
|
|
994
|
+
if (error) {
|
|
995
|
+
return (
|
|
996
|
+
<div className="mx-auto flex w-full max-w-7xl flex-col items-start gap-3 px-4 py-8 sm:px-6 lg:px-8">
|
|
997
|
+
<p role="alert" className="text-destructive text-sm">
|
|
998
|
+
{error}
|
|
999
|
+
</p>
|
|
1000
|
+
<Button
|
|
1001
|
+
type="button"
|
|
1002
|
+
variant="outline"
|
|
1003
|
+
size="sm"
|
|
1004
|
+
disabled={pending}
|
|
1005
|
+
onClick={() => loadPage(requestedPageRef.current)}
|
|
1006
|
+
>
|
|
1007
|
+
Reintentar
|
|
1008
|
+
</Button>
|
|
1009
|
+
</div>
|
|
1010
|
+
)
|
|
1011
|
+
}
|
|
1012
|
+
const totalPages = Math.max(1, Math.ceil(count / pageSize))
|
|
1013
|
+
return (
|
|
1014
|
+
<div id="account-orders">
|
|
1015
|
+
<OrderHistoryBlock
|
|
1016
|
+
orders={orders.map(toOrderHistoryOrder)}
|
|
1017
|
+
presentation="cards"
|
|
1018
|
+
labels={labels}
|
|
1019
|
+
onViewOrder={(id) => router.push(`/account/orders/${encodeURIComponent(id)}`)}
|
|
1020
|
+
/>
|
|
1021
|
+
{count > pageSize ? (
|
|
1022
|
+
<div className="mx-auto flex w-full max-w-7xl items-center justify-between gap-4 px-4 pb-6 sm:px-6 lg:px-8">
|
|
1023
|
+
<p className="text-muted-foreground text-sm" role="status" aria-live="polite">
|
|
1024
|
+
{`${count} pedidos · página ${page} de ${totalPages}`}
|
|
1025
|
+
</p>
|
|
1026
|
+
<div className="flex gap-2">
|
|
1027
|
+
<Button
|
|
1028
|
+
variant="outline"
|
|
1029
|
+
size="sm"
|
|
1030
|
+
disabled={pending || page <= 1}
|
|
1031
|
+
onClick={() => loadPage(page - 1)}
|
|
1032
|
+
>
|
|
1033
|
+
Anterior
|
|
1034
|
+
</Button>
|
|
1035
|
+
<Button
|
|
1036
|
+
variant="outline"
|
|
1037
|
+
size="sm"
|
|
1038
|
+
disabled={pending || page >= totalPages}
|
|
1039
|
+
onClick={() => loadPage(page + 1)}
|
|
1040
|
+
>
|
|
1041
|
+
Siguiente
|
|
1042
|
+
</Button>
|
|
1043
|
+
</div>
|
|
1044
|
+
</div>
|
|
1045
|
+
) : null}
|
|
1046
|
+
</div>
|
|
1047
|
+
)
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
export const registry: ViewRegistry = {
|
|
1051
|
+
AccountBoot: ({ emit }) => <AccountBoundary emit={emit} />,
|
|
1052
|
+
AccountOrderDetail: (ctx) => <AccountOrderDetailPanel {...ctx} />,
|
|
1053
|
+
AccountFulfillmentEvidence: (ctx) => <AccountFulfillmentEvidencePanel {...ctx} />,
|
|
1054
|
+
AccountShell: (ctx) => <AccountShellPanel {...ctx} />,
|
|
1055
|
+
AccountOverview: () => <AccountOverviewPanel />,
|
|
1056
|
+
AccountModulePlaceholder: () => <AccountModulePlaceholderPanel />,
|
|
1057
|
+
AccountOrders: (ctx) => <AccountOrdersPanel {...ctx} />,
|
|
1058
|
+
AccountLogin: (ctx) => <AccountLoginPanel {...ctx} />,
|
|
1059
|
+
AccountRegister: (ctx) => <AccountRegisterPanel {...ctx} />,
|
|
1060
|
+
AccountFrame: (ctx) => <AccountFramePanel {...ctx} />,
|
|
1061
|
+
AccountOrdersAdvanced: (ctx) => <AccountOrdersAdvancedPanel {...ctx} />,
|
|
1062
|
+
CustomerProfile: (ctx) => <customerProfileRegistry.CustomerProfile {...ctx} />,
|
|
1063
|
+
AddressBookAdvanced: (ctx) => <addressBookAdvancedRegistry.AddressBookAdvanced {...ctx} />,
|
|
549
1064
|
}
|