Skip to content

Commit 002051b

Browse files
author
Manuela Paula Ritter
committed
add taskhistory, remove currenttask in state
1 parent e36e150 commit 002051b

File tree

7 files changed

+59
-25
lines changed

7 files changed

+59
-25
lines changed

src/app/matrix/matrix.effects.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,22 @@ export class MatrixEffects {
2020
new Topic(3, 'LongName Topic', Color.orange, true, false),
2121
];
2222
private tasks: Task[] = [
23-
new Task(1, 'Tast', 1, 60, false, new Date('2020-12-01T12:00:00.000Z')),
23+
new Task(1, 'Tast', 1, 60, new Date('2020-12-01T12:00:00.000Z')),
2424
new Task(2, 'Tast 2', 2, 20),
2525
new Task(3, 'Tast 3', 1, 33),
2626
new Task(4, 'Tast 4', 2, 99),
2727
new Task(5, 'Long Name Task', 3, 44),
28+
new Task(6, 'tttttt anen', 3, 55, new Date('2020-12-02T12:00:00.000Z')),
29+
new Task(7, 'Tast 2930', 1, 1, new Date('2020-11-20T12:00:00.000Z')),
30+
new Task(8, 'nenene', 2, 2, new Date('2020-11-28T12:00:00.000Z')),
31+
new Task(
32+
9,
33+
'nnn',
34+
3,
35+
90,
36+
new Date('2020-12-10T12:00:00.000Z'),
37+
'test description',
38+
),
2839
];
2940
private data: MatrixData = {
3041
topics: this.topics,

src/app/matrix/matrix.interfaces.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ export class Task {
2121
public name: string,
2222
public topic: number,
2323
public importance: number,
24-
public done: boolean = false,
2524
public dueDate: Date = new Date(),
25+
public description: string = '',
26+
public done: boolean = false,
2627
public deleted: boolean = false,
2728
public createdAt: Date = new Date(),
2829
public updatedAt: Date = new Date(),
@@ -47,7 +48,7 @@ export interface MatrixData {
4748
export interface MatrixState {
4849
topics: Topic[];
4950
tasks: TaskDictionary;
50-
currentTask: CurrentTask;
51+
taskHistory: CurrentTask[];
5152
isLoading: boolean;
5253
errorMessage?: string;
5354
}
@@ -63,6 +64,6 @@ export interface MatrixService {
6364
selectIsLoading: () => Observable<boolean>;
6465
selectErrorMessage: () => Observable<string | undefined>;
6566
selectTasks: () => Observable<TaskDictionary>;
66-
selectCurrentTask: () => Observable<Task | undefined>;
67+
selectTaskHistory: () => Observable<Task[]>;
6768
selectTopicById: (id: number) => Observable<Topic | undefined>;
6869
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { async, TestBed } from '@angular/core/testing';
22
import { Color } from '../shared/color.interfaces';
3-
import { AppState } from '../store/app.state';
43
import {
54
AddTopic,
65
AddTopicFailed,
@@ -33,7 +32,7 @@ describe('MatrixReducers', () => {
3332
defaultState = {
3433
topics: [],
3534
tasks: {},
36-
currentTask: { taskId: undefined, topicId: undefined },
35+
taskHistory: [],
3736
isLoading: false,
3837
errorMessage: undefined,
3938
};
@@ -80,7 +79,7 @@ describe('MatrixReducers', () => {
8079
defaultState = {
8180
topics: [],
8281
tasks: {},
83-
currentTask: { taskId: undefined, topicId: undefined },
82+
taskHistory: [],
8483
isLoading: true,
8584
errorMessage: undefined,
8685
};
@@ -126,7 +125,7 @@ describe('MatrixReducers', () => {
126125
defaultState = {
127126
topics: [new Topic(1, 'test', Color.orange, true, false)],
128127
tasks: { [1]: [] },
129-
currentTask: { taskId: undefined, topicId: undefined },
128+
taskHistory: [],
130129
isLoading: true,
131130
errorMessage: 'test message',
132131
};
@@ -178,7 +177,7 @@ describe('MatrixReducers', () => {
178177
emptyState = {
179178
topics: [],
180179
tasks: {},
181-
currentTask: { taskId: undefined, topicId: undefined },
180+
taskHistory: [],
182181
isLoading: true,
183182
errorMessage: undefined,
184183
};
@@ -204,7 +203,7 @@ describe('MatrixReducers', () => {
204203
...defaultState,
205204
tasks: { [1]: [new Task(1, 'test', 1, 1)] },
206205
};
207-
const updatedTask = new Task(1, 'test', 1, 1, true);
206+
const updatedTask = new Task(1, 'test', 7, 1);
208207
const action: MatrixAction = new UpdateTaskSuccess(updatedTask);
209208
const newState = matrixReducer(defaultState, action);
210209
expect(newState.tasks[1]).toContain(updatedTask);
@@ -235,7 +234,7 @@ describe('MatrixReducers', () => {
235234
new Topic(2, 'test', Color.orange, false, false),
236235
],
237236
tasks: {},
238-
currentTask: { taskId: undefined, topicId: undefined },
237+
taskHistory: [],
239238
isLoading: false,
240239
errorMessage: undefined,
241240
};

src/app/matrix/matrix.reducers.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ import { MatrixState, Task, TaskDictionary } from './matrix.interfaces';
2222
export const initialState: MatrixState = {
2323
topics: [],
2424
tasks: {},
25-
currentTask: { taskId: undefined, topicId: undefined },
25+
taskHistory: [],
2626
isLoading: false,
2727
errorMessage: undefined,
2828
};
2929

3030
export function matrixReducer(state = initialState, action: Action) {
31-
return produce((draft: MatrixState, matrixAction) => {
31+
return produce((draft: MatrixState, matrixAction: MatrixAction) => {
3232
switch (matrixAction.type) {
3333
// === loading cases ===
3434
case GET_MATRIX_DATA:
@@ -76,7 +76,17 @@ export function matrixReducer(state = initialState, action: Action) {
7676
return;
7777
// === frontend only actions ===
7878
case SELECT_TASK:
79-
draft.currentTask = matrixAction.currentTask;
79+
console.log(matrixAction.currentTask);
80+
const alreadyInHistory = draft.taskHistory.findIndex(
81+
(t) => t.taskId === matrixAction.currentTask.taskId,
82+
);
83+
if (alreadyInHistory > -1) {
84+
draft.taskHistory.splice(alreadyInHistory, 1);
85+
}
86+
draft.taskHistory.unshift(matrixAction.currentTask);
87+
if (draft.taskHistory.length > 6) {
88+
draft.taskHistory.pop();
89+
}
8090
return;
8191
case TOGGLE_TOPIC_VISIBLITY:
8292
const topicIdx = draft.topics.findIndex(

src/app/matrix/matrix.selectors.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createSelector } from '@ngrx/store';
22
import { AppState } from '../store/app.state';
3-
import { MatrixState, TaskDictionary, Topic } from './matrix.interfaces';
3+
import { MatrixState, Task, TaskDictionary, Topic } from './matrix.interfaces';
44

55
export const selectMatrix = (state: AppState) => state.matrix;
66

@@ -29,13 +29,24 @@ export const selectMatrixTopic = createSelector(
2929
(topics: Topic[], id: number) => topics.find((t) => t.id === id),
3030
);
3131

32-
export const selectCurrentTask = createSelector(
32+
export const selectTaskHistory = createSelector(
3333
selectMatrix,
3434
(state: MatrixState) => {
35-
if (state.currentTask.topicId && state.currentTask.taskId) {
36-
return state.tasks[state.currentTask.topicId].find(
37-
(t) => t.id === state.currentTask.taskId,
38-
);
35+
const result: Task[] = [];
36+
37+
if (state.taskHistory.length > 0) {
38+
state.taskHistory.forEach((c) => {
39+
if (c.topicId) {
40+
const foundTask = state.tasks[c.topicId].find(
41+
(t) => t.id === c.taskId,
42+
);
43+
if (foundTask) {
44+
result.push(foundTask);
45+
}
46+
}
47+
});
3948
}
49+
50+
return result;
4051
},
4152
);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Injectable } from '@angular/core';
22
import { Observable, of } from 'rxjs';
33
import { Color } from '../shared/color.interfaces';
44
import {
5-
CurrentTask,
65
MatrixService,
76
Task,
87
TaskDictionary,
@@ -35,6 +34,9 @@ export class MatrixMockService implements MatrixService {
3534
selectCurrentTask(): Observable<Task | undefined> {
3635
return of(this.testTask);
3736
}
37+
selectTaskHistory(): Observable<Task[]> {
38+
return of([this.testTask]);
39+
}
3840
selectTopicById(id: number): Observable<Topic | undefined> {
3941
return of({ ...this.testTopic } as Topic);
4042
}

src/app/matrix/matrix.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@ import {
1212
UpdateTopic,
1313
} from './matrix.actions';
1414
import {
15-
CurrentTask,
1615
MatrixService as IMatrixService,
1716
Task,
1817
TaskDictionary,
1918
Topic,
2019
} from './matrix.interfaces';
2120
import {
22-
selectCurrentTask,
2321
selectMatrixErrorMessage,
2422
selectMatrixIsLoading,
2523
selectMatrixTasks,
2624
selectMatrixTopic,
2725
selectMatrixTopics,
26+
selectTaskHistory,
2827
} from './matrix.selectors';
2928

3029
@Injectable({
@@ -53,6 +52,7 @@ export class MatrixService implements IMatrixService, OnDestroy {
5352
}
5453

5554
selectTask(task: Task): void {
55+
console.log(task);
5656
this.store.dispatch(
5757
new SelectTask({ taskId: task.id, topicId: task.topic }),
5858
);
@@ -93,9 +93,9 @@ export class MatrixService implements IMatrixService, OnDestroy {
9393
);
9494
}
9595

96-
selectCurrentTask(): Observable<Task | undefined> {
96+
selectTaskHistory(): Observable<Task[]> {
9797
return this.store.pipe(
98-
select(selectCurrentTask),
98+
select(selectTaskHistory),
9999
takeUntil(this.unsubscribe$),
100100
);
101101
}

0 commit comments

Comments
 (0)