From 7a2c8cb2e4451925e75d223791322393225811d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Kiss?= Date: Fri, 22 Feb 2019 19:00:17 +0100 Subject: [PATCH] refactor: ngrx-store use enum ActionTypes and Action classes everywhere (#919) --- package-lock.json | 23 +- package.json | 4 +- packages/uhk-common/src/util/index.ts | 22 - packages/uhk-web/package-lock.json | 12 - packages/uhk-web/package.json | 3 - .../mouse-speed/mouse-speed.component.ts | 10 +- .../keymap/add/keymap-add.component.ts | 7 +- .../keymap/edit/keymap-edit-guard.service.ts | 12 +- .../keymap/edit/keymap-edit.component.ts | 48 +- .../keymap/header/keymap-header.component.ts | 22 +- .../macro/edit/macro-edit.component.ts | 45 +- .../macro/header/macro-header.component.ts | 8 +- .../macro-not-found-guard.service.ts | 5 +- .../components/popover/popover.component.ts | 5 +- .../popover/tab/macro/macro-tab.component.ts | 5 +- .../side-menu/side-menu.component.ts | 4 +- .../svg-keyboard-key.component.ts | 5 +- .../svg/wrap/svg-keyboard-wrap.component.ts | 38 +- .../directives/tooltip/tooltip.directive.ts | 2 +- .../app/store/actions/app-update.action.ts | 33 +- packages/uhk-web/src/app/store/actions/app.ts | 77 ++- .../app/store/actions/auto-update-settings.ts | 41 +- .../app/store/actions/contributors.action.ts | 25 +- .../uhk-web/src/app/store/actions/device.ts | 104 ++-- .../uhk-web/src/app/store/actions/index.ts | 2 - .../uhk-web/src/app/store/actions/keymap.ts | 322 +++++------- .../uhk-web/src/app/store/actions/macro.ts | 162 +++--- .../src/app/store/actions/user-config.ts | 57 +- .../src/app/store/effects/app-update.ts | 6 +- packages/uhk-web/src/app/store/effects/app.ts | 12 +- .../app/store/effects/auto-update-settings.ts | 6 +- .../app/store/effects/contributors.effect.ts | 4 +- .../uhk-web/src/app/store/effects/device.ts | 39 +- .../uhk-web/src/app/store/effects/keymap.ts | 29 +- .../uhk-web/src/app/store/effects/macro.ts | 20 +- .../src/app/store/effects/user-config.ts | 39 +- packages/uhk-web/src/app/store/index.ts | 17 +- .../app/store/reducers/app-update.reducer.ts | 11 +- .../src/app/store/reducers/app.reducer.ts | 61 +-- .../store/reducers/auto-update-settings.ts | 46 +- .../store/reducers/contributors.reducer.ts | 17 +- .../uhk-web/src/app/store/reducers/device.ts | 64 +-- .../uhk-web/src/app/store/reducers/preset.ts | 8 +- .../store/reducers/user-configuration.spec.ts | 28 +- .../app/store/reducers/user-configuration.ts | 492 +++++++++++------- packages/uhk-web/tsconfig.json | 2 + 46 files changed, 1026 insertions(+), 978 deletions(-) delete mode 100644 packages/uhk-web/src/app/store/actions/index.ts diff --git a/package-lock.json b/package-lock.json index 9da14a03..c10bed83 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2149,10 +2149,13 @@ "dev": true }, "@types/jquery": { - "version": "3.3.1", - "resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.3.1.tgz", - "integrity": "sha512-N3h+rzN518yl2xKrW0o6KKdNmWZ+OwG6SoM5TBEQFF0tTv5wXPEsoOuYQ2Kt3/89XbcSZUJLdjiT/2c3BR/ApQ==", - "dev": true + "version": "3.3.29", + "resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.3.29.tgz", + "integrity": "sha512-FhJvBninYD36v3k6c+bVk1DSZwh7B5Dpb/Pyk3HKVsiohn0nhbefZZ+3JXbWQhFyt0MxSl2jRDdGQPHeOHFXrQ==", + "dev": true, + "requires": { + "@types/sizzle": "*" + } }, "@types/jsonfile": { "version": "4.0.1", @@ -2197,6 +2200,12 @@ "integrity": "sha512-41qEJgBH/TWgo5NFSvBCJ1qkoi3Q6ONSF2avrHq1LVEZfYpdHmj0y9SuTK+u9ZhG1sYQKBL1AWXKyLWP4RaUoQ==", "dev": true }, + "@types/sizzle": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/@types/sizzle/-/sizzle-2.3.2.tgz", + "integrity": "sha512-7EJYyKTL7tFR8+gDbB6Wwz/arpGa0Mywk1TJbNzKzHtzbwVmY4HR9WqS5VV7dsBUKQmPNr192jHr/VpBluj/hg==", + "dev": true + }, "@types/usb": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/@types/usb/-/usb-1.1.3.tgz", @@ -16763,9 +16772,9 @@ "dev": true }, "typescript": { - "version": "2.6.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.6.2.tgz", - "integrity": "sha1-PFtv1/beCRQmkCfwPAlGdY92c6Q=", + "version": "2.8.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.8.4.tgz", + "integrity": "sha512-IIU5cN1mR5J3z9jjdESJbnxikTrEz3lzAw/D0Tf45jHpBp55nY31UkUvmVHoffCfKHTqJs3fCLPDxknQTTFegQ==", "dev": true }, "uglify-js": { diff --git a/package.json b/package.json index bbcba3a0..8e5834b3 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "@types/electron-settings": "3.0.0", "@types/fs-extra": "5.0.1", "@types/jasmine": "2.8.8", - "@types/jquery": "3.3.1", + "@types/jquery": "3.3.29", "@types/jsonfile": "4.0.1", "@types/lodash": "4.14.121", "@types/node": "8.0.53", @@ -74,7 +74,7 @@ "ts-loader": "2.3.1", "ts-node": "7.0.1", "tslint": "5.9.1", - "typescript": "2.6.2", + "typescript": "2.8.4", "webpack": "3.12.0" }, "pre-commit": [ diff --git a/packages/uhk-common/src/util/index.ts b/packages/uhk-common/src/util/index.ts index 9add7e2a..d6a3db3c 100644 --- a/packages/uhk-common/src/util/index.ts +++ b/packages/uhk-common/src/util/index.ts @@ -15,28 +15,6 @@ export function capitalizeFirstLetter(text: string): string { return text.charAt(0).toUpperCase() + text.slice(1); } -/** - * This function coerces a string into a string literal type. - * Using tagged union types in TypeScript 2.0, this enables - * powerful typechecking of our reducers. - * - * Since every action label passes through this function it - * is a good place to ensure all of our action labels - * are unique. - */ - -const typeCache: { [label: string]: boolean } = {}; - -export function type(label: T | ''): T { - if (typeCache[label]) { - throw new Error(`Action type "${label}" is not unique"`); - } - - typeCache[label] = true; - - return label; -} - export function runInElectron() { return window && (window).process && (window).process.type; } diff --git a/packages/uhk-web/package-lock.json b/packages/uhk-web/package-lock.json index 341be964..98c31565 100644 --- a/packages/uhk-web/package-lock.json +++ b/packages/uhk-web/package-lock.json @@ -343,12 +343,6 @@ "@types/jasmine": "*" } }, - "@types/jquery": { - "version": "3.2.9", - "resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.2.9.tgz", - "integrity": "sha512-AmYGadmTv+Xh6re2CH5ruyvV3znvtJbhxyT00JQAGFP2U+xgqhf+C2xfjdP/GgK5d9YmSif/UYs2ssMl4gW6fw==", - "dev": true - }, "@types/node": { "version": "9.6.2", "resolved": "https://registry.npmjs.org/@types/node/-/node-9.6.2.tgz", @@ -9318,12 +9312,6 @@ "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=", "dev": true }, - "reselect": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/reselect/-/reselect-3.0.1.tgz", - "integrity": "sha1-79qpjqdFEyTQkrKyFjpqHXqaIUc=", - "dev": true - }, "resolve": { "version": "1.8.1", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.8.1.tgz", diff --git a/packages/uhk-web/package.json b/packages/uhk-web/package.json index fc8064a0..6acba574 100644 --- a/packages/uhk-web/package.json +++ b/packages/uhk-web/package.json @@ -42,7 +42,6 @@ "@types/file-saver": "0.0.1", "@types/jasmine": "2.8.8", "@types/jasminewd2": "2.0.3", - "@types/jquery": "3.2.9", "@types/usb": "1.1.3", "angular-confirmation-popover": "3.2.0", "angular-notifier": "2.0.0", @@ -71,11 +70,9 @@ "nouislider": "^11.1.0", "postcss-url": "^7.1.2", "protractor": "5.4.0", - "reselect": "3.0.1", "rxjs": "5.5.8", "semver": "5.6.0", "ts-keycode-enum": "^1.0.6", - "typescript": "2.6.2", "uhk-common": "1.0.0", "xml-loader": "1.2.1", "zone.js": "0.8.26", diff --git a/packages/uhk-web/src/app/components/device/mouse-speed/mouse-speed.component.ts b/packages/uhk-web/src/app/components/device/mouse-speed/mouse-speed.component.ts index 26e94b46..bd51b9f6 100644 --- a/packages/uhk-web/src/app/components/device/mouse-speed/mouse-speed.component.ts +++ b/packages/uhk-web/src/app/components/device/mouse-speed/mouse-speed.component.ts @@ -2,7 +2,6 @@ import { Component, OnInit, OnDestroy } from '@angular/core'; import { Store } from '@ngrx/store'; import { AppState, getUserConfiguration } from '../../../store'; import { SetUserConfigurationValueAction } from '../../../store/actions/user-config'; -import { DefaultUserConfigurationService } from '../../../services/default-user-configuration.service'; import { SliderPips, SliderProps } from '../../slider-wrapper/slider-wrapper.component'; import { Subscription } from 'rxjs/Subscription'; import { UserConfiguration } from 'uhk-common'; @@ -117,7 +116,8 @@ export class MouseSpeedComponent implements OnInit, OnDestroy { private userConfig$: Store; private userConfigSubscription: Subscription; - constructor(private store: Store, private defaultUserConfigurationService: DefaultUserConfigurationService) {} + constructor(private store: Store) { + } ngOnInit(): void { this.userConfig$ = this.store.select(getUserConfiguration); @@ -132,7 +132,9 @@ export class MouseSpeedComponent implements OnInit, OnDestroy { } ngOnDestroy(): void { - this.userConfigSubscription.unsubscribe(); + if (this.userConfigSubscription) { + this.userConfigSubscription.unsubscribe(); + } } onSetPropertyValue(propertyName: string, value: number): void { @@ -143,6 +145,6 @@ export class MouseSpeedComponent implements OnInit, OnDestroy { } resetToDefault() { - this.store.dispatch(new ResetMouseSpeedSettingsAction()); + this.store.dispatch(new ResetMouseSpeedSettingsAction()); } } diff --git a/packages/uhk-web/src/app/components/keymap/add/keymap-add.component.ts b/packages/uhk-web/src/app/components/keymap/add/keymap-add.component.ts index f82d2abd..43f83662 100644 --- a/packages/uhk-web/src/app/components/keymap/add/keymap-add.component.ts +++ b/packages/uhk-web/src/app/components/keymap/add/keymap-add.component.ts @@ -2,12 +2,11 @@ import { ChangeDetectionStrategy, Component } from '@angular/core'; import { Store } from '@ngrx/store'; import { Keymap } from 'uhk-common'; -import { BehaviorSubject } from 'rxjs/BehaviorSubject'; -import { Observable } from 'rxjs/Observable'; +import { BehaviorSubject, Observable } from 'rxjs'; import { combineLatest, publishReplay, refCount } from 'rxjs/operators'; import { AppState } from '../../../store'; -import { KeymapActions } from '../../../store/actions'; +import { AddKeymapAction } from '../../../store/actions/keymap'; @Component({ selector: 'keymap-add', @@ -42,6 +41,6 @@ export class KeymapAddComponent { } addKeymap(keymap: Keymap) { - this.store.dispatch(KeymapActions.addKeymap(keymap)); + this.store.dispatch(new AddKeymapAction(keymap)); } } diff --git a/packages/uhk-web/src/app/components/keymap/edit/keymap-edit-guard.service.ts b/packages/uhk-web/src/app/components/keymap/edit/keymap-edit-guard.service.ts index 9cbf3a10..c3d428d6 100644 --- a/packages/uhk-web/src/app/components/keymap/edit/keymap-edit-guard.service.ts +++ b/packages/uhk-web/src/app/components/keymap/edit/keymap-edit-guard.service.ts @@ -2,16 +2,13 @@ import { Injectable } from '@angular/core'; import { CanActivate, Router } from '@angular/router'; import { Keymap } from 'uhk-common'; -import { Observable } from 'rxjs/Observable'; +import { Observable } from 'rxjs'; import { of } from 'rxjs/observable/of'; import { switchMap, tap } from 'rxjs/operators'; -import 'rxjs/add/operator/let'; - import { Store } from '@ngrx/store'; -import { AppState } from '../../../store'; -import { getKeymaps } from '../../../store/reducers/user-configuration'; +import { AppState, getDefaultKeymap } from '../../../store'; @Injectable() export class KeymapEditGuard implements CanActivate { @@ -20,10 +17,9 @@ export class KeymapEditGuard implements CanActivate { canActivate(): Observable { return this.store - .let(getKeymaps()) + .select(getDefaultKeymap) .pipe( - tap((keymaps: Keymap[]) => { - const defaultKeymap = keymaps.find(keymap => keymap.isDefault); + tap((defaultKeymap: Keymap) => { if (defaultKeymap) { this.router.navigate(['/keymap', defaultKeymap.abbreviation]); } diff --git a/packages/uhk-web/src/app/components/keymap/edit/keymap-edit.component.ts b/packages/uhk-web/src/app/components/keymap/edit/keymap-edit.component.ts index cc2dec81..2ba7e136 100644 --- a/packages/uhk-web/src/app/components/keymap/edit/keymap-edit.component.ts +++ b/packages/uhk-web/src/app/components/keymap/edit/keymap-edit.component.ts @@ -1,18 +1,23 @@ -import { ChangeDetectionStrategy, Component, HostListener } from '@angular/core'; +import { ChangeDetectionStrategy, Component, HostListener, OnDestroy } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { Store } from '@ngrx/store'; import { Keymap } from 'uhk-common'; -import { Observable } from 'rxjs/Observable'; -import { combineLatest, first, map, pluck, publishReplay, refCount, switchMap } from 'rxjs/operators'; -import 'rxjs/add/operator/let'; +import { Observable, Subscription } from 'rxjs'; +import { combineLatest, first, map, pluck, switchMap } from 'rxjs/operators'; import { saveAs } from 'file-saver'; -import { layerDoubleTapSupported, AppState, getKeyboardLayout } from '../../../store'; -import { getKeymap, getKeymaps, getUserConfiguration } from '../../../store/reducers/user-configuration'; +import { + getUserConfiguration, + getSelectedKeymap, + isKeymapDeletable, + layerDoubleTapSupported, + AppState, + getKeyboardLayout +} from '../../../store'; import { KeyboardLayout } from '../../../keyboard/keyboard-layout.enum'; -import { KeymapActions } from '../../../store/actions'; +import { EditDescriptionAction, SelectKeymapAction } from '../../../store/actions/keymap'; import { ChangeKeymapDescription } from '../../../models/ChangeKeymapDescription'; @Component({ @@ -24,7 +29,7 @@ import { ChangeKeymapDescription } from '../../../models/ChangeKeymapDescription 'class': 'container-fluid' } }) -export class KeymapEditComponent { +export class KeymapEditComponent implements OnDestroy { keyboardSplit: boolean; @@ -33,26 +38,29 @@ export class KeymapEditComponent { keyboardLayout$: Observable; allowLayerDoubleTap$: Observable; + private routeSubscription: Subscription; + constructor(protected store: Store, route: ActivatedRoute) { - this.keymap$ = route + this.routeSubscription = route .params .pipe( - pluck<{}, string>('abbr'), - switchMap((abbr: string) => store.let(getKeymap(abbr))), - publishReplay(1), - refCount() - ); + pluck<{}, string>('abbr') + ) + .subscribe(abbr => store.dispatch(new SelectKeymapAction(abbr))); - this.deletable$ = store.let(getKeymaps()) - .pipe( - map((keymaps: Keymap[]) => keymaps.length > 1) - ); + this.keymap$ = store.select(getSelectedKeymap); + + this.deletable$ = store.select(isKeymapDeletable); this.keyboardLayout$ = store.select(getKeyboardLayout); this.allowLayerDoubleTap$ = store.select(layerDoubleTapSupported); } + ngOnDestroy(): void { + this.routeSubscription.unsubscribe(); + } + downloadKeymap() { const exportableJSON$: Observable = this.keymap$ .pipe( @@ -79,12 +87,12 @@ export class KeymapEditComponent { } descriptionChanged(event: ChangeKeymapDescription): void { - this.store.dispatch(new KeymapActions.EditDescriptionAction(event)); + this.store.dispatch(new EditDescriptionAction(event)); } private toExportableJSON(keymap: Keymap): Observable { return this.store - .let(getUserConfiguration()) + .select(getUserConfiguration) .pipe( first(), map(userConfiguration => { diff --git a/packages/uhk-web/src/app/components/keymap/header/keymap-header.component.ts b/packages/uhk-web/src/app/components/keymap/header/keymap-header.component.ts index 425bff27..8eff010a 100644 --- a/packages/uhk-web/src/app/components/keymap/header/keymap-header.component.ts +++ b/packages/uhk-web/src/app/components/keymap/header/keymap-header.component.ts @@ -16,7 +16,13 @@ import { Keymap } from 'uhk-common'; import { Store } from '@ngrx/store'; import { AppState } from '../../../store'; -import { KeymapActions } from '../../../store/actions'; +import { + DuplicateKeymapAction, + EditKeymapAbbreviationAction, + EditKeymapNameAction, + RemoveKeymapAction, + SetDefaultKeymapAction +} from '../../../store/actions/keymap'; import * as util from '../../../util'; const DEFAULT_TRASH_TITLE = 'Delete keymap'; @@ -59,18 +65,18 @@ export class KeymapHeaderComponent implements OnChanges { setDefault() { if (!this.keymap.isDefault) { - this.store.dispatch(KeymapActions.setDefault(this.keymap.abbreviation)); + this.store.dispatch(new SetDefaultKeymapAction(this.keymap.abbreviation)); } } removeKeymap() { if (this.deletable) { - this.store.dispatch(KeymapActions.removeKeymap(this.keymap.abbreviation)); + this.store.dispatch(new RemoveKeymapAction(this.keymap.abbreviation)); } } duplicateKeymap() { - this.store.dispatch(KeymapActions.duplicateKeymap(this.keymap)); + this.store.dispatch(new DuplicateKeymapAction(this.keymap)); } editKeymapName(name: string) { @@ -79,7 +85,7 @@ export class KeymapHeaderComponent implements OnChanges { return; } - this.store.dispatch(KeymapActions.editKeymapName(this.keymap.abbreviation, name)); + this.store.dispatch(new EditKeymapNameAction({ abbr: this.keymap.abbreviation, name })); } editKeymapAbbr(newAbbr: string) { @@ -91,7 +97,11 @@ export class KeymapHeaderComponent implements OnChanges { } newAbbr = newAbbr.toUpperCase(); - this.store.dispatch(KeymapActions.editKeymapAbbr(this.keymap.name, this.keymap.abbreviation, newAbbr)); + this.store.dispatch(new EditKeymapAbbreviationAction({ + name: this.keymap.name, + abbr: this.keymap.abbreviation, + newAbbr + })); } setKeymapTitle(): void { diff --git a/packages/uhk-web/src/app/components/macro/edit/macro-edit.component.ts b/packages/uhk-web/src/app/components/macro/edit/macro-edit.component.ts index af81692f..e8087b3f 100644 --- a/packages/uhk-web/src/app/components/macro/edit/macro-edit.component.ts +++ b/packages/uhk-web/src/app/components/macro/edit/macro-edit.component.ts @@ -3,13 +3,17 @@ import { ActivatedRoute } from '@angular/router'; import { Store } from '@ngrx/store'; import { Macro, MacroAction } from 'uhk-common'; -import { Observable } from 'rxjs/Observable'; -import { Subscription } from 'rxjs/Subscription'; -import { pluck, switchMap } from 'rxjs/operators'; +import { Observable, Subscription } from 'rxjs'; -import { MacroActions } from '../../../store/actions'; -import { AppState, macroPlaybackSupported } from '../../../store'; -import { getMacro } from '../../../store/reducers/user-configuration'; +import { + AddMacroActionAction, + DeleteMacroActionAction, + ReorderMacroActionAction, + SaveMacroActionAction, + SelectMacroAction +} from '../../../store/actions/macro'; +import { AppState, getSelectedMacro, macroPlaybackSupported } from '../../../store'; +import { pluck } from 'rxjs/operators'; @Component({ selector: 'macro-edit', @@ -25,18 +29,20 @@ export class MacroEditComponent implements OnDestroy { macroId: number; macroPlaybackSupported$: Observable; - private subscription: Subscription; + private selectedMacroSubscription: Subscription; + private routeSubscription: Subscription; - constructor(private store: Store, public route: ActivatedRoute) { - this.subscription = route + constructor(private store: Store, + public route: ActivatedRoute) { + + this.routeSubscription = route .params .pipe( - pluck<{}, string>('id'), - switchMap((id: string) => { - this.macroId = +id; - return store.let(getMacro(this.macroId)); - }) + pluck<{}, string>('id') ) + .subscribe(id => store.dispatch(new SelectMacroAction(+id))); + + this.selectedMacroSubscription = store.select(getSelectedMacro) .subscribe((macro: Macro) => { this.macro = macro; }); @@ -46,22 +52,23 @@ export class MacroEditComponent implements OnDestroy { } ngOnDestroy() { - this.subscription.unsubscribe(); + this.selectedMacroSubscription.unsubscribe(); + this.routeSubscription.unsubscribe(); } addAction(macroId: number, action: MacroAction) { - this.store.dispatch(MacroActions.addMacroAction(macroId, action)); + this.store.dispatch(new AddMacroActionAction({ id: macroId, action })); } editAction(macroId: number, index: number, action: MacroAction) { - this.store.dispatch(MacroActions.saveMacroAction(macroId, index, action)); + this.store.dispatch(new SaveMacroActionAction({ id: macroId, index, action })); } deleteAction(macroId: number, index: number, action: MacroAction) { - this.store.dispatch(MacroActions.deleteMacroAction(macroId, index, action)); + this.store.dispatch(new DeleteMacroActionAction({ id: macroId, index, action })); } reorderAction(macroId: number, oldIndex: number, newIndex: number) { - this.store.dispatch(MacroActions.reorderMacroAction(macroId, oldIndex, newIndex)); + this.store.dispatch(new ReorderMacroActionAction({ id: macroId, oldIndex, newIndex })); } } diff --git a/packages/uhk-web/src/app/components/macro/header/macro-header.component.ts b/packages/uhk-web/src/app/components/macro/header/macro-header.component.ts index 72c37b10..2f08e89d 100644 --- a/packages/uhk-web/src/app/components/macro/header/macro-header.component.ts +++ b/packages/uhk-web/src/app/components/macro/header/macro-header.component.ts @@ -13,7 +13,7 @@ import { import { Store } from '@ngrx/store'; import { Macro } from 'uhk-common'; -import { MacroActions } from '../../../store/actions'; +import { DuplicateMacroAction, EditMacroNameAction, RemoveMacroAction } from '../../../store/actions/macro'; import { AppState } from '../../../store'; import * as util from '../../../util'; @@ -51,11 +51,11 @@ export class MacroHeaderComponent implements AfterViewInit, OnChanges { } removeMacro() { - this.store.dispatch(MacroActions.removeMacro(this.macro.id)); + this.store.dispatch(new RemoveMacroAction(this.macro.id)); } duplicateMacro() { - this.store.dispatch(MacroActions.duplicateMacro(this.macro)); + this.store.dispatch(new DuplicateMacroAction(this.macro)); } editMacroName(name: string) { @@ -64,7 +64,7 @@ export class MacroHeaderComponent implements AfterViewInit, OnChanges { return; } - this.store.dispatch(MacroActions.editMacroName(this.macro.id, name)); + this.store.dispatch(new EditMacroNameAction({ id: this.macro.id, name })); } calculateHeaderTextWidth(text): void { diff --git a/packages/uhk-web/src/app/components/macro/not-found/macro-not-found-guard.service.ts b/packages/uhk-web/src/app/components/macro/not-found/macro-not-found-guard.service.ts index 4881318e..eb8dc571 100644 --- a/packages/uhk-web/src/app/components/macro/not-found/macro-not-found-guard.service.ts +++ b/packages/uhk-web/src/app/components/macro/not-found/macro-not-found-guard.service.ts @@ -8,8 +8,7 @@ import 'rxjs/add/operator/let'; import { Store } from '@ngrx/store'; -import { AppState } from '../../../store/index'; -import { getMacros } from '../../../store/reducers/user-configuration'; +import { AppState, getMacros } from '../../../store'; @Injectable() export class MacroNotFoundGuard implements CanActivate { @@ -18,7 +17,7 @@ export class MacroNotFoundGuard implements CanActivate { canActivate(): Observable { return this.store - .let(getMacros()) + .select(getMacros) .pipe( map((macros: Macro[]) => { const hasMacros = macros.length > 0; diff --git a/packages/uhk-web/src/app/components/popover/popover.component.ts b/packages/uhk-web/src/app/components/popover/popover.component.ts index bbc19a61..42383fab 100644 --- a/packages/uhk-web/src/app/components/popover/popover.component.ts +++ b/packages/uhk-web/src/app/components/popover/popover.component.ts @@ -31,8 +31,7 @@ import { import { Tab } from './tab'; -import { AppState, macroPlaybackSupported } from '../../store'; -import { getKeymaps } from '../../store/reducers/user-configuration'; +import { AppState, getKeymaps, macroPlaybackSupported } from '../../store'; import { KeyActionRemap } from '../../models/key-action-remap'; import { RemapInfo } from '../../models/remap-info'; @@ -153,7 +152,7 @@ export class PopoverComponent implements OnChanges { constructor(private store: Store, private cdRef: ChangeDetectorRef) { this.animationState = 'closed'; - this.keymaps$ = store.let(getKeymaps()) + this.keymaps$ = store.select(getKeymaps) .pipe( combineLatest(this.currentKeymap$), map(([keymaps, currentKeymap]: [Keymap[], Keymap]) => diff --git a/packages/uhk-web/src/app/components/popover/tab/macro/macro-tab.component.ts b/packages/uhk-web/src/app/components/popover/tab/macro/macro-tab.component.ts index 77a1a5bf..dfb3ed08 100644 --- a/packages/uhk-web/src/app/components/popover/tab/macro/macro-tab.component.ts +++ b/packages/uhk-web/src/app/components/popover/tab/macro/macro-tab.component.ts @@ -5,8 +5,7 @@ import { KeyAction, Macro, PlayMacroAction } from 'uhk-common'; import { Tab } from '../tab'; -import { AppState } from '../../../../store'; -import { getMacros } from '../../../../store/reducers/user-configuration'; +import { AppState, getMacros } from '../../../../store'; import { SelectOptionData } from '../../../../models/select-option-data'; @Component({ @@ -26,7 +25,7 @@ export class MacroTabComponent extends Tab implements OnInit, OnChanges, OnDestr constructor(store: Store) { super(); - this.subscription = store.let(getMacros()) + this.subscription = store.select(getMacros) .subscribe((macros: Macro[]) => this.macros = macros); this.macroOptions = []; this.selectedMacroIndex = 0; diff --git a/packages/uhk-web/src/app/components/side-menu/side-menu.component.ts b/packages/uhk-web/src/app/components/side-menu/side-menu.component.ts index 2c0495d9..d72c1933 100644 --- a/packages/uhk-web/src/app/components/side-menu/side-menu.component.ts +++ b/packages/uhk-web/src/app/components/side-menu/side-menu.component.ts @@ -14,7 +14,7 @@ import { Store } from '@ngrx/store'; import { Subscription } from 'rxjs/Subscription'; import { AppState, getSideMenuPageState } from '../../store'; -import { MacroActions } from '../../store/actions'; +import { AddMacroAction } from '../../store/actions/macro'; import { RenameUserConfigurationAction } from '../../store/actions/user-config'; import { SideMenuPageState } from '../../models/side-menu-page-state'; @@ -92,7 +92,7 @@ export class SideMenuComponent implements OnInit, OnDestroy { } addMacro() { - this.store.dispatch(MacroActions.addMacro()); + this.store.dispatch(new AddMacroAction()); } editDeviceName(name: string): void { diff --git a/packages/uhk-web/src/app/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts b/packages/uhk-web/src/app/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts index 59960eab..b4a0da22 100644 --- a/packages/uhk-web/src/app/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts +++ b/packages/uhk-web/src/app/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts @@ -26,8 +26,7 @@ import { import { CaptureService } from '../../../../services/capture.service'; import { MapperService } from '../../../../services/mapper.service'; -import { AppState } from '../../../../store'; -import { getMacros } from '../../../../store/reducers/user-configuration'; +import { AppState, getMacros } from '../../../../store'; import { SvgKeyCaptureEvent, SvgKeyClickEvent } from '../../../../models/svg-key-events'; import { OperatingSystem } from '../../../../models/operating-system'; import { KeyModifierModel } from '../../../../models/key-modifier-model'; @@ -114,7 +113,7 @@ export class SvgKeyboardKeyComponent implements OnInit, OnChanges, OnDestroy { private element: ElementRef, private captureService: CaptureService ) { - this.subscription = store.let(getMacros()) + this.subscription = store.select(getMacros) .subscribe((macros: Macro[]) => this.macros = macros); this.reset(); diff --git a/packages/uhk-web/src/app/components/svg/wrap/svg-keyboard-wrap.component.ts b/packages/uhk-web/src/app/components/svg/wrap/svg-keyboard-wrap.component.ts index 47dacc4b..0531014b 100644 --- a/packages/uhk-web/src/app/components/svg/wrap/svg-keyboard-wrap.component.ts +++ b/packages/uhk-web/src/app/components/svg/wrap/svg-keyboard-wrap.component.ts @@ -36,8 +36,8 @@ import { } from 'uhk-common'; import { MapperService } from '../../../services/mapper.service'; -import { AppState } from '../../../store'; -import { KeymapActions } from '../../../store/actions'; +import { AppState, getKeymaps, getMacros } from '../../../store'; +import { SaveKeyAction } from '../../../store/actions/keymap'; import { PopoverComponent } from '../../popover'; import { KeyboardLayout } from '../../../keyboard/keyboard-layout.enum'; import { ChangeKeymapDescription } from '../../../models/ChangeKeymapDescription'; @@ -71,7 +71,7 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges { @Output() descriptionChanged = new EventEmitter(); - @ViewChild(PopoverComponent, {read: ElementRef}) popover: ElementRef; + @ViewChild(PopoverComponent, { read: ElementRef }) popover: ElementRef; popoverShown: boolean; keyEditConfig: { moduleId: number, keyId: number }; @@ -185,27 +185,29 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges { keystrokeAction.modifierMask = mapLeftRigthModifierToKeyActionModifier(event.captured.left, event.captured.right); this.store.dispatch( - KeymapActions.saveKey( - this.keymap, - this.currentLayer, - event.moduleId, - event.keyId, - { + new SaveKeyAction({ + keymap: this.keymap, + layer: this.currentLayer, + module: event.moduleId, + key: event.keyId, + keyAction: { remapOnAllKeymap: event.shiftPressed, remapOnAllLayer: event.altPressed, action: keystrokeAction - }) + } + }) ); } onRemap(keyAction: KeyActionRemap): void { this.store.dispatch( - KeymapActions.saveKey( - this.keymap, - this.currentLayer, - this.keyEditConfig.moduleId, - this.keyEditConfig.keyId, - keyAction) + new SaveKeyAction({ + keymap: this.keymap, + layer: this.currentLayer, + module: this.keyEditConfig.moduleId, + key: this.keyEditConfig.keyId, + keyAction + }) ); this.hidePopover(); } @@ -318,7 +320,7 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges { } else if (keyAction instanceof PlayMacroAction) { const playMacroAction: PlayMacroAction = keyAction; return this.store - .select(appState => appState.userConfiguration.macros) + .select(getMacros) .pipe( map(macroState => macroState.find(macro => { return macro.id === playMacroAction.macroId; @@ -340,7 +342,7 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges { } else if (keyAction instanceof SwitchKeymapAction) { const switchKeymapAction: SwitchKeymapAction = keyAction; return this.store - .select(appState => appState.userConfiguration.keymaps) + .select(getKeymaps) .pipe( map(keymaps => keymaps.find(keymap => keymap.abbreviation === switchKeymapAction.keymapAbbreviation).name), map(keymapName => { diff --git a/packages/uhk-web/src/app/directives/tooltip/tooltip.directive.ts b/packages/uhk-web/src/app/directives/tooltip/tooltip.directive.ts index f2ac5122..49af2d67 100644 --- a/packages/uhk-web/src/app/directives/tooltip/tooltip.directive.ts +++ b/packages/uhk-web/src/app/directives/tooltip/tooltip.directive.ts @@ -1,4 +1,4 @@ -/// +/// import { AfterContentInit, Directive, ElementRef, HostBinding, Input, OnChanges, SimpleChanges } from '@angular/core'; import { DomSanitizer } from '@angular/platform-browser'; diff --git a/packages/uhk-web/src/app/store/actions/app-update.action.ts b/packages/uhk-web/src/app/store/actions/app-update.action.ts index 01d780ce..c85975d2 100644 --- a/packages/uhk-web/src/app/store/actions/app-update.action.ts +++ b/packages/uhk-web/src/app/store/actions/app-update.action.ts @@ -1,44 +1,41 @@ import { Action } from '@ngrx/store'; -import { type } from 'uhk-common'; + import { UpdateInfo } from '../../models/update-info'; -const PREFIX = '[app-update] '; - -// tslint:disable-next-line:variable-name -export const ActionTypes = { - UPDATE_AVAILABLE: type(PREFIX + 'update available'), - UPDATE_APP: type(PREFIX + 'update app'), - DO_NOT_UPDATE_APP: type(PREFIX + 'do not update app'), - UPDATE_DOWNLOADED: type(PREFIX + 'update downloaded'), - UPDATING: type(PREFIX + 'updating'), - UPDATE_ERROR: type(PREFIX + 'error') -}; +export enum ActionTypes { + UpdateAvailable = '[app-update] update available', + UpdateApp = '[app-update] update app', + DoNotUpdateApp = '[app-update] do not update app', + UpdateDownloaded = '[app-update] update downloaded', + Updating = '[app-update] updating', + UpdateError = '[app-update] error' +} export class UpdateAvailableAction implements Action { - type = ActionTypes.UPDATE_AVAILABLE; + type = ActionTypes.UpdateAvailable; } export class UpdateAppAction implements Action { - type = ActionTypes.UPDATE_APP; + type = ActionTypes.UpdateApp; } export class DoNotUpdateAppAction implements Action { - type = ActionTypes.DO_NOT_UPDATE_APP; + type = ActionTypes.DoNotUpdateApp; } export class UpdateDownloadedAction implements Action { - type = ActionTypes.UPDATE_DOWNLOADED; + type = ActionTypes.UpdateDownloaded; constructor(public payload: UpdateInfo) { } } export class UpdatingAction implements Action { - type = ActionTypes.UPDATING; + type = ActionTypes.Updating; } export class UpdateErrorAction implements Action { - type = ActionTypes.UPDATE_ERROR; + type = ActionTypes.UpdateError; constructor(public payload: any) {} } diff --git a/packages/uhk-web/src/app/store/actions/app.ts b/packages/uhk-web/src/app/store/actions/app.ts index 874b0e49..df9694d1 100644 --- a/packages/uhk-web/src/app/store/actions/app.ts +++ b/packages/uhk-web/src/app/store/actions/app.ts @@ -1,121 +1,118 @@ import { Action } from '@ngrx/store'; -import { AppStartInfo, HardwareConfiguration, Notification, type } from 'uhk-common'; +import { AppStartInfo, HardwareConfiguration, Notification } from 'uhk-common'; import { ElectronLogEntry } from '../../models/xterm-log'; -const PREFIX = '[app] '; +export enum ActionTypes { + AppBootstrapped = '[app] bootstrapped', + AppStarted = '[app] started', + AppShowNotification = '[app] show notification', + ApplyAppStartInfo = '[app] apply command line args', + AppProcessStartInfo = '[app] process start info', + UndoLast = '[app] undo last action', + UndoLastSuccess = '[app] undo last action success', + DismissUndoNotification = '[app] dismiss notification action', + LoadHardwareConfigurationSuccess = '[app] load hardware configuration success', + ElectronMainLogReceived = '[app] Electron main log received', + OpenUrlInNewWindow = '[app] Open URL in new Window', + PrivilegeWhatWillThisDo = '[app] What will this do clicked', + SetupPermissionError = '[app] Setup permission error', + LoadAppStartInfo = '[app] Load app start info', + StartKeypressCapturing = '[app] Start keypress capturing', + StopKeypressCapturing = '[app] Stop keypress capturing' +} -// tslint:disable-next-line:variable-name -export const ActionTypes = { - APP_BOOTSRAPPED: type(PREFIX + 'bootstrapped'), - APP_STARTED: type(PREFIX + 'started'), - APP_SHOW_NOTIFICATION: type(PREFIX + 'show notification'), - APPLY_APP_START_INFO: type(PREFIX + 'apply command line args'), - APP_PROCESS_START_INFO: type(PREFIX + 'process start info'), - UNDO_LAST: type(PREFIX + 'undo last action'), - UNDO_LAST_SUCCESS: type(PREFIX + 'undo last action success'), - DISMISS_UNDO_NOTIFICATION: type(PREFIX + 'dismiss notification action'), - LOAD_HARDWARE_CONFIGURATION_SUCCESS: type(PREFIX + 'load hardware configuration success'), - ELECTRON_MAIN_LOG_RECEIVED: type(PREFIX + 'Electron main log received'), - OPEN_URL_IN_NEW_WINDOW: type(PREFIX + 'Open URL in new Window'), - PRIVILEGE_WHAT_WILL_THIS_DO: type(PREFIX + 'What will this do clicked'), - SETUP_PERMISSION_ERROR: type(PREFIX + 'Setup permission error'), - LOAD_APP_START_INFO: type(PREFIX + 'Load app start info'), - START_KEYPRESS_CAPTURING: type(PREFIX + 'Start keypress capturing'), - STOP_KEYPRESS_CAPTURING: type(PREFIX + 'Stop keypress capturing') -}; - -export class AppBootsrappedAction implements Action { - type = ActionTypes.APP_BOOTSRAPPED; +export class AppBootstrappedAction implements Action { + type = ActionTypes.AppBootstrapped; } export class AppStartedAction implements Action { - type = ActionTypes.APP_STARTED; + type = ActionTypes.AppStarted; } export class ShowNotificationAction implements Action { - type = ActionTypes.APP_SHOW_NOTIFICATION; + type = ActionTypes.AppShowNotification; constructor(public payload: Notification) { } } export class ApplyAppStartInfoAction implements Action { - type = ActionTypes.APPLY_APP_START_INFO; + type = ActionTypes.ApplyAppStartInfo; constructor(public payload: AppStartInfo) { } } export class ProcessAppStartInfoAction implements Action { - type = ActionTypes.APP_PROCESS_START_INFO; + type = ActionTypes.AppProcessStartInfo; constructor(public payload: AppStartInfo) { } } export class UndoLastAction implements Action { - type = ActionTypes.UNDO_LAST; + type = ActionTypes.UndoLast; constructor(public payload: any) { } } export class UndoLastSuccessAction implements Action { - type = ActionTypes.UNDO_LAST_SUCCESS; + type = ActionTypes.UndoLastSuccess; } export class DismissUndoNotificationAction implements Action { - type = ActionTypes.DISMISS_UNDO_NOTIFICATION; + type = ActionTypes.DismissUndoNotification; } export class LoadHardwareConfigurationSuccessAction implements Action { - type = ActionTypes.LOAD_HARDWARE_CONFIGURATION_SUCCESS; + type = ActionTypes.LoadHardwareConfigurationSuccess; constructor(public payload: HardwareConfiguration) { } } export class ElectronMainLogReceivedAction implements Action { - type = ActionTypes.ELECTRON_MAIN_LOG_RECEIVED; + type = ActionTypes.ElectronMainLogReceived; constructor(public payload: ElectronLogEntry) { } } export class OpenUrlInNewWindowAction implements Action { - type = ActionTypes.OPEN_URL_IN_NEW_WINDOW; + type = ActionTypes.OpenUrlInNewWindow; constructor(public payload: string) { } } export class PrivilegeWhatWillThisDoAction implements Action { - type = ActionTypes.PRIVILEGE_WHAT_WILL_THIS_DO; + type = ActionTypes.PrivilegeWhatWillThisDo; } export class SetupPermissionErrorAction implements Action { - type = ActionTypes.SETUP_PERMISSION_ERROR; + type = ActionTypes.SetupPermissionError; constructor(public payload: string) { } } export class LoadAppStartInfoAction implements Action { - type = ActionTypes.LOAD_APP_START_INFO; + type = ActionTypes.LoadAppStartInfo; } export class StartKeypressCapturingAction implements Action { - type = ActionTypes.START_KEYPRESS_CAPTURING; + type = ActionTypes.StartKeypressCapturing; } export class StopKeypressCapturingAction implements Action { - type = ActionTypes.STOP_KEYPRESS_CAPTURING; + type = ActionTypes.StopKeypressCapturing; } export type Actions = AppStartedAction - | AppBootsrappedAction + | AppBootstrappedAction | ShowNotificationAction | ApplyAppStartInfoAction | ProcessAppStartInfoAction diff --git a/packages/uhk-web/src/app/store/actions/auto-update-settings.ts b/packages/uhk-web/src/app/store/actions/auto-update-settings.ts index 35626ec8..83cd787d 100644 --- a/packages/uhk-web/src/app/store/actions/auto-update-settings.ts +++ b/packages/uhk-web/src/app/store/actions/auto-update-settings.ts @@ -1,68 +1,65 @@ import { Action } from '@ngrx/store'; -import { AutoUpdateSettings, type } from 'uhk-common'; +import { AutoUpdateSettings } from 'uhk-common'; -const PREFIX = '[app-update-config] '; - -// tslint:disable-next-line:variable-name -export const ActionTypes = { - TOGGLE_CHECK_FOR_UPDATE_ON_STARTUP: type(PREFIX + 'Check for update on startup'), - CHECK_FOR_UPDATE_NOW: type(PREFIX + 'Check for update now'), - CHECK_FOR_UPDATE_SUCCESS: type(PREFIX + 'Check for update success'), - CHECK_FOR_UPDATE_FAILED: type(PREFIX + 'Check for update faild'), - TOGGLE_PRE_RELEASE_FLAG: type(PREFIX + 'Toggle pre release update flag'), - LOAD_AUTO_UPDATE_SETTINGS: type(PREFIX + 'Load auto update settings'), - LOAD_AUTO_UPDATE_SETTINGS_SUCCESS: type(PREFIX + 'Load auto update settings success'), - SAVE_AUTO_UPDATE_SETTINGS_SUCCESS: type(PREFIX + 'Save auto update settings success') -}; +export enum ActionTypes { + ToggleCheckForUpdateOnStartup = '[app-update-config] Check for update on startup', + CheckForUpdateNow = '[app-update-config] Check for update now', + CheckForUpdateSuccess = '[app-update-config] Check for update success', + CheckForUpdateFailed = '[app-update-config] Check for update failed', + TogglePreReleaseFlag = '[app-update-config] Toggle pre release update flag', + LoadAutoUpdateSettings = '[app-update-config] Load auto update settings', + LoadAutoUpdateSettingSuccess = '[app-update-config] Load auto update settings success', + SaveAutoUpdateSettingSuccess = '[app-update-config] Save auto update settings success' +} export class ToggleCheckForUpdateOnStartupAction implements Action { - type = ActionTypes.TOGGLE_CHECK_FOR_UPDATE_ON_STARTUP; + type = ActionTypes.ToggleCheckForUpdateOnStartup; constructor(public payload: boolean) { } } export class CheckForUpdateNowAction implements Action { - type = ActionTypes.CHECK_FOR_UPDATE_NOW; + type = ActionTypes.CheckForUpdateNow; constructor(public payload?: boolean) { } } export class CheckForUpdateSuccessAction implements Action { - type = ActionTypes.CHECK_FOR_UPDATE_SUCCESS; + type = ActionTypes.CheckForUpdateSuccess; constructor(public payload?: string) { } } export class CheckForUpdateFailedAction implements Action { - type = ActionTypes.CHECK_FOR_UPDATE_FAILED; + type = ActionTypes.CheckForUpdateFailed; constructor(public payload: any) { } } export class TogglePreReleaseFlagAction implements Action { - type = ActionTypes.TOGGLE_PRE_RELEASE_FLAG; + type = ActionTypes.TogglePreReleaseFlag; constructor(public payload: boolean) { } } export class LoadAutoUpdateSettingsAction implements Action { - type = ActionTypes.LOAD_AUTO_UPDATE_SETTINGS_SUCCESS; + type = ActionTypes.LoadAutoUpdateSettingSuccess; } export class LoadAutoUpdateSettingsSuccessAction implements Action { - type = ActionTypes.LOAD_AUTO_UPDATE_SETTINGS_SUCCESS; + type = ActionTypes.LoadAutoUpdateSettingSuccess; constructor(public payload: AutoUpdateSettings) { } } export class SaveAutoUpdateSettingsSuccessAction implements Action { - type = ActionTypes.SAVE_AUTO_UPDATE_SETTINGS_SUCCESS; + type = ActionTypes.SaveAutoUpdateSettingSuccess; } export type Actions diff --git a/packages/uhk-web/src/app/store/actions/contributors.action.ts b/packages/uhk-web/src/app/store/actions/contributors.action.ts index c76cda70..81bf627d 100644 --- a/packages/uhk-web/src/app/store/actions/contributors.action.ts +++ b/packages/uhk-web/src/app/store/actions/contributors.action.ts @@ -1,35 +1,30 @@ import { Action } from '@ngrx/store'; -import { type } from 'uhk-common'; - import { UHKContributor } from '../../models/uhk-contributor'; -const PREFIX = '[contributors] '; - -// tslint:disable-next-line:variable-name -export const ActionTypes = { - GET_AGENT_CONTRIBUTORS: type(PREFIX + 'Get'), - FETCH_AGENT_CONTRIBUTORS: type(PREFIX + 'Fetch'), - AGENT_CONTRIBUTORS_AVAILABLE: type(PREFIX + 'Available'), - AGENT_CONTRIBUTORS_NOT_AVAILABLE: type(PREFIX + 'Not available') -}; +export enum ActionTypes { + GetAgentContributors = '[contributors] Get', + FetchAgentContributors = '[contributors] Fetch', + AgentContributorsAvailable = '[contributors] Available', + AgentContributorsNotAvailable = '[contributors] Not available' +} export class GetAgentContributorsAction implements Action { - type = ActionTypes.GET_AGENT_CONTRIBUTORS; + type = ActionTypes.GetAgentContributors; } export class FetchAgentContributorsAction implements Action { - type = ActionTypes.FETCH_AGENT_CONTRIBUTORS; + type = ActionTypes.FetchAgentContributors; } export class AgentContributorsAvailableAction implements Action { - type = ActionTypes.AGENT_CONTRIBUTORS_AVAILABLE; + type = ActionTypes.AgentContributorsAvailable; constructor(public payload: UHKContributor[]) { } } export class AgentContributorsNotAvailableAction implements Action { - type = ActionTypes.AGENT_CONTRIBUTORS_NOT_AVAILABLE; + type = ActionTypes.AgentContributorsNotAvailable; constructor(public payload: Error) { console.error(payload); diff --git a/packages/uhk-web/src/app/store/actions/device.ts b/packages/uhk-web/src/app/store/actions/device.ts index de8dfac0..160057d4 100644 --- a/packages/uhk-web/src/app/store/actions/device.ts +++ b/packages/uhk-web/src/app/store/actions/device.ts @@ -1,158 +1,154 @@ import { Action } from '@ngrx/store'; -import { DeviceConnectionState, FirmwareUpgradeIpcResponse, HardwareModules, IpcResponse, type } from 'uhk-common'; +import { DeviceConnectionState, FirmwareUpgradeIpcResponse, HardwareModules, IpcResponse } from 'uhk-common'; import { FirmwareUpgradeError } from '../../models/firmware-upgrade-error'; -const PREFIX = '[device] '; - -// tslint:disable-next-line:variable-name -export const ActionTypes = { - SET_PRIVILEGE_ON_LINUX: type(PREFIX + 'set privilege on linux'), - SET_PRIVILEGE_ON_LINUX_REPLY: type(PREFIX + 'set privilege on linux reply'), - CONNECTION_STATE_CHANGED: type(PREFIX + 'connection state changed'), - SAVE_CONFIGURATION: type(PREFIX + 'save configuration'), - SAVE_CONFIGURATION_REPLY: type(PREFIX + 'save configuration reply'), - SAVING_CONFIGURATION: type(PREFIX + 'saving configuration'), - SHOW_SAVE_TO_KEYBOARD_BUTTON: type(PREFIX + 'show save to keyboard button'), - SAVE_TO_KEYBOARD_SUCCESS: type(PREFIX + 'save to keyboard success'), - SAVE_TO_KEYBOARD_FAILED: type(PREFIX + 'save to keyboard failed'), - HIDE_SAVE_TO_KEYBOARD_BUTTON: type(PREFIX + 'hide save to keyboard button'), - RESET_USER_CONFIGURATION: type(PREFIX + 'reset user configuration'), - RESET_MOUSE_SPEED_SETTINGS: type(PREFIX + 'reset mouse speed settings'), - UPDATE_FIRMWARE: type(PREFIX + 'update firmware'), - UPDATE_FIRMWARE_WITH: type(PREFIX + 'update firmware with'), - UPDATE_FIRMWARE_REPLY: type(PREFIX + 'update firmware reply'), - UPDATE_FIRMWARE_SUCCESS: type(PREFIX + 'update firmware success'), - UPDATE_FIRMWARE_FAILED: type(PREFIX + 'update firmware failed'), - UPDATE_FIRMWARE_OK_BUTTON: type(PREFIX + 'update firmware ok button click'), - MODULES_INFO_LOADED: type(PREFIX + 'module info loaded'), - HAS_BACKUP_USER_CONFIGURATION: type(PREFIX + 'Store backup user configuration'), - RESTORE_CONFIGURATION_FROM_BACKUP: type(PREFIX + 'Restore configuration from backup'), - RESTORE_CONFIGURATION_FROM_BACKUP_SUCCESS: type(PREFIX + 'Restore configuration from backup success'), - RECOVERY_DEVICE: type(PREFIX + 'Recovery device'), - ENABLE_USB_STACK_TEST: type(PREFIX + 'USB stack test'), - START_CONNECTION_POLLER: type(PREFIX + 'Start connection poller') -}; +export enum ActionTypes { + SetPrivilegeOnLinux = '[device] set privilege on linux', + SetPrivilegeOnLinuxReply = '[device] set privilege on linux reply', + ConnectionStateChanged = '[device] connection state changed', + SaveConfiguration = '[device] save configuration', + SaveConfigurationReply = '[device] save configuration reply', + SavingConfiguration = '[device] saving configuration', + ShowSaveToKeyboardButton = '[device] show save to keyboard button', + SaveToKeyboardSuccess = '[device] save to keyboard success', + SaveToKeyboardFailed = '[device] save to keyboard failed', + HideSaveToKeyboardButton = '[device] hide save to keyboard button', + ResetUserConfiguration = '[device] reset user configuration', + ResetMouseSpeedSettings = '[device] reset mouse speed settings', + UpdateFirmware = '[device] update firmware', + UpdateFirmwareWith = '[device] update firmware with', + UpdateFirmwareReply = '[device] update firmware reply', + UpdateFirmwareSuccess = '[device] update firmware success', + UpdateFirmwareFailed = '[device] update firmware failed', + ModulesInfoLoaded = '[device] module info loaded', + HasBackupUserConfiguration = '[device] Store backup user configuration', + RestoreConfigurationFromBackup = '[device] Restore configuration from backup', + RestoreConfigurationFromBackupSuccess = '[device] Restore configuration from backup success', + RecoveryDevice = '[device] Recovery device', + EnableUsbStackTest = '[device] USB stack test', + StartConnectionPoller = '[device] Start connection poller' +} export class SetPrivilegeOnLinuxAction implements Action { - type = ActionTypes.SET_PRIVILEGE_ON_LINUX; + type = ActionTypes.SetPrivilegeOnLinux; } export class SetPrivilegeOnLinuxReplyAction implements Action { - type = ActionTypes.SET_PRIVILEGE_ON_LINUX_REPLY; + type = ActionTypes.SetPrivilegeOnLinuxReply; constructor(public payload: IpcResponse) { } } export class ConnectionStateChangedAction implements Action { - type = ActionTypes.CONNECTION_STATE_CHANGED; + type = ActionTypes.ConnectionStateChanged; constructor(public payload: DeviceConnectionState) { } } export class SaveConfigurationAction implements Action { - type = ActionTypes.SAVE_CONFIGURATION; + type = ActionTypes.SaveConfiguration; constructor() { } } export class SaveConfigurationReplyAction implements Action { - type = ActionTypes.SAVE_CONFIGURATION_REPLY; + type = ActionTypes.SaveConfigurationReply; constructor(public payload: IpcResponse) { } } export class ShowSaveToKeyboardButtonAction implements Action { - type = ActionTypes.SHOW_SAVE_TO_KEYBOARD_BUTTON; + type = ActionTypes.ShowSaveToKeyboardButton; } export class SaveToKeyboardSuccessAction implements Action { - type = ActionTypes.SAVE_TO_KEYBOARD_SUCCESS; + type = ActionTypes.SaveToKeyboardSuccess; } export class SaveToKeyboardSuccessFailed implements Action { - type = ActionTypes.SAVE_TO_KEYBOARD_FAILED; + type = ActionTypes.SaveToKeyboardFailed; } export class HideSaveToKeyboardButton implements Action { - type = ActionTypes.HIDE_SAVE_TO_KEYBOARD_BUTTON; + type = ActionTypes.HideSaveToKeyboardButton; } export class ResetUserConfigurationAction implements Action { - type = ActionTypes.RESET_USER_CONFIGURATION; + type = ActionTypes.ResetUserConfiguration; } export class UpdateFirmwareAction implements Action { - type = ActionTypes.UPDATE_FIRMWARE; + type = ActionTypes.UpdateFirmware; } export class UpdateFirmwareWithAction implements Action { - type = ActionTypes.UPDATE_FIRMWARE_WITH; + type = ActionTypes.UpdateFirmwareWith; constructor(public payload: Array) { } } export class UpdateFirmwareReplyAction implements Action { - type = ActionTypes.UPDATE_FIRMWARE_REPLY; + type = ActionTypes.UpdateFirmwareReply; constructor(public payload: FirmwareUpgradeIpcResponse) { } } export class UpdateFirmwareSuccessAction implements Action { - type = ActionTypes.UPDATE_FIRMWARE_SUCCESS; + type = ActionTypes.UpdateFirmwareSuccess; constructor(public payload: HardwareModules) { } } export class UpdateFirmwareFailedAction implements Action { - type = ActionTypes.UPDATE_FIRMWARE_FAILED; + type = ActionTypes.UpdateFirmwareFailed; constructor(public payload: FirmwareUpgradeError) { } } export class ResetMouseSpeedSettingsAction implements Action { - type = ActionTypes.RESET_MOUSE_SPEED_SETTINGS; + type = ActionTypes.ResetMouseSpeedSettings; } export class HardwareModulesLoadedAction implements Action { - type = ActionTypes.MODULES_INFO_LOADED; + type = ActionTypes.ModulesInfoLoaded; constructor(public payload: HardwareModules) { } } export class RestoreUserConfigurationFromBackupAction implements Action { - type = ActionTypes.RESTORE_CONFIGURATION_FROM_BACKUP; + type = ActionTypes.RestoreConfigurationFromBackup; } export class HasBackupUserConfigurationAction implements Action { - type = ActionTypes.HAS_BACKUP_USER_CONFIGURATION; + type = ActionTypes.HasBackupUserConfiguration; constructor(public payload: boolean) { } } export class RestoreUserConfigurationFromBackupSuccessAction implements Action { - type = ActionTypes.RESTORE_CONFIGURATION_FROM_BACKUP_SUCCESS; + type = ActionTypes.RestoreConfigurationFromBackupSuccess; } export class RecoveryDeviceAction implements Action { - type = ActionTypes.RECOVERY_DEVICE; + type = ActionTypes.RecoveryDevice; } export class EnableUsbStackTestAction implements Action { - type = ActionTypes.ENABLE_USB_STACK_TEST; + type = ActionTypes.EnableUsbStackTest; } export class StartConnectionPollerAction implements Action { - type = ActionTypes.START_CONNECTION_POLLER; + type = ActionTypes.StartConnectionPoller; } export type Actions diff --git a/packages/uhk-web/src/app/store/actions/index.ts b/packages/uhk-web/src/app/store/actions/index.ts deleted file mode 100644 index 0da8f3ab..00000000 --- a/packages/uhk-web/src/app/store/actions/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './keymap'; -export * from './macro'; diff --git a/packages/uhk-web/src/app/store/actions/keymap.ts b/packages/uhk-web/src/app/store/actions/keymap.ts index 56335f2f..8e4ce88d 100644 --- a/packages/uhk-web/src/app/store/actions/keymap.ts +++ b/packages/uhk-web/src/app/store/actions/keymap.ts @@ -1,199 +1,139 @@ import { Action } from '@ngrx/store'; -import { Keymap, Macro } from 'uhk-common'; -import { UndoUserConfigData } from '../../models/undo-user-config-data'; +import { Keymap } from 'uhk-common'; + import { ChangeKeymapDescription } from '../../models/ChangeKeymapDescription'; import { KeyActionRemap } from '../../models/key-action-remap'; +import { UndoUserConfigData } from '../../models/undo-user-config-data'; -export type KeymapAction = - KeymapActions.AddKeymapAction | - KeymapActions.DuplicateKeymapAction | - KeymapActions.EditKeymapNameAction | - KeymapActions.EditKeymapAbbreviationAction | - KeymapActions.LoadKeymapSuccessAction | - KeymapActions.SetDefaultAction | - KeymapActions.RemoveKeymapAction | - KeymapActions.SaveKeyAction | - KeymapActions.CheckMacroAction | - KeymapActions.EditDescriptionAction; +export enum ActionTypes { + Add = '[Keymap] Add keymap', + Duplicate = '[Keymap] Duplicate keymap', + EditAbbr = '[Keymap] Edit keymap abbreviation', + EditName = '[Keymap] Edit keymap title', + SaveKey = '[Keymap] Save key action', + SetDefault = '[Keymap] Set default option', + Remove = '[Keymap] Remove keymap', + CheckMacro = '[Keymap] Check deleted macro', + LoadKeymaps = '[Keymap] Load keymaps', + LoadKeymapsSuccess = '[Keymap] Load keymaps success', + EditDescription = '[Keymap] Edit description', + UndoLastAction = '[Keymap] Undo last action', + Select = '[Keymap] Select keymap action' +} -export namespace KeymapActions { - export const ADD = '[Keymap] Add keymap'; +export class AddKeymapAction implements Action { + type = ActionTypes.Add; - export type AddKeymapAction = { - type: typeof ADD, - payload: Keymap - }; - - export const DUPLICATE = '[Keymap] Duplicate keymap'; - - export type DuplicateKeymapAction = { - type: typeof DUPLICATE, - payload: Keymap - }; - - export const EDIT_ABBR = '[Keymap] Edit keymap abbreviation'; - - export type EditKeymapAbbreviationAction = { - type: typeof EDIT_ABBR, - payload: { - abbr: string; - newAbbr: string; - name: string; - } - }; - - export const EDIT_NAME = '[Keymap] Edit keymap title'; - - export type EditKeymapNameAction = { - type: typeof EDIT_NAME, - payload: { - abbr: string; - name: string; - } - }; - - export const SAVE_KEY = '[Keymap] Save key action'; - - export type SaveKeyAction = { - type: typeof SAVE_KEY, - payload: { - keymap: Keymap; - layer: number; - module: number; - key: number; - keyAction: KeyActionRemap; - } - }; - - export const SET_DEFAULT = '[Keymap] Set default option'; - - export type SetDefaultAction = { - type: typeof SET_DEFAULT, - payload: string - }; - - export const REMOVE = '[Keymap] Remove keymap'; - - export type RemoveKeymapAction = { - type: typeof REMOVE, - payload: string - }; - - export const CHECK_MACRO = '[Keymap] Check deleted macro'; - - export type CheckMacroAction = { - type: typeof CHECK_MACRO, - payload: Macro - }; - - export const LOAD_KEYMAPS = '[Keymap] Load keymaps'; - export const LOAD_KEYMAPS_SUCCESS = '[Keymap] Load keymaps success'; - - export type LoadKeymapSuccessAction = { - type: typeof LOAD_KEYMAPS_SUCCESS, - payload: Keymap[] - }; - - export const UNDO_LAST_ACTION = '[Keymap] Undo last action'; - - export type UndoLastAction = { - type: typeof UNDO_LAST_ACTION, - payload: UndoUserConfigData - }; - - export const EDIT_DESCRIPTION = '[Keymap] Edit description'; - - export class EditDescriptionAction { - type = EDIT_DESCRIPTION; - - constructor(public payload: ChangeKeymapDescription) { - - } - } - - export function loadKeymaps(): Action { - return { - type: KeymapActions.LOAD_KEYMAPS - }; - } - - export function loadKeymapsSuccess(keymaps: Keymap[]): LoadKeymapSuccessAction { - return { - type: KeymapActions.LOAD_KEYMAPS_SUCCESS, - payload: keymaps - }; - } - - export function addKeymap(item: Keymap): AddKeymapAction { - return { - type: KeymapActions.ADD, - payload: item - }; - } - - export function setDefault(abbr: string): SetDefaultAction { - return { - type: KeymapActions.SET_DEFAULT, - payload: abbr - }; - } - - export function removeKeymap(abbr: string): RemoveKeymapAction { - return { - type: KeymapActions.REMOVE, - payload: abbr - }; - } - - export function duplicateKeymap(keymap: Keymap): DuplicateKeymapAction { - return { - type: KeymapActions.DUPLICATE, - payload: keymap - }; - } - - export function editKeymapName(abbr: string, name: string): EditKeymapNameAction { - return { - type: KeymapActions.EDIT_NAME, - payload: { - abbr: abbr, - name: name - } - }; - } - - export function editKeymapAbbr(name: string, abbr: string, newAbbr: string): EditKeymapAbbreviationAction { - return { - type: KeymapActions.EDIT_ABBR, - payload: { - name, - abbr, - newAbbr - } - }; - } - - export function saveKey(keymap: Keymap, - layer: number, - module: number, - key: number, - keyAction: KeyActionRemap): SaveKeyAction { - return { - type: KeymapActions.SAVE_KEY, - payload: { - keymap, - layer, - module, - key, - keyAction - } - }; - } - - export function checkMacro(macro: Macro): CheckMacroAction { - return { - type: KeymapActions.CHECK_MACRO, - payload: macro - }; + constructor(public payload: Keymap) { } } + +export class DuplicateKeymapAction implements Action { + type = ActionTypes.Duplicate; + + constructor(public payload: Keymap) { + } +} + +export class EditKeymapAbbreviationAction implements Action { + type = ActionTypes.EditAbbr; + + constructor(public payload: { + abbr: string; + newAbbr: string; + name: string; + }) { + } +} + +export class EditKeymapNameAction implements Action { + type = ActionTypes.EditName; + + constructor(public payload: { + abbr: string; + name: string; + }) { + } +} + +export class SaveKeyAction implements Action { + type = ActionTypes.SaveKey; + + constructor(public payload: { + keymap: Keymap; + layer: number; + module: number; + key: number; + keyAction: KeyActionRemap; + }) { + } +} + +export class SetDefaultKeymapAction implements Action { + type = ActionTypes.SetDefault; + + constructor(public payload: string) { + } +} + +export class RemoveKeymapAction implements Action { + type = ActionTypes.Remove; + + constructor(public payload: string) { + } +} + +export class CheckMacroAction implements Action { + type = ActionTypes.CheckMacro; + + constructor(public payload: number) { + } +} + +export class LoadKeymapsAction implements Action { + type = ActionTypes.LoadKeymaps; +} + +export class LoadKeymapSuccessAction implements Action { + type = ActionTypes.LoadKeymapsSuccess; + + constructor(public payload: Keymap[]) { + } +} + +export class EditDescriptionAction implements Action { + type = ActionTypes.EditDescription; + + constructor(public payload: ChangeKeymapDescription) { + } +} + +export class UndoLastAction implements Action { + type = ActionTypes.UndoLastAction; + + constructor(public payload: UndoUserConfigData) { + } +} + +export class SelectKeymapAction implements Action { + type = ActionTypes.Select; + + constructor(public payload: string) { + } +} + +export type Actions + = AddKeymapAction + | DuplicateKeymapAction + | EditKeymapAbbreviationAction + | EditKeymapNameAction + | SaveKeyAction + | SetDefaultKeymapAction + | RemoveKeymapAction + | CheckMacroAction + | LoadKeymapsAction + | LoadKeymapSuccessAction + | EditDescriptionAction + | UndoLastAction + | SelectKeymapAction + ; diff --git a/packages/uhk-web/src/app/store/actions/macro.ts b/packages/uhk-web/src/app/store/actions/macro.ts index ce41ac20..1b038c29 100644 --- a/packages/uhk-web/src/app/store/actions/macro.ts +++ b/packages/uhk-web/src/app/store/actions/macro.ts @@ -1,91 +1,87 @@ import { Action } from '@ngrx/store'; import { Macro, MacroAction as ConfigItemMacroAction } from 'uhk-common'; -export type MacroAction = Action & { payload?: any }; +export enum ActionTypes { + Duplicate = '[Macro] Duplicate macro', + EditName = '[Macro] Edit macro title', + Remove = '[Macro] Remove macro', + Add = '[Macro] Add macro', + Select = '[Macro] Select macro', -export namespace MacroActions { - export const PREFIX = '[Macro] '; + AddAction = '[Macro] Add macro action', + SaveAction = '[Macro] Save macro action', + DeleteAction = '[Macro] Delete macro action', + ReorderAction = '[Macro] Reorder macro action' +} - export const DUPLICATE = MacroActions.PREFIX + 'Duplicate macro'; - export const EDIT_NAME = MacroActions.PREFIX + 'Edit macro title'; - export const REMOVE = MacroActions.PREFIX + 'Remove macro'; - export const ADD = MacroActions.PREFIX + 'Add macro'; +export class DuplicateMacroAction implements Action { + type = ActionTypes.Duplicate; - export const ADD_ACTION = MacroActions.PREFIX + 'Add macro action'; - export const SAVE_ACTION = MacroActions.PREFIX + 'Save macro action'; - export const DELETE_ACTION = MacroActions.PREFIX + 'Delete macro action'; - export const REORDER_ACTION = MacroActions.PREFIX + 'Reorder macro action'; - - export function addMacro(): MacroAction { - return { - type: MacroActions.ADD - }; - } - - export function removeMacro(macroId: number): MacroAction { - return { - type: MacroActions.REMOVE, - payload: macroId - }; - } - - export function duplicateMacro(macro: Macro): MacroAction { - return { - type: MacroActions.DUPLICATE, - payload: macro - }; - } - - export function editMacroName(id: number, name: string): MacroAction { - return { - type: MacroActions.EDIT_NAME, - payload: { - id: id, - name: name - } - }; - } - - export function addMacroAction(id: number, action: ConfigItemMacroAction): MacroAction { - return { - type: MacroActions.ADD_ACTION, - payload: { - id: id, - action: action - } - }; - } - - export function saveMacroAction(id: number, index: number, action: ConfigItemMacroAction): MacroAction { - return { - type: MacroActions.SAVE_ACTION, - payload: { - id: id, - index: index, - action: action - } - }; - } - - export function deleteMacroAction(id: number, index: number, action: ConfigItemMacroAction): MacroAction { - return { - type: MacroActions.DELETE_ACTION, - payload: { - id: id, - index: index, - action: action - } - }; - } - - export function reorderMacroAction(id: number, oldIndex: number, newIndex: number): MacroAction { - return { - type: MacroActions.REORDER_ACTION, - payload: { - id: id, - oldIndex: oldIndex, - newIndex: newIndex - } - }; + constructor(public payload: Macro) { } } + +export class AddMacroAction implements Action { + type = ActionTypes.Add; +} + +export class RemoveMacroAction implements Action { + type = ActionTypes.Remove; + + constructor(public payload: number) { + } +} + +export class EditMacroNameAction implements Action { + type = ActionTypes.EditName; + + constructor(public payload: { id: number, name: string }) { + } +} + +export class SelectMacroAction implements Action { + type = ActionTypes.Select; + + constructor(public payload: number) { + } +} + +export class AddMacroActionAction implements Action { + type = ActionTypes.AddAction; + + constructor(public payload: { id: number, action: ConfigItemMacroAction }) { + } +} + +export class SaveMacroActionAction implements Action { + type = ActionTypes.SaveAction; + + constructor(public payload: { id: number, index: number, action: ConfigItemMacroAction }) { + } +} + +export class DeleteMacroActionAction implements Action { + type = ActionTypes.DeleteAction; + + constructor(public payload: { id: number, index: number, action: ConfigItemMacroAction }) { + } +} + +export class ReorderMacroActionAction implements Action { + type = ActionTypes.ReorderAction; + + constructor(public payload: { id: number, oldIndex: number, newIndex: number }) { + } +} + +export type Actions + = DuplicateMacroAction + | AddMacroAction + | RemoveMacroAction + | EditMacroNameAction + | SelectMacroAction + | AddMacroActionAction + | SaveMacroActionAction + | DeleteMacroActionAction + | ReorderMacroActionAction + ; diff --git a/packages/uhk-web/src/app/store/actions/user-config.ts b/packages/uhk-web/src/app/store/actions/user-config.ts index ebf2cdfd..4df74859 100644 --- a/packages/uhk-web/src/app/store/actions/user-config.ts +++ b/packages/uhk-web/src/app/store/actions/user-config.ts @@ -1,93 +1,90 @@ import { Action } from '@ngrx/store'; -import { type, UserConfiguration, ConfigurationReply } from 'uhk-common'; +import { UserConfiguration, ConfigurationReply } from 'uhk-common'; import { UserConfigurationValue } from '../../models/user-configuration-value'; import { UploadFileData } from '../../models/upload-file-data'; -const PREFIX = '[user-config] '; - -// tslint:disable-next-line:variable-name -export const ActionTypes = { - LOAD_USER_CONFIG: type(PREFIX + 'Load User Config'), - LOAD_CONFIG_FROM_DEVICE: type(PREFIX + 'Load User Config from Device'), - LOAD_CONFIG_FROM_DEVICE_REPLY: type(PREFIX + 'Load User Config from Device reply'), - LOAD_USER_CONFIG_SUCCESS: type(PREFIX + 'Load User Config Success'), - SAVE_USER_CONFIG_SUCCESS: type(PREFIX + 'Save User Config Success'), - SAVE_USER_CONFIG_IN_JSON_FILE: type(PREFIX + 'Save User Config in JSON file'), - SAVE_USER_CONFIG_IN_BIN_FILE: type(PREFIX + 'Save User Config in binary file'), - LOAD_RESET_USER_CONFIGURATION: type(PREFIX + 'Load reset user configuration'), - RENAME_USER_CONFIGURATION: type(PREFIX + 'Rename user configuration'), - SET_USER_CONFIGURATION_VALUE: type(PREFIX + 'Set user configuration value'), - LOAD_USER_CONFIGURATION_FROM_FILE: type(PREFIX + 'Load user configuration from file'), - APPLY_USER_CONFIGURATION_FROM_FILE: type(PREFIX + 'Apply user configuration from file') -}; +export enum ActionTypes { + LoadUserConfig = '[user-config] Load User Config', + LoadConfigFromDevice = '[user-config] Load User Config from Device', + LoadConfigFromDeviceReply = '[user-config] Load User Config from Device reply', + LoadUserConfigSuccess = '[user-config] Load User Config Success', + SaveUserConfigSuccess = '[user-config] Save User Config Success', + SaveUserConfigInJsonFile = '[user-config] Save User Config in JSON file', + SaveUserConfigInBinFile = '[user-config] Save User Config in binary file', + LoadResetUserConfiguration = '[user-config] Load reset user configuration', + RenameUserConfiguration = '[user-config] Rename user configuration', + SetUserConfigurationValue = '[user-config] Set user configuration value', + LoadUserConfigurationFromFile = '[user-config] Load user configuration from file', + ApplyUserConfigurationFromFile = '[user-config] Apply user configuration from file' +} export class LoadUserConfigAction implements Action { - type = ActionTypes.LOAD_USER_CONFIG; + type = ActionTypes.LoadUserConfig; } export class LoadConfigFromDeviceAction implements Action { - type = ActionTypes.LOAD_CONFIG_FROM_DEVICE; + type = ActionTypes.LoadConfigFromDevice; } export class LoadConfigFromDeviceReplyAction implements Action { - type = ActionTypes.LOAD_CONFIG_FROM_DEVICE_REPLY; + type = ActionTypes.LoadConfigFromDeviceReply; constructor(public payload: ConfigurationReply) { } } export class LoadUserConfigSuccessAction implements Action { - type = ActionTypes.LOAD_USER_CONFIG_SUCCESS; + type = ActionTypes.LoadUserConfigSuccess; constructor(public payload: UserConfiguration) { } } export class SaveUserConfigSuccessAction implements Action { - type = ActionTypes.SAVE_USER_CONFIG_SUCCESS; + type = ActionTypes.SaveUserConfigSuccess; constructor(public payload: UserConfiguration) { } } export class SaveUserConfigInJsonFileAction implements Action { - type = ActionTypes.SAVE_USER_CONFIG_IN_JSON_FILE; + type = ActionTypes.SaveUserConfigInJsonFile; } export class SaveUserConfigInBinaryFileAction implements Action { - type = ActionTypes.SAVE_USER_CONFIG_IN_BIN_FILE; + type = ActionTypes.SaveUserConfigInBinFile; } export class LoadResetUserConfigurationAction implements Action { - type = ActionTypes.LOAD_RESET_USER_CONFIGURATION; + type = ActionTypes.LoadResetUserConfiguration; constructor(public payload: UserConfiguration) { } } export class RenameUserConfigurationAction implements Action { - type = ActionTypes.RENAME_USER_CONFIGURATION; + type = ActionTypes.RenameUserConfiguration; constructor(public payload: string) { } } export class SetUserConfigurationValueAction implements Action { - type = ActionTypes.SET_USER_CONFIGURATION_VALUE; + type = ActionTypes.SetUserConfigurationValue; constructor(public payload: UserConfigurationValue) { } } export class LoadUserConfigurationFromFileAction implements Action { - type = ActionTypes.LOAD_USER_CONFIGURATION_FROM_FILE; + type = ActionTypes.LoadUserConfigurationFromFile; constructor(public payload: UploadFileData) { } } export class ApplyUserConfigurationFromFileAction implements Action { - type = ActionTypes.APPLY_USER_CONFIGURATION_FROM_FILE; + type = ActionTypes.ApplyUserConfigurationFromFile; constructor(public payload: UserConfiguration) { } diff --git a/packages/uhk-web/src/app/store/effects/app-update.ts b/packages/uhk-web/src/app/store/effects/app-update.ts index 43f7165e..88ceff20 100644 --- a/packages/uhk-web/src/app/store/effects/app-update.ts +++ b/packages/uhk-web/src/app/store/effects/app-update.ts @@ -15,7 +15,7 @@ import { AppUpdateRendererService } from '../../services/app-update-renderer.ser export class AppUpdateEffect { @Effect({ dispatch: false }) appStart$: Observable = this.actions$ - .ofType(ActionTypes.UPDATE_APP) + .ofType(ActionTypes.UpdateApp) .pipe( first(), tap(() => { @@ -24,7 +24,7 @@ export class AppUpdateEffect { ); @Effect({ dispatch: false }) checkForUpdate$ = this.actions$ - .ofType(AutoUpdateActionTypes.CHECK_FOR_UPDATE_NOW) + .ofType(AutoUpdateActionTypes.CheckForUpdateNow) .pipe( map(action => action.payload), tap((allowPrerelease: boolean) => { @@ -34,7 +34,7 @@ export class AppUpdateEffect { ); @Effect() handleError$: Observable = this.actions$ - .ofType(ActionTypes.UPDATE_ERROR) + .ofType(ActionTypes.UpdateError) .pipe( map(action => action.payload), map((message: string) => { diff --git a/packages/uhk-web/src/app/store/effects/app.ts b/packages/uhk-web/src/app/store/effects/app.ts index d27bac35..db5f38eb 100644 --- a/packages/uhk-web/src/app/store/effects/app.ts +++ b/packages/uhk-web/src/app/store/effects/app.ts @@ -26,7 +26,7 @@ export class ApplicationEffects { @Effect() appStart$: Observable = this.actions$ - .ofType(ActionTypes.APP_BOOTSRAPPED) + .ofType(ActionTypes.AppBootstrapped) .pipe( startWith(new AppStartedAction()), tap(() => { @@ -39,14 +39,14 @@ export class ApplicationEffects { @Effect({dispatch: false}) appStartInfo$: Observable = this.actions$ - .ofType(ActionTypes.LOAD_APP_START_INFO) + .ofType(ActionTypes.LoadAppStartInfo) .do(() => { this.appRendererService.getAppStartInfo(); }); @Effect({dispatch: false}) showNotification$: Observable = this.actions$ - .ofType(ActionTypes.APP_SHOW_NOTIFICATION) + .ofType(ActionTypes.AppShowNotification) .pipe( map(action => action.payload), tap((notification: Notification) => { @@ -59,7 +59,7 @@ export class ApplicationEffects { @Effect() processStartInfo$: Observable = this.actions$ - .ofType(ActionTypes.APP_PROCESS_START_INFO) + .ofType(ActionTypes.AppProcessStartInfo) .map(action => action.payload) .mergeMap((appInfo: AppStartInfo) => { this.logService.debug('[AppEffect][processStartInfo] payload:', appInfo); @@ -70,12 +70,12 @@ export class ApplicationEffects { }); @Effect() undoLastNotification$: Observable = this.actions$ - .ofType(ActionTypes.UNDO_LAST) + .ofType(ActionTypes.UndoLast) .map(action => action.payload) .mergeMap((action: Action) => [action, new DismissUndoNotificationAction()]); @Effect({dispatch: false}) openUrlInNewWindow$ = this.actions$ - .ofType(ActionTypes.OPEN_URL_IN_NEW_WINDOW) + .ofType(ActionTypes.OpenUrlInNewWindow) .pipe( withLatestFrom(this.store.select(runningInElectron)), tap(([action, inElectron]) => { diff --git a/packages/uhk-web/src/app/store/effects/auto-update-settings.ts b/packages/uhk-web/src/app/store/effects/auto-update-settings.ts index 88868954..4565cbd8 100644 --- a/packages/uhk-web/src/app/store/effects/auto-update-settings.ts +++ b/packages/uhk-web/src/app/store/effects/auto-update-settings.ts @@ -21,7 +21,7 @@ import { ShowNotificationAction } from '../actions/app'; @Injectable() export class AutoUpdateSettingsEffects { @Effect() loadUserConfig$: Observable = this.actions$ - .ofType(ActionTypes.LOAD_AUTO_UPDATE_SETTINGS) + .ofType(ActionTypes.LoadAutoUpdateSettings) .pipe( startWith(new LoadAutoUpdateSettingsAction()), switchMap(() => { @@ -34,7 +34,7 @@ export class AutoUpdateSettingsEffects { ); @Effect() saveAutoUpdateConfig$: Observable = this.actions$ - .ofType(ActionTypes.TOGGLE_CHECK_FOR_UPDATE_ON_STARTUP, ActionTypes.TOGGLE_PRE_RELEASE_FLAG) + .ofType(ActionTypes.ToggleCheckForUpdateOnStartup, ActionTypes.TogglePreReleaseFlag) .pipe( withLatestFrom(this.store.select(getAutoUpdateSettings)), map(([action, config]) => { @@ -44,7 +44,7 @@ export class AutoUpdateSettingsEffects { ); @Effect() sendNotification$: Observable = this.actions$ - .ofType(ActionTypes.CHECK_FOR_UPDATE_FAILED, ActionTypes.CHECK_FOR_UPDATE_SUCCESS) + .ofType(ActionTypes.CheckForUpdateFailed, ActionTypes.CheckForUpdateSuccess) .pipe( map(toPayload), map((message: string) => { diff --git a/packages/uhk-web/src/app/store/effects/contributors.effect.ts b/packages/uhk-web/src/app/store/effects/contributors.effect.ts index 360a850d..49f16b3c 100644 --- a/packages/uhk-web/src/app/store/effects/contributors.effect.ts +++ b/packages/uhk-web/src/app/store/effects/contributors.effect.ts @@ -23,7 +23,7 @@ import { @Injectable() export class ContributorsEffect { @Effect() getContributors$: Observable = this.actions$ - .ofType(ActionTypes.GET_AGENT_CONTRIBUTORS) + .ofType(ActionTypes.GetAgentContributors) .pipe( withLatestFrom(this.store.select(contributors)), map(([action, state]) => { @@ -35,7 +35,7 @@ export class ContributorsEffect { ); @Effect() fetchContributors$: Observable = this.actions$ - .ofType(ActionTypes.FETCH_AGENT_CONTRIBUTORS) + .ofType(ActionTypes.FetchAgentContributors) .pipe( mergeMap(() => this.http.get(Constants.AGENT_CONTRIBUTORS_GITHUB_API_URL)), switchMap((response: UHKContributor[]) => { diff --git a/packages/uhk-web/src/app/store/effects/device.ts b/packages/uhk-web/src/app/store/effects/device.ts index e345e010..4e4ce800 100644 --- a/packages/uhk-web/src/app/store/effects/device.ts +++ b/packages/uhk-web/src/app/store/effects/device.ts @@ -52,7 +52,7 @@ import { getVersions } from '../../util'; export class DeviceEffects { @Effect() deviceConnectionStateChange$: Observable = this.actions$ - .ofType(ActionTypes.CONNECTION_STATE_CHANGED) + .ofType(ActionTypes.ConnectionStateChanged) .pipe( withLatestFrom(this.store.select(getRouterState), this.store.select(deviceConnected)), tap(([action, route]) => { @@ -93,7 +93,7 @@ export class DeviceEffects { @Effect({ dispatch: false }) setPrivilegeOnLinux$: Observable = this.actions$ - .ofType(ActionTypes.SET_PRIVILEGE_ON_LINUX) + .ofType(ActionTypes.SetPrivilegeOnLinux) .pipe( tap(() => { this.deviceRendererService.setPrivilegeOnLinux(); @@ -102,7 +102,7 @@ export class DeviceEffects { @Effect() setPrivilegeOnLinuxReply$: Observable = this.actions$ - .ofType(ActionTypes.SET_PRIVILEGE_ON_LINUX_REPLY) + .ofType(ActionTypes.SetPrivilegeOnLinuxReply) .pipe( map(action => action.payload), switchMap((response: any): any => { @@ -117,18 +117,21 @@ export class DeviceEffects { @Effect({ dispatch: false }) saveConfiguration$: Observable = this.actions$ - .ofType(ActionTypes.SAVE_CONFIGURATION) + .ofType(ActionTypes.SaveConfiguration) .pipe( withLatestFrom(this.store), tap(([action, state]) => { - setTimeout(() => this.sendUserConfigToKeyboard(state.userConfiguration, state.app.hardwareConfig), 100); + setTimeout(() => this.sendUserConfigToKeyboard( + state.userConfiguration.userConfiguration, + state.app.hardwareConfig), + 100); }), switchMap(() => empty()) ); @Effect() saveConfigurationReply$: Observable = this.actions$ - .ofType(ActionTypes.SAVE_CONFIGURATION_REPLY) + .ofType(ActionTypes.SaveConfigurationReply) .pipe( map(action => action.payload), mergeMap((response: IpcResponse) => { @@ -150,7 +153,7 @@ export class DeviceEffects { @Effect() autoHideSaveToKeyboardButton$: Observable = this.actions$ - .ofType(ActionTypes.SAVE_TO_KEYBOARD_SUCCESS) + .ofType(ActionTypes.SaveToKeyboardSuccess) .pipe( withLatestFrom(this.store), switchMap(([action, state]) => timer(1000) @@ -169,7 +172,7 @@ export class DeviceEffects { @Effect() resetMouseSpeedSettings$: Observable = this.actions$ - .ofType(ActionTypes.RESET_MOUSE_SPEED_SETTINGS) + .ofType(ActionTypes.ResetMouseSpeedSettings) .pipe( switchMap(() => { const config = this.defaultUserConfigurationService.getDefault(); @@ -194,7 +197,7 @@ export class DeviceEffects { ); @Effect() resetUserConfiguration$: Observable = this.actions$ - .ofType(ActionTypes.RESET_USER_CONFIGURATION) + .ofType(ActionTypes.ResetUserConfiguration) .pipe( switchMap(() => { const config = this.defaultUserConfigurationService.getDefault(); @@ -205,8 +208,8 @@ export class DeviceEffects { @Effect() saveResetUserConfigurationToDevice$ = this.actions$ .ofType( - UserConfigActions.LOAD_RESET_USER_CONFIGURATION, - UserConfigActions.APPLY_USER_CONFIGURATION_FROM_FILE) + UserConfigActions.LoadResetUserConfiguration, + UserConfigActions.ApplyUserConfigurationFromFile) .pipe( map(action => action.payload), switchMap((config: UserConfiguration) => { @@ -217,7 +220,7 @@ export class DeviceEffects { ); @Effect({ dispatch: false }) updateFirmware$ = this.actions$ - .ofType(ActionTypes.UPDATE_FIRMWARE) + .ofType(ActionTypes.UpdateFirmware) .pipe( tap(() => this.deviceRendererService.updateFirmware({ versionInformation: getVersions() @@ -225,7 +228,7 @@ export class DeviceEffects { ); @Effect({ dispatch: false }) updateFirmwareWith$ = this.actions$ - .ofType(ActionTypes.UPDATE_FIRMWARE_WITH) + .ofType(ActionTypes.UpdateFirmwareWith) .pipe( map(action => action.payload), tap(data => this.deviceRendererService.updateFirmware({ @@ -235,7 +238,7 @@ export class DeviceEffects { ); @Effect() updateFirmwareReply$ = this.actions$ - .ofType(ActionTypes.UPDATE_FIRMWARE_REPLY) + .ofType(ActionTypes.UpdateFirmwareReply) .pipe( map(action => action.payload), switchMap((response: FirmwareUpgradeIpcResponse) @@ -253,25 +256,25 @@ export class DeviceEffects { ); @Effect() restoreUserConfiguration$ = this.actions$ - .ofType(ActionTypes.RESTORE_CONFIGURATION_FROM_BACKUP) + .ofType(ActionTypes.RestoreConfigurationFromBackup) .pipe( map(() => new SaveConfigurationAction()) ); @Effect({ dispatch: false }) recoveryDevice$ = this.actions$ - .ofType(ActionTypes.RECOVERY_DEVICE) + .ofType(ActionTypes.RecoveryDevice) .pipe( tap(() => this.deviceRendererService.recoveryDevice()) ); @Effect({ dispatch: false }) enableUsbStackTest$ = this.actions$ - .ofType(ActionTypes.ENABLE_USB_STACK_TEST) + .ofType(ActionTypes.EnableUsbStackTest) .pipe( tap(() => this.deviceRendererService.enableUsbStackTest()) ); @Effect({ dispatch: false }) startConnectionPoller$ = this.actions$ - .ofType(ActionTypes.START_CONNECTION_POLLER) + .ofType(ActionTypes.StartConnectionPoller) .pipe( tap(() => this.deviceRendererService.startConnectionPoller()) ); diff --git a/packages/uhk-web/src/app/store/effects/keymap.ts b/packages/uhk-web/src/app/store/effects/keymap.ts index 7fa192fc..36baf9fe 100644 --- a/packages/uhk-web/src/app/store/effects/keymap.ts +++ b/packages/uhk-web/src/app/store/effects/keymap.ts @@ -9,30 +9,29 @@ import { map, pairwise, startWith, switchMap, tap, withLatestFrom } from 'rxjs/o import { Keymap } from 'uhk-common'; import { findNewItem } from '../../util'; -import { KeymapActions } from '../actions'; -import { AppState } from '../index'; -import { getKeymaps } from '../reducers/user-configuration'; +import * as Keymaps from '../actions/keymap'; +import { AppState, getKeymaps } from '../index'; @Injectable() export class KeymapEffects { @Effect() loadKeymaps$: Observable = this.actions$ - .ofType(KeymapActions.LOAD_KEYMAPS) + .ofType(Keymaps.ActionTypes.LoadKeymaps) .pipe( - startWith(KeymapActions.loadKeymaps()), + startWith(new Keymaps.LoadKeymapsAction()), switchMap(() => { const presetsRequireContext = (require).context('../../../res/presets', false, /.json$/); const uhkPresets = presetsRequireContext.keys().map(presetsRequireContext) // load the presets into an array .map((keymap: any) => new Keymap().fromJsonObject(keymap)); - return of(KeymapActions.loadKeymapsSuccess(uhkPresets)); + return of(new Keymaps.LoadKeymapSuccessAction(uhkPresets)); }) ); @Effect({ dispatch: false }) addOrDuplicate$: any = this.actions$ - .ofType(KeymapActions.ADD, KeymapActions.DUPLICATE) + .ofType(Keymaps.ActionTypes.Add, Keymaps.ActionTypes.Duplicate) .pipe( - withLatestFrom(this.store.let(getKeymaps()) + withLatestFrom(this.store.select(getKeymaps) .pipe( pairwise() ) @@ -45,10 +44,10 @@ export class KeymapEffects { ); @Effect({ dispatch: false }) remove$: any = this.actions$ - .ofType(KeymapActions.REMOVE) + .ofType(Keymaps.ActionTypes.Remove) .pipe( - withLatestFrom(this.store), - map(latest => latest[1].userConfiguration.keymaps), + withLatestFrom(this.store.select(getKeymaps)), + map(latest => latest[1]), tap(keymaps => { if (keymaps.length === 0) { this.router.navigate(['/keymap/add']); @@ -60,11 +59,11 @@ export class KeymapEffects { ); @Effect({ dispatch: false }) editAbbr$: any = this.actions$ - .ofType(KeymapActions.EDIT_ABBR) + .ofType(Keymaps.ActionTypes.EditAbbr) .pipe( - withLatestFrom(this.store), - tap(([action, store]: [KeymapActions.EditKeymapAbbreviationAction, AppState]) => { - for (const keymap of store.userConfiguration.keymaps) { + withLatestFrom(this.store.select(getKeymaps)), + tap(([action, keymaps]: [Keymaps.EditKeymapAbbreviationAction, Keymap[]]) => { + for (const keymap of keymaps) { if (keymap.name === action.payload.name && keymap.abbreviation === action.payload.newAbbr) { this.router.navigate(['/keymap', action.payload.newAbbr]); return; diff --git a/packages/uhk-web/src/app/store/effects/macro.ts b/packages/uhk-web/src/app/store/effects/macro.ts index ca888b69..9bb66063 100644 --- a/packages/uhk-web/src/app/store/effects/macro.ts +++ b/packages/uhk-web/src/app/store/effects/macro.ts @@ -6,20 +6,20 @@ import { Store, Action } from '@ngrx/store'; import { map, pairwise, tap, withLatestFrom } from 'rxjs/operators'; import { Macro } from 'uhk-common'; -import { KeymapActions, MacroAction, MacroActions } from '../actions'; -import { AppState } from '../index'; -import { getMacros } from '../reducers/user-configuration'; +import * as Keymaps from '../actions/keymap'; +import * as Macros from '../actions/macro'; +import { AppState, getMacros } from '..'; import { findNewItem } from '../../util'; @Injectable() export class MacroEffects { @Effect({ dispatch: false }) remove$: any = this.actions$ - .ofType(MacroActions.REMOVE) + .ofType(Macros.ActionTypes.Remove) .pipe( - tap(action => this.store.dispatch(KeymapActions.checkMacro(action.payload))), - withLatestFrom(this.store), - map(([action, state]) => state.userConfiguration.macros), + tap(action => this.store.dispatch(new Keymaps.CheckMacroAction(action.payload))), + withLatestFrom(this.store.select(getMacros)), + map(([action, macros]) => macros), tap(macros => { if (macros.length === 0) { return this.router.navigate(['/macro']); @@ -31,9 +31,9 @@ export class MacroEffects { ); @Effect({ dispatch: false }) addOrDuplicate$: any = this.actions$ - .ofType(MacroActions.ADD, MacroActions.DUPLICATE) + .ofType(Macros.ActionTypes.Add, Macros.ActionTypes.Duplicate) .pipe( - withLatestFrom(this.store.let(getMacros()) + withLatestFrom(this.store.select(getMacros) .pipe( pairwise() ) @@ -42,7 +42,7 @@ export class MacroEffects { tap(([action, prevMacros, newMacros]: [Action, Macro[], Macro[]]) => { const newMacro = findNewItem(prevMacros, newMacros); const commands = ['/macro', newMacro.id]; - if (action.type === MacroActions.ADD) { + if (action.type === Macros.ActionTypes.Add) { commands.push('new'); } this.router.navigate(commands); diff --git a/packages/uhk-web/src/app/store/effects/user-config.ts b/packages/uhk-web/src/app/store/effects/user-config.ts index 4371e3c2..839b42ce 100644 --- a/packages/uhk-web/src/app/store/effects/user-config.ts +++ b/packages/uhk-web/src/app/store/effects/user-config.ts @@ -31,7 +31,8 @@ import { import { DataStorageRepositoryService } from '../../services/datastorage-repository.service'; import { DefaultUserConfigurationService } from '../../services/default-user-configuration.service'; import { AppState, getPrevUserConfiguration, getRouterState, getUserConfiguration } from '../index'; -import { KeymapAction, KeymapActions, MacroAction, MacroActions } from '../actions'; +import * as Keymaps from '../actions/keymap'; +import * as Macros from '../actions/macro'; import { DismissUndoNotificationAction, LoadHardwareConfigurationSuccessAction, @@ -56,23 +57,25 @@ export class UserConfigEffects { @Effect() saveUserConfig$: Observable = (this.actions$ .ofType( - KeymapActions.ADD, KeymapActions.DUPLICATE, KeymapActions.EDIT_NAME, KeymapActions.EDIT_ABBR, - KeymapActions.SET_DEFAULT, KeymapActions.REMOVE, KeymapActions.SAVE_KEY, KeymapActions.EDIT_DESCRIPTION, - MacroActions.ADD, MacroActions.DUPLICATE, MacroActions.EDIT_NAME, MacroActions.REMOVE, MacroActions.ADD_ACTION, - MacroActions.SAVE_ACTION, MacroActions.DELETE_ACTION, MacroActions.REORDER_ACTION, - ActionTypes.RENAME_USER_CONFIGURATION, ActionTypes.SET_USER_CONFIGURATION_VALUE - ) as Observable) + Keymaps.ActionTypes.Add, Keymaps.ActionTypes.Duplicate, Keymaps.ActionTypes.EditName, Keymaps.ActionTypes.EditAbbr, + Keymaps.ActionTypes.SetDefault, Keymaps.ActionTypes.Remove, Keymaps.ActionTypes.SaveKey, + Keymaps.ActionTypes.EditDescription, + Macros.ActionTypes.Add, Macros.ActionTypes.Duplicate, Macros.ActionTypes.EditName, Macros.ActionTypes.Remove, + Macros.ActionTypes.AddAction, Macros.ActionTypes.SaveAction, Macros.ActionTypes.DeleteAction, + Macros.ActionTypes.ReorderAction, + ActionTypes.RenameUserConfiguration, ActionTypes.SetUserConfigurationValue + ) as Observable) .pipe( withLatestFrom(this.store.select(getUserConfiguration), this.store.select(getPrevUserConfiguration)), mergeMap(([action, config, prevUserConfiguration]) => { config.recalculateConfigurationLength(); this.dataStorageRepository.saveConfig(config); - if (action.type === KeymapActions.REMOVE || action.type === MacroActions.REMOVE) { - const text = action.type === KeymapActions.REMOVE ? 'Keymap' : 'Macro'; - const pathPrefix = action.type === KeymapActions.REMOVE ? 'keymap' : 'macro'; + if (action.type === Keymaps.ActionTypes.Remove || action.type === Macros.ActionTypes.Remove) { + const text = action.type === Keymaps.ActionTypes.Remove ? 'Keymap' : 'Macro'; + const pathPrefix = action.type === Keymaps.ActionTypes.Remove ? 'keymap' : 'macro'; const payload: UndoUserConfigData = { - path: `/${pathPrefix}/${action.payload}`, + path: `/${pathPrefix}/${(action as Keymaps.RemoveKeymapAction | Macros.RemoveMacroAction).payload}`, config: prevUserConfiguration.toJsonObject() }; @@ -83,7 +86,7 @@ export class UserConfigEffects { message: `${text} has been deleted`, extra: { payload, - type: KeymapActions.UNDO_LAST_ACTION + type: Keymaps.ActionTypes.UndoLastAction } }), new ShowSaveToKeyboardButtonAction() @@ -99,7 +102,7 @@ export class UserConfigEffects { ); @Effect() undoUserConfig$: Observable = this.actions$ - .ofType(KeymapActions.UNDO_LAST_ACTION) + .ofType(Keymaps.ActionTypes.UndoLastAction) .pipe( map(action => action.payload), mergeMap((payload: UndoUserConfigData) => { @@ -112,13 +115,13 @@ export class UserConfigEffects { ); @Effect({ dispatch: false }) loadConfigFromDevice$ = this.actions$ - .ofType(ActionTypes.LOAD_CONFIG_FROM_DEVICE) + .ofType(ActionTypes.LoadConfigFromDevice) .pipe( tap(() => this.deviceRendererService.loadConfigurationFromKeyboard()) ); @Effect() loadConfigFromDeviceReply$ = this.actions$ - .ofType(ActionTypes.LOAD_CONFIG_FROM_DEVICE_REPLY) + .ofType(ActionTypes.LoadConfigFromDeviceReply) .pipe( withLatestFrom(this.store.select(getRouterState)), mergeMap(([action, route]): any => { @@ -175,7 +178,7 @@ export class UserConfigEffects { ); @Effect({ dispatch: false }) saveUserConfigInJsonFile$ = this.actions$ - .ofType(ActionTypes.SAVE_USER_CONFIG_IN_JSON_FILE) + .ofType(ActionTypes.SaveUserConfigInJsonFile) .pipe( withLatestFrom(this.store.select(getUserConfiguration)), tap(([action, userConfiguration]) => { @@ -186,7 +189,7 @@ export class UserConfigEffects { ); @Effect({ dispatch: false }) saveUserConfigInBinFile$ = this.actions$ - .ofType(ActionTypes.SAVE_USER_CONFIG_IN_BIN_FILE) + .ofType(ActionTypes.SaveUserConfigInBinFile) .pipe( withLatestFrom(this.store.select(getUserConfiguration)), tap(([action, userConfiguration]) => { @@ -198,7 +201,7 @@ export class UserConfigEffects { ); @Effect() loadUserConfigurationFromFile$ = this.actions$ - .ofType(ActionTypes.LOAD_USER_CONFIGURATION_FROM_FILE) + .ofType(ActionTypes.LoadUserConfigurationFromFile) .pipe( map(action => action.payload), map((info: UploadFileData) => { diff --git a/packages/uhk-web/src/app/store/index.ts b/packages/uhk-web/src/app/store/index.ts index a69d0ed7..ada71cd6 100644 --- a/packages/uhk-web/src/app/store/index.ts +++ b/packages/uhk-web/src/app/store/index.ts @@ -1,5 +1,4 @@ -import { createSelector } from 'reselect'; -import { ActionReducerMap, MetaReducer } from '@ngrx/store'; +import { ActionReducerMap, createSelector, MetaReducer } from '@ngrx/store'; import { routerReducer, RouterReducerState } from '@ngrx/router-store'; import { storeFreeze } from 'ngrx-store-freeze'; import { HardwareModules, Keymap, UserConfiguration } from 'uhk-common'; @@ -20,7 +19,7 @@ import { isVersionGte } from '../util'; // State interface for the application export interface AppState { - userConfiguration: UserConfiguration; + userConfiguration: fromUserConfig.State; presetKeymaps: Keymap[]; autoUpdateSettings: autoUpdateSettings.State; app: fromApp.State; @@ -45,10 +44,18 @@ export const metaReducers: MetaReducer[] = environment.production ? [] : [storeFreeze]; -export const getUserConfiguration = (state: AppState) => state.userConfiguration; +export const userConfigState = (state: AppState) => state.userConfiguration; + +export const getUserConfiguration = createSelector(userConfigState, fromUserConfig.getUserConfiguration); +export const getKeymaps = createSelector(userConfigState, fromUserConfig.getKeymaps); +export const getDefaultKeymap = createSelector(userConfigState, fromUserConfig.getDefaultKeymap); +export const getSelectedKeymap = createSelector(userConfigState, fromUserConfig.getSelectedKeymap); +export const getMacros = createSelector(userConfigState, fromUserConfig.getMacros); +export const getSelectedMacro = createSelector(userConfigState, fromUserConfig.getSelectedMacro); +export const isKeymapDeletable = createSelector(userConfigState, fromUserConfig.isKeymapDeletable); +export const hasMacro = createSelector(userConfigState, fromUserConfig.hasMacro); export const appState = (state: AppState) => state.app; - export const showAddonMenu = createSelector(appState, fromApp.showAddonMenu); export const getUndoableNotification = createSelector(appState, fromApp.getUndoableNotification); export const getPrevUserConfiguration = createSelector(appState, fromApp.getPrevUserConfiguration); diff --git a/packages/uhk-web/src/app/store/reducers/app-update.reducer.ts b/packages/uhk-web/src/app/store/reducers/app-update.reducer.ts index 503e87d4..19c2cb90 100644 --- a/packages/uhk-web/src/app/store/reducers/app-update.reducer.ts +++ b/packages/uhk-web/src/app/store/reducers/app-update.reducer.ts @@ -1,5 +1,6 @@ -import { Actions, ActionTypes, UpdateDownloadedAction } from '../actions/app-update.action'; +import * as AppUpdate from '../actions/app-update.action'; import { UpdateInfo } from '../../models/update-info'; +import { UpdateDownloadedAction } from '../actions/app-update.action'; export interface State { updateAvailable: boolean; @@ -18,22 +19,22 @@ export const initialState: State = { } }; -export function reducer(state = initialState, action: Actions) { +export function reducer(state = initialState, action: AppUpdate.Actions) { switch (action.type) { - case ActionTypes.UPDATE_AVAILABLE: + case AppUpdate.ActionTypes.UpdateAvailable: return { ...state, updateAvailable: true }; - case ActionTypes.UPDATE_DOWNLOADED: + case AppUpdate.ActionTypes.UpdateDownloaded: return { ...state, updateDownloaded: true, updateInfo: (action as UpdateDownloadedAction).payload }; - case ActionTypes.DO_NOT_UPDATE_APP: + case AppUpdate.ActionTypes.DoNotUpdateApp: return { ...state, doNotUpdateApp: true diff --git a/packages/uhk-web/src/app/store/reducers/app.reducer.ts b/packages/uhk-web/src/app/store/reducers/app.reducer.ts index 1928d1d8..e4edaaf0 100644 --- a/packages/uhk-web/src/app/store/reducers/app.reducer.ts +++ b/packages/uhk-web/src/app/store/reducers/app.reducer.ts @@ -1,7 +1,5 @@ -import { ROUTER_NAVIGATION } from '@ngrx/router-store'; -import { Action } from '@ngrx/store'; +import { ROUTER_NAVIGATION, RouterNavigationAction } from '@ngrx/router-store'; import { - AppStartInfo, CommandLineArgs, HardwareConfiguration, Notification, @@ -11,9 +9,9 @@ import { VersionInformation } from 'uhk-common'; -import { ActionTypes, ShowNotificationAction } from '../actions/app'; -import { ActionTypes as UserConfigActionTypes } from '../actions/user-config'; -import { ActionTypes as DeviceActionTypes } from '../actions/device'; +import * as App from '../actions/app'; +import { ActionTypes as UserConfigActionTypes, SaveUserConfigSuccessAction } from '../actions/user-config'; +import { ActionTypes as DeviceActionTypes, ConnectionStateChangedAction } from '../actions/device'; import { KeyboardLayout } from '../../keyboard/keyboard-layout.enum'; import { getVersions } from '../../util'; @@ -45,17 +43,19 @@ export const initialState: State = { keypressCapturing: false }; -export function reducer(state = initialState, action: Action & { payload: any }) { +export function reducer( + state = initialState, + action: App.Actions | RouterNavigationAction | SaveUserConfigSuccessAction | ConnectionStateChangedAction): State { switch (action.type) { - case ActionTypes.APP_STARTED: { + case App.ActionTypes.AppStarted: { return { ...state, started: true }; } - case ActionTypes.APPLY_APP_START_INFO: { - const payload = action.payload as AppStartInfo; + case App.ActionTypes.ApplyAppStartInfo: { + const payload = (action as App.ApplyAppStartInfoAction).payload; return { ...state, @@ -65,8 +65,8 @@ export function reducer(state = initialState, action: Action & { payload: any }) }; } - case ActionTypes.APP_SHOW_NOTIFICATION: { - const currentAction = action; + case App.ActionTypes.AppShowNotification: { + const currentAction = action; if (currentAction.payload.type !== NotificationType.Undoable) { return state; } @@ -81,7 +81,7 @@ export function reducer(state = initialState, action: Action & { payload: any }) // When deleted a keymap or macro the app automaticaly navigate to other keymap, or macro, so // so we have to count the navigations and when reach the 2nd then remove the dialog. case ROUTER_NAVIGATION: { - const newState = {...state}; + const newState = { ...state }; newState.navigationCountAfterNotification++; if (newState.navigationCountAfterNotification > 1) { @@ -91,40 +91,41 @@ export function reducer(state = initialState, action: Action & { payload: any }) return newState; } - case ActionTypes.UNDO_LAST_SUCCESS: - case ActionTypes.DISMISS_UNDO_NOTIFICATION: { + case App.ActionTypes.UndoLastSuccess: + case App.ActionTypes.DismissUndoNotification: { return { ...state, undoableNotification: null }; } - case UserConfigActionTypes.LOAD_USER_CONFIG_SUCCESS: - case UserConfigActionTypes.SAVE_USER_CONFIG_SUCCESS: { + case UserConfigActionTypes.LoadUserConfigSuccess: + case UserConfigActionTypes.SaveUserConfigSuccess: { return { ...state, - prevUserConfig: action.payload, + prevUserConfig: (action as SaveUserConfigSuccessAction).payload, configLoading: false }; } - case UserConfigActionTypes.LOAD_CONFIG_FROM_DEVICE: - case UserConfigActionTypes.LOAD_USER_CONFIG: { + case UserConfigActionTypes.LoadConfigFromDevice: + case UserConfigActionTypes.LoadUserConfig: { return { ...state, configLoading: true }; } - case ActionTypes.LOAD_HARDWARE_CONFIGURATION_SUCCESS: + case App.ActionTypes.LoadHardwareConfigurationSuccess: return { ...state, - hardwareConfig: action.payload + hardwareConfig: (action as App.LoadHardwareConfigurationSuccessAction).payload }; - case DeviceActionTypes.CONNECTION_STATE_CHANGED: { + case DeviceActionTypes.ConnectionStateChanged: { + const connectionState = (action as ConnectionStateChangedAction).payload; - if (action.payload === true) { + if (connectionState && connectionState.connected === true) { return state; } @@ -134,31 +135,31 @@ export function reducer(state = initialState, action: Action & { payload: any }) }; } - case ActionTypes.PRIVILEGE_WHAT_WILL_THIS_DO: + case App.ActionTypes.PrivilegeWhatWillThisDo: return { ...state, privilegeWhatWillThisDoClicked: true }; - case ActionTypes.SETUP_PERMISSION_ERROR: + case App.ActionTypes.SetupPermissionError: return { ...state, - permissionError: action.payload + permissionError: (action as App.SetupPermissionErrorAction).payload }; - case DeviceActionTypes.SET_PRIVILEGE_ON_LINUX: + case DeviceActionTypes.SetPrivilegeOnLinux: return { ...state, permissionError: null }; - case ActionTypes.START_KEYPRESS_CAPTURING: + case App.ActionTypes.StartKeypressCapturing: return { ...state, keypressCapturing: true }; - case ActionTypes.STOP_KEYPRESS_CAPTURING: + case App.ActionTypes.StopKeypressCapturing: return { ...state, keypressCapturing: false diff --git a/packages/uhk-web/src/app/store/reducers/auto-update-settings.ts b/packages/uhk-web/src/app/store/reducers/auto-update-settings.ts index 55e0a73b..933673d7 100644 --- a/packages/uhk-web/src/app/store/reducers/auto-update-settings.ts +++ b/packages/uhk-web/src/app/store/reducers/auto-update-settings.ts @@ -1,8 +1,7 @@ -import { Action } from '@ngrx/store'; import { AutoUpdateSettings } from 'uhk-common'; -import { ActionTypes } from '../actions/auto-update-settings'; -import { ActionTypes as UpdateActions } from '../actions/app-update.action'; +import * as AutoUpdate from '../actions/auto-update-settings'; +import * as AppUpdate from '../actions/app-update.action'; export interface State extends AutoUpdateSettings { checkingForUpdate: boolean; @@ -14,28 +13,43 @@ export const initialState: State = { checkingForUpdate: false }; -export function reducer(state = initialState, action: Action & { payload?: any}): State { +export function reducer(state = initialState, action: AutoUpdate.Actions | AppUpdate.Actions): State { switch (action.type) { - case ActionTypes.TOGGLE_CHECK_FOR_UPDATE_ON_STARTUP: { - return Object.assign({}, state, { checkForUpdateOnStartUp: action.payload }); + case AutoUpdate.ActionTypes.ToggleCheckForUpdateOnStartup: { + return { + ...state, + checkForUpdateOnStartUp: (action as AutoUpdate.ToggleCheckForUpdateOnStartupAction).payload + }; } - case ActionTypes.TOGGLE_PRE_RELEASE_FLAG: { - return Object.assign({}, state, { usePreReleaseUpdate: action.payload }); + case AutoUpdate.ActionTypes.TogglePreReleaseFlag: { + return { + ...state, + usePreReleaseUpdate: (action as AutoUpdate.TogglePreReleaseFlagAction).payload + }; } - case ActionTypes.LOAD_AUTO_UPDATE_SETTINGS_SUCCESS: { - return Object.assign({}, action.payload); + case AutoUpdate.ActionTypes.LoadAutoUpdateSettingSuccess: { + return { + ...state, + ...(action as AutoUpdate.LoadAutoUpdateSettingsSuccessAction).payload + }; } - case ActionTypes.CHECK_FOR_UPDATE_NOW: { - return Object.assign({}, state, { checkingForUpdate: true}); + case AutoUpdate.ActionTypes.CheckForUpdateNow: { + return { + ...state, + checkingForUpdate: true + }; } - case UpdateActions.UPDATE_ERROR: - case ActionTypes.CHECK_FOR_UPDATE_SUCCESS: - case ActionTypes.CHECK_FOR_UPDATE_FAILED: { - return Object.assign({}, state, { checkingForUpdate: false }); + case AppUpdate.ActionTypes.UpdateError: + case AutoUpdate.ActionTypes.CheckForUpdateSuccess: + case AutoUpdate.ActionTypes.CheckForUpdateFailed: { + return { + ...state, + checkingForUpdate: false + }; } default: diff --git a/packages/uhk-web/src/app/store/reducers/contributors.reducer.ts b/packages/uhk-web/src/app/store/reducers/contributors.reducer.ts index 23881774..9deaedca 100644 --- a/packages/uhk-web/src/app/store/reducers/contributors.reducer.ts +++ b/packages/uhk-web/src/app/store/reducers/contributors.reducer.ts @@ -1,5 +1,4 @@ -import { Actions, ActionTypes } from '../actions/contributors.action'; -import { AgentContributorsAvailableAction, AgentContributorsNotAvailableAction } from '../actions/contributors.action'; +import * as Contributors from '../actions/contributors.action'; import { UHKContributor } from '../../models/uhk-contributor'; export interface State { @@ -14,33 +13,33 @@ export const initialState: State = { error: null }; -export function reducer(state = initialState, action: Actions) { +export function reducer(state = initialState, action: Contributors.Actions) { switch (action.type) { - case ActionTypes.GET_AGENT_CONTRIBUTORS: { + case Contributors.ActionTypes.GetAgentContributors: { return { ...state }; } - case ActionTypes.FETCH_AGENT_CONTRIBUTORS: { + case Contributors.ActionTypes.FetchAgentContributors: { return { ...state, isLoading: true }; } - case ActionTypes.AGENT_CONTRIBUTORS_AVAILABLE: { + case Contributors.ActionTypes.AgentContributorsAvailable: { return { ...state, - contributors: (action).payload, + contributors: (action).payload, isLoading: false }; } - case ActionTypes.AGENT_CONTRIBUTORS_NOT_AVAILABLE: { + case Contributors.ActionTypes.AgentContributorsNotAvailable: { return { ...state, - error: (action).payload, + error: (action).payload, isLoading: false }; } diff --git a/packages/uhk-web/src/app/store/reducers/device.ts b/packages/uhk-web/src/app/store/reducers/device.ts index 03be3629..5c0a9dee 100644 --- a/packages/uhk-web/src/app/store/reducers/device.ts +++ b/packages/uhk-web/src/app/store/reducers/device.ts @@ -1,16 +1,8 @@ import { Action } from '@ngrx/store'; import { HardwareModules, UdevRulesInfo } from 'uhk-common'; -import { - ActionTypes, - ConnectionStateChangedAction, - HardwareModulesLoadedAction, - HasBackupUserConfigurationAction, - SaveConfigurationAction, - UpdateFirmwareFailedAction, - UpdateFirmwareSuccessAction -} from '../actions/device'; -import { ActionTypes as AppActions, ElectronMainLogReceivedAction } from '../actions/app'; +import * as Device from '../actions/device'; +import * as App from '../actions/app'; import { initProgressButtonState, ProgressButtonState } from './progress-button-state'; import { XtermCssClass, XtermLog } from '../../models/xterm-log'; import { RestoreConfigurationState } from '../../models/restore-configuration-state'; @@ -60,8 +52,8 @@ export const initialState: State = { export function reducer(state = initialState, action: Action): State { switch (action.type) { - case ActionTypes.CONNECTION_STATE_CHANGED: { - const data = (action).payload; + case Device.ActionTypes.ConnectionStateChanged: { + const data = (action).payload; return { ...state, connected: data.connected, @@ -72,25 +64,25 @@ export function reducer(state = initialState, action: Action): State { }; } - case ActionTypes.SAVING_CONFIGURATION: { + case Device.ActionTypes.SavingConfiguration: { return { ...state, savingToKeyboard: true }; } - case ActionTypes.SHOW_SAVE_TO_KEYBOARD_BUTTON: { + case Device.ActionTypes.ShowSaveToKeyboardButton: { return { ...state, saveToKeyboard: { showButton: true, text: 'Save to keyboard', - action: new SaveConfigurationAction() + action: new Device.SaveConfigurationAction() } }; } - case ActionTypes.SAVE_CONFIGURATION: { + case Device.ActionTypes.SaveConfiguration: { return { ...state, saveToKeyboard: { @@ -101,7 +93,7 @@ export function reducer(state = initialState, action: Action): State { }; } - case ActionTypes.SAVE_TO_KEYBOARD_SUCCESS: { + case Device.ActionTypes.SaveToKeyboardSuccess: { return { ...state, saveToKeyboard: { @@ -113,26 +105,26 @@ export function reducer(state = initialState, action: Action): State { }; } - case ActionTypes.SAVE_TO_KEYBOARD_FAILED: { + case Device.ActionTypes.SaveToKeyboardFailed: { return { ...state, saveToKeyboard: { showButton: true, text: 'Save to keyboard', - action: new SaveConfigurationAction() + action: new Device.SaveConfigurationAction() } }; } - case ActionTypes.HIDE_SAVE_TO_KEYBOARD_BUTTON: { + case Device.ActionTypes.HideSaveToKeyboardButton: { return { ...state, saveToKeyboard: initProgressButtonState }; } - case ActionTypes.UPDATE_FIRMWARE_WITH: - case ActionTypes.UPDATE_FIRMWARE: + case Device.ActionTypes.UpdateFirmwareWith: + case Device.ActionTypes.UpdateFirmware: return { ...state, updatingFirmware: true, @@ -142,17 +134,17 @@ export function reducer(state = initialState, action: Action): State { log: [{message: 'Start flashing firmware', cssClass: XtermCssClass.standard}] }; - case ActionTypes.UPDATE_FIRMWARE_SUCCESS: + case Device.ActionTypes.UpdateFirmwareSuccess: return { ...state, updatingFirmware: false, firmwareUpdateFinished: true, firmwareUpdateSuccess: true, - modules: (action as UpdateFirmwareSuccessAction).payload + modules: (action as Device.UpdateFirmwareSuccessAction).payload }; - case ActionTypes.UPDATE_FIRMWARE_FAILED: { - const data = (action as UpdateFirmwareFailedAction).payload; + case Device.ActionTypes.UpdateFirmwareFailed: { + const data = (action as Device.UpdateFirmwareFailedAction).payload; const logEntry = { message: data.error.message, cssClass: XtermCssClass.error @@ -168,12 +160,12 @@ export function reducer(state = initialState, action: Action): State { }; } - case AppActions.ELECTRON_MAIN_LOG_RECEIVED: { + case App.ActionTypes.ElectronMainLogReceived: { if (!state.updatingFirmware) { return state; } - const payload = (action as ElectronMainLogReceivedAction).payload; + const payload = (action as App.ElectronMainLogReceivedAction).payload; if (payload.message.indexOf('UHK Device not found:') > -1) { return state; @@ -190,32 +182,32 @@ export function reducer(state = initialState, action: Action): State { }; } - case ActionTypes.MODULES_INFO_LOADED: + case Device.ActionTypes.ModulesInfoLoaded: return { ...state, - modules: (action as HardwareModulesLoadedAction).payload + modules: (action as Device.HardwareModulesLoadedAction).payload }; - case ActionTypes.RESET_USER_CONFIGURATION: - case ActionTypes.RESTORE_CONFIGURATION_FROM_BACKUP: + case Device.ActionTypes.ResetUserConfiguration: + case Device.ActionTypes.RestoreConfigurationFromBackup: return { ...state, restoringUserConfiguration: true }; - case ActionTypes.HAS_BACKUP_USER_CONFIGURATION: + case Device.ActionTypes.HasBackupUserConfiguration: return { ...state, - hasBackupUserConfiguration: (action as HasBackupUserConfigurationAction).payload + hasBackupUserConfiguration: (action as Device.HasBackupUserConfigurationAction).payload }; - case ActionTypes.RESTORE_CONFIGURATION_FROM_BACKUP_SUCCESS: + case Device.ActionTypes.RestoreConfigurationFromBackupSuccess: return { ...state, hasBackupUserConfiguration: false }; - case ActionTypes.RECOVERY_DEVICE: { + case Device.ActionTypes.RecoveryDevice: { return { ...state, updatingFirmware: true, diff --git a/packages/uhk-web/src/app/store/reducers/preset.ts b/packages/uhk-web/src/app/store/reducers/preset.ts index 5e1180ac..b4578970 100644 --- a/packages/uhk-web/src/app/store/reducers/preset.ts +++ b/packages/uhk-web/src/app/store/reducers/preset.ts @@ -1,13 +1,13 @@ import { Keymap } from 'uhk-common'; -import { KeymapAction, KeymapActions } from '../actions'; +import * as Keymaps from '../actions/keymap'; export const initialState: Keymap[] = []; -export function reducer(state = initialState, action: KeymapAction): Keymap[] { +export function reducer(state = initialState, action: Keymaps.Actions): Keymap[] { switch (action.type) { - case KeymapActions.LOAD_KEYMAPS_SUCCESS: { - return (action as KeymapActions.LoadKeymapSuccessAction).payload ; + case Keymaps.ActionTypes.LoadKeymapsSuccess: { + return (action as Keymaps.LoadKeymapSuccessAction).payload ; } default: diff --git a/packages/uhk-web/src/app/store/reducers/user-configuration.spec.ts b/packages/uhk-web/src/app/store/reducers/user-configuration.spec.ts index 8c1b4c88..d14452c8 100644 --- a/packages/uhk-web/src/app/store/reducers/user-configuration.spec.ts +++ b/packages/uhk-web/src/app/store/reducers/user-configuration.spec.ts @@ -10,7 +10,7 @@ import { } from 'uhk-common'; import { getDefaultUserConfig } from '../../../../test/user-config-helper'; -import { KeymapActions } from '../actions'; +import * as Keymaps from '../actions/keymap'; describe('user-configuration reducer', () => { it('should be initiate with default state', () => { @@ -21,10 +21,10 @@ describe('user-configuration reducer', () => { describe('SAVE_KEY', () => { it('should process KeyStrokeAction', () => { const defaultUserConfig = new UserConfiguration().fromJsonObject(getDefaultUserConfig()); - const state = new UserConfiguration().fromJsonObject(getDefaultUserConfig()); - const keystrokeAction = new KeystrokeAction({_scancode: 100, type: KeystrokeType.basic} as any); - const saveKeyAction: KeymapActions.SaveKeyAction = { - type: KeymapActions.SAVE_KEY, + const state = { userConfiguration: new UserConfiguration().fromJsonObject(getDefaultUserConfig()) }; + const keystrokeAction = new KeystrokeAction({ _scancode: 100, type: KeystrokeType.basic } as any); + const saveKeyAction: Keymaps.SaveKeyAction = { + type: Keymaps.ActionTypes.SaveKey, payload: { keymap: new Keymap(defaultUserConfig.keymaps[0]), layer: 0, @@ -37,7 +37,7 @@ describe('user-configuration reducer', () => { } } }; - const result = reducer(state, saveKeyAction); + const result = reducer(state, saveKeyAction).userConfiguration; const expectedKeyAction = result.keymaps[0].layers[0].modules[0].keyActions[0]; expect(expectedKeyAction).toEqual(keystrokeAction); expect(result).not.toBe(defaultUserConfig); @@ -52,15 +52,15 @@ describe('user-configuration reducer', () => { it('should copy the SwitchLayerAction to the destination layer', () => { const defaultUserConfig = new UserConfiguration().fromJsonObject(getDefaultUserConfig()); - const state = new UserConfiguration().fromJsonObject(getDefaultUserConfig()); + const state = { userConfiguration: new UserConfiguration().fromJsonObject(getDefaultUserConfig()) }; const destinationLayerId = LayerName.mod; const switchLayerAction = new SwitchLayerAction({ switchLayerMode: SwitchLayerMode.toggle, layer: destinationLayerId } as any); - const saveKeyAction: KeymapActions.SaveKeyAction = { - type: KeymapActions.SAVE_KEY, + const saveKeyAction: Keymaps.SaveKeyAction = { + type: Keymaps.ActionTypes.SaveKey, payload: { keymap: new Keymap(defaultUserConfig.keymaps[0]), layer: 0, @@ -74,7 +74,7 @@ describe('user-configuration reducer', () => { } }; - const result = reducer(state, saveKeyAction); + const result = reducer(state, saveKeyAction).userConfiguration; expect(result).not.toBe(defaultUserConfig); expect(result.toJsonObject()).toEqual({ userConfigVersion: 4, @@ -238,15 +238,15 @@ describe('user-configuration reducer', () => { it('should copy the SwitchLayerAction to the destination layer and clear the modified', () => { const defaultUserConfig = new UserConfiguration().fromJsonObject(getDefaultUserConfig()); - const state = new UserConfiguration().fromJsonObject(getDefaultUserConfig()); + const state = { userConfiguration: new UserConfiguration().fromJsonObject(getDefaultUserConfig()) }; const destinationLayerId = LayerName.fn; const switchLayerAction = new SwitchLayerAction({ switchLayerMode: SwitchLayerMode.toggle, layer: destinationLayerId } as any); - const saveKeyAction: KeymapActions.SaveKeyAction = { - type: KeymapActions.SAVE_KEY, + const saveKeyAction: Keymaps.SaveKeyAction = { + type: Keymaps.ActionTypes.SaveKey, payload: { keymap: new Keymap(defaultUserConfig.keymaps[0]), layer: 0, @@ -260,7 +260,7 @@ describe('user-configuration reducer', () => { } }; - const result = reducer(state, saveKeyAction); + const result = reducer(state, saveKeyAction).userConfiguration; expect(result).not.toBe(defaultUserConfig); expect(result.toJsonObject()).toEqual({ userConfigVersion: 4, diff --git a/packages/uhk-web/src/app/store/reducers/user-configuration.ts b/packages/uhk-web/src/app/store/reducers/user-configuration.ts index 55943eeb..dfd500ae 100644 --- a/packages/uhk-web/src/app/store/reducers/user-configuration.ts +++ b/packages/uhk-web/src/app/store/reducers/user-configuration.ts @@ -1,9 +1,3 @@ -import { Action } from '@ngrx/store'; - -import { Observable } from 'rxjs/Observable'; -import { of } from 'rxjs/observable/of'; -import { map } from 'rxjs/operators'; - import { KeyAction, KeyActionHelper, @@ -17,102 +11,148 @@ import { SwitchLayerAction, UserConfiguration } from 'uhk-common'; -import { KeymapActions, MacroActions } from '../actions'; -import { AppState } from '../index'; -import { ActionTypes } from '../actions/user-config'; +import * as KeymapActions from '../actions/keymap'; +import * as MacroActions from '../actions/macro'; +import * as UserConfig from '../actions/user-config'; import { isValidName } from '../../util'; -export const initialState: UserConfiguration = new UserConfiguration(); +export interface State { + userConfiguration: UserConfiguration; + selectedKeymapAbbr?: string; + selectedMacroId?: number; +} -export function reducer(state = initialState, action: Action & { payload?: any }): UserConfiguration { - const changedUserConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state); +export const initialState: State = { + userConfiguration: new UserConfiguration() +}; +export function reducer( + state = initialState, + action: KeymapActions.Actions | MacroActions.Actions | UserConfig.Actions +): State { switch (action.type) { - case ActionTypes.APPLY_USER_CONFIGURATION_FROM_FILE: - case ActionTypes.LOAD_RESET_USER_CONFIGURATION: - case ActionTypes.LOAD_USER_CONFIG_SUCCESS: { - Object.assign(changedUserConfiguration, action.payload); - changedUserConfiguration.keymaps = [...changedUserConfiguration.keymaps]; - changedUserConfiguration.keymaps.sort((first: Keymap, second: Keymap) => first.name.localeCompare(second.name)); - changedUserConfiguration.macros = [...changedUserConfiguration.macros]; - changedUserConfiguration.macros.sort((first: Macro, second: Macro) => first.name.localeCompare(second.name)); - return changedUserConfiguration; + case UserConfig.ActionTypes.ApplyUserConfigurationFromFile: + case UserConfig.ActionTypes.LoadResetUserConfiguration: + case UserConfig.ActionTypes.LoadUserConfigSuccess: { + const userConfig = (action as UserConfig.LoadUserConfigSuccessAction).payload; + + const userConfiguration = Object.assign(new UserConfiguration(), { + ...state.userConfiguration, + ...userConfig, + keymaps: userConfig.keymaps.sort((first: Keymap, second: Keymap) => first.name.localeCompare(second.name)), + macros: userConfig.macros.sort((first: Macro, second: Macro) => first.name.localeCompare(second.name)) + }); + + return { + ...state, + userConfiguration + }; } - case KeymapActions.ADD: - case KeymapActions.DUPLICATE: { - const newKeymap: Keymap = new Keymap(action.payload); - newKeymap.abbreviation = generateAbbr(state.keymaps, newKeymap.abbreviation); - newKeymap.name = generateName(state.keymaps, newKeymap.name); - newKeymap.isDefault = (state.keymaps.length === 0); + case KeymapActions.ActionTypes.Add: + case KeymapActions.ActionTypes.Duplicate: { + const newKeymap: Keymap = new Keymap((action as KeymapActions.AddKeymapAction).payload); + newKeymap.abbreviation = generateAbbr(state.userConfiguration.keymaps, newKeymap.abbreviation); + newKeymap.name = generateName(state.userConfiguration.keymaps, newKeymap.name); + newKeymap.isDefault = (state.userConfiguration.keymaps.length === 0); - changedUserConfiguration.keymaps = insertItemInNameOrder(state.keymaps, newKeymap); - break; + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.keymaps = insertItemInNameOrder(state.userConfiguration.keymaps, newKeymap); + + return { + ...state, + userConfiguration + }; } - case KeymapActions.EDIT_NAME: { - if (!isValidName(action.payload.name)) { + + case KeymapActions.ActionTypes.EditName: { + const payload = (action as KeymapActions.EditKeymapNameAction).payload; + + if (!isValidName(payload.name)) { break; } - const name: string = action.payload.name.trim(); + const name: string = payload.name.trim(); let keymapToRename: Keymap = null; - const duplicate = state.keymaps.some((keymap: Keymap) => { - if (keymap.abbreviation === action.payload.abbr) { + const duplicate = state.userConfiguration.keymaps.some((keymap: Keymap) => { + if (keymap.abbreviation === payload.abbr) { keymapToRename = keymap; } - return keymap.name === name && keymap.abbreviation !== action.payload.abbr; + return keymap.name === name && keymap.abbreviation !== payload.abbr; }); if (duplicate) { break; } - const newKeymap = Object.assign(new Keymap(), keymapToRename, {name}); + const newKeymap = Object.assign(new Keymap(), keymapToRename, { name }); - changedUserConfiguration.keymaps = insertItemInNameOrder( - state.keymaps, + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.keymaps = insertItemInNameOrder( + state.userConfiguration.keymaps, newKeymap, keymap => keymap.abbreviation !== newKeymap.abbreviation ); - break; - } - case KeymapActions.EDIT_ABBR: { - const abbr: string = action.payload.newAbbr.toUpperCase(); - const duplicate = state.keymaps.some((keymap: Keymap) => { - return keymap.name !== action.payload.name && keymap.abbreviation === abbr; + return { + ...state, + userConfiguration + }; + } + + case KeymapActions.ActionTypes.EditAbbr: { + const payload = (action as KeymapActions.EditKeymapAbbreviationAction).payload; + const abbr: string = payload.newAbbr.toUpperCase(); + + const duplicate = state.userConfiguration.keymaps.some((keymap: Keymap) => { + return keymap.name !== payload.name && keymap.abbreviation === abbr; }); - changedUserConfiguration.keymaps = state.keymaps.map((keymap: Keymap) => { + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.keymaps = state.userConfiguration.keymaps.map((keymap: Keymap) => { keymap = Object.assign(new Keymap(), keymap); - if (!duplicate && keymap.abbreviation === action.payload.abbr) { + if (!duplicate && keymap.abbreviation === payload.abbr) { keymap.abbreviation = abbr; } else { - keymap = keymap.renameKeymap(action.payload.abbr, action.payload.newAbbr); + keymap = keymap.renameKeymap(payload.abbr, payload.newAbbr); } return keymap; }); - break; + + return { + ...state, + userConfiguration + }; } - case KeymapActions.SET_DEFAULT: - changedUserConfiguration.keymaps = state.keymaps.map((keymap: Keymap) => { - if (keymap.abbreviation === action.payload || keymap.isDefault) { + case KeymapActions.ActionTypes.SetDefault: { + const payload = (action as KeymapActions.SetDefaultKeymapAction).payload; + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.keymaps = state.userConfiguration.keymaps.map((keymap: Keymap) => { + if (keymap.abbreviation === payload || keymap.isDefault) { keymap = Object.assign(new Keymap(), keymap); - keymap.isDefault = keymap.abbreviation === action.payload; + keymap.isDefault = keymap.abbreviation === payload; } return keymap; }); - break; - case KeymapActions.REMOVE: + + return { + ...state, + userConfiguration + }; + } + + case KeymapActions.ActionTypes.Remove: { + const payload = (action as KeymapActions.RemoveKeymapAction).payload; + let isDefault: boolean; - const filtered: Keymap[] = state.keymaps.filter((keymap: Keymap) => { - if (keymap.abbreviation === action.payload) { + const filtered: Keymap[] = state.userConfiguration.keymaps.filter((keymap: Keymap) => { + if (keymap.abbreviation === payload) { isDefault = keymap.isDefault; return false; } @@ -128,27 +168,35 @@ export function reducer(state = initialState, action: Action & { payload?: any } } // Check for the deleted keymap in other keymaps - changedUserConfiguration.keymaps = filtered.map(keymap => { + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.keymaps = filtered.map(keymap => { keymap = Object.assign(new Keymap(), keymap); - keymap.layers = checkExistence(keymap.layers, 'keymapAbbreviation', action.payload); + keymap.layers = checkExistence(keymap.layers, 'keymapAbbreviation', payload); return keymap; }); - break; - case KeymapActions.SAVE_KEY: { - const keyIndex: number = action.payload.key; - const layerIndex: number = action.payload.layer; - const moduleIndex: number = action.payload.module; - const keyActionRemap = action.payload.keyAction; + return { + ...state, + userConfiguration + }; + } + + case KeymapActions.ActionTypes.SaveKey: { + const payload = (action as KeymapActions.SaveKeyAction).payload; + const keyIndex: number = payload.key; + const layerIndex: number = payload.layer; + const moduleIndex: number = payload.module; + const keyActionRemap = payload.keyAction; const newKeyAction = keyActionRemap.action; - const newKeymap: Keymap = action.payload.keymap; + const newKeymap: Keymap = payload.keymap; const isSwitchLayerAction = newKeyAction instanceof SwitchLayerAction; const isSwitchKeymapAction = newKeyAction instanceof SwitchKeymapAction; - changedUserConfiguration.keymaps = state.keymaps.map(keymap => { + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.keymaps = state.userConfiguration.keymaps.map(keymap => { // SwitchKeymapAction not allow to refer to itself - if (isSwitchKeymapAction && keymap.abbreviation === newKeyAction.keymapAbbreviation) { + if (isSwitchKeymapAction && keymap.abbreviation === (newKeyAction as any).keymapAbbreviation) { return keymap; } @@ -180,49 +228,76 @@ export function reducer(state = initialState, action: Action & { payload?: any } return keymap; }); - break; + + return { + ...state, + userConfiguration + }; } - case KeymapActions.CHECK_MACRO: - changedUserConfiguration.keymaps = state.keymaps.map(keymap => { + case KeymapActions.ActionTypes.CheckMacro: { + const payload = (action as KeymapActions.CheckMacroAction).payload; + + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.keymaps = state.userConfiguration.keymaps.map(keymap => { keymap = Object.assign(new Keymap(), keymap); - keymap.layers = checkExistence(keymap.layers, '_macroId', action.payload); + keymap.layers = checkExistence(keymap.layers, '_macroId', payload); return keymap; }); - break; - case MacroActions.ADD: { + + return { + ...state, + userConfiguration + }; + } + + case MacroActions.ActionTypes.Add: { const newMacro = new Macro(); - newMacro.id = generateMacroId(state.macros); - newMacro.name = generateName(state.macros, 'New macro'); + newMacro.id = generateMacroId(state.userConfiguration.macros); + newMacro.name = generateName(state.userConfiguration.macros, 'New macro'); newMacro.isLooped = false; newMacro.isPrivate = true; newMacro.macroActions = []; - changedUserConfiguration.macros = insertItemInNameOrder(state.macros, newMacro); - break; - } - case MacroActions.DUPLICATE: { - const newMacro = new Macro(action.payload); - newMacro.name = generateName(state.macros, newMacro.name); - newMacro.id = generateMacroId(state.macros); + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.macros = insertItemInNameOrder(state.userConfiguration.macros, newMacro); - changedUserConfiguration.macros = insertItemInNameOrder(state.macros, newMacro); - break; + return { + ...state, + userConfiguration + }; } - case MacroActions.EDIT_NAME: { - if (!isValidName(action.payload.name)) { + + case MacroActions.ActionTypes.Duplicate: { + const payload = (action as MacroActions.DuplicateMacroAction).payload; + const newMacro = new Macro(payload); + newMacro.name = generateName(state.userConfiguration.macros, newMacro.name); + newMacro.id = generateMacroId(state.userConfiguration.macros); + + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.macros = insertItemInNameOrder(state.userConfiguration.macros, newMacro); + + return { + ...state, + userConfiguration + }; + } + + case MacroActions.ActionTypes.EditName: { + const payload = (action as MacroActions.EditMacroNameAction).payload; + if (!isValidName(payload.name)) { break; } - const name: string = action.payload.name.trim(); + const name: string = payload.name.trim(); let macroToRename: Macro = null; - const duplicate = state.macros.some((macro: Macro) => { - if (macro.id === action.payload.id) { - macroToRename = macro; + const duplicate = state.userConfiguration.macros.some((macro: Macro) => { + if (macro.id === payload.id) { + macroToRename = macro; } - return macro.id !== action.payload.id && macro.name === name; + return macro.id !== payload.id && macro.name === name; }); if (duplicate) { @@ -230,16 +305,25 @@ export function reducer(state = initialState, action: Action & { payload?: any } } const newMacro = Object.assign(new Macro(), macroToRename, { name }); - changedUserConfiguration.macros = insertItemInNameOrder(state.macros, newMacro, macro => macro.id !== newMacro.id); - break; + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.macros = insertItemInNameOrder( + state.userConfiguration.macros, + newMacro, + macro => macro.id !== newMacro.id); + + return { + ...state, + userConfiguration + }; } - case MacroActions.REMOVE: - const macroId = action.payload; - changedUserConfiguration.macros = state.macros.filter((macro: Macro) => macro.id !== macroId); + case MacroActions.ActionTypes.Remove: { + const macroId = (action as MacroActions.RemoveMacroAction).payload; + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.macros = state.userConfiguration.macros.filter((macro: Macro) => macro.id !== macroId); - for (let k = 0; k < changedUserConfiguration.keymaps.length; k++) { - const keymap = changedUserConfiguration.keymaps[k]; + for (let k = 0; k < userConfiguration.keymaps.length; k++) { + const keymap = userConfiguration.keymaps[k]; let hasChanges = false; for (const layer of keymap.layers) { @@ -256,48 +340,80 @@ export function reducer(state = initialState, action: Action & { payload?: any } } if (hasChanges) { - changedUserConfiguration.keymaps[k] = new Keymap(keymap); + userConfiguration.keymaps[k] = new Keymap(keymap); } } - break; - case MacroActions.ADD_ACTION: - changedUserConfiguration.macros = state.macros.map((macro: Macro) => { - if (macro.id === action.payload.id) { + return { + ...state, + userConfiguration + }; + } + + case MacroActions.ActionTypes.AddAction: { + const payload = (action as MacroActions.AddMacroActionAction).payload; + + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.macros = state.userConfiguration.macros.map((macro: Macro) => { + if (macro.id === payload.id) { macro = new Macro(macro); - macro.macroActions.push(action.payload.action); + macro.macroActions.push(payload.action); } return macro; }); - break; - case MacroActions.SAVE_ACTION: - changedUserConfiguration.macros = state.macros.map((macro: Macro) => { - if (macro.id === action.payload.id) { + + return { + ...state, + userConfiguration + }; + } + + case MacroActions.ActionTypes.SaveAction: { + const payload = (action as MacroActions.SaveMacroActionAction).payload; + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.macros = state.userConfiguration.macros.map((macro: Macro) => { + if (macro.id === payload.id) { macro = new Macro(macro); - macro.macroActions[action.payload.index] = action.payload.action; + macro.macroActions[payload.index] = payload.action; } return macro; }); - break; - case MacroActions.DELETE_ACTION: - changedUserConfiguration.macros = state.macros.map((macro: Macro) => { - if (macro.id === action.payload.id) { + + return { + ...state, + userConfiguration + }; + } + + case MacroActions.ActionTypes.DeleteAction: { + const payload = (action as MacroActions.DeleteMacroActionAction).payload; + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.macros = state.userConfiguration.macros.map((macro: Macro) => { + if (macro.id === payload.id) { macro = new Macro(macro); - macro.macroActions.splice(action.payload.index, 1); + macro.macroActions.splice(payload.index, 1); } return macro; }); - break; - case MacroActions.REORDER_ACTION: - changedUserConfiguration.macros = state.macros.map((macro: Macro) => { - if (macro.id === action.payload.id) { - let newIndex: number = action.payload.newIndex; + + return { + ...state, + userConfiguration + }; + } + + case MacroActions.ActionTypes.ReorderAction: { + const payload = (action as MacroActions.ReorderMacroActionAction).payload; + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.macros = state.userConfiguration.macros.map((macro: Macro) => { + if (macro.id === payload.id) { + let newIndex: number = payload.newIndex; // We need to reduce the new index for one when we are moving action down - if (newIndex > action.payload.oldIndex) { + if (newIndex > payload.oldIndex) { --newIndex; } @@ -305,95 +421,101 @@ export function reducer(state = initialState, action: Action & { payload?: any } macro.macroActions.splice( newIndex, 0, - macro.macroActions.splice(action.payload.oldIndex, 1)[0] + macro.macroActions.splice(payload.oldIndex, 1)[0] ); } return macro; }); - break; - case ActionTypes.RENAME_USER_CONFIGURATION: { - if (isValidName(action.payload)) { - changedUserConfiguration.deviceName = action.payload.trim(); + return { + ...state, + userConfiguration + }; + } + + case UserConfig.ActionTypes.RenameUserConfiguration: { + const payload = (action as UserConfig.RenameUserConfigurationAction).payload; + + if (isValidName(payload)) { + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.deviceName = payload.trim(); + + return { + ...state, + userConfiguration + }; } - break; + + return state; } - case ActionTypes.SET_USER_CONFIGURATION_VALUE: { - changedUserConfiguration[action.payload.propertyName] = action.payload.value; - break; + case UserConfig.ActionTypes.SetUserConfigurationValue: { + const payload = (action as UserConfig.SetUserConfigurationValueAction).payload; + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration[payload.propertyName] = payload.value; + + return { + ...state, + userConfiguration + }; } - case KeymapActions.EDIT_DESCRIPTION: { + case KeymapActions.ActionTypes.EditDescription: { const data = (action as KeymapActions.EditDescriptionAction).payload; - changedUserConfiguration.keymaps = state.keymaps.map(keymap => { + const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration); + userConfiguration.keymaps = state.userConfiguration.keymaps.map(keymap => { if (keymap.abbreviation === data.abbr) { keymap.description = data.description; } return keymap; }); - break; + + return { + ...state, + userConfiguration + }; } + case KeymapActions.ActionTypes.Select: + return { + ...state, + selectedKeymapAbbr: (action as KeymapActions.SelectKeymapAction).payload + }; + + case MacroActions.ActionTypes.Select: + return { + ...state, + selectedMacroId: (action as MacroActions.SelectMacroAction).payload + }; + default: - break; - } - - return changedUserConfiguration; -} - -export function getUserConfiguration(): (state$: Observable) => Observable { - return (state$: Observable) => state$ - .map(state => state.userConfiguration); -} - -export function getKeymaps(): (state$: Observable) => Observable { - return (state$: Observable) => state$ - .map(state => state.userConfiguration.keymaps); -} - -export function getKeymap(abbr: string) { - if (abbr === undefined) { - return getDefaultKeymap(); - } - - return (state$: Observable) => getKeymaps()(state$) - .pipe( - map((keymaps: Keymap[]) => - keymaps.find((keymap: Keymap) => keymap.abbreviation === abbr) - ) - ); -} - -export function getDefaultKeymap() { - return (state$: Observable) => getKeymaps()(state$) - .pipe( - map((keymaps: Keymap[]) => - keymaps.find((keymap: Keymap) => keymap.isDefault) - ) - ); -} - -export function getMacros(): (state$: Observable) => Observable { - return (state$: Observable) => state$ - .pipe( - map(state => state.userConfiguration.macros) - ); -} - -export function getMacro(id: number) { - if (isNaN(id)) { - return () => of(undefined); - } else { - return (state$: Observable) => getMacros()(state$) - .pipe( - map((macros: Macro[]) => macros.find((macro: Macro) => macro.id === id)) - ); + return state; } } +export const getUserConfiguration = (state: State): UserConfiguration => state.userConfiguration; +export const getKeymaps = (state: State): Keymap[] => state.userConfiguration.keymaps; +export const getDefaultKeymap = (state: State): Keymap => state.userConfiguration.keymaps.find(keymap => keymap.isDefault); +export const getSelectedKeymap = (state: State): Keymap => { + if (state.selectedKeymapAbbr === undefined) { + return getDefaultKeymap(state); + } + + return state.userConfiguration.keymaps.find(keymap => keymap.abbreviation === state.selectedKeymapAbbr); +}; +export const getMacros = (state: State): Macro[] => state.userConfiguration.macros; +export const getSelectedMacro = (state: State): Macro => { + if (isNaN(state.selectedMacroId)) { + return undefined; + } + + return state.userConfiguration.macros.find(macro => macro.id === state.selectedMacroId); +}; +export const isKeymapDeletable = (state: State): boolean => state.userConfiguration.keymaps.length > 1; +export const hasMacro = (state: State): boolean => state.userConfiguration.macros.length > 0; + function generateAbbr(keymaps: Keymap[], abbr: string): string { const chars: string[] = '23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); let position = 0; diff --git a/packages/uhk-web/tsconfig.json b/packages/uhk-web/tsconfig.json index fb7081c9..fd11e245 100644 --- a/packages/uhk-web/tsconfig.json +++ b/packages/uhk-web/tsconfig.json @@ -4,6 +4,8 @@ "outDir": "../uhk-agent/dist/out-tsc", "baseUrl": ".", "declaration": false, + "module": "es2015", + "moduleResolution": "node", "target": "es5", "lib": [ "es2015.iterable",