Skip to content

Commit 5d0b308

Browse files
author
Manuela Paula Ritter
committed
update matrix state structure to use array instead of TaskDictionary
1 parent f2ecd3a commit 5d0b308

File tree

8 files changed

+57
-51
lines changed

8 files changed

+57
-51
lines changed

src/app/matrix/canvas/canvas.component.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { select, Store } from '@ngrx/store';
32
import { map } from 'rxjs/operators';
4-
import { AppState } from 'src/app/store/app.state';
53
import { Task, TaskDictionary, Topic } from '../matrix.interfaces';
6-
import { selectMatrixTasks, selectMatrixTopics } from '../matrix.selectors';
74
import { MatrixService } from '../matrix.service';
85

96
@Component({
@@ -22,10 +19,9 @@ export class CanvasComponent implements OnInit {
2219

2320
ngOnInit(): void {
2421
this.matrixService
25-
.selectTasks()
22+
.selectTasksByTopics()
2623
.pipe(
2724
map((tasks) => {
28-
console.log(tasks);
2925
if (tasks) {
3026
this.tasks = tasks;
3127
}

src/app/matrix/matrix.actions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Action, createAction, props } from '@ngrx/store';
2-
import { CurrentTask, MatrixData, Task, Topic } from './matrix.interfaces';
1+
import { Action } from '@ngrx/store';
2+
import { MatrixData, Task, Topic } from './matrix.interfaces';
33

44
export const GET_MATRIX_DATA = '[MATRIX] get all matrix data from sever';
55
export const GET_MATRIX_DATA_SUCCESS =
@@ -101,7 +101,7 @@ export class UpdateTopicFailed implements Action {
101101

102102
export class SelectTask implements Action {
103103
readonly type = SELECT_TASK;
104-
constructor(public currentTask: CurrentTask) {}
104+
constructor(public currentTaskId: number) {}
105105
}
106106

107107
export class ToggleTopicVisibility implements Action {

src/app/matrix/matrix.interfaces.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ export interface TaskDictionary {
3737
[topicId: number]: Task[];
3838
}
3939

40-
export interface CurrentTask {
41-
topicId?: number;
42-
taskId?: number;
40+
export interface TopicDictionary {
41+
[id: number]: Topic;
4342
}
4443

4544
export interface MatrixData {
@@ -49,8 +48,8 @@ export interface MatrixData {
4948

5049
export interface MatrixState {
5150
topics: Topic[];
52-
tasks: TaskDictionary;
53-
taskHistory: CurrentTask[];
51+
tasks: Task[];
52+
taskHistory: number[];
5453
isLoading: boolean;
5554
errorMessage?: string;
5655
}
@@ -65,7 +64,8 @@ export interface MatrixService {
6564
selectTopics: () => Observable<Topic[]>;
6665
selectIsLoading: () => Observable<boolean>;
6766
selectErrorMessage: () => Observable<string | undefined>;
68-
selectTasks: () => Observable<TaskDictionary>;
67+
selectTasks: () => Observable<Task[]>;
68+
selectTasksByTopics: () => Observable<TaskDictionary>;
6969
selectTaskHistory: () => Observable<Task[]>;
7070
selectTopicById: (id: number) => Observable<Topic | undefined>;
7171
}

src/app/matrix/matrix.reducers.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('MatrixReducers', () => {
3131
beforeEach(() => {
3232
defaultState = {
3333
topics: [],
34-
tasks: {},
34+
tasks: [],
3535
taskHistory: [],
3636
isLoading: false,
3737
errorMessage: undefined,
@@ -80,7 +80,7 @@ describe('MatrixReducers', () => {
8080
beforeEach(() => {
8181
defaultState = {
8282
topics: [],
83-
tasks: {},
83+
tasks: [],
8484
taskHistory: [],
8585
isLoading: true,
8686
errorMessage: undefined,
@@ -126,7 +126,7 @@ describe('MatrixReducers', () => {
126126
beforeEach(() => {
127127
defaultState = {
128128
topics: [new Topic(1, 'test', Color.orange, true, false)],
129-
tasks: { [1]: [] },
129+
tasks: [],
130130
taskHistory: [],
131131
isLoading: true,
132132
errorMessage: 'test message',
@@ -178,7 +178,7 @@ describe('MatrixReducers', () => {
178178
beforeEach(() => {
179179
emptyState = {
180180
topics: [],
181-
tasks: {},
181+
tasks: [],
182182
taskHistory: [],
183183
isLoading: true,
184184
errorMessage: undefined,
@@ -187,7 +187,7 @@ describe('MatrixReducers', () => {
187187
defaultState = {
188188
...emptyState,
189189
topics: [new Topic(1, 'test', Color.orange, true, false)],
190-
tasks: {},
190+
tasks: [],
191191
};
192192
});
193193

@@ -235,7 +235,7 @@ describe('MatrixReducers', () => {
235235
new Topic(1, 'test', Color.green, true, false),
236236
new Topic(2, 'test', Color.orange, false, false),
237237
],
238-
tasks: {},
238+
tasks: [],
239239
taskHistory: [],
240240
isLoading: false,
241241
errorMessage: undefined,

src/app/matrix/matrix.reducers.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import {
1717
UPDATE_TOPIC_FAILED,
1818
UPDATE_TOPIC_SUCCESS,
1919
} from './matrix.actions';
20-
import { MatrixState, Task, TaskDictionary } from './matrix.interfaces';
20+
import { MatrixState } from './matrix.interfaces';
2121

2222
export const initialState: MatrixState = {
2323
topics: [],
24-
tasks: {},
24+
tasks: [],
2525
taskHistory: [],
2626
isLoading: false,
2727
errorMessage: undefined,
@@ -49,15 +49,15 @@ export function matrixReducer(state = initialState, action: Action) {
4949
// === success cases ===
5050
case GET_MATRIX_DATA_SUCCESS:
5151
draft.topics = matrixAction.data.topics;
52-
draft.tasks = createTaskDictionary(matrixAction.data.tasks);
52+
draft.tasks = matrixAction.data.tasks;
5353
setUnloading(draft);
5454
return;
5555
case UPDATE_TASK_SUCCESS:
56-
const taskIndex = draft.tasks[matrixAction.task.topic].findIndex(
56+
const taskIndex = draft.tasks.findIndex(
5757
(task) => task.id === matrixAction.task.id,
5858
);
5959
if (taskIndex > -1) {
60-
draft.tasks[matrixAction.task.topic][taskIndex] = matrixAction.task;
60+
draft.tasks[taskIndex] = matrixAction.task;
6161
}
6262
setUnloading(draft);
6363
return;
@@ -76,14 +76,13 @@ export function matrixReducer(state = initialState, action: Action) {
7676
return;
7777
// === frontend only actions ===
7878
case SELECT_TASK:
79-
console.log(matrixAction.currentTask);
8079
const alreadyInHistory = draft.taskHistory.findIndex(
81-
(t) => t.taskId === matrixAction.currentTask.taskId,
80+
(t) => t === matrixAction.currentTaskId,
8281
);
8382
if (alreadyInHistory > -1) {
8483
draft.taskHistory.splice(alreadyInHistory, 1);
8584
}
86-
draft.taskHistory.unshift(matrixAction.currentTask);
85+
draft.taskHistory.unshift(matrixAction.currentTaskId);
8786
if (draft.taskHistory.length > 6) {
8887
draft.taskHistory.pop();
8988
}
@@ -104,16 +103,3 @@ const setUnloading = (draft: MatrixState): void => {
104103
draft.isLoading = false;
105104
draft.errorMessage = undefined;
106105
};
107-
108-
const createTaskDictionary = (tasks: Task[]): TaskDictionary => {
109-
const dict: TaskDictionary = {};
110-
tasks.forEach((task) => {
111-
if (dict[task.topic]) {
112-
dict[task.topic].push(task);
113-
} else {
114-
dict[task.topic] = [task];
115-
}
116-
});
117-
console.log(dict);
118-
return dict;
119-
};

src/app/matrix/matrix.selectors.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export const selectMatrixTasks = createSelector(
2424
(state: MatrixState) => state.tasks,
2525
);
2626

27+
export const selectMatrixTasksByTopics = createSelector(
28+
selectMatrix,
29+
(state: MatrixState) => createTaskDictionary(state.tasks),
30+
);
31+
2732
export const selectMatrixTopic = createSelector(
2833
selectMatrixTopics,
2934
(topics: Topic[], id: number) => topics.find((t) => t.id === id),
@@ -36,10 +41,8 @@ export const selectTaskHistory = createSelector(
3641

3742
if (state.taskHistory.length > 0) {
3843
state.taskHistory.forEach((c) => {
39-
if (c.topicId) {
40-
const foundTask = state.tasks[c.topicId].find(
41-
(t) => t.id === c.taskId,
42-
);
44+
if (c) {
45+
const foundTask = state.tasks.find((t) => t.id === c);
4346
if (foundTask) {
4447
result.push(foundTask);
4548
}
@@ -50,3 +53,15 @@ export const selectTaskHistory = createSelector(
5053
return result;
5154
},
5255
);
56+
57+
const createTaskDictionary = (tasks: Task[]): TaskDictionary => {
58+
const dict: TaskDictionary = {};
59+
tasks.forEach((task) => {
60+
if (dict[task.topic]) {
61+
dict[task.topic].push(task);
62+
} else {
63+
dict[task.topic] = [task];
64+
}
65+
});
66+
return dict;
67+
};

src/app/matrix/matrix.service.mock.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ export class MatrixMockService implements MatrixService {
2828
selectErrorMessage(): Observable<string | undefined> {
2929
return of(undefined);
3030
}
31-
selectTasks(): Observable<TaskDictionary> {
31+
selectTasks(): Observable<Task[]> {
32+
return of([this.testTask]);
33+
}
34+
selectTasksByTopics(): Observable<TaskDictionary> {
3235
return of({ [1]: [this.testTask] });
3336
}
3437
selectCurrentTask(): Observable<Task | undefined> {

src/app/matrix/matrix.service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
selectMatrixErrorMessage,
2222
selectMatrixIsLoading,
2323
selectMatrixTasks,
24+
selectMatrixTasksByTopics,
2425
selectMatrixTopic,
2526
selectMatrixTopics,
2627
selectTaskHistory,
@@ -53,9 +54,7 @@ export class MatrixService implements IMatrixService, OnDestroy {
5354

5455
selectTask(task: Task): void {
5556
console.log(task);
56-
this.store.dispatch(
57-
new SelectTask({ taskId: task.id, topicId: task.topic }),
58-
);
57+
this.store.dispatch(new SelectTask(task.id));
5958
}
6059
toggleTopicVisibility(topicId: number): void {
6160
this.store.dispatch(new ToggleTopicVisibility(topicId));
@@ -86,13 +85,20 @@ export class MatrixService implements IMatrixService, OnDestroy {
8685
);
8786
}
8887

89-
selectTasks(): Observable<TaskDictionary> {
88+
selectTasks(): Observable<Task[]> {
9089
return this.store.pipe(
9190
select(selectMatrixTasks),
9291
takeUntil(this.unsubscribe$),
9392
);
9493
}
9594

95+
selectTasksByTopics(): Observable<TaskDictionary> {
96+
return this.store.pipe(
97+
select(selectMatrixTasksByTopics),
98+
takeUntil(this.unsubscribe$),
99+
);
100+
}
101+
96102
selectTaskHistory(): Observable<Task[]> {
97103
return this.store.pipe(
98104
select(selectTaskHistory),
@@ -102,7 +108,7 @@ export class MatrixService implements IMatrixService, OnDestroy {
102108

103109
selectTopicById(id: number): Observable<Topic | undefined> {
104110
return this.store.pipe(
105-
select(selectMatrixTopic),
111+
select(selectMatrixTopic, id),
106112
takeUntil(this.unsubscribe$),
107113
);
108114
}

0 commit comments

Comments
 (0)