Skip to content

Commit c937524

Browse files
committed
refactor: add local/remote setting actions
1 parent a88ccec commit c937524

File tree

6 files changed

+82
-67
lines changed

6 files changed

+82
-67
lines changed

src/app/library/database/settings.schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const SettingsSchema = {
99
type: 'string'
1010
},
1111
value: {
12-
type: 'string'
12+
type: ['string', 'boolean', 'number', 'integer', 'null', 'array', 'object']
1313
}
1414
},
1515
required: ['key', 'value']

src/app/library/models.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface InkDropMeta {
2828
}
2929
export interface InkSettingsMeta {
3030
key: string;
31-
value: string;
31+
value: any;
3232
_id?: string;
3333
}
3434

@@ -45,7 +45,7 @@ export type InkAppSettings = InkAppSettingsItem[];
4545

4646
export interface InkAppSettingsItem {
4747
key: string;
48-
value: string;
48+
value: any;
4949
}
5050

5151
/* doc methods */

src/app/library/services/gist.service.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,20 @@ export class InkGistService {
1313
constructor(private _http: HttpClient, private _utilityService: InkUtilsService) {}
1414
create(data) {
1515
return this._http
16-
.post('https://api.github.com/gists', {
17-
description: GIST_NAME,
18-
public: false,
19-
files: {
20-
'inkapp-database.json': {
21-
content: JSON.stringify(data, null, ' ')
22-
},
23-
'last-sync.json': {
24-
content: JSON.stringify(this._utilityService.getCurrentStatus(), null, ' ')
25-
}
26-
}
27-
})
16+
.post('https://api.github.com/gists', this._getGistPostData(data))
2817
.pipe(map(gist => this._getGistBasicData(gist)));
2918
}
3019

31-
edit(gistId, data) {
32-
return this._http
33-
.patch(`https://api.github.com/gists/${gistId}`, {
34-
files: {
35-
'inkapp-database.json': {
36-
content: JSON.stringify(data, null, ' ')
37-
},
38-
'last-sync.json': {
39-
content: JSON.stringify(this._utilityService.getCurrentStatus(), null, ' ')
40-
}
20+
edit(gistId: string, gistData: object) {
21+
return this._http.patch(`https://api.github.com/gists/${gistId}`, this._getGistPostData(gistData)).pipe(
22+
map(gist => this._getGistBasicData(gist)),
23+
catchError(err => {
24+
if (err.status === 404) {
25+
return this.create(gistData);
4126
}
27+
return of(new Error('Something went wrong!'));
4228
})
43-
.pipe(
44-
map(gist => this._getGistBasicData(gist)),
45-
catchError(err => {
46-
if (err.status === 404) {
47-
return this.create(data);
48-
}
49-
return of(new Error('Something went wrong!'));
50-
})
51-
);
29+
);
5230
}
5331

5432
get() {
@@ -80,4 +58,17 @@ export class InkGistService {
8058
owner_profile: gist.owner.html_url
8159
};
8260
}
61+
62+
private _getGistPostData(gistData) {
63+
return {
64+
files: {
65+
'inkapp-database.json': {
66+
content: JSON.stringify(gistData, null, ' ')
67+
},
68+
'last-sync.json': {
69+
content: JSON.stringify(this._utilityService.getCurrentStatus(), null, ' ')
70+
}
71+
}
72+
};
73+
}
8374
}

src/app/library/store/actions/settings.action.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { InkAppSettings } from '@lib/models';
1+
import { InkAppSettings, InkAppSettingsItem, InkSettingsMeta } from '@lib/models';
22

33
export class PopulateSettings {
44
static readonly type = '[Settings] load all settings';
@@ -10,9 +10,19 @@ export class MergeSettings {
1010
constructor(public settings: InkAppSettings) {}
1111
}
1212

13+
export class CreateSettingsItem {
14+
static readonly type = '[Settings] create settings';
15+
constructor(public key: string, public value: any) {}
16+
}
17+
18+
export class AddSettingsItem {
19+
static readonly type = '[Settings] add settings';
20+
constructor(public settingsItem: InkSettingsMeta) {}
21+
}
22+
1323
export class UpdateSettingsItem {
1424
static readonly type = '[Settings] update';
15-
constructor(public key: string, public value: string) {}
25+
constructor(public key: string, public value: any) {}
1626
}
1727

1828
export class DeleteSettingsItem {

src/app/library/store/states/settings.state.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import {
66
MergeSettings,
77
FetchRemoteGist,
88
CreateRemoteGist,
9-
UpdateRemoteGist
9+
UpdateRemoteGist,
10+
AddSettingsItem,
11+
CreateSettingsItem
1012
} from '@store/actions';
1113
import { SelectMultipleControlValueAccessor } from '@angular/forms';
1214
import { InkSettingsService, InkGistService } from '@lib/services';
13-
import { tap } from 'rxjs/operators';
15+
import { tap, map } from 'rxjs/operators';
1416

1517
@State<Partial<InkAppSettings>>({
1618
name: 'settings',
@@ -43,6 +45,19 @@ export class SettingsState implements NgxsOnInit {
4345
});
4446
}
4547

48+
@Action(CreateSettingsItem)
49+
createSettingsItem(ctx: StateContext<InkAppSettings>, action: CreateSettingsItem) {
50+
this._service.add(action.key, action.value).then(settingsItem => {
51+
ctx.dispatch(new AddSettingsItem({ key: action.key, value: action.value }));
52+
});
53+
}
54+
55+
@Action(AddSettingsItem)
56+
addSettingsItem(ctx: StateContext<InkAppSettings>, action: AddSettingsItem) {
57+
const state = ctx.getState();
58+
ctx.setState([...state, action.settingsItem]);
59+
}
60+
4661
@Action(PopulateSettings)
4762
populateSettings(ctx: StateContext<InkAppSettings>, action: PopulateSettings) {
4863
ctx.setState(action.settings);
@@ -58,14 +73,17 @@ export class SettingsState implements NgxsOnInit {
5873
return this._gistService.get();
5974
}
6075

61-
// @Action(CreateRemoteGist)
62-
// createRemoteGist(ctx: StateContext<InkAppSettings>, action: CreateRemoteGist) {
63-
// const state = ctx.getState();
64-
// ctx.setState([...state, ...action.settings]);
65-
// }
66-
// @Action(UpdateRemoteGist)
67-
// updateRemoteGist(ctx: StateContext<InkAppSettings>, action: UpdateRemoteGist) {
68-
// const state = ctx.getState();
69-
// ctx.setState([...state, ...action.settings]);
70-
// }
76+
@Action(CreateRemoteGist)
77+
createRemoteGist(ctx: StateContext<InkAppSettings>, action: CreateRemoteGist) {
78+
return this._gistService
79+
.create(action.gistData)
80+
.pipe(tap(gist => ctx.dispatch(new CreateSettingsItem('gist', gist))));
81+
}
82+
83+
@Action(UpdateRemoteGist)
84+
updateRemoteGist(ctx: StateContext<InkAppSettings>, action: UpdateRemoteGist) {
85+
return this._gistService
86+
.edit(action.gistId, action.gistData)
87+
.pipe(tap(gist => ctx.dispatch(new UpdateSettingsItem('gist', gist))));
88+
}
7189
}

src/app/pages/settings/settings.page.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ import merge from 'deepmerge';
44
import { environment } from '../../../environments/environment';
55
import { InkAppSettingsItem, InkGist } from '@lib/models';
66
import { InkDatabaseService, InkSettingsService, InkGithubService, InkGistService } from '@lib/services';
7-
import { PopulateSettings, UpdateSettingsItem, MergeSettings, FetchRemoteGist } from '@store/actions';
7+
import {
8+
PopulateSettings,
9+
UpdateSettingsItem,
10+
MergeSettings,
11+
FetchRemoteGist,
12+
CreateRemoteGist,
13+
UpdateRemoteGist
14+
} from '@store/actions';
815
import { GITHUB_ACCESS_TOKEN_NAME } from '../../ink.config';
916
import { Observable } from 'rxjs';
1017
import { SettingsState } from '@store/states';
1118

1219
@Component({
1320
selector: 'inkapp-settings-page',
14-
templateUrl: './settings.page.html',
15-
styles: []
21+
templateUrl: './settings.page.html'
1622
})
1723
export class SettingsPage implements OnInit {
1824
@Select(SettingsState.view) view$: Observable<string>;
@@ -31,7 +37,7 @@ export class SettingsPage implements OnInit {
3137
this._settingsService.getAll().then(doc => {
3238
const user = doc.filter(s => s.key === 'gist');
3339
if (user.length > 0) {
34-
this.userData = JSON.parse(user[0].value);
40+
this.userData = user[0].value;
3541
}
3642
this._store.dispatch(new MergeSettings(doc));
3743
});
@@ -47,23 +53,13 @@ export class SettingsPage implements OnInit {
4753
const data = await db.dump();
4854
const gist = await db.settings.findOne({ key: 'gist' }).exec();
4955
if (gist) {
50-
const gistData = JSON.parse(gist.value);
51-
this._gistService.edit(gistData.id, data).subscribe(
52-
res => {
53-
this._settingsService.update('gist', JSON.stringify(res)).then(doc => {
54-
console.log('settings updated with sync', doc);
55-
});
56-
},
57-
err => {
58-
console.log('something went wrong', err);
59-
}
60-
);
56+
this._store.dispatch(new UpdateRemoteGist(gist.value.id, data)).subscribe(res => {
57+
console.log('hooohoo remote gist is updated!');
58+
});
6159
return;
6260
}
63-
this._gistService.create(data).subscribe(res => {
64-
this._settingsService.add('gist', JSON.stringify(res)).then(doc => {
65-
console.log('settings updatedw ith sync', doc);
66-
});
61+
this._store.dispatch(new CreateRemoteGist(data)).subscribe(res => {
62+
console.log('hooohoo remote gist ready!');
6763
});
6864
}
6965
async loadGists() {

0 commit comments

Comments
 (0)