UNPKG

11.3 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = orderedReducer;
7
8var _merge2 = _interopRequireDefault(require("lodash/fp/merge"));
9
10var _isEqual2 = _interopRequireDefault(require("lodash/isEqual"));
11
12var _keyBy2 = _interopRequireDefault(require("lodash/keyBy"));
13
14var _map2 = _interopRequireDefault(require("lodash/map"));
15
16var _omit2 = _interopRequireDefault(require("lodash/omit"));
17
18var _reject2 = _interopRequireDefault(require("lodash/reject"));
19
20var _unionBy2 = _interopRequireDefault(require("lodash/unionBy"));
21
22var _get2 = _interopRequireDefault(require("lodash/get"));
23
24var _size2 = _interopRequireDefault(require("lodash/size"));
25
26var _constants = require("../constants");
27
28var _reducers = require("../utils/reducers");
29
30var _actionHandlers;
31
32function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
34function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
35
36function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
37
38function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
39
40function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
41
42function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
43
44function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
45
46function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
47
48function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
49
50function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
51
52function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
53
54function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
55
56function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
57
58function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
59
60var DOCUMENT_ADDED = _constants.actionTypes.DOCUMENT_ADDED,
61 GET_SUCCESS = _constants.actionTypes.GET_SUCCESS,
62 LISTENER_RESPONSE = _constants.actionTypes.LISTENER_RESPONSE,
63 CLEAR_DATA = _constants.actionTypes.CLEAR_DATA,
64 DELETE_SUCCESS = _constants.actionTypes.DELETE_SUCCESS,
65 DOCUMENT_REMOVED = _constants.actionTypes.DOCUMENT_REMOVED,
66 DOCUMENT_MODIFIED = _constants.actionTypes.DOCUMENT_MODIFIED;
67
68function newArrayWithItemMoved(collectionState, meta, ordered, newValue) {
69 var doc = meta.doc;
70
71 var _ref = ordered || {},
72 oldIndex = _ref.oldIndex,
73 newIndex = _ref.newIndex;
74
75 var arrayWithoutItem = [].concat(_toConsumableArray(collectionState.slice(0, oldIndex)), _toConsumableArray(collectionState.slice(oldIndex + 1)));
76 return [].concat(_toConsumableArray(arrayWithoutItem.slice(0, newIndex)), [_objectSpread({
77 id: doc
78 }, newValue) || _objectSpread({}, collectionState[oldIndex])], _toConsumableArray(arrayWithoutItem.slice(newIndex)));
79}
80
81function modifyDoc(collectionState, action) {
82 if (action.payload.ordered) {
83 var _action$payload$order = action.payload.ordered,
84 newIndex = _action$payload$order.newIndex,
85 oldIndex = _action$payload$order.oldIndex;
86
87 if (newIndex !== null && oldIndex !== null && newIndex > -1 && oldIndex > -1 && newIndex !== oldIndex) {
88 return newArrayWithItemMoved(collectionState, action.meta, action.payload.ordered, action.payload.data);
89 }
90 }
91
92 if (!action.meta.subcollections || action.meta.storeAs) {
93 return (0, _reducers.updateItemInArray)(collectionState, action.meta.doc, function (item) {
94 return _objectSpread({
95 id: action.meta.doc
96 }, action.payload.data);
97 });
98 }
99
100 var _pathToArr = (0, _reducers.pathToArr)(action.meta.path),
101 _pathToArr2 = _slicedToArray(_pathToArr, 4),
102 docId = _pathToArr2[1],
103 subcollectionName = _pathToArr2[2],
104 subDocId = _pathToArr2[3];
105
106 return (0, _reducers.updateItemInArray)(collectionState, docId, function (item) {
107 return _objectSpread(_objectSpread({}, item), {}, _defineProperty({}, subcollectionName, (0, _reducers.updateItemInArray)((0, _get2.default)(item, subcollectionName, []), subDocId, function (subitem) {
108 return (0, _merge2.default)(subitem, action.payload.data);
109 })));
110 });
111}
112
113function addDoc() {
114 var array = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
115 var action = arguments.length > 1 ? arguments[1] : undefined;
116 var meta = action.meta,
117 payload = action.payload;
118
119 if (!meta.subcollections || meta.storeAs) {
120 return [].concat(_toConsumableArray(array.slice(0, payload.ordered.newIndex)), [_objectSpread({
121 id: meta.doc
122 }, payload.data)], _toConsumableArray(array.slice(payload.ordered.newIndex)));
123 }
124
125 return modifyDoc(array, action);
126}
127
128function removeDoc(array, action) {
129 if (!action.meta.subcollections || action.meta.storeAs) {
130 return (0, _reject2.default)(array, {
131 id: action.meta.doc
132 });
133 }
134
135 var subcollectionSetting = action.meta.subcollections[0];
136
137 if (!subcollectionSetting.doc) {
138 return (0, _reducers.updateItemInArray)(array, action.meta.doc, function (item) {
139 return (0, _omit2.default)(item, [subcollectionSetting.collection]);
140 });
141 }
142
143 return (0, _reducers.updateItemInArray)(array, action.meta.doc, function (item) {
144 var subcollectionVal = (0, _get2.default)(item, subcollectionSetting.collection, []);
145
146 if (subcollectionVal.length) {
147 return _objectSpread(_objectSpread({}, item), {}, _defineProperty({}, subcollectionSetting.collection, (0, _reject2.default)(array, {
148 id: subcollectionSetting.doc
149 })));
150 }
151
152 return item;
153 });
154}
155
156function writeCollection(collectionState, action) {
157 var meta = action.meta,
158 _action$merge = action.merge,
159 merge = _action$merge === void 0 ? {
160 doc: true,
161 collections: true
162 } : _action$merge;
163
164 if (meta.storeAs) {
165 return action.payload.ordered;
166 }
167
168 var collectionStateSize = (0, _size2.default)(collectionState);
169 var payloadExists = !!(0, _size2.default)(action.payload.ordered);
170
171 if (meta.doc && merge.doc && collectionStateSize) {
172 return modifyDoc(collectionState, action);
173 }
174
175 if (collectionStateSize && merge.collections) {
176 if (!payloadExists) {
177 return [];
178 }
179
180 var existingKeys = collectionState && (0, _keyBy2.default)(collectionState, 'id');
181 return (0, _map2.default)(action.payload.ordered, function (newDocObj) {
182 var existingDoc = (0, _get2.default)(existingKeys, newDocObj.id);
183 return !!existingDoc && !(0, _isEqual2.default)(existingDoc, newDocObj) ? _objectSpread(_objectSpread({}, existingDoc), newDocObj) : newDocObj;
184 });
185 }
186
187 if (meta.doc && meta.subcollections) {
188 var subcollectionConfig = meta.subcollections[0];
189
190 if (!collectionStateSize) {
191 return [_defineProperty({
192 id: meta.doc
193 }, subcollectionConfig.collection, action.payload.ordered)];
194 }
195
196 return (0, _reducers.updateItemInArray)(collectionState, meta.doc, function (item) {
197 return payloadExists ? _objectSpread(_objectSpread({}, item), {}, _defineProperty({}, subcollectionConfig.collection, (0, _unionBy2.default)((0, _get2.default)(item, subcollectionConfig.collection, []), action.payload.ordered, 'id'))) : (0, _omit2.default)(item, [subcollectionConfig.collection]);
198 });
199 }
200
201 if (meta.doc && collectionStateSize) {
202 if (!(0, _size2.default)(action.payload.ordered)) {
203 return collectionState;
204 }
205
206 return (0, _reducers.updateItemInArray)(collectionState, meta.doc, function (item) {
207 return (0, _merge2.default)(item, action.payload.ordered[0]);
208 });
209 }
210
211 return action.payload.ordered;
212}
213
214var actionHandlers = (_actionHandlers = {}, _defineProperty(_actionHandlers, DOCUMENT_ADDED, addDoc), _defineProperty(_actionHandlers, DOCUMENT_MODIFIED, modifyDoc), _defineProperty(_actionHandlers, DOCUMENT_REMOVED, removeDoc), _defineProperty(_actionHandlers, DELETE_SUCCESS, removeDoc), _defineProperty(_actionHandlers, LISTENER_RESPONSE, writeCollection), _defineProperty(_actionHandlers, GET_SUCCESS, writeCollection), _actionHandlers);
215var orderedCollectionReducer = (0, _reducers.createReducer)(undefined, actionHandlers);
216
217function orderedReducer() {
218 var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
219 var action = arguments.length > 1 ? arguments[1] : undefined;
220
221 if (!action.type) {
222 return state;
223 }
224
225 if (action.type === CLEAR_DATA) {
226 if (action.preserve && action.preserve.ordered) {
227 return (0, _reducers.preserveValuesFromState)(state, action.preserve.ordered, {});
228 }
229
230 return {};
231 }
232
233 if (!Object.prototype.hasOwnProperty.call(actionHandlers, action.type)) {
234 return state;
235 }
236
237 if (!action.meta || !action.meta.storeAs && !action.meta.collection) {
238 return state;
239 }
240
241 var storeUnderKey = action.meta.storeAs || action.meta.collection;
242 var collectionStateSlice = (0, _get2.default)(state, storeUnderKey);
243 return _objectSpread(_objectSpread({}, state), {}, _defineProperty({}, storeUnderKey, orderedCollectionReducer(collectionStateSlice, action)));
244}
\No newline at end of file