/
create-issue-reducers.js
149 lines (144 loc) · 4.37 KB
/
create-issue-reducers.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* @flow */
import {createReducer} from 'redux-create-reducer';
import * as types from './create-issue-action-types';
import {LOG_OUT} from '../../actions/action-types';
import type {CustomField, FieldValue, Attachment} from '../../flow/CustomFields';
import type {IssueFull} from '../../flow/Issue';
const notSelectedProject = {
id: null,
name: 'Not selected'
};
export type CreateIssueState = {
processing: boolean,
attachingImage: ?Object,
predefinedDraftId: ?string,
issue: {
summary: string,
description: string,
attachments: Array<Object>,
fields: Array<CustomField>,
project: Object
}
};
const initialState: CreateIssueState = {
processing: false,
attachingImage: null,
predefinedDraftId: null,
issue: {
summary: '',
description: '',
attachments: [],
fields: [],
project: notSelectedProject
}
};
export default createReducer(initialState, {
[LOG_OUT](state: CreateIssueState, action: {draftId: string}): CreateIssueState {
return initialState;
},
[types.SET_ISSUE_PREDEFINED_DRAFT_ID](state: CreateIssueState, action: {draftId: string}): CreateIssueState {
return {...state, predefinedDraftId: action.draftId};
},
[types.SET_ISSUE_DRAFT](state: CreateIssueState, action: {issue: IssueFull}): CreateIssueState {
return {...state, issue: {...state.issue, ...action.issue}};
},
[types.RESET_ISSUE_DRAFT](state: CreateIssueState): CreateIssueState {
return {
...state,
issue: {
...state.issue,
id: null
}
};
},
[types.SET_ISSUE_PROJECT](state: CreateIssueState, action: {project: Object}): CreateIssueState {
const {project} = action;
return {...state, issue: {...state.issue, project}};
},
[types.SET_DRAFT_PROJECT_ID](state: CreateIssueState, action: {projectId: string}): CreateIssueState {
return {
...state,
issue: {
...state.issue,
project: {
...state.issue.project,
id: action.projectId
}
}
};
},
[types.CLEAR_DRAFT_PROJECT](state: CreateIssueState): CreateIssueState {
return {
...state,
issue: {
...state.issue,
project: notSelectedProject
}
};
},
[types.SET_ISSUE_SUMMARY](state: CreateIssueState, action: {summary: string}): CreateIssueState {
const {summary} = action;
return {...state, issue: {...state.issue, summary}};
},
[types.SET_ISSUE_DESCRIPTION](state: CreateIssueState, action: {description: string}): CreateIssueState {
const {description} = action;
return {...state, issue: {...state.issue, description}};
},
[types.START_ISSUE_CREATION](state: CreateIssueState): CreateIssueState {
return {...state, processing: true};
},
[types.STOP_ISSUE_CREATION](state: CreateIssueState): CreateIssueState {
return {...state, processing: false};
},
[types.ISSUE_CREATED](state: CreateIssueState): CreateIssueState {
return initialState;
},
[types.RESET_CREATION](state: CreateIssueState): CreateIssueState {
return initialState;
},
[types.START_IMAGE_ATTACHING](state: CreateIssueState, action: {attachingImage: Object}): CreateIssueState {
const {attachingImage} = action;
return {
...state,
issue: {
...state.issue,
attachments: [...state.issue.attachments, attachingImage],
},
attachingImage
};
},
[types.REMOVE_ATTACHING_IMAGE](state: CreateIssueState): CreateIssueState {
return {
...state,
issue: {
...state.issue,
attachments: state.issue.attachments.filter(attach => attach !== state.attachingImage),
},
attachingImage: null
};
},
[types.STOP_IMAGE_ATTACHING](state: CreateIssueState): CreateIssueState {
return {...state, attachingImage: null};
},
[types.REMOVE_ATTACHMENT](state: CreateIssueState, action: {attachment: Attachment}): CreateIssueState {
return {
...state,
issue: {
...state.issue,
attachments: state.issue.attachments.filter(attach => attach !== action.attachment)
}
};
},
[types.SET_ISSUE_FIELD_VALUE](state: CreateIssueState, action: {field: CustomField, value: FieldValue}): CreateIssueState {
const {field, value} = action;
return {
...state,
issue: {
...state.issue,
fields: [...state.issue.fields].map(it => {
return it === field ? {...it, value} : it;
})
}
};
}
});