Skip to content

Commit

Permalink
fix(settings): fix changed settings consantly resetting #2080
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Nov 28, 2024
1 parent bc61337 commit 9354acf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/components/device-page/DeviceSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class DeviceSettings extends Component<DeviceSettingsProps, DeviceSetting

updatedDeviceConfig: {},
};
formData: KVP | KVP[] | undefined = undefined;
getGenericDeviceSettingsSchema(): JSONSchema7 {
const {
bridgeInfo: { config_schema: configSchema = {} },
Expand Down Expand Up @@ -53,6 +54,10 @@ export class DeviceSettings extends Component<DeviceSettingsProps, DeviceSetting

render(): ReactNode {
const { schema, data, uiSchema } = this.getSchemaAndConfig();
// Put formData in separate variable to prevent overwrites on re-render.
if (!this.formData) {
this.formData = data;
}
return (
<>
<ReadTheDocsInfo
Expand All @@ -61,7 +66,8 @@ export class DeviceSettings extends Component<DeviceSettingsProps, DeviceSetting

<Form
schema={schema}
formData={data}
formData={this.formData}
onChange={(data) => this.formData = data.formData}
onSubmit={this.updateConfig}
uiSchema={uiSchema}
fields={{ TitleField, DescriptionField }}
Expand Down
8 changes: 7 additions & 1 deletion src/components/settings-page/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class SettingsPage extends Component<
state = {
keyName: ROOT_KEY_NAME,
};
settingsFormData: {[s: string]: Record<string, unknown>} = {};
renderCategoriesTabs(): JSX.Element {
const { t } = this.props;
return (
Expand Down Expand Up @@ -380,6 +381,10 @@ class SettingsPage extends Component<
renderSettings(): JSX.Element {
const { keyName } = this.state;
const { currentSchema, currentConfig } = this.getSettingsInfo();
// Put formData in separate variable to prevent overwrites on re-render.
if (!(keyName in this.settingsFormData)) {
this.settingsFormData[keyName] = currentConfig;
}
return (
<div className="tab">
{this.renderSettingsTabs()}
Expand All @@ -388,7 +393,8 @@ class SettingsPage extends Component<
<Form
idPrefix={keyName}
schema={currentSchema}
formData={currentConfig}
formData={this.settingsFormData[keyName]}
onChange={(data) => this.settingsFormData[keyName] = data.formData}
onSubmit={this.onSettingsSave}
uiSchema={uiSchemas[keyName] as UiSchema}
fields={{ TitleField, DescriptionField }}
Expand Down

0 comments on commit 9354acf

Please sign in to comment.