-
Notifications
You must be signed in to change notification settings - Fork 383
/
styleeditor.js
132 lines (128 loc) · 3.64 KB
/
styleeditor.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
/*
* Copyright 2018, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import {
UPDATE_TEMPORARY_STYLE,
UPDATE_STATUS,
ERROR_STYLE,
ADD_STYLE,
RESET_STYLE_EDITOR,
LOADING_STYLE,
LOADED_STYLE,
INIT_STYLE_SERVICE,
SET_EDIT_PERMISSION,
UPDATE_EDITOR_METADATA
} from '../actions/styleeditor';
import isString from 'lodash/isString';
function styleeditor(state = {}, action) {
switch (action.type) {
case INIT_STYLE_SERVICE: {
return {
...state,
service: action.service,
canEdit: action.canEdit
};
}
case SET_EDIT_PERMISSION: {
return {
...state,
canEdit: action.canEdit
};
}
case UPDATE_TEMPORARY_STYLE: {
return {
...state,
temporaryId: action.temporaryId,
templateId: action.templateId,
code: action.code,
format: action.format,
error: null,
languageVersion: action.languageVersion,
initialCode: action.init ? action.code : state.initialCode
};
}
case UPDATE_STATUS: {
if (action.status === '') {
return {
...state,
status: action.status,
code: '',
templateId: '',
initialCode: '',
addStyle: false,
error: {}
};
}
return {
...state,
status: action.status
};
}
case RESET_STYLE_EDITOR: {
return {
service: state.service && {...state.service} || {},
canEdit: state.canEdit
};
}
case ADD_STYLE: {
return {...state, addStyle: action.add};
}
case LOADING_STYLE: {
return {
...state,
loading: action.status ? action.status : true,
error: {}
};
}
case LOADED_STYLE: {
return {
...state,
loading: false,
enabled: true
};
}
case ERROR_STYLE: {
const message = action?.error?.statusText || action?.error?.message || '';
const messageIdParam = isString(action?.error?.messageId)
&& { messageId: action.error.messageId };
const position = message.match(/line\s([\d]+)|column\s([\d]+)|lineNumber:\s([\d]+)|columnNumber:\s([\d]+)/g);
const errorInfo = position && position.length === 2 && position.reduce((info, pos) => {
const splittedValues = pos.split(' ');
const param = splittedValues[0].replace(/Number:/g, '');
const value = parseFloat(splittedValues[1]);
return param && !isNaN(value) && {
...info,
[param]: value
} || { ...info };
}, { message, ...messageIdParam }) || { message, ...messageIdParam };
return {
...state,
loading: false,
canEdit: !(action.error && (action.error.status === 401 || action.error.status === 403)),
error: {
...state.error,
[action.status || 'global']: {
status: action.error && action.error.status || 404,
...errorInfo
}
}
};
}
case UPDATE_EDITOR_METADATA: {
return {
...state,
metadata: {
...state.metadata,
...action.metadata
}
};
}
default:
return state;
}
}
export default styleeditor;