@thorprovider/storefront 0.7.0 → 0.9.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 +621 -80
- package/assets/templates/app/lib/__STOREFRONT__/addresses-port.ts +107 -0
- package/assets/templates/app/lib/__STOREFRONT__/commerce.ts +346 -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__/order-detail-port.ts +26 -0
- package/assets/templates/app/lib/__STOREFRONT__/orders-port.ts +30 -0
- package/assets/templates/app/lib/__STOREFRONT__/ports.ts +18 -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 +41 -0
- package/assets/templates/views/account/assembly/index.skeleton.tsx +229 -0
- package/assets/templates/views/account/assembly/registry.skeleton.tsx +1116 -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/boot/AccountBoot/catalog.part +5 -0
- package/assets/templates/views/account/assembly/slots/boot/AccountBoot/element.part +5 -0
- package/assets/templates/views/account/assembly/slots/boot/AccountBoot/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/index.tsx +218 -35
- 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 +166 -0
- package/assets/templates/views/account/orders-advanced.defaults.ts +230 -0
- package/assets/templates/views/account/registry.tsx +944 -369
- 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 +22 -1
- package/assets/templates/views/home/assembly/index.skeleton.tsx +1 -0
- package/assets/templates/views/home/content.ts +2 -0
- package/assets/templates/views/home/index.tsx +1 -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 +343 -13
- package/package.json +2 -1
- package/recipes/adapter-surface.json +158 -0
- package/recipes/archetypes.json +38 -3
- package/recipes/catalog.json +138 -1
- 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 +34 -5
- 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 +5 -4
- package/scripts/composers.mjs +7 -2
- package/scripts/default-sections.mjs +78 -0
- package/scripts/file-classes.mjs +14 -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 +38 -3
- package/skills/sf-view-gen/assets/recipes/catalog.json +138 -1
- package/skills/sf-view-gen/assets/recipes/features.json +72 -1
- package/skills/sf-view-gen/assets/recipes/recipes.json +34 -5
- 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
|
+
formatMoney,
|
|
36
|
+
} from '@thorprovider/blocks'
|
|
18
37
|
import type {
|
|
38
|
+
AddressBookLabels,
|
|
39
|
+
CustomerAccountOverviewProps,
|
|
40
|
+
CustomerAccountPlaceholderProps,
|
|
41
|
+
CustomerAccountV1Props,
|
|
19
42
|
OrderHistoryLabels,
|
|
20
43
|
OrderHistoryOrder,
|
|
21
44
|
OrderHistoryStatusTone,
|
|
22
45
|
} from '@thorprovider/blocks'
|
|
23
|
-
import
|
|
46
|
+
import { addressBookAdvancedRegistry as addressBookAdvancedRegistry } from '../sections/address-book-advanced.client'
|
|
47
|
+
import { customerProfileRegistry as customerProfileRegistry } from '../sections/customer-profile.client'
|
|
48
|
+
import { AccountOrdersAdvancedPanel as AccountOrdersAdvancedPanel } from './orders-advanced.client'
|
|
24
49
|
import { useStateStore } from '@json-render/react'
|
|
25
50
|
import type { Customer } from '@thorprovider/types'
|
|
26
|
-
import {
|
|
27
|
-
|
|
51
|
+
import {
|
|
52
|
+
createAccountAddressAction,
|
|
53
|
+
deleteAccountAddressAction,
|
|
54
|
+
loadAccountAddressesAction,
|
|
55
|
+
loadOrdersPageAction,
|
|
56
|
+
setDefaultAccountAddressAction,
|
|
57
|
+
updateAccountAddressAction,
|
|
58
|
+
updateAccountProfileAction,
|
|
59
|
+
} from '../../actions'
|
|
60
|
+
import { ACCOUNT_ORDERS_DEFAULT_PAGE_SIZE, accountIdentityKey, buildAccountProfileFields, isAccountModuleVisible } from '../../account-private.core'
|
|
28
61
|
import { AccountBoundary } from '../../account-boundary.client'
|
|
29
62
|
import { useAccountLifecycle } from '../../account-lifecycle.client'
|
|
30
|
-
import type { AccountOrderView } from '../../state.core'
|
|
63
|
+
import type { AccountAddressView, AccountOrderView } from '../../state.core'
|
|
31
64
|
import type { ViewRegistry } from '../types'
|
|
65
|
+
import { accountContent } from './content'
|
|
66
|
+
import { AccountOrderDetailPanel } from './order-detail.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,14 +566,7 @@ function toOrderHistoryOrder(order: AccountOrderView): OrderHistoryOrder {
|
|
|
130
566
|
}
|
|
131
567
|
}
|
|
132
568
|
|
|
133
|
-
// Inline order detail assembled from the already-fetched list row (no second
|
|
134
|
-
// backend request). Only fields the backend supplied are shown. `onClose` clears
|
|
135
|
-
// the selection so the detail is dismissible (a real open/close cycle, no
|
|
136
|
-
// navigation and no second destination).
|
|
137
569
|
function OrderDetail({ order, onClose }: { order: AccountOrderView; onClose: () => void }) {
|
|
138
|
-
// Amounts are formatted at the display site from their own currency; an
|
|
139
|
-
// amount the backend did not supply (or supplied malformed) renders the
|
|
140
|
-
// unavailable marker, never a fabricated zero.
|
|
141
570
|
const amount = (value: AccountOrderView['total']) => formatMoney(value) ?? '—'
|
|
142
571
|
const totals = (
|
|
143
572
|
[
|
|
@@ -193,357 +622,503 @@ function OrderDetail({ order, onClose }: { order: AccountOrderView; onClose: ()
|
|
|
193
622
|
)
|
|
194
623
|
}
|
|
195
624
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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
|
-
)
|
|
625
|
+
function AccountLoginPanel({ props, emit }: PanelProps) {
|
|
626
|
+
const { state } = useStateStore()
|
|
627
|
+
const lifecycle = useAccountLifecycle()
|
|
628
|
+
const parsed = accountLoginPropsSchema.parse(props)
|
|
629
|
+
const error = typeof state.accountError === 'string' ? state.accountError : null
|
|
630
|
+
const pending = Boolean(state.accountPending)
|
|
631
|
+
return (
|
|
632
|
+
<>
|
|
633
|
+
<AuthNotice pending={pending} error={error} />
|
|
634
|
+
<AuthCanvas>
|
|
635
|
+
<fieldset disabled={pending} aria-busy={pending} className="contents">
|
|
636
|
+
<LoginPageBlock
|
|
637
|
+
presentation="card"
|
|
638
|
+
logoText={parsed.logoText}
|
|
639
|
+
welcomeTitleLabel={parsed.welcomeTitleLabel}
|
|
640
|
+
welcomeDescriptionLabel={parsed.welcomeDescriptionLabel}
|
|
641
|
+
emailLabel={parsed.emailLabel}
|
|
642
|
+
passwordLabel={parsed.passwordLabel}
|
|
643
|
+
signInLabel={parsed.signInLabel}
|
|
644
|
+
createAccountLabel={parsed.createAccountLabel}
|
|
645
|
+
rememberMeLabel="Recuérdame"
|
|
646
|
+
onSubmit={(data) => {
|
|
647
|
+
void lifecycle.login({ email: data.email, password: data.password })
|
|
648
|
+
}}
|
|
649
|
+
onCreateAccount={() => emit('showRegister')}
|
|
650
|
+
/>
|
|
651
|
+
</fieldset>
|
|
652
|
+
</AuthCanvas>
|
|
653
|
+
</>
|
|
654
|
+
)
|
|
655
|
+
}
|
|
372
656
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
657
|
+
function AccountRegisterPanel({ props, emit }: PanelProps) {
|
|
658
|
+
const { state } = useStateStore()
|
|
659
|
+
const lifecycle = useAccountLifecycle()
|
|
660
|
+
const parsed = accountRegisterPropsSchema.parse(props)
|
|
661
|
+
const error = typeof state.accountError === 'string' ? state.accountError : null
|
|
662
|
+
const pending = Boolean(state.accountPending)
|
|
663
|
+
return (
|
|
664
|
+
<>
|
|
665
|
+
<AuthNotice pending={pending} error={error} />
|
|
666
|
+
<AuthCanvas stack>
|
|
667
|
+
<fieldset disabled={pending} aria-busy={pending} className="contents">
|
|
668
|
+
<SignUpBlock
|
|
669
|
+
logoText={parsed.logoText}
|
|
670
|
+
createAccountTitleLabel={parsed.createAccountTitleLabel}
|
|
671
|
+
createAccountDescriptionLabel={parsed.createAccountDescriptionLabel}
|
|
672
|
+
fullNameLabel={parsed.fullNameLabel}
|
|
673
|
+
emailLabel={parsed.emailLabel}
|
|
674
|
+
passwordLabel={parsed.passwordLabel}
|
|
675
|
+
createAccountLabel={parsed.createAccountLabel}
|
|
676
|
+
signInLabel={parsed.signInLabel}
|
|
677
|
+
onSubmit={(data) => {
|
|
678
|
+
const parts = data.fullName.trim().split(/\s+/).filter(Boolean)
|
|
679
|
+
void lifecycle.register({
|
|
680
|
+
email: data.email,
|
|
681
|
+
password: data.password,
|
|
682
|
+
firstName: parts[0] ?? '',
|
|
683
|
+
lastName: parts.slice(1).join(' '),
|
|
684
|
+
})
|
|
685
|
+
}}
|
|
686
|
+
onSignIn={() => emit('showLogin')}
|
|
687
|
+
/>
|
|
688
|
+
</fieldset>
|
|
689
|
+
</AuthCanvas>
|
|
690
|
+
</>
|
|
691
|
+
)
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* The account frame: the signed-in identity and the module navigation. It is
|
|
696
|
+
* rendered only for an authenticated shopper (the element visibility gate) and
|
|
697
|
+
* it seeds the shape/tab state the module panels read.
|
|
698
|
+
*/
|
|
699
|
+
function AccountFramePanel({ props }: PanelProps) {
|
|
700
|
+
const { state, set } = useStateStore()
|
|
701
|
+
const parsed = accountFramePropsSchema.parse(props)
|
|
702
|
+
const presentation = parsed.presentation ?? 'stack'
|
|
703
|
+
const customer = (state.accountCustomer ?? null) as Customer | null
|
|
704
|
+
const modules = installedModules(state)
|
|
705
|
+
const activeModule = typeof state.accountTab === 'string' ? state.accountTab : undefined
|
|
416
706
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
// reset already happened even when this panel was unmounted (logout /
|
|
423
|
-
// expiry / unavailable). This effect only drives the fetch; a
|
|
424
|
-
// same-identity re-resolution leaves `identityKey` unchanged, so it does
|
|
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
|
|
707
|
+
useEffect(() => {
|
|
708
|
+
if (presentation === 'tabs') {
|
|
709
|
+
set('/accountShape', 'tabs')
|
|
710
|
+
if (!modules.some((module) => module.id === activeModule)) {
|
|
711
|
+
set('/accountTab', modules[0]?.id ?? 'orders')
|
|
430
712
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
435
|
-
}, [identityKey])
|
|
436
|
-
|
|
437
|
-
const pending = Boolean(state.accountOrdersPending)
|
|
438
|
-
const error = typeof state.accountOrdersError === 'string' ? state.accountOrdersError : null
|
|
439
|
-
const allOrders = Array.isArray(state.accountOrders)
|
|
440
|
-
? (state.accountOrders as AccountOrderView[])
|
|
441
|
-
: []
|
|
442
|
-
// Only rows loaded FOR the current identity may display: a stale row set from
|
|
443
|
-
// a previous customer is hidden until the new identity's page lands.
|
|
444
|
-
const orders = loadedKeyRef.current === identityKey ? allOrders : []
|
|
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 }),
|
|
713
|
+
} else {
|
|
714
|
+
set('/accountShape', 'stack')
|
|
715
|
+
set('/accountTab', null)
|
|
463
716
|
}
|
|
717
|
+
}, [presentation, modules.length, activeModule, set])
|
|
464
718
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
719
|
+
if (!state.accountAuthenticated || !customer) return null
|
|
720
|
+
const displayName =
|
|
721
|
+
[customer.firstName, customer.lastName].filter(Boolean).join(' ') || customer.email
|
|
722
|
+
const labels = parsed.labels ?? {}
|
|
723
|
+
return (
|
|
724
|
+
<div className="mx-auto w-full max-w-7xl px-4 pt-10 sm:px-6 lg:px-8">
|
|
725
|
+
<AccountFrameBlock
|
|
726
|
+
displayName={displayName}
|
|
727
|
+
{...(customer.email ? { email: customer.email } : {})}
|
|
728
|
+
modules={modules}
|
|
729
|
+
{...(activeModule ? { activeModule } : {})}
|
|
730
|
+
presentation={presentation}
|
|
731
|
+
labels={labels}
|
|
732
|
+
onSelectModule={(id) => set('/accountTab', id)}
|
|
733
|
+
/>
|
|
734
|
+
</div>
|
|
735
|
+
)
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
function AccountProfilePanel({ props }: PanelProps) {
|
|
739
|
+
const { state, set } = useStateStore()
|
|
740
|
+
const parsed = accountProfilePropsSchema.parse(props)
|
|
741
|
+
const customer = (state.accountCustomer ?? null) as Customer | null
|
|
742
|
+
const visible = useModuleVisible('profile')
|
|
743
|
+
if (state.accountProfileAvailable !== true || !visible || !customer) return null
|
|
744
|
+
|
|
745
|
+
const pending = Boolean(state.accountProfilePending)
|
|
746
|
+
const error = typeof state.accountProfileError === 'string' ? state.accountProfileError : null
|
|
747
|
+
const emailEditable = state.accountEmailEditable === true
|
|
748
|
+
|
|
749
|
+
return (
|
|
750
|
+
<div className="mx-auto w-full max-w-7xl px-4 py-8 sm:px-6 lg:px-8" id="account-profile">
|
|
751
|
+
<ProfileFormBlock
|
|
752
|
+
fields={buildAccountProfileFields(customer, emailEditable)}
|
|
753
|
+
labels={parsed.labels}
|
|
754
|
+
pending={pending}
|
|
755
|
+
error={error}
|
|
756
|
+
onSubmit={(values) => {
|
|
757
|
+
set('/accountProfilePending', true)
|
|
758
|
+
set('/accountProfileError', null)
|
|
759
|
+
void updateAccountProfileAction({
|
|
760
|
+
firstName: values.firstName,
|
|
761
|
+
lastName: values.lastName,
|
|
762
|
+
phone: values.phone,
|
|
763
|
+
...(emailEditable && values.email !== undefined ? { email: values.email } : {}),
|
|
764
|
+
}).then((result) => {
|
|
765
|
+
set('/accountProfilePending', false)
|
|
766
|
+
if (result.ok) {
|
|
767
|
+
set('/accountCustomer', { ...customer, ...result.customer })
|
|
768
|
+
return
|
|
769
|
+
}
|
|
770
|
+
set('/accountProfileError', result.error)
|
|
771
|
+
})
|
|
772
|
+
}}
|
|
773
|
+
/>
|
|
774
|
+
</div>
|
|
775
|
+
)
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
function toAddressEntry(address: AccountAddressView): {
|
|
779
|
+
id: string
|
|
780
|
+
recipientName: string
|
|
781
|
+
lines: string[]
|
|
782
|
+
isDefaultShipping?: boolean
|
|
783
|
+
isDefaultBilling?: boolean
|
|
784
|
+
} {
|
|
785
|
+
const lines = [
|
|
786
|
+
address.address1,
|
|
787
|
+
address.address2,
|
|
788
|
+
[address.city, address.province, address.postalCode].filter(Boolean).join(', '),
|
|
789
|
+
address.countryCode,
|
|
790
|
+
].filter((line): line is string => typeof line === 'string' && line.length > 0)
|
|
791
|
+
return {
|
|
792
|
+
id: address.id,
|
|
793
|
+
recipientName: [address.firstName, address.lastName].filter(Boolean).join(' '),
|
|
794
|
+
lines,
|
|
795
|
+
...(address.isDefaultShipping ? { isDefaultShipping: true } : {}),
|
|
796
|
+
...(address.isDefaultBilling ? { isDefaultBilling: true } : {}),
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
function AccountAddressesPanel({ props }: PanelProps) {
|
|
801
|
+
const { state, set } = useStateStore()
|
|
802
|
+
const parsed = accountAddressPropsSchema.parse(props)
|
|
803
|
+
const { identityKey } = useAccountLifecycle()
|
|
804
|
+
const visible = useModuleVisible('addresses')
|
|
805
|
+
const available = state.accountAddressesAvailable === true
|
|
806
|
+
const loadedKeyRef = useRef<string | null>(null)
|
|
807
|
+
|
|
808
|
+
const addresses = Array.isArray(state.accountAddresses)
|
|
809
|
+
? (state.accountAddresses as AccountAddressView[])
|
|
810
|
+
: []
|
|
811
|
+
const pending = Boolean(state.accountAddressesPending)
|
|
812
|
+
const error = typeof state.accountAddressesError === 'string' ? state.accountAddressesError : null
|
|
813
|
+
const formOpen = Boolean(state.accountAddressFormOpen)
|
|
814
|
+
const editingId = typeof state.accountAddressEditingId === 'string' ? state.accountAddressEditingId : null
|
|
815
|
+
const mutationPending = Boolean(state.accountAddressMutationPending)
|
|
816
|
+
const mutationError =
|
|
817
|
+
typeof state.accountAddressMutationError === 'string' ? state.accountAddressMutationError : null
|
|
818
|
+
const fieldErrors =
|
|
819
|
+
state.accountAddressFieldErrors && typeof state.accountAddressFieldErrors === 'object'
|
|
820
|
+
? (state.accountAddressFieldErrors as Record<string, string>)
|
|
821
|
+
: {}
|
|
822
|
+
|
|
823
|
+
const load = useCallback(() => {
|
|
824
|
+
set('/accountAddressesPending', true)
|
|
825
|
+
set('/accountAddressesError', null)
|
|
826
|
+
void loadAccountAddressesAction().then((result) => {
|
|
827
|
+
set('/accountAddressesPending', false)
|
|
828
|
+
if (result.ok) {
|
|
829
|
+
set('/accountAddresses', result.addresses)
|
|
830
|
+
return
|
|
831
|
+
}
|
|
832
|
+
set('/accountAddressesError', result.error)
|
|
833
|
+
})
|
|
834
|
+
}, [set])
|
|
835
|
+
|
|
836
|
+
useEffect(() => {
|
|
837
|
+
if (!available || !visible) return
|
|
838
|
+
if (loadedKeyRef.current === identityKey) return
|
|
839
|
+
// A new identity reloads its own address book; the previous identity's rows
|
|
840
|
+
// were already cleared by the account claim write path.
|
|
841
|
+
loadedKeyRef.current = identityKey
|
|
842
|
+
load()
|
|
843
|
+
}, [available, visible, identityKey, load])
|
|
844
|
+
|
|
845
|
+
if (!available || !visible) return null
|
|
846
|
+
|
|
847
|
+
const editing = editingId ? addresses.find((address) => address.id === editingId) ?? null : null
|
|
848
|
+
const labels = (parsed.labels ?? {}) as Partial<AddressBookLabels>
|
|
849
|
+
const closeForm = () => {
|
|
850
|
+
set('/accountAddressFormOpen', false)
|
|
851
|
+
set('/accountAddressEditingId', null)
|
|
852
|
+
set('/accountAddressMutationError', null)
|
|
853
|
+
set('/accountAddressFieldErrors', {})
|
|
854
|
+
}
|
|
855
|
+
const applyResult = (result: { ok: true; addresses: AccountAddressView[] } | { ok: false; error: string; fieldErrors?: Record<string, string> }) => {
|
|
856
|
+
set('/accountAddressMutationPending', false)
|
|
857
|
+
if (result.ok) {
|
|
858
|
+
set('/accountAddresses', result.addresses)
|
|
859
|
+
closeForm()
|
|
860
|
+
return
|
|
472
861
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
862
|
+
set('/accountAddressMutationError', result.error)
|
|
863
|
+
set('/accountAddressFieldErrors', result.fieldErrors ?? {})
|
|
864
|
+
}
|
|
865
|
+
const submitForm = (values: Record<string, string>) => {
|
|
866
|
+
set('/accountAddressMutationPending', true)
|
|
867
|
+
set('/accountAddressMutationError', null)
|
|
868
|
+
set('/accountAddressFieldErrors', {})
|
|
869
|
+
const input = {
|
|
870
|
+
firstName: values.firstName ?? '',
|
|
871
|
+
lastName: values.lastName ?? '',
|
|
872
|
+
address1: values.address1 ?? '',
|
|
873
|
+
address2: values.address2,
|
|
874
|
+
city: values.city ?? '',
|
|
875
|
+
province: values.province,
|
|
876
|
+
postalCode: values.postalCode,
|
|
877
|
+
countryCode: values.countryCode ?? '',
|
|
878
|
+
phone: values.phone,
|
|
879
|
+
label: values.label,
|
|
483
880
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
881
|
+
const request = editingId
|
|
882
|
+
? updateAccountAddressAction({ ...input, id: editingId })
|
|
883
|
+
: createAccountAddressAction(input)
|
|
884
|
+
void request.then(applyResult)
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
return (
|
|
888
|
+
<div className="mx-auto w-full max-w-7xl px-4 pb-12 sm:px-6 lg:px-8" id="account-addresses">
|
|
889
|
+
{pending ? (
|
|
890
|
+
<p role="status" aria-live="polite" className="text-muted-foreground py-8 text-sm">
|
|
891
|
+
{accountContent.addresses.loadingLabel}
|
|
892
|
+
</p>
|
|
893
|
+
) : error ? (
|
|
894
|
+
<div className="flex flex-col items-start gap-3 py-8">
|
|
491
895
|
<p role="alert" className="text-destructive text-sm">
|
|
492
896
|
{error}
|
|
493
897
|
</p>
|
|
494
|
-
<Button
|
|
495
|
-
|
|
496
|
-
variant="outline"
|
|
497
|
-
size="sm"
|
|
498
|
-
disabled={pending}
|
|
499
|
-
onClick={() => loadPage(requestedPageRef.current)}
|
|
500
|
-
>
|
|
501
|
-
Reintentar
|
|
898
|
+
<Button type="button" variant="outline" size="sm" onClick={load}>
|
|
899
|
+
{accountContent.addresses.retryLabel}
|
|
502
900
|
</Button>
|
|
503
901
|
</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"
|
|
902
|
+
) : formOpen ? (
|
|
903
|
+
<AddressFormBlock
|
|
904
|
+
fields={accountContent.addressFields.map((field) => ({
|
|
905
|
+
...field,
|
|
906
|
+
value: editing ? (editing as unknown as Record<string, string>)[field.name] ?? '' : '',
|
|
907
|
+
}))}
|
|
515
908
|
labels={labels}
|
|
516
|
-
|
|
909
|
+
pending={mutationPending}
|
|
910
|
+
error={mutationError}
|
|
911
|
+
fieldErrors={fieldErrors}
|
|
912
|
+
onSubmit={submitForm}
|
|
913
|
+
onCancel={closeForm}
|
|
517
914
|
/>
|
|
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
|
-
|
|
915
|
+
) : (
|
|
916
|
+
<AddressBookBlock
|
|
917
|
+
addresses={addresses.map(toAddressEntry)}
|
|
918
|
+
labels={labels}
|
|
919
|
+
onCreate={() => {
|
|
920
|
+
set('/accountAddressEditingId', null)
|
|
921
|
+
set('/accountAddressFormOpen', true)
|
|
922
|
+
}}
|
|
923
|
+
onEdit={(id) => {
|
|
924
|
+
set('/accountAddressEditingId', id)
|
|
925
|
+
set('/accountAddressFormOpen', true)
|
|
926
|
+
}}
|
|
927
|
+
onDelete={(id) => {
|
|
928
|
+
set('/accountAddressMutationPending', true)
|
|
929
|
+
void deleteAccountAddressAction({ id }).then(applyResult)
|
|
930
|
+
}}
|
|
931
|
+
onSetDefaultShipping={(id) => {
|
|
932
|
+
set('/accountAddressMutationPending', true)
|
|
933
|
+
void setDefaultAccountAddressAction({ id, kind: 'shipping' }).then(applyResult)
|
|
934
|
+
}}
|
|
935
|
+
onSetDefaultBilling={(id) => {
|
|
936
|
+
set('/accountAddressMutationPending', true)
|
|
937
|
+
void setDefaultAccountAddressAction({ id, kind: 'billing' }).then(applyResult)
|
|
938
|
+
}}
|
|
939
|
+
/>
|
|
940
|
+
)}
|
|
941
|
+
</div>
|
|
942
|
+
)
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
function AccountOrdersPanel({ props }: PanelProps) {
|
|
946
|
+
const { state, set, getSnapshot } = useStateStore()
|
|
947
|
+
const parsed = accountOrdersPropsSchema.parse(props)
|
|
948
|
+
const { identityKey, reconciling } = useAccountLifecycle()
|
|
949
|
+
const visible = useModuleVisible('orders')
|
|
950
|
+
|
|
951
|
+
const generationRef = useRef(0)
|
|
952
|
+
const loadedKeyRef = useRef<string | null>(null)
|
|
953
|
+
const requestedPageRef = useRef(1)
|
|
954
|
+
|
|
955
|
+
const currentIdentityKey = useCallback(
|
|
956
|
+
() =>
|
|
957
|
+
accountIdentityKey(
|
|
958
|
+
(getSnapshot().accountCustomer ?? null) as { id?: string | null } | null,
|
|
959
|
+
),
|
|
960
|
+
[getSnapshot],
|
|
961
|
+
)
|
|
962
|
+
|
|
963
|
+
const loadPage = (page: number) => {
|
|
964
|
+
const capturedKey = currentIdentityKey()
|
|
965
|
+
const capturedGeneration = generationRef.current
|
|
966
|
+
requestedPageRef.current = page
|
|
967
|
+
set('/accountOrdersPending', true)
|
|
968
|
+
set('/accountOrdersError', null)
|
|
969
|
+
void loadOrdersPageAction({ page, pageSize: ORDERS_PAGE_SIZE })
|
|
970
|
+
.then((result) => {
|
|
971
|
+
if (
|
|
972
|
+
currentIdentityKey() !== capturedKey ||
|
|
973
|
+
generationRef.current !== capturedGeneration
|
|
974
|
+
) {
|
|
975
|
+
return
|
|
976
|
+
}
|
|
977
|
+
set('/accountOrdersPending', false)
|
|
978
|
+
if (result.ok) {
|
|
979
|
+
set('/accountOrders', result.orders)
|
|
980
|
+
set('/accountOrdersCount', result.count)
|
|
981
|
+
set('/accountOrdersPage', page)
|
|
982
|
+
set('/accountOrdersPageSize', result.limit)
|
|
983
|
+
set('/accountOrdersError', null)
|
|
984
|
+
loadedKeyRef.current = capturedKey
|
|
985
|
+
return
|
|
986
|
+
}
|
|
987
|
+
set('/accountOrdersError', result.error)
|
|
988
|
+
})
|
|
989
|
+
.catch((error) => {
|
|
990
|
+
if (
|
|
991
|
+
currentIdentityKey() !== capturedKey ||
|
|
992
|
+
generationRef.current !== capturedGeneration
|
|
993
|
+
) {
|
|
994
|
+
return
|
|
995
|
+
}
|
|
996
|
+
console.error(
|
|
997
|
+
'[storefront/account] loadOrders failed',
|
|
998
|
+
error instanceof Error ? error.message : error,
|
|
999
|
+
)
|
|
1000
|
+
set('/accountOrdersPending', false)
|
|
1001
|
+
set('/accountOrdersError', parsed.errorLabel ?? 'No se pudieron cargar tus pedidos.')
|
|
1002
|
+
})
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
useEffect(() => {
|
|
1006
|
+
if (loadedKeyRef.current !== identityKey) {
|
|
1007
|
+
generationRef.current += 1
|
|
1008
|
+
loadedKeyRef.current = null
|
|
1009
|
+
}
|
|
1010
|
+
if (identityKey === 'anonymous') return
|
|
1011
|
+
if (loadedKeyRef.current === identityKey) return
|
|
1012
|
+
loadPage(1)
|
|
1013
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1014
|
+
}, [identityKey])
|
|
1015
|
+
|
|
1016
|
+
if (!visible) return null
|
|
1017
|
+
|
|
1018
|
+
const pending = Boolean(state.accountOrdersPending)
|
|
1019
|
+
const error = typeof state.accountOrdersError === 'string' ? state.accountOrdersError : null
|
|
1020
|
+
const allOrders = Array.isArray(state.accountOrders)
|
|
1021
|
+
? (state.accountOrders as AccountOrderView[])
|
|
1022
|
+
: []
|
|
1023
|
+
const orders = loadedKeyRef.current === identityKey ? allOrders : []
|
|
1024
|
+
const count = typeof state.accountOrdersCount === 'number' ? state.accountOrdersCount : orders.length
|
|
1025
|
+
const page = typeof state.accountOrdersPage === 'number' ? state.accountOrdersPage : 1
|
|
1026
|
+
const pageSize =
|
|
1027
|
+
typeof state.accountOrdersPageSize === 'number' && state.accountOrdersPageSize > 0
|
|
1028
|
+
? state.accountOrdersPageSize
|
|
1029
|
+
: ORDERS_PAGE_SIZE
|
|
1030
|
+
const selectedId =
|
|
1031
|
+
typeof state.accountSelectedOrderId === 'string' ? state.accountSelectedOrderId : null
|
|
1032
|
+
const selected = selectedId ? orders.find((order) => order.id === selectedId) ?? null : null
|
|
1033
|
+
const labels: Partial<OrderHistoryLabels> = {
|
|
1034
|
+
...(parsed.labels ?? {}),
|
|
1035
|
+
...(parsed.titleLabel === undefined ? {} : { title: parsed.titleLabel }),
|
|
1036
|
+
...(parsed.descriptionLabel === undefined ? {} : { description: parsed.descriptionLabel }),
|
|
1037
|
+
...(parsed.emptyLabel === undefined ? {} : { emptyLabel: parsed.emptyLabel }),
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
if (reconciling) return <ReconcilingStatus />
|
|
1041
|
+
if (pending) {
|
|
1042
|
+
return (
|
|
1043
|
+
<p
|
|
1044
|
+
role="status"
|
|
1045
|
+
aria-live="polite"
|
|
1046
|
+
className="text-muted-foreground mx-auto max-w-7xl px-4 py-8 text-sm"
|
|
1047
|
+
>
|
|
1048
|
+
{parsed.loadingLabel ?? 'Cargando pedidos…'}
|
|
1049
|
+
</p>
|
|
547
1050
|
)
|
|
548
|
-
}
|
|
1051
|
+
}
|
|
1052
|
+
if (error) {
|
|
1053
|
+
return (
|
|
1054
|
+
<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">
|
|
1055
|
+
<p role="alert" className="text-destructive text-sm">
|
|
1056
|
+
{error}
|
|
1057
|
+
</p>
|
|
1058
|
+
<Button
|
|
1059
|
+
type="button"
|
|
1060
|
+
variant="outline"
|
|
1061
|
+
size="sm"
|
|
1062
|
+
disabled={pending}
|
|
1063
|
+
onClick={() => loadPage(requestedPageRef.current)}
|
|
1064
|
+
>
|
|
1065
|
+
Reintentar
|
|
1066
|
+
</Button>
|
|
1067
|
+
</div>
|
|
1068
|
+
)
|
|
1069
|
+
}
|
|
1070
|
+
const totalPages = Math.max(1, Math.ceil(count / pageSize))
|
|
1071
|
+
return (
|
|
1072
|
+
<div id="account-orders">
|
|
1073
|
+
<OrderHistoryBlock
|
|
1074
|
+
orders={orders.map(toOrderHistoryOrder)}
|
|
1075
|
+
presentation="cards"
|
|
1076
|
+
labels={labels}
|
|
1077
|
+
onViewOrder={(id) => set('/accountSelectedOrderId', id)}
|
|
1078
|
+
/>
|
|
1079
|
+
{count > pageSize ? (
|
|
1080
|
+
<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">
|
|
1081
|
+
<p className="text-muted-foreground text-sm" role="status" aria-live="polite">
|
|
1082
|
+
{`${count} pedidos · página ${page} de ${totalPages}`}
|
|
1083
|
+
</p>
|
|
1084
|
+
<div className="flex gap-2">
|
|
1085
|
+
<Button
|
|
1086
|
+
variant="outline"
|
|
1087
|
+
size="sm"
|
|
1088
|
+
disabled={pending || page <= 1}
|
|
1089
|
+
onClick={() => loadPage(page - 1)}
|
|
1090
|
+
>
|
|
1091
|
+
Anterior
|
|
1092
|
+
</Button>
|
|
1093
|
+
<Button
|
|
1094
|
+
variant="outline"
|
|
1095
|
+
size="sm"
|
|
1096
|
+
disabled={pending || page >= totalPages}
|
|
1097
|
+
onClick={() => loadPage(page + 1)}
|
|
1098
|
+
>
|
|
1099
|
+
Siguiente
|
|
1100
|
+
</Button>
|
|
1101
|
+
</div>
|
|
1102
|
+
</div>
|
|
1103
|
+
) : null}
|
|
1104
|
+
{selected ? (
|
|
1105
|
+
<OrderDetail order={selected} onClose={() => set('/accountSelectedOrderId', null)} />
|
|
1106
|
+
) : null}
|
|
1107
|
+
</div>
|
|
1108
|
+
)
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
export const registry: ViewRegistry = {
|
|
1112
|
+
AccountOrderDetail: (ctx) => <AccountOrderDetailPanel {...ctx} />,
|
|
1113
|
+
AccountShell: (ctx) => <AccountShellPanel {...ctx} />,
|
|
1114
|
+
AccountOverview: () => <AccountOverviewPanel />,
|
|
1115
|
+
AccountModulePlaceholder: () => <AccountModulePlaceholderPanel />,
|
|
1116
|
+
AccountOrders: (ctx) => <AccountOrdersPanel {...ctx} />,
|
|
1117
|
+
AccountBoot: ({ emit }) => <AccountBoundary emit={emit} />,
|
|
1118
|
+
AccountLogin: (ctx) => <AccountLoginPanel {...ctx} />,
|
|
1119
|
+
AccountRegister: (ctx) => <AccountRegisterPanel {...ctx} />,
|
|
1120
|
+
AccountFrame: (ctx) => <AccountFramePanel {...ctx} />,
|
|
1121
|
+
AccountOrdersAdvanced: (ctx) => <AccountOrdersAdvancedPanel {...ctx} />,
|
|
1122
|
+
CustomerProfile: (ctx) => <customerProfileRegistry.CustomerProfile {...ctx} />,
|
|
1123
|
+
AddressBookAdvanced: (ctx) => <addressBookAdvancedRegistry.AddressBookAdvanced {...ctx} />,
|
|
549
1124
|
}
|