Skip to content

Commit 41936ac

Browse files
committed
feat(wip): updated ink colors service
1 parent d5b6d28 commit 41936ac

File tree

8 files changed

+111
-47
lines changed

8 files changed

+111
-47
lines changed

src/app/components/bucket/bucket.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
</inkapp-magic-title>
44
<div class="d-flex flex-wrap">
55
<ng-container *ngFor="let color of ink|async">
6-
<inkapp-ink [inkData]="color"></inkapp-ink>
6+
<inkapp-ink [inkData]="color" [bucketId]="bucketData._id"></inkapp-ink>
77
</ng-container>
88
<span class="ink-color-sticker -empty" (click)="addNewInk()">
99
<div class="ink-color">

src/app/components/bucket/bucket.component.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Component, OnInit, Input } from '@angular/core';
22
import { Store } from '@ngxs/store';
3-
import { InkBucket, Ink, InkBucketMeta } from '../../models';
3+
import { InkBucket, Ink, InkBucketMeta, InkColorMeta } from '../../models';
44
import { Observable } from 'rxjs';
55
import { filter, tap, map } from 'rxjs/operators';
6-
import { AddNewInk } from '../../store/actions/ink.action';
6+
import { AddInkColor, LoadInkColorsInBucket } from '../../store/actions/ink.action';
77
import { BucketService } from '../../services/bucket.service';
88
import { ENETDOWN } from 'constants';
99
import { RenameBucket } from '../../store/actions/bucket.action';
10+
import { InkColorService } from '../../services/ink.service';
1011

1112
@Component({
1213
selector: 'inkapp-bucket',
@@ -17,13 +18,25 @@ export class BucketComponent implements OnInit {
1718
@Input() index: number;
1819
@Input() bucketData: InkBucketMeta;
1920
ink: Observable<Ink>;
20-
constructor(private _store: Store, private _bucketService: BucketService) {
21+
constructor(private _store: Store, private _bucketService: BucketService, private _inkColorService: InkColorService) {
2122
this.ink = this._store.select(s => s.ink).pipe(map(x => x.filter(y => y.bucketId === this.bucketData._id)));
2223
}
2324

24-
ngOnInit() {}
25+
ngOnInit() {
26+
this._inkColorService.getInkColorsInBuckets(this.bucketData._id).then(docs => {
27+
this._store.dispatch(new LoadInkColorsInBucket(docs));
28+
});
29+
}
2530
addNewInk() {
26-
this._store.dispatch(new AddNewInk({ bucketId: this.bucketData._id, value: '#fff', meta: {} }));
31+
const data: InkColorMeta = {
32+
bucketId: this.bucketData._id,
33+
displayValue: 'white',
34+
meta: {},
35+
name: '#fff'
36+
};
37+
this._inkColorService.addInkColor(this.bucketData._id, data).then(doc => {
38+
this._store.dispatch(new AddInkColor(this.bucketData._id, doc as any));
39+
});
2740
}
2841
onTitleChange(newTitle) {
2942
this._bucketService.changeBucketName(this.bucketData._id, newTitle).then(bucket => {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="ink-color-sticker">
2-
<div class="ink-color" [style.backgroundColor]="inkData.value" (click)="openColorPicker()"></div>
2+
<div class="ink-color" [style.backgroundColor]="inkColor.displayValue" (click)="openColorPicker()"></div>
33
<div class="ink-name">
4-
{{inkData.value}}
4+
{{inkColor.displayValue}}
55
</div>
6-
<!-- <color-chrome (onChange)="colorChanged(inkData.id,$event)"></color-chrome> -->
6+
<!-- <color-chrome (onChange)="colorChanged(inkColor.id,$event)"></color-chrome> -->
77
</div>

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import { Component, OnInit, Input, OnDestroy, ComponentRef, ElementRef } from '@angular/core';
22
import { Store } from '@ngxs/store';
3-
import { InkColor } from '../../models';
3+
import { InkColorMeta, InkDocMethods } from '../../models';
44
import { UpdateInkColor } from '../../store/actions/ink.action';
55
import { Overlay, OverlayRef, OverlayConfig } from '@angular/cdk/overlay';
66
import { ComponentPortal } from '@angular/cdk/portal';
77
import { ChromeComponent } from 'ngx-color/chrome';
88
import { map, debounceTime } from 'rxjs/operators';
9+
import { InkColorService } from '../../services/ink.service';
910

1011
@Component({
1112
selector: 'inkapp-ink',
1213
templateUrl: './ink.component.html',
1314
styles: []
1415
})
1516
export class InkComponent implements OnInit, OnDestroy {
16-
@Input() inkData: InkColor;
17+
@Input() inkData: InkColorMeta;
1718
@Input() bucketId: string;
1819
overlayRef: OverlayRef;
1920
colorPanelPortal: ComponentPortal<ChromeComponent>;
2021
colorPickerRef: ComponentRef<ChromeComponent>;
21-
constructor(private store: Store, private overlay: Overlay, private elRef: ElementRef) {}
22+
inkColor: InkColorMeta = null;
23+
constructor(
24+
private _store: Store,
25+
private overlay: Overlay,
26+
private elRef: ElementRef,
27+
private _inkColorService: InkColorService
28+
) {}
2229

2330
ngOnInit() {
2431
const config = new OverlayConfig();
@@ -27,6 +34,7 @@ export class InkComponent implements OnInit, OnDestroy {
2734
config.positionStrategy = this.overlay
2835
.position()
2936
.connectedTo(this.elRef, { originX: 'start', originY: 'bottom' }, { overlayX: 'start', overlayY: 'top' });
37+
this.inkColor = this.inkData;
3038

3139
this.overlayRef = this.overlay.create(config);
3240
this.colorPanelPortal = new ComponentPortal(ChromeComponent);
@@ -40,19 +48,30 @@ export class InkComponent implements OnInit, OnDestroy {
4048
this.colorPickerRef.instance.color = this.inkData.meta.hsl;
4149
}
4250
this.colorPickerRef.instance.onChange.pipe(map(c => c.color)).subscribe(color => {
43-
this.store.dispatch(
44-
new UpdateInkColor({ bucketId: this.inkData.bucketId, value: color.hex, meta: color, id: this.inkData.id })
45-
);
51+
this.inkColor = {
52+
name: color.hex,
53+
displayValue: color.hex,
54+
meta: color,
55+
bucketId: this.bucketId,
56+
_id: this.inkData._id
57+
};
4658
});
4759
this.overlayRef.backdropClick().subscribe(b => {
48-
console.log('backdrop clocked', b);
60+
if (this.inkColor.name !== this.inkData.name) {
61+
this.updateInkColor(this.inkColor);
62+
}
4963
if (this.overlayRef && this.overlayRef.hasAttached()) {
5064
this.overlayRef.detach();
5165
this.colorPickerRef.destroy();
5266
return;
5367
}
5468
});
5569
}
70+
updateInkColor(data: InkColorMeta) {
71+
this._inkColorService.updateInkColor(this.inkData._id, data).then(doc => {
72+
this._store.dispatch(new UpdateInkColor(this.inkData._id, data));
73+
});
74+
}
5675
ngOnDestroy() {
5776
if (this.overlayRef) {
5877
this.overlayRef.dispose();

src/app/schema/ink.schema.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ export const InkSchema = {
55
type: 'object',
66
uniqueItems: true,
77
properties: {
8-
id: {
9-
type: 'string',
10-
primary: true
8+
name: {
9+
type: 'string'
1110
},
12-
value: {
11+
displayValue: {
1312
type: 'string'
1413
},
1514
meta: {
@@ -22,5 +21,5 @@ export const InkSchema = {
2221
type: 'string'
2322
}
2423
},
25-
required: ['id', 'name', 'meta', 'bucketId']
24+
required: ['displayValue', 'meta', 'bucketId']
2625
};

src/app/services/ink.service.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Injectable } from '@angular/core';
2+
import { LocalDatabase } from './localdb.service';
3+
import { InkColorMeta } from '../models';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class InkColorService {
9+
constructor(private _localDatabase: LocalDatabase) {}
10+
11+
async addInkColor(bucketId, inkData: InkColorMeta) {
12+
const db = await this._localDatabase.getDatabase();
13+
return db.ink.insert({ ...inkData, bucketId }).catch(error => {
14+
console.error('Error while saving ink color!');
15+
});
16+
}
17+
18+
async updateInkColor(inkId, inkData: InkColorMeta) {
19+
const db = await this._localDatabase.getDatabase();
20+
return db.ink
21+
.findOne(inkId)
22+
.update({
23+
$set: {
24+
...inkData
25+
}
26+
})
27+
.catch(error => {
28+
console.error('Error while updating ink color!', error);
29+
});
30+
}
31+
32+
async getInkColorsInBuckets(bucketId) {
33+
const db = await this._localDatabase.getDatabase();
34+
return db.ink.find({ bucketId }).exec();
35+
}
36+
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { InkColor } from '../../models';
1+
import { InkColorMeta } from '../../models';
22

3-
export class AddNewInk {
3+
export class AddInkColor {
44
static readonly type = '[Ink] Add new ink';
5-
constructor(public inkData: InkColor) {}
5+
constructor(public bucketId: string, public inkData: InkColorMeta) {}
66
}
77

88
export class UpdateInkColor {
99
static readonly type = '[Ink] Update color';
10-
constructor(public inkData: InkColor) {}
10+
constructor(public id: string, public inkData: InkColorMeta) {}
11+
}
12+
13+
export class LoadInkColorsInBucket {
14+
static readonly type = '[Ink] get colors in bucket';
15+
constructor(public colors: InkColorMeta[]) {}
1116
}

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { State, Action, StateContext } from '@ngxs/store';
22
import { Ink } from '../../models';
3-
import { AddNewInk, UpdateInkColor } from '../actions/ink.action';
3+
import { UpdateInkColor, AddInkColor, LoadInkColorsInBucket } from '../actions/ink.action';
44
import { LoadInitialData } from '../actions/general.action';
55
import { DBService } from '../../services/db.service';
66
import { UtilService } from '../../services/util.service';
@@ -11,35 +11,27 @@ import { UtilService } from '../../services/util.service';
1111
})
1212
export class InkState {
1313
constructor(private db: DBService, private util: UtilService) {}
14-
@Action(AddNewInk)
15-
createBucket(ctx: StateContext<Ink>, action: AddNewInk) {
14+
@Action(AddInkColor)
15+
addInkColor(ctx: StateContext<Ink>, action: AddInkColor) {
1616
const state = ctx.getState();
17-
state.push({ ...action.inkData, id: this.util.ID });
17+
state.push(action.inkData);
1818
ctx.setState([...state]);
19-
this.db.saveDocument('ink', state);
2019
}
2120

2221
@Action(UpdateInkColor)
2322
updateInkColor(ctx: StateContext<Ink>, action: UpdateInkColor) {
24-
let state: any = ctx.getState();
25-
state = state.map((a: any) => {
26-
if (a.id === action.inkData.id) {
27-
a.value = action.inkData.value;
28-
a.bucketId = action.inkData.bucketId;
29-
a.meta = action.inkData.meta;
23+
const state: any = ctx.getState();
24+
const newState = state.map(c => {
25+
if (c._id === action.id) {
26+
return action.inkData;
3027
}
31-
return a;
28+
return c;
3229
});
33-
ctx.setState([...state]);
34-
this.db.updateDocument('ink', { id: action.inkData.id }, action.inkData);
30+
ctx.setState([...newState]);
3531
}
36-
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);
32+
@Action(LoadInkColorsInBucket)
33+
loadInkColorsInBucket(ctx: StateContext<Ink>, action: LoadInkColorsInBucket) {
34+
const state: any = ctx.getState();
35+
ctx.setState([...state, ...action.colors]);
4436
}
4537
}

0 commit comments

Comments
 (0)