forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-image-templates.ts
104 lines (92 loc) · 3.68 KB
/
map-image-templates.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Type } from 'class-transformer';
import { IsString, IsUUID, ValidateNested } from 'class-validator';
import { MapImageTemplate } from '../../models';
import { ImageProperties } from '../../models/utils';
import type { ExerciseState } from '../../state';
import type { Mutable } from '../../utils';
import { cloneDeepMutable, UUID, uuidValidationOptions } from '../../utils';
import { IsValue } from '../../utils/validators';
import type { Action, ActionReducer } from '../action-reducer';
import { ReducerError } from '../reducer-error';
export class AddMapImageTemplateAction implements Action {
@IsValue('[MapImageTemplate] Add mapImageTemplate' as const)
public readonly type = '[MapImageTemplate] Add mapImageTemplate';
@ValidateNested()
@Type(() => MapImageTemplate)
public readonly mapImageTemplate!: MapImageTemplate;
}
export class EditMapImageTemplateAction implements Action {
@IsValue('[MapImageTemplate] Edit mapImageTemplate' as const)
public readonly type = '[MapImageTemplate] Edit mapImageTemplate';
@IsUUID(4, uuidValidationOptions)
public readonly id!: UUID;
@IsString()
public readonly name!: string;
@ValidateNested()
@Type(() => ImageProperties)
public readonly image!: ImageProperties;
}
export class DeleteMapImageTemplateAction implements Action {
@IsValue('[MapImageTemplate] Delete mapImageTemplate' as const)
public readonly type = '[MapImageTemplate] Delete mapImageTemplate';
@IsUUID(4, uuidValidationOptions)
public readonly id!: UUID;
}
export namespace MapImageTemplatesActionReducers {
export const addMapImageTemplate: ActionReducer<AddMapImageTemplateAction> =
{
action: AddMapImageTemplateAction,
reducer: (draftState, { mapImageTemplate }) => {
if (
draftState.mapImageTemplates.some(
(template) => template.id === mapImageTemplate.id
)
) {
throw new ReducerError(
`MapImageTemplate with id ${mapImageTemplate.id} already exists`
);
}
draftState.mapImageTemplates.push(
cloneDeepMutable(mapImageTemplate)
);
return draftState;
},
rights: 'trainer',
};
export const editMapImageTemplate: ActionReducer<EditMapImageTemplateAction> =
{
action: EditMapImageTemplateAction,
reducer: (draftState, { id, name, image }) => {
const mapImageTemplate = getMapImageTemplate(draftState, id);
mapImageTemplate.name = name;
mapImageTemplate.image = cloneDeepMutable(image);
return draftState;
},
rights: 'trainer',
};
export const deleteMapImageTemplate: ActionReducer<DeleteMapImageTemplateAction> =
{
action: DeleteMapImageTemplateAction,
reducer: (draftState, { id }) => {
getMapImageTemplate(draftState, id);
draftState.mapImageTemplates =
draftState.mapImageTemplates.filter(
(template) => template.id !== id
);
return draftState;
},
rights: 'trainer',
};
}
function getMapImageTemplate(
state: Mutable<ExerciseState>,
id: UUID
): Mutable<MapImageTemplate> {
const mapImageTemplate = state.mapImageTemplates.find(
(template) => template.id === id
);
if (!mapImageTemplate) {
throw new ReducerError(`MapImageTemplate with id ${id} does not exist`);
}
return mapImageTemplate;
}