Skip to content

Commit 07c6eea

Browse files
committed
refactor: enhance app update logic in Subscription Page Builder to handle old IDs
1 parent 72dd3c3 commit 07c6eea

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

src/pages/dashboard/utils/subscription-page-builder/ui/components/app-form/app-form.component.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,19 @@ export const AppForm = (props: AppFormProps) => {
6666
<Stack gap="sm">
6767
<TextInput
6868
label={t('app-form.component.app-id')}
69-
onChange={(e) => updateApp({ id: e.target.value as `${Lowercase<string>}` })}
69+
onChange={(e) => {
70+
const oldId = localApp.id
71+
const newId = e.target.value as `${Lowercase<string>}`
72+
73+
const updatedApp: AppConfig = {
74+
...localApp,
75+
id: newId
76+
}
77+
78+
setLocalApp(updatedApp)
79+
80+
onChange({ ...updatedApp, _oldId: oldId } as AppConfig)
81+
}}
7082
value={localApp.id}
7183
/>
7284

src/pages/dashboard/utils/subscription-page-builder/ui/components/subscription-page-builder.page.component.tsx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,42 @@ export const SubscriptionPageBuilderComponent = () => {
4646
const newApp = createEmptyApp(activeTab)
4747
const updatedConfig = { ...config }
4848
updatedConfig[activeTab] = [...updatedConfig[activeTab], newApp]
49+
4950
setConfig(updatedConfig)
50-
setSelectedAppId(newApp.id)
51+
52+
setTimeout(() => {
53+
setSelectedAppId(newApp.id)
54+
}, 10)
5155
}
5256

53-
const updateApp = (updatedApp: AppConfig) => {
57+
const updateApp = (updatedApp: AppConfig & { _oldId?: string }) => {
5458
const updatedConfig = { ...config }
55-
const appIndex = updatedConfig[activeTab].findIndex((app) => app.id === updatedApp.id)
56-
if (appIndex !== -1) {
57-
updatedConfig[activeTab][appIndex] = updatedApp
58-
setConfig(updatedConfig)
59+
60+
if (updatedApp._oldId && updatedApp._oldId !== updatedApp.id) {
61+
const appIndex = updatedConfig[activeTab].findIndex(
62+
(app) => app.id === updatedApp._oldId
63+
)
64+
if (appIndex !== -1) {
65+
const appWithoutOldId = { ...updatedApp } as AppConfig
66+
delete (appWithoutOldId as AppConfig & { _oldId?: string })._oldId
67+
updatedConfig[activeTab][appIndex] = appWithoutOldId
68+
69+
if (selectedAppId === updatedApp._oldId) {
70+
setSelectedAppId(updatedApp.id)
71+
}
72+
73+
setConfig(updatedConfig)
74+
}
75+
} else {
76+
const appIndex = updatedConfig[activeTab].findIndex((app) => app.id === updatedApp.id)
77+
if (appIndex !== -1) {
78+
const appWithoutOldId = { ...updatedApp } as AppConfig
79+
if ((updatedApp as AppConfig & { _oldId?: string })._oldId) {
80+
delete (appWithoutOldId as AppConfig & { _oldId?: string })._oldId
81+
}
82+
updatedConfig[activeTab][appIndex] = appWithoutOldId
83+
setConfig(updatedConfig)
84+
}
5985
}
6086
}
6187

0 commit comments

Comments
 (0)