Skip to content

Commit df3ae6b

Browse files
committed
feat: updated store and added color picker
1 parent 8152f65 commit df3ae6b

22 files changed

+262
-27
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"keypress.js": "^2.1.5",
3535
"nedb": "^1.8.0",
3636
"ngx-clipboard": "^11.0.0",
37+
"ngx-color": "^1.3.1",
3738
"ngx-smart-modal": "^6.0.0",
3839
"ngx-toastr": "^8.5.1",
3940
"pouchdb-adapter-http": "^6.4.3",
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
<inkapp-ink></inkapp-ink>
1+
<h3>{{bucketData.name}}</h3>
2+
<button class="btn primary-outline" (click)="addNewInk()">Add new color</button>
3+
<div class="d-flex">
4+
<ng-container *ngFor="let color of ink|async">
5+
<inkapp-ink [inkData]="color"></inkapp-ink>
6+
</ng-container>
7+
</div>
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, Input } from '@angular/core';
2+
import { Store } from '@ngxs/store';
3+
import { InkBucket, Ink, InkBucketMeta } from '../../ink.model';
4+
import { Observable } from 'rxjs';
5+
import { filter, tap, map } from 'rxjs/operators';
6+
import { AddNewInk } from '../../store/actions/ink.action';
27

38
@Component({
49
selector: 'inkapp-bucket',
510
templateUrl: './bucket.component.html',
611
styles: []
712
})
813
export class BucketComponent implements OnInit {
9-
10-
constructor() { }
11-
12-
ngOnInit() {
14+
@Input() collectionId: number;
15+
@Input() bucketData: InkBucketMeta;
16+
ink: Observable<Ink>;
17+
constructor(private store: Store) {
18+
this.ink = this.store.select(s => s.ink).pipe(map(x => x.filter(y => y.bucketId === this.bucketData.id)));
1319
}
1420

21+
ngOnInit() {}
22+
addNewInk() {
23+
this.store.dispatch(new AddNewInk({ bucketId: this.bucketData.id, value: '#fff', meta: {} }));
24+
}
1525
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
<inkapp-bucket></inkapp-bucket>
1+
<ng-container *ngIf="collection as coll">
2+
<h4>{{coll.name}}</h4>
3+
<ng-container *ngFor="let b of bucket|async">
4+
<inkapp-bucket [bucketData]="b"></inkapp-bucket>
5+
</ng-container>
6+
</ng-container>
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { Store, Select } from '@ngxs/store';
3+
import { InkBucket, InkCollection } from '../../ink.model';
4+
import { Observable } from 'rxjs';
5+
import { map } from 'rxjs/operators';
26

37
@Component({
48
selector: 'inkapp-collection',
59
templateUrl: './collection.component.html',
610
styles: []
711
})
812
export class CollectionComponent implements OnInit {
9-
10-
constructor() { }
13+
bucket: Observable<InkBucket>;
14+
collection: InkCollection;
15+
constructor(private store: Store) {
16+
this.store
17+
.select(s => s.collection)
18+
.pipe(map(x => x[0]))
19+
.subscribe(r => {
20+
console.log('CColl', r);
21+
this.collection = r;
22+
});
23+
}
1124

1225
ngOnInit() {
26+
this.bucket = this.store.select(s => s.bucket).pipe(map(x => x.filter(y => y.collectionId === this.collection.id)));
1327
}
14-
1528
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
<p>
2-
ink works!
3-
</p>
1+
<div class="comp-ink">
2+
<div class="color" [style.backgroundColor]="inkData.value" (click)="openColorPicker()"></div>
3+
<div class="data">
4+
{{inkData.value}}
5+
</div>
6+
<color-chrome (onChange)="colorChanged(inkData.id,$event)"></color-chrome>
7+
</div>
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, Input } from '@angular/core';
2+
import { Store } from '@ngxs/store';
3+
import { InkColor } from '../../ink.model';
4+
import { UpdateInkColor } from '../../store/actions/ink.action';
25

36
@Component({
47
selector: 'inkapp-ink',
58
templateUrl: './ink.component.html',
69
styles: []
710
})
811
export class InkComponent implements OnInit {
9-
constructor() {}
12+
@Input() inkData: InkColor;
13+
@Input() bucketId: number;
14+
constructor(private store: Store) {}
1015

1116
ngOnInit() {}
17+
openColorPicker() {}
18+
colorChanged(id, c) {
19+
console.log('c', c);
20+
this.store.dispatch(new UpdateInkColor({ bucketId: this.bucketId, value: c.color.hex, meta: c.color, id }));
21+
}
1222
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
<div class="collapse navbar-collapse" id="navbarSupportedContent">
1010
<ul class="navbar-nav ml-auto">
1111
<li class="nav-item active">
12-
<a class="btn btn-primary" href="#">Home
13-
<span class="sr-only">(current)</span>
14-
</a>
12+
<a class="btn btn-primary" href="#" (click)="newBucket()">New bucket </a>
1513
</li>
1614
<li class="nav-item">
1715
<a class="nav-link" href="#">Settings</a>
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { Store } from '@ngxs/store';
3+
import { CreateBucket } from '../../store/actions/bucket.action';
24

35
@Component({
46
selector: 'inkapp-navbar',
57
templateUrl: './navbar.component.html',
68
styles: []
79
})
810
export class NavbarComponent implements OnInit {
11+
constructor(private store: Store) {}
912

10-
constructor() { }
13+
ngOnInit() {}
1114

12-
ngOnInit() {
15+
newBucket() {
16+
this.store.dispatch(new CreateBucket({ collectionId: 1, id: 1, name: 'Hello-123' }));
1317
}
14-
1518
}

src/app/ink.model.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,32 @@ export enum InkColorType {
44
HSL
55
}
66

7+
export interface InkBucketMeta {
8+
id: number;
9+
name: string;
10+
collectionId: number;
11+
}
12+
13+
export type InkBucket = InkBucketMeta[];
14+
715
export interface InkColor {
16+
id?: string;
817
name?: string;
918
value: string;
10-
type: InkColorType;
11-
belongsTo: string;
19+
meta: any;
20+
bucketId: number;
1221
}
1322

14-
export interface InkCollection {
23+
export type Ink = InkColor[];
24+
25+
export interface InkCollectionMeta {
26+
id: number;
1527
name: string;
16-
colors: InkColor[];
1728
created: Date;
1829
}
30+
31+
export type InkCollection = InkCollectionMeta[];
32+
1933
export enum InkAppView {
2034
ROUND,
2135
RECT,

src/app/ink.module.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ import { InkApp } from './ink.component';
1515
import { RoutingModule } from './ink.routing';
1616
import { HomePage } from './pages/home/home.component';
1717
import { SettingsPage } from './pages/settings/settings.component';
18-
import { SettingsState } from './store/states/settings.state';
18+
import { SettingsState, BucketState, CollectionState, InkState } from './store/states';
19+
import { ColorModule } from './modules/color/color.module';
1920

20-
export const MODULES = [RoutingModule, NgxsModule.forRoot([SettingsState]), NgxsReduxDevtoolsPluginModule.forRoot()];
21+
export const MODULES = [
22+
RoutingModule,
23+
NgxsModule.forRoot([SettingsState, BucketState, CollectionState, InkState]),
24+
NgxsReduxDevtoolsPluginModule.forRoot(),
25+
ColorModule
26+
];
2127
export const COMPONENTS = [
2228
InkApp,
2329
NavbarComponent,

src/app/modules/color/color.module.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { NgModule } from '@angular/core';
2+
import { ColorAlphaModule } from 'ngx-color/alpha'; // <color-alpha-picker></color-alpha-picker>
3+
import { ColorBlockModule } from 'ngx-color/block'; // <color-block></color-block>
4+
import { ColorChromeModule } from 'ngx-color/chrome'; // <color-chrome></color-chrome>
5+
import { ColorCircleModule } from 'ngx-color/circle'; // <color-circle></color-circle>
6+
import { ColorCompactModule } from 'ngx-color/compact'; // <color-compact></color-compact>
7+
import { ColorGithubModule } from 'ngx-color/github'; // <color-github></color-github>
8+
import { ColorHueModule } from 'ngx-color/hue'; // <color-hue-picker></color-hue-picker>
9+
import { ColorMaterialModule } from 'ngx-color/material'; // <color-material></color-material>
10+
import { ColorPhotoshopModule } from 'ngx-color/photoshop'; // <color-photoshop></color-photoshop>
11+
import { ColorSketchModule } from 'ngx-color/sketch'; // <color-sketch></color-sketch>
12+
import { ColorSliderModule } from 'ngx-color/slider'; // <color-slider></color-slider>
13+
import { ColorSwatchesModule } from 'ngx-color/swatches'; // <color-swatches></color-swatches>
14+
import { ColorTwitterModule } from 'ngx-color/twitter'; // <color-twitter></color-twitter>
15+
16+
import { CommonModule } from '@angular/common';
17+
18+
@NgModule({
19+
imports: [CommonModule, ColorChromeModule],
20+
exports: [ColorChromeModule],
21+
declarations: [],
22+
providers: []
23+
})
24+
export class ColorModule {}
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 CreateBucket {
4+
static readonly type = '[Bucket] Create new bucket';
5+
constructor(public bucketData: InkBucketMeta) {}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { InkAppView, InkCollection } from '../../ink.model';
2+
3+
export class CreateCollection {
4+
static readonly type = '[Collection] Create new collection';
5+
constructor(public collectionData: InkCollection) {}
6+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { InkColor } from '../../ink.model';
2+
3+
export class AddNewInk {
4+
static readonly type = '[Ink] Add new ink';
5+
constructor(public inkData: InkColor) {}
6+
}
7+
8+
export class UpdateInkColor {
9+
static readonly type = '[Ink] Update color';
10+
constructor(public inkData: InkColor) {}
11+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { State, Action, StateContext } from '@ngxs/store';
2+
import { InkAppSettings, InkBucket } from '../../ink.model';
3+
import { CreateBucket } from '../actions/bucket.action';
4+
5+
@State<InkBucket>({
6+
name: 'bucket',
7+
defaults: []
8+
})
9+
export class BucketState {
10+
@Action(CreateBucket)
11+
createBucket(ctx: StateContext<InkBucket>, action: CreateBucket) {
12+
const state = ctx.getState();
13+
state.push(action.bucketData);
14+
ctx.setState([...state]);
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { State, Action, StateContext } from '@ngxs/store';
2+
import { InkCollection } from '../../ink.model';
3+
4+
@State<InkCollection>({
5+
name: 'collection',
6+
defaults: [
7+
{
8+
id: 1,
9+
created: new Date(),
10+
name: 'Default'
11+
}
12+
]
13+
})
14+
export class CollectionState {}

src/app/store/states/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { BucketState } from './bucket.state';
2+
export { CollectionState } from './collection.state';
3+
export { SettingsState } from './settings.state';
4+
export { InkState } from './ink.state';

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { State, Action, StateContext } from '@ngxs/store';
2+
import { Ink } from '../../ink.model';
3+
import { AddNewInk, UpdateInkColor } from '../actions/ink.action';
4+
5+
@State<Ink>({
6+
name: 'ink',
7+
defaults: []
8+
})
9+
export class InkState {
10+
@Action(AddNewInk)
11+
createBucket(ctx: StateContext<Ink>, action: AddNewInk) {
12+
const state = ctx.getState();
13+
state.push({ ...action.inkData, id: this.getID() });
14+
ctx.setState([...state]);
15+
}
16+
17+
@Action(UpdateInkColor)
18+
updateInkColor(ctx: StateContext<Ink>, action: UpdateInkColor) {
19+
let state: any = ctx.getState();
20+
console.log('state', state);
21+
state = state.map((a: any) => {
22+
if (a.id === action.inkData.id) {
23+
a.value = action.inkData.value;
24+
}
25+
return a;
26+
});
27+
ctx.setState([...state]);
28+
}
29+
30+
private getID() {
31+
return (
32+
Math.random()
33+
.toString(36)
34+
.substring(2, 5) +
35+
Math.random()
36+
.toString(36)
37+
.substring(2, 5)
38+
);
39+
}
40+
}

src/styles/components/__index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
@import './basic-input';
2+
@import './ink';

0 commit comments

Comments
 (0)