From 6e81cf1f704c21392a5f116ea03769e00deb0be0 Mon Sep 17 00:00:00 2001 From: Julius Jurgelenas Date: Tue, 16 Feb 2021 00:20:48 +0200 Subject: [PATCH] Persist user defines mode and manual user defines txt in local storage --- src/ui/components/DeviceOptionsForm/index.tsx | 4 +- .../commands/mergeUserDefinesFromStorage.ts | 36 ------------ .../mergeWithDeviceOptionsFromStorage.ts | 55 +++++++++++++++++++ .../storage/commands/persistDeviceOptions.ts | 26 +++++++++ .../commands/persistDeviceUserDefines.ts | 29 ---------- src/ui/storage/index.ts | 28 +++++++--- src/ui/views/ConfiguratorView/index.tsx | 26 ++++----- 7 files changed, 115 insertions(+), 89 deletions(-) delete mode 100644 src/ui/storage/commands/mergeUserDefinesFromStorage.ts create mode 100644 src/ui/storage/commands/mergeWithDeviceOptionsFromStorage.ts create mode 100644 src/ui/storage/commands/persistDeviceOptions.ts delete mode 100644 src/ui/storage/commands/persistDeviceUserDefines.ts diff --git a/src/ui/components/DeviceOptionsForm/index.tsx b/src/ui/components/DeviceOptionsForm/index.tsx index f42d43b95..30d23b531 100644 --- a/src/ui/components/DeviceOptionsForm/index.tsx +++ b/src/ui/components/DeviceOptionsForm/index.tsx @@ -216,8 +216,8 @@ const DeviceOptionsForm: FunctionComponent = ( className={styles.textarea} multiline label="user_defines.txt" - onChange={onUserDefinesTxt} - value={deviceOptions.userDefinesTxt} + onBlur={onUserDefinesTxt} + defaultValue={deviceOptions.userDefinesTxt} fullWidth rows={10} /> diff --git a/src/ui/storage/commands/mergeUserDefinesFromStorage.ts b/src/ui/storage/commands/mergeUserDefinesFromStorage.ts deleted file mode 100644 index 7a3aea113..000000000 --- a/src/ui/storage/commands/mergeUserDefinesFromStorage.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { DeviceTarget, UserDefine } from '../../gql/generated/types'; -import { IApplicationStorage } from '../index'; - -const mergeUserDefinesFromStorage = async ( - storage: IApplicationStorage, - deviceTarget: DeviceTarget | null, - userDefines: UserDefine[] -): Promise => { - if (deviceTarget === null) { - return userDefines; - } - const savedBindingPhrase = await storage.GetBindingPhrase(); - const savedTargetOptions = await storage.GetDeviceOptions(deviceTarget); - return userDefines.map((deviceOption) => { - const savedOption = savedTargetOptions.find( - ({ key }) => key === deviceOption.key - ); - let enabled = false; - let value: string | null | undefined; - if (savedOption !== undefined) { - enabled = savedOption.enabled; - value = savedOption.value; - } - if (savedBindingPhrase.length > 0) { - value = savedBindingPhrase; - } - const opt: UserDefine = { - ...deviceOption, - enabled, - value, - }; - return opt; - }); -}; - -export default mergeUserDefinesFromStorage; diff --git a/src/ui/storage/commands/mergeWithDeviceOptionsFromStorage.ts b/src/ui/storage/commands/mergeWithDeviceOptionsFromStorage.ts new file mode 100644 index 000000000..c0c7ac68e --- /dev/null +++ b/src/ui/storage/commands/mergeWithDeviceOptionsFromStorage.ts @@ -0,0 +1,55 @@ +import { + DeviceTarget, + UserDefine, + UserDefineKey, +} from '../../gql/generated/types'; +import { DeviceOptions, IApplicationStorage } from '../index'; + +const mergeWithDeviceOptionsFromStorage = async ( + storage: IApplicationStorage, + deviceTarget: DeviceTarget | null, + deviceOptions: DeviceOptions +): Promise => { + if (deviceTarget === null) { + return deviceOptions; + } + const savedBindingPhrase = await storage.GetBindingPhrase(); + const savedTargetOptions = await storage.GetDeviceOptions(deviceTarget); + const userDefineOptions = deviceOptions.userDefineOptions.map( + (deviceOption) => { + if (savedTargetOptions === null) { + return deviceOption; + } + const savedOption = savedTargetOptions?.userDefineOptions.find( + ({ key }) => key === deviceOption.key + ); + if (savedOption === undefined) { + return deviceOption; + } + + const { enabled } = savedOption; + let { value } = savedOption; + if ( + deviceOption.key === UserDefineKey.BINDING_PHRASE && + savedBindingPhrase.length > 0 + ) { + value = savedBindingPhrase; + } + const opt: UserDefine = { + ...deviceOption, + enabled, + value, + }; + return opt; + } + ); + return { + userDefinesMode: + savedTargetOptions?.userDefinesMode ?? deviceOptions.userDefinesMode, + userDefinesTxt: + savedTargetOptions?.userDefinesTxt ?? deviceOptions?.userDefinesTxt, + userDefineOptions, + }; +}; + +export default mergeWithDeviceOptionsFromStorage; diff --git a/src/ui/storage/commands/persistDeviceOptions.ts b/src/ui/storage/commands/persistDeviceOptions.ts new file mode 100644 index 000000000..e1819edb9 --- /dev/null +++ b/src/ui/storage/commands/persistDeviceOptions.ts @@ -0,0 +1,26 @@ +import { DeviceTarget, UserDefineKey } from '../../gql/generated/types'; +import { DeviceOptions, IApplicationStorage } from '../index'; + +const persistDeviceOptions = async ( + storage: IApplicationStorage, + deviceTarget: DeviceTarget, + deviceOptions: DeviceOptions +): Promise => { + if (deviceTarget !== null) { + await storage.SaveDeviceOptions(deviceTarget, deviceOptions); + + const bindingPhrase = deviceOptions.userDefineOptions.find( + ({ key }) => key === UserDefineKey.BINDING_PHRASE + ); + if ( + bindingPhrase !== undefined && + bindingPhrase.enabled && + bindingPhrase.value && + bindingPhrase.value?.length > 0 + ) { + await storage.SetBindingPhrase(bindingPhrase.value); + } + } +}; + +export default persistDeviceOptions; diff --git a/src/ui/storage/commands/persistDeviceUserDefines.ts b/src/ui/storage/commands/persistDeviceUserDefines.ts deleted file mode 100644 index 4a1785dd9..000000000 --- a/src/ui/storage/commands/persistDeviceUserDefines.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { - DeviceTarget, - UserDefine, - UserDefineKey, -} from '../../gql/generated/types'; -import { IApplicationStorage } from '../index'; - -const persistDeviceUserDefines = async ( - storage: IApplicationStorage, - deviceTarget: DeviceTarget, - userDefines: UserDefine[] -): Promise => { - if (deviceTarget !== null && userDefines.length > 0) { - await storage.SaveDeviceOptions(deviceTarget, userDefines); - const bindingPhrase = userDefines.find( - ({ key }) => key === UserDefineKey.BINDING_PHRASE - ); - if ( - bindingPhrase !== undefined && - bindingPhrase.enabled && - bindingPhrase.value && - bindingPhrase.value?.length > 0 - ) { - await storage.SetBindingPhrase(bindingPhrase.value); - } - } -}; - -export default persistDeviceUserDefines; diff --git a/src/ui/storage/index.ts b/src/ui/storage/index.ts index a9188fd11..3637eb78f 100644 --- a/src/ui/storage/index.ts +++ b/src/ui/storage/index.ts @@ -1,12 +1,22 @@ -import { DeviceTarget, UserDefine } from '../gql/generated/types'; +import { + DeviceTarget, + UserDefine, + UserDefinesMode, +} from '../gql/generated/types'; + +export interface DeviceOptions { + userDefinesMode: UserDefinesMode; + userDefineOptions: UserDefine[]; + userDefinesTxt: string; +} export interface IApplicationStorage { SaveDeviceOptions( deviceTarget: DeviceTarget, - userDefines: UserDefine[] + deviceOptions: DeviceOptions ): Promise; - GetDeviceOptions(deviceTarget: DeviceTarget): Promise; + GetDeviceOptions(deviceTarget: DeviceTarget): Promise; SetBindingPhrase(bindingPhrase: string): Promise; @@ -19,23 +29,25 @@ const BINDING_PHRASE_KEY = 'binding_phrase'; export default class ApplicationStorage implements IApplicationStorage { async SaveDeviceOptions( deviceTarget: DeviceTarget, - userDefines: UserDefine[] + deviceOptions: DeviceOptions ): Promise { const key = `${DEVICE_OPTIONS_BY_TARGET_KEYSPACE}.${deviceTarget}`; - localStorage.setItem(key, JSON.stringify(userDefines)); + localStorage.setItem(key, JSON.stringify(deviceOptions)); } - async GetDeviceOptions(deviceTarget: DeviceTarget): Promise { + async GetDeviceOptions( + deviceTarget: DeviceTarget + ): Promise { const key = `${DEVICE_OPTIONS_BY_TARGET_KEYSPACE}.${deviceTarget}`; const value = localStorage.getItem(key); if (value === null) { - return []; + return null; } try { return JSON.parse(value); } catch (e) { console.error(`failed to parse ${e}`); - return []; + return null; } } diff --git a/src/ui/views/ConfiguratorView/index.tsx b/src/ui/views/ConfiguratorView/index.tsx index 127b9376b..24765076b 100644 --- a/src/ui/views/ConfiguratorView/index.tsx +++ b/src/ui/views/ConfiguratorView/index.tsx @@ -40,8 +40,8 @@ import BuildResponse from '../../components/BuildResponse'; import { IpcRequest, OpenFileLocationRequestBody } from '../../../ipc'; import UserDefinesValidator from './UserDefinesValidator'; import ApplicationStorage from '../../storage'; -import persistDeviceUserDefines from '../../storage/commands/persistDeviceUserDefines'; -import mergeUserDefinesFromStorage from '../../storage/commands/mergeUserDefinesFromStorage'; +import persistDeviceOptions from '../../storage/commands/persistDeviceOptions'; +import mergeWithDeviceOptionsFromStorage from '../../storage/commands/mergeWithDeviceOptionsFromStorage'; export const validateFirmwareVersionData = ( data: FirmwareVersionDataInput @@ -216,15 +216,16 @@ const ConfiguratorView: FunctionComponent = () => { ) { const handleUpdate = async () => { const storage = new ApplicationStorage(); - const userDefineOptions = await mergeUserDefinesFromStorage( + const userDefineOptions = await mergeWithDeviceOptionsFromStorage( storage, deviceTarget, - [...deviceOptionsResponse.targetDeviceOptions] + { + ...deviceOptionsFormData, + userDefineOptions: [...deviceOptionsResponse.targetDeviceOptions], + } ); - setDeviceOptionsFormData({ - ...deviceOptionsFormData, - userDefineOptions, - }); + console.log('merged values', userDefineOptions); + setDeviceOptionsFormData(userDefineOptions); }; handleUpdate().catch((err) => { console.error(`failed to update device options form data: ${err}`); @@ -234,13 +235,10 @@ const ConfiguratorView: FunctionComponent = () => { const onUserDefines = (data: DeviceOptionsFormData) => { setDeviceOptionsFormData(data); - if (deviceTarget !== null && data.userDefineOptions.length > 0) { + if (deviceTarget !== null) { const storage = new ApplicationStorage(); - persistDeviceUserDefines( - storage, - deviceTarget, - data.userDefineOptions - ).catch((err) => { + console.log('trying to persist', data); + persistDeviceOptions(storage, deviceTarget, data).catch((err) => { console.error(`failed to persist user defines: ${err}`); }); }