PluginProbe
ElasticPress / 4.2.1
ElasticPress v4.2.1
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / assets / js / synonyms / reducers / editorReducer.js

editorReducer.js in ElasticPress 4.2.1, at assets/js/synonyms/reducers/editorReducer.js

146 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { reduceSolrToState, reduceStateToSolr, mapEntry } from '../utils';
2
3 /**
4 * The synonym editor reducer.
5 */
6
7 const { alternatives, sets, initialMode } = window.epSynonyms.data;
8 const mappedSets = sets ? sets.map(mapEntry) : [mapEntry()];
9 const mappedAlternatives = alternatives ? alternatives.map(mapEntry) : [mapEntry()];
10 const initialState = {
11 isSolrEditable: initialMode === 'advanced',
12 isSolrVisible: initialMode === 'advanced',
13 alternatives: mappedAlternatives,
14 sets: mappedSets,
15 solr: reduceStateToSolr({ sets: mappedSets, alternatives: mappedAlternatives }),
16 dirty: false,
17 submit: false,
18 };
19
20 /**
21 * editorReducer
22 *
23 * @param {object} state Current state.
24 * @param {object} action The action.
25 * @returns {object} New state.
26 */
27 const editorReducer = (state, action) => {
28 switch (action.type) {
29 case 'ADD_SET':
30 return {
31 ...state,
32 sets: [...state.sets, mapEntry()],
33 dirty: true,
34 };
35 case 'UPDATE_SET':
36 return {
37 ...state,
38 sets: state.sets.map((entry) => {
39 if (entry.id !== action.data.id) {
40 return entry;
41 }
42 return mapEntry(action.data.tokens, action.data.id);
43 }),
44 dirty: true,
45 };
46 case 'REMOVE_SET':
47 return {
48 ...state,
49 sets: state.sets.filter(({ id }) => id !== action.data),
50 dirty: true,
51 };
52 case 'ADD_ALTERNATIVE':
53 return {
54 ...state,
55 alternatives: [...state.alternatives, mapEntry()],
56 dirty: true,
57 };
58 case 'UPDATE_ALTERNATIVE':
59 return {
60 ...state,
61 alternatives: [
62 ...state.alternatives.map((entry) => {
63 if (entry.id !== action.data.id) {
64 return entry;
65 }
66 return mapEntry(
67 [...action.data.tokens, ...entry.synonyms.filter((t) => t.primary)],
68 action.data.id,
69 );
70 }),
71 ],
72 dirty: true,
73 };
74 case 'UPDATE_ALTERNATIVE_PRIMARY':
75 return {
76 ...state,
77 alternatives: [
78 ...state.alternatives.map((entry) => {
79 if (entry.id !== action.data.id) {
80 return entry;
81 }
82 return mapEntry(
83 [action.data.token, ...entry.synonyms.filter((t) => !t.primary)],
84 action.data.id,
85 );
86 }),
87 ],
88 dirty: true,
89 };
90 case 'REMOVE_ALTERNATIVE':
91 return {
92 ...state,
93 alternatives: state.alternatives.filter(({ id }) => id !== action.data),
94 dirty: true,
95 };
96 case 'SET_SOLR_EDITABLE':
97 return {
98 ...state,
99 isSolrEditable: !!action.data,
100 isSolrVisible: !!action.data,
101 };
102 case 'UPDATE_SOLR':
103 return {
104 ...state,
105 solr: action.data,
106 dirty: true,
107 };
108 case 'REDUCE_SOLR_TO_STATE':
109 return {
110 ...reduceSolrToState(state.solr, state),
111 dirty: true,
112 };
113 case 'REDUCE_STATE_TO_SOLR':
114 return {
115 ...state,
116 solr: reduceStateToSolr(state),
117 };
118 case 'VALIDATE_ALL':
119 return {
120 ...state,
121 sets: state.sets.map((set) => ({
122 ...set,
123 valid: set.synonyms.length > 1,
124 })),
125 alternatives: state.alternatives.map((alternative) => ({
126 ...alternative,
127 valid:
128 alternative.synonyms.length > 1 &&
129 !!alternative.synonyms.filter(
130 ({ primary, value }) => primary && value.length,
131 ).length,
132 })),
133 dirty: false,
134 };
135 case 'SUBMIT':
136 return {
137 ...state,
138 submit: true,
139 };
140 default:
141 return state;
142 }
143 };
144
145 export { editorReducer, initialState };
146