diff --git a/shared/src/store/actions/user-config.ts b/shared/src/store/actions/user-config.ts index c44ef726..18caffdf 100644 --- a/shared/src/store/actions/user-config.ts +++ b/shared/src/store/actions/user-config.ts @@ -8,7 +8,8 @@ const PREFIX = '[user-config] '; // tslint:disable-next-line:variable-name export const ActionTypes = { LOAD_USER_CONFIG: type(PREFIX + 'Load User Config'), - LOAD_USER_CONFIG_SUCCESS: type(PREFIX + 'Load User Config Success') + LOAD_USER_CONFIG_SUCCESS: type(PREFIX + 'Load User Config Success'), + SAVE_USER_CONFIG_SUCCESS: type(PREFIX + 'Save User Config Success') }; export class LoadUserConfigAction implements Action { @@ -23,6 +24,11 @@ export class LoadUserConfigSuccessAction implements Action { } } +export class SaveUserConfigSuccessAction implements Action { + type = ActionTypes.SAVE_USER_CONFIG_SUCCESS; +} + export type Actions = LoadUserConfigAction - | LoadUserConfigSuccessAction; + | LoadUserConfigSuccessAction + | SaveUserConfigSuccessAction; diff --git a/shared/src/store/effects/user-config.ts b/shared/src/store/effects/user-config.ts index 67183e10..60cb20b4 100644 --- a/shared/src/store/effects/user-config.ts +++ b/shared/src/store/effects/user-config.ts @@ -1,16 +1,27 @@ -import { Injectable, Inject } from '@angular/core'; -import { Effect, Actions } from '@ngrx/effects'; +import { Inject, Injectable } from '@angular/core'; +import { Actions, Effect } from '@ngrx/effects'; import { Observable } from 'rxjs/Observable'; -import { Action } from '@ngrx/store'; +import { Action, Store } from '@ngrx/store'; +import 'rxjs/add/operator/map'; import 'rxjs/add/operator/switchMap'; import 'rxjs/add/operator/startWith'; +import 'rxjs/add/operator/withLatestFrom'; import 'rxjs/add/observable/of'; -import { ActionTypes, LoadUserConfigAction, LoadUserConfigSuccessAction } from '../actions/user-config'; +import { + ActionTypes, + LoadUserConfigAction, + LoadUserConfigSuccessAction, + SaveUserConfigSuccessAction +} from '../actions/user-config'; + import { UserConfiguration } from '../../config-serializer/config-items/UserConfiguration'; -import { DataStorageRepositoryService, DATA_STORAGE_REPOSITORY } from '../../services/datastorage-repository.service'; +import { DATA_STORAGE_REPOSITORY, DataStorageRepositoryService } from '../../services/datastorage-repository.service'; import { DefaultUserConfigurationService } from '../../services/default-user-configuration.service'; +import { AppState, getUserConfiguration } from '../index'; +import { KeymapActions } from '../actions/keymap'; +import { MacroActions } from '../actions/macro'; @Injectable() export class UserConfigEffects { @@ -26,7 +37,20 @@ export class UserConfigEffects { return Observable.of(new LoadUserConfigSuccessAction(config)); }); + @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.CHECK_MACRO, + MacroActions.ADD, MacroActions.DUPLICATE, MacroActions.EDIT_NAME, MacroActions.REMOVE, MacroActions.ADD_ACTION, + MacroActions.SAVE_ACTION, MacroActions.DELETE_ACTION, MacroActions.REORDER_ACTION) + .withLatestFrom(this.store.select(getUserConfiguration)) + .map(([action, config]) => { + this.dataStorageRepository.saveConfig(config); + return new SaveUserConfigSuccessAction(); + }); + constructor(private actions$: Actions, - @Inject(DATA_STORAGE_REPOSITORY)private dataStorageRepository: DataStorageRepositoryService, - private defaultUserConfigurationService: DefaultUserConfigurationService) { } + @Inject(DATA_STORAGE_REPOSITORY) private dataStorageRepository: DataStorageRepositoryService, + private store: Store, + private defaultUserConfigurationService: DefaultUserConfigurationService) { + } } diff --git a/shared/src/store/index.ts b/shared/src/store/index.ts index cd64b72c..7ccc0839 100644 --- a/shared/src/store/index.ts +++ b/shared/src/store/index.ts @@ -6,3 +6,5 @@ export interface AppState { userConfiguration: UserConfiguration; presetKeymaps: Keymap[]; } + +export const getUserConfiguration = (state: AppState) => state.userConfiguration;