mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-20 14:06:27 +00:00
fix(settings): let an empty user value fall back to the admin default
The admin "user defaults" merge kept any user value that was merely present, so an empty string shadowed the system-wide default. The client's Settings save always writes every field — a blank Mapbox token included — so a user who ever opened Map Settings got '' instead of the admin token, and clearing their own token could never restore the fallback (#1634). Treat an empty/null user value for a defaultable key as "not set" when an admin default exists, so it falls through. Non-defaultable keys and keys without an admin default keep their exact stored value.
This commit is contained in:
@@ -36,6 +36,8 @@ export const DEFAULTABLE_USER_SETTING_KEYS = [
|
||||
|
||||
type DefaultableKey = typeof DEFAULTABLE_USER_SETTING_KEYS[number];
|
||||
|
||||
const DEFAULTABLE_USER_SETTING_KEY_SET = new Set<string>(DEFAULTABLE_USER_SETTING_KEYS);
|
||||
|
||||
const VALID_VALUES: Partial<Record<DefaultableKey, unknown[]>> = {
|
||||
temperature_unit: ['fahrenheit', 'celsius'],
|
||||
distance_unit: ['metric', 'imperial'],
|
||||
@@ -132,8 +134,24 @@ export function getUserSettings(userId: number): Record<string, unknown> {
|
||||
}
|
||||
}
|
||||
|
||||
// Admin defaults fill in only for keys the user hasn't explicitly set
|
||||
return { ...adminDefaults, ...userSettings };
|
||||
// Admin defaults fill in for keys the user hasn't explicitly set. For a
|
||||
// defaultable key an *empty* user value counts as "not set": the client's
|
||||
// Settings save always writes every field (a blank Mapbox token included), so
|
||||
// an empty string would otherwise shadow the admin/system-wide default and the
|
||||
// user could never fall back to it — even after clearing their own value
|
||||
// (#1634). Non-defaultable keys keep their exact stored value.
|
||||
const merged: Record<string, unknown> = { ...adminDefaults };
|
||||
for (const [key, value] of Object.entries(userSettings)) {
|
||||
if (
|
||||
DEFAULTABLE_USER_SETTING_KEY_SET.has(key) &&
|
||||
(value === '' || value === null || value === undefined) &&
|
||||
key in adminDefaults
|
||||
) {
|
||||
continue; // empty user value → keep the admin default
|
||||
}
|
||||
merged[key] = value;
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
function serializeValue(key: string, value: unknown): string {
|
||||
|
||||
Reference in New Issue
Block a user