Skip to content

Commit 23af522

Browse files
ert78gbmondalaci
authored andcommitted
fix(user-config): Fix user configuration savings (#433)
* test(user-config): Fix saveAs functionality * feat(user-config): Add 2 space indent when save user-config to json file * refactor(user-config): Move user config savings into user config effect * build: Remove unused package
1 parent 35c0d98 commit 23af522

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

packages/uhk-web/package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/uhk-web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"dragula": "^3.7.2",
5353
"exports-loader": "^0.6.3",
5454
"file-loader": "^0.10.0",
55-
"filesaver.js": "^0.2.0",
55+
"file-saver": "1.3.3",
5656
"font-awesome": "^4.7.0",
5757
"html-webpack-plugin": "^2.29.0",
5858
"istanbul-instrumenter-loader": "^2.0.0",

packages/uhk-web/src/app/app.component.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Component, HostListener, ViewEncapsulation } from '@angular/core';
22
import { animate, style, transition, trigger } from '@angular/animations';
33
import { Observable } from 'rxjs/Observable';
44
import { Action, Store } from '@ngrx/store';
5-
import { UhkBuffer } from 'uhk-common';
65

76
import 'rxjs/add/operator/last';
87

@@ -14,8 +13,8 @@ import {
1413
runningInElectron,
1514
saveToKeyboardState
1615
} from './store';
17-
import { getUserConfiguration } from './store/reducers/user-configuration';
1816
import { ProgressButtonState } from './store/reducers/progress-button-state';
17+
import { SaveUserConfigInBinaryFileAction, SaveUserConfigInJsonFileAction } from './store/actions/user-config';
1918

2019
@Component({
2120
selector: 'main-app',
@@ -65,28 +64,13 @@ export class MainAppComponent {
6564
onAltJ(event: KeyboardEvent): void {
6665
event.preventDefault();
6766
event.stopPropagation();
68-
this.store
69-
.let(getUserConfiguration())
70-
.first()
71-
.subscribe(userConfiguration => {
72-
const asString = JSON.stringify(userConfiguration.toJsonObject());
73-
const asBlob = new Blob([asString], {type: 'text/plain'});
74-
saveAs(asBlob, 'UserConfiguration.json');
75-
});
67+
this.store.dispatch(new SaveUserConfigInJsonFileAction());
7668
}
7769

7870
@HostListener('window:keydown.alt.b', ['$event'])
7971
onAltB(event: KeyboardEvent): void {
8072
event.preventDefault();
8173
event.stopPropagation();
82-
this.store
83-
.let(getUserConfiguration())
84-
.first()
85-
.map(userConfiguration => {
86-
const uhkBuffer = new UhkBuffer();
87-
userConfiguration.toBinary(uhkBuffer);
88-
return new Blob([uhkBuffer.getBufferContent()]);
89-
})
90-
.subscribe(blob => saveAs(blob, 'UserConfiguration.bin'));
74+
this.store.dispatch(new SaveUserConfigInBinaryFileAction());
9175
}
9276
}

packages/uhk-web/src/app/store/actions/user-config.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export const ActionTypes = {
99
LOAD_CONFIG_FROM_DEVICE: type(PREFIX + 'Load User Config from Device'),
1010
LOAD_CONFIG_FROM_DEVICE_REPLY: type(PREFIX + 'Load User Config from Device reply'),
1111
LOAD_USER_CONFIG_SUCCESS: type(PREFIX + 'Load User Config Success'),
12-
SAVE_USER_CONFIG_SUCCESS: type(PREFIX + 'Save User Config Success')
12+
SAVE_USER_CONFIG_SUCCESS: type(PREFIX + 'Save User Config Success'),
13+
SAVE_USER_CONFIG_IN_JSON_FILE: type(PREFIX + 'Save User Config in JSON file'),
14+
SAVE_USER_CONFIG_IN_BIN_FILE: type(PREFIX + 'Save User Config in binary file')
1315
};
1416

1517
export class LoadUserConfigAction implements Action {
@@ -38,10 +40,20 @@ export class SaveUserConfigSuccessAction implements Action {
3840
constructor(public payload: UserConfiguration) { }
3941
}
4042

43+
export class SaveUserConfigInJsonFileAction implements Action {
44+
type = ActionTypes.SAVE_USER_CONFIG_IN_JSON_FILE;
45+
}
46+
47+
export class SaveUserConfigInBinaryFileAction implements Action {
48+
type = ActionTypes.SAVE_USER_CONFIG_IN_BIN_FILE;
49+
}
50+
4151
export type Actions
4252
= LoadUserConfigAction
4353
| LoadUserConfigSuccessAction
4454
| SaveUserConfigSuccessAction
4555
| LoadConfigFromDeviceAction
4656
| LoadConfigFromDeviceReplyAction
57+
| SaveUserConfigInJsonFileAction
58+
| SaveUserConfigInBinaryFileAction
4759
;

packages/uhk-web/src/app/store/effects/user-config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { go } from '@ngrx/router-store';
33
import { Actions, Effect, toPayload } from '@ngrx/effects';
44
import { Observable } from 'rxjs/Observable';
55
import { Action, Store } from '@ngrx/store';
6+
import { saveAs } from 'file-saver';
67

78
import 'rxjs/add/operator/map';
89
import 'rxjs/add/operator/switchMap';
@@ -148,6 +149,25 @@ export class UserConfigEffects {
148149
}
149150
});
150151

152+
@Effect({dispatch: false}) saveUserConfigInJsonFile$ = this.actions$
153+
.ofType(ActionTypes.SAVE_USER_CONFIG_IN_JSON_FILE)
154+
.withLatestFrom(this.store.select(getUserConfiguration))
155+
.do(([action, userConfiguration]) => {
156+
const asString = JSON.stringify(userConfiguration.toJsonObject(), null, 2);
157+
const asBlob = new Blob([asString], {type: 'text/plain'});
158+
saveAs(asBlob, 'UserConfiguration.json');
159+
});
160+
161+
@Effect({dispatch: false}) saveUserConfigInBinFile$ = this.actions$
162+
.ofType(ActionTypes.SAVE_USER_CONFIG_IN_BIN_FILE)
163+
.withLatestFrom(this.store.select(getUserConfiguration))
164+
.do(([action, userConfiguration]) => {
165+
const uhkBuffer = new UhkBuffer();
166+
userConfiguration.toBinary(uhkBuffer);
167+
const blob = new Blob([uhkBuffer.getBufferContent()]);
168+
saveAs(blob, 'UserConfiguration.bin');
169+
});
170+
151171
constructor(private actions$: Actions,
152172
private dataStorageRepository: DataStorageRepositoryService,
153173
private store: Store<AppState>,

0 commit comments

Comments
 (0)