Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): send a pinned type once its card is edited

Yudhi Armyndharis8ef8c8d0fe17
diff --git a/dashboard/src/hooks/useInfraConfigForm.test.ts b/dashboard/src/hooks/useInfraConfigForm.test.ts
index e692c9a5c..978a1fd6e 100644
--- a/dashboard/src/hooks/useInfraConfigForm.test.ts
+++ b/dashboard/src/hooks/useInfraConfigForm.test.ts
@@ -355,3 +355,19 @@ test('one pinned selection is left out without the others', () => {
assert.deepEqual([database?.type, redis?.enabled, storage?.type], ['postgres', true, undefined]);
});
+
+// An edited field of a pinned database or storage card belongs to the pinned mode, so the save names that mode:
+// applied to a stored selection of the other mode, the edit would be dropped. The Redis switch needs no such rule,
+// as the gateway saves the Redis fields whatever the switch.
+test('editing a field of a pinned database or storage card sends its type, a Redis field keeps the switch out', () => {
+ const { result } = renderForm({ status: PINNED_SELECTIONS, saved: SAVED });
+
+ rtl.act(() => {
+ result.current.updateDbConfig('sslEnabled', false);
+ result.current.updateStorageConfig('s3Bucket', 'media-2');
+ result.current.updateRedisConfig('host', 'redis-2.internal');
+ });
+ const { database, redis, storage } = result.current.buildSavePayload();
+
+ assert.deepEqual([database?.type, redis?.enabled, storage?.type], ['postgres', undefined, 's3']);
+});
diff --git a/dashboard/src/hooks/useInfraConfigForm.ts b/dashboard/src/hooks/useInfraConfigForm.ts
index de62592cf..fad0d8497 100644
--- a/dashboard/src/hooks/useInfraConfigForm.ts
+++ b/dashboard/src/hooks/useInfraConfigForm.ts
@@ -137,10 +137,10 @@ export function useInfraConfigForm(
// their selection (#735).
const engineHydrated = useRef(false);
const engineTouched = useRef(false);
- // The database type, storage type and Redis switch the operator has changed here. Until then a value the
- // environment pins is the pin, not a choice, and a save leaves it out (see buildSavePayload).
- const dbTypeTouched = useRef(false);
- const storageTypeTouched = useRef(false);
+ // Whether the operator has edited the database card, the storage card or the Redis switch here. Until then a
+ // selection the environment pins is the pin, not a choice, and a save leaves it out (see buildSavePayload).
+ const dbTouched = useRef(false);
+ const storageTouched = useRef(false);
const redisEnabledTouched = useRef(false);
/** Whether engineConfig.type reflects a real value (seeded from the saved config or user-picked)
@@ -217,13 +217,13 @@ export function useInfraConfigForm(
}, [savedConfig]);
const updateDbConfig = (key: keyof DatabaseConfig, value: string | number | boolean) => {
- if (key === 'type') dbTypeTouched.current = true;
+ dbTouched.current = true;
setDbConfig(prev => ({ ...prev, [key]: value }));
};
const updateRedisConfig = (key: keyof RedisConfig, value: string | boolean) =>
setRedisConfig(prev => ({ ...prev, [key]: value }));
const updateStorageConfig = (key: keyof StorageConfig, value: string | boolean) => {
- if (key === 'type') storageTypeTouched.current = true;
+ storageTouched.current = true;
setStorageConfig(prev => ({ ...prev, [key]: value }));
};
const updateEngineConfig = (key: keyof EngineConfig, value: string | boolean) => {
@@ -243,7 +243,9 @@ export function useInfraConfigForm(
// /config reports the EFFECTIVE selection, so under a pin the form shows the pinned value. Sending it back would
// write the pin into data/.env.generated over the stored choice, which unsetting the variable was supposed to
// reveal (#1082), and drop the stored settings of the mode it replaces. Untouched, it is left out, and the
- // gateway keeps the stored selection. Read at save time, like the engine pin below.
+ // gateway keeps the stored selection. Any edit in the database or storage card sends it: the edited fields belong
+ // to the mode the card shows, and a stored selection of the other mode would drop them. Read at save time, like
+ // the engine pin below.
const pinnedSeed = (key: string, touched: { current: boolean }) =>
!touched.current && !!infraStatus?.envPinned?.includes(key);
@@ -253,7 +255,7 @@ export function useInfraConfigForm(
// An emptied pool size is NaN, which JSON writes as null and the backend refuses: leave it out.
database: {
...dbConfig,
- type: pinnedSeed('DATABASE_TYPE', dbTypeTouched) ? undefined : dbConfig.type,
+ type: pinnedSeed('DATABASE_TYPE', dbTouched) ? undefined : dbConfig.type,
password: dbConfig.builtIn ? '' : dbConfig.password,
poolSize: Number.isInteger(dbConfig.poolSize) ? dbConfig.poolSize : undefined,
},
@@ -269,7 +271,7 @@ export function useInfraConfigForm(
queue: { enabled: queueEnabled },
storage: {
...storageConfig,
- type: pinnedSeed('STORAGE_TYPE', storageTypeTouched) ? undefined : storageConfig.type,
+ type: pinnedSeed('STORAGE_TYPE', storageTouched) ? undefined : storageConfig.type,
},
// Only send `type` once we actually know it — either the radio seeded from /config or the
// operator picked one. If /config never resolved, engineConfig.type still holds its useState
diff --git a/dashboard/src/pages/Infrastructure.test.ts b/dashboard/src/pages/Infrastructure.test.ts
index 836cb5d36..01b403ce9 100644
--- a/dashboard/src/pages/Infrastructure.test.ts
+++ b/dashboard/src/pages/Infrastructure.test.ts
@@ -1670,6 +1670,28 @@ test('saving while the selections are pinned leaves them out so the stored choic
assert.equal(body.database.username, 'openwa_admin', 'the rest of the section still goes out');
});
+// The compose .env route: the type is pinned and the file holds the first-boot SQLite. A PostgreSQL field edited on
+// the page belongs to the pinned mode, so the save names it rather than letting the stored SQLite drop the edit.
+test('a field edited in a pinned database card is saved with its type', async () => {
+ const { screen, waitFor, fireEvent } = rtl;
+ resetFetchCalls();
+ overrides = { status: { ...INFRA_STATUS, envPinned: ['DATABASE_TYPE', 'STORAGE_TYPE'] } };
+ const { container } = renderInfrastructure();
+
+ await screen.findByText('Database Configuration');
+ await awaitConfigHydrated(container);
+ fireEvent.click(toggleInput(container, 'SSL Connection'));
+ fireEvent.click(screen.getByRole('button', { name: 'Save Configuration' }));
+
+ await waitFor(() => assert.ok(findFetchCall('PUT', '/api/infra/config'), 'expected a PUT to /infra/config'));
+ const body = findFetchCall('PUT', '/api/infra/config')!.body as {
+ database: { type?: string; sslEnabled?: boolean };
+ storage: Record<string, unknown>;
+ };
+ assert.deepEqual([body.database.type, body.database.sslEnabled], ['postgres', true]);
+ assert.equal('type' in body.storage, false, 'the untouched storage card');
+});
+
test('a pinned selection the operator changes is saved', async () => {
const { screen, waitFor, fireEvent } = rtl;
resetFetchCalls();