Skip to content

Commit 8e50805

Browse files
committed
feat: store actions added
1 parent d02fd13 commit 8e50805

16 files changed

+204
-80
lines changed

src/app/components/collection/collection.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core';
22
import { Store, Select } from '@ngxs/store';
33
import { InkBucket, InkCollection, InkCollectionMeta } from '../../ink.model';
44
import { Observable } from 'rxjs';
5-
import { map } from 'rxjs/operators';
5+
import { map, filter } from 'rxjs/operators';
66

77
@Component({
88
selector: 'inkapp-collection',
@@ -15,7 +15,7 @@ export class CollectionComponent implements OnInit {
1515
constructor(private store: Store) {
1616
this.store
1717
.select(s => s.collection)
18-
.pipe(map(x => x[0]))
18+
.pipe(filter(s => s.length > 0), map(x => x[0]))
1919
.subscribe(r => {
2020
console.log('CColl', r);
2121
this.collection = r;

src/app/components/ink/ink.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { map, debounceTime } from 'rxjs/operators';
1414
})
1515
export class InkComponent implements OnInit, OnDestroy {
1616
@Input() inkData: InkColor;
17-
@Input() bucketId: number;
17+
@Input() bucketId: string;
1818
overlayRef: OverlayRef;
1919
colorPanelPortal: ComponentPortal<ChromeComponent>;
2020
colorPickerRef: ComponentRef<ChromeComponent>;
@@ -41,7 +41,7 @@ export class InkComponent implements OnInit, OnDestroy {
4141
}
4242
this.colorPickerRef.instance.onChange.pipe(map(c => c.color)).subscribe(color => {
4343
this.store.dispatch(
44-
new UpdateInkColor({ bucketId: this.bucketId, value: color.hex, meta: color, id: this.inkData.id })
44+
new UpdateInkColor({ bucketId: this.inkData.bucketId, value: color.hex, meta: color, id: this.inkData.id })
4545
);
4646
});
4747
this.overlayRef.backdropClick().subscribe(b => {

src/app/components/navbar/navbar.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<nav class="navbar navbar-expand-lg navbar-light bg-light">
1+
<nav class="navbar navbar-expand-lg navbar-light bg-transparent">
22
<div class="container">
33
<a class="navbar-brand" href="#">InkBucket</a>
44
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
import { Component, OnInit } from '@angular/core';
22
import { Store } from '@ngxs/store';
33
import { CreateBucket } from '../../store/actions/bucket.action';
4+
import { map, tap, filter } from 'rxjs/operators';
45

56
@Component({
67
selector: 'inkapp-navbar',
78
templateUrl: './navbar.component.html',
89
styles: []
910
})
1011
export class NavbarComponent implements OnInit {
12+
currentCollectionId: string;
1113
constructor(private store: Store) {}
1214

13-
ngOnInit() {}
15+
ngOnInit() {
16+
this.store
17+
.select(s => s.collection)
18+
.pipe(filter(s => s.length > 0), map(s => s[0].id))
19+
.subscribe(r => (this.currentCollectionId = r));
20+
}
1421

1522
newBucket() {
16-
this.store.dispatch(new CreateBucket({ collectionId: 1, id: 1, name: 'Hello-123' }));
23+
if (this.currentCollectionId) {
24+
this.store.dispatch(new CreateBucket({ collectionId: this.currentCollectionId, name: 'Hello-123' }));
25+
}
1726
}
1827
}

src/app/ink.model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ export enum InkColorType {
55
}
66

77
export interface InkBucketMeta {
8-
id: number;
8+
id?: string;
99
name: string;
10-
collectionId: number;
10+
collectionId: string;
1111
}
1212

1313
export type InkBucket = InkBucketMeta[];
@@ -17,13 +17,13 @@ export interface InkColor {
1717
name?: string;
1818
value: string;
1919
meta: any;
20-
bucketId: number;
20+
bucketId: string;
2121
}
2222

2323
export type Ink = InkColor[];
2424

2525
export interface InkCollectionMeta {
26-
id: number;
26+
id: string;
2727
name: string;
2828
created: Date;
2929
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
<inkapp-navbar></inkapp-navbar>
2-
<inkapp-collection></inkapp-collection>
2+
<div class="container">
3+
<inkapp-collection></inkapp-collection>
4+
</div>

src/app/services/db.service.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
import { Injectable } from '@angular/core';
22
import * as Datastore from 'nedb';
3+
import { bindCallback } from 'rxjs';
4+
import { Store } from '@ngxs/store';
5+
import { LoadInitialData } from '../store/actions/general.action';
36

47
@Injectable({
58
providedIn: 'root'
69
})
710
export class DBService {
8-
db;
9-
_constructor() {
10-
this.db = new Datastore({ filename: 'ink/db' });
11-
this.db.loadDatabase(e => {});
12-
const doc = {
13-
hello: 'Hambyui'
14-
};
15-
this.db.insert(doc, function(err, newDoc) {
16-
console.log('database insert done!', doc);
11+
db = { bucket: null, ink: null, collection: null };
12+
constructor(private store: Store) {
13+
Object.keys(this.db).forEach(key => {
14+
this.db[key] = new Datastore({ filename: `ink/db/${key}`, autoload: true });
15+
this.db[key].find({}, (err, docs) => {
16+
this.store.dispatch(new LoadInitialData(key, docs));
17+
console.log('while loading..', docs);
18+
});
1719
});
18-
this.db.find({}, function(err, docs) {
20+
}
21+
22+
findDocument(coll, q = {}) {
23+
this.db[coll].find(q, function(err, docs) {
1924
console.log('database fond done!', docs);
2025
});
2126
}
27+
28+
saveDocument(coll, doc) {
29+
return bindCallback(this.db[coll].insert(doc));
30+
}
31+
32+
updateDocument(coll, query = {}, doc) {
33+
this.db[coll].update(query, doc);
34+
}
2235
}

src/app/services/util.service.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Injectable } from '@angular/core';
2+
3+
@Injectable({
4+
providedIn: 'root'
5+
})
6+
export class UtilService {
7+
get ID(): string {
8+
return (
9+
Math.random()
10+
.toString(36)
11+
.substring(2, 5) +
12+
Math.random()
13+
.toString(36)
14+
.substring(2, 5)
15+
);
16+
}
17+
}

src/app/store/actions/bucket.action.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ export class CreateBucket {
44
static readonly type = '[Bucket] Create new bucket';
55
constructor(public bucketData: InkBucketMeta) {}
66
}
7+
export class UpdateBucket {
8+
static readonly type = '[Ink] Update bucket';
9+
constructor(public bucketData: InkBucketMeta) {}
10+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { InkAppView, InkCollection } from '../../ink.model';
1+
import { InkAppView, InkCollection, InkCollectionMeta } from '../../ink.model';
22

33
export class CreateCollection {
44
static readonly type = '[Collection] Create new collection';
5-
constructor(public collectionData: InkCollection) {}
5+
constructor(public collectionData: InkCollectionMeta) {}
6+
}
7+
8+
export class UpdateCollection {
9+
static readonly type = '[Collection] Update collection';
10+
constructor(public collectionData: InkCollectionMeta) {}
611
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { InkBucket, InkBucketMeta } from '../../ink.model';
2+
3+
export class LoadInitialData {
4+
static readonly type = '[General] Load initial data';
5+
constructor(public db: string, public value: any) {}
6+
}

src/app/store/states/bucket.state.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
11
import { State, Action, StateContext } from '@ngxs/store';
22
import { InkAppSettings, InkBucket } from '../../ink.model';
3-
import { CreateBucket } from '../actions/bucket.action';
3+
import { CreateBucket, UpdateBucket } from '../actions/bucket.action';
4+
import { DBService } from '../../services/db.service';
5+
import { LoadInitialData } from '../actions/general.action';
6+
import { UtilService } from '../../services/util.service';
47

58
@State<InkBucket>({
69
name: 'bucket',
710
defaults: []
811
})
912
export class BucketState {
13+
constructor(private db: DBService, private util: UtilService) {}
14+
1015
@Action(CreateBucket)
1116
createBucket(ctx: StateContext<InkBucket>, action: CreateBucket) {
1217
const state = ctx.getState();
13-
state.push(action.bucketData);
18+
state.push({ ...action.bucketData, id: this.util.ID });
19+
ctx.setState([...state]);
20+
this.db.saveDocument('bucket', state);
21+
}
22+
23+
@Action(UpdateBucket)
24+
updateBucket(ctx: StateContext<InkBucket>, action: UpdateBucket) {
25+
let state: any = ctx.getState();
26+
state = state.map((a: any) => {
27+
if (a.id === action.bucketData.id) {
28+
a.name = action.bucketData.name;
29+
}
30+
return a;
31+
});
1432
ctx.setState([...state]);
33+
this.db.updateDocument('bucket', { id: action.bucketData.id }, action.bucketData);
34+
}
35+
36+
@Action(LoadInitialData)
37+
loadBucketData(ctx: StateContext<InkBucket>, action: LoadInitialData) {
38+
if (action.db !== 'bucket') {
39+
return;
40+
}
41+
ctx.setState(action.value);
42+
this.db.saveDocument('bucket', action.value);
1543
}
1644
}
Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,50 @@
11
import { State, Action, StateContext } from '@ngxs/store';
2-
import { InkCollection } from '../../ink.model';
2+
import { InkCollection, InkCollectionMeta } from '../../ink.model';
3+
import { LoadInitialData } from '../actions/general.action';
4+
import { UtilService } from '../../services/util.service';
5+
import { CreateCollection, UpdateCollection } from '../actions/collection.actions';
6+
import { DBService } from '../../services/db.service';
37

48
@State<InkCollection>({
59
name: 'collection',
6-
defaults: [
7-
{
8-
id: 1,
10+
defaults: []
11+
})
12+
export class CollectionState {
13+
constructor(private db: DBService, private util: UtilService) {}
14+
15+
@Action(CreateCollection)
16+
createCollection(ctx: StateContext<InkCollection>, action: CreateCollection) {
17+
const state = ctx.getState();
18+
state.push({ ...action.collectionData, id: this.util.ID });
19+
ctx.setState([...state]);
20+
this.db.saveDocument('collection', state);
21+
}
22+
23+
@Action(UpdateCollection)
24+
updateCollection(ctx: StateContext<InkCollection>, action: UpdateCollection) {
25+
let state: any = ctx.getState();
26+
state = state.map((a: any) => {
27+
if (a.id === action.collectionData.id) {
28+
a.name = action.collectionData.name;
29+
}
30+
return a;
31+
});
32+
ctx.setState([...state]);
33+
this.db.updateDocument('collection', { id: action.collectionData.id }, action.collectionData);
34+
}
35+
36+
@Action(LoadInitialData)
37+
loadCollectionData(ctx: StateContext<InkCollection>, action: LoadInitialData) {
38+
if (action.db !== 'collection') {
39+
return;
40+
}
41+
const defaultData = {
42+
id: this.util.ID,
943
created: new Date(),
1044
name: 'Default'
11-
}
12-
]
13-
})
14-
export class CollectionState {}
45+
};
46+
const data = action.value.length === 0 ? defaultData : action.value;
47+
ctx.setState(data);
48+
this.db.saveDocument('collection', data);
49+
}
50+
}

src/app/store/states/ink.state.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
import { State, Action, StateContext } from '@ngxs/store';
22
import { Ink } from '../../ink.model';
33
import { AddNewInk, UpdateInkColor } from '../actions/ink.action';
4+
import { LoadInitialData } from '../actions/general.action';
5+
import { DBService } from '../../services/db.service';
6+
import { UtilService } from '../../services/util.service';
47

58
@State<Ink>({
69
name: 'ink',
710
defaults: []
811
})
912
export class InkState {
13+
constructor(private db: DBService, private util: UtilService) {}
1014
@Action(AddNewInk)
1115
createBucket(ctx: StateContext<Ink>, action: AddNewInk) {
1216
const state = ctx.getState();
13-
state.push({ ...action.inkData, id: this.getID() });
17+
state.push({ ...action.inkData, id: this.util.ID });
1418
ctx.setState([...state]);
19+
this.db.saveDocument('ink', state);
1520
}
1621

1722
@Action(UpdateInkColor)
1823
updateInkColor(ctx: StateContext<Ink>, action: UpdateInkColor) {
1924
let state: any = ctx.getState();
20-
console.log('state', state);
2125
state = state.map((a: any) => {
2226
if (a.id === action.inkData.id) {
2327
a.value = action.inkData.value;
28+
a.bucketId = action.inkData.bucketId;
2429
a.meta = action.inkData.meta;
2530
}
2631
return a;
2732
});
2833
ctx.setState([...state]);
34+
this.db.updateDocument('ink', { id: action.inkData.id }, action.inkData);
2935
}
3036

31-
private getID() {
32-
return (
33-
Math.random()
34-
.toString(36)
35-
.substring(2, 5) +
36-
Math.random()
37-
.toString(36)
38-
.substring(2, 5)
39-
);
37+
@Action(LoadInitialData)
38+
loadInkData(ctx: StateContext<Ink>, action: LoadInitialData) {
39+
if (action.db !== 'ink') {
40+
return;
41+
}
42+
ctx.setState(action.value);
43+
this.db.saveDocument('ink', action.value);
4044
}
4145
}

src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<title>InkBucket</title>
77
<base href="/">
88
<meta name="viewport" content="width=device-width, initial-scale=1">
9+
<link href="https://fonts.googleapis.com/css?family=Rubik:300,400,500,700" rel="stylesheet">
910
<link rel="icon" type="image/x-icon" href="favicon.ico">
1011
</head>
1112

0 commit comments

Comments
 (0)