UNPKG

3.84 kBJavaScriptView Raw
1"use strict";
2var actions_1 = require('../actions');
3var storeUtils_1 = require('../data/storeUtils');
4var assign = require('lodash.assign');
5var isEqual = require('lodash.isequal');
6function queries(previousState, action) {
7 if (previousState === void 0) { previousState = {}; }
8 if (actions_1.isQueryInitAction(action)) {
9 var newState = assign({}, previousState);
10 var previousQuery = previousState[action.queryId];
11 var previousVariables = void 0;
12 if (action.storePreviousVariables && previousQuery) {
13 if (!isEqual(previousQuery.variables, action.variables)) {
14 previousVariables = previousQuery.variables;
15 }
16 }
17 newState[action.queryId] = {
18 queryString: action.queryString,
19 query: action.query,
20 minimizedQueryString: action.minimizedQueryString,
21 minimizedQuery: action.minimizedQuery,
22 variables: action.variables,
23 previousVariables: previousVariables,
24 loading: true,
25 stopped: false,
26 networkError: null,
27 graphQLErrors: null,
28 forceFetch: action.forceFetch,
29 returnPartialData: action.returnPartialData,
30 lastRequestId: action.requestId,
31 fragmentMap: action.fragmentMap,
32 };
33 return newState;
34 }
35 else if (actions_1.isQueryResultAction(action)) {
36 if (!previousState[action.queryId]) {
37 return previousState;
38 }
39 if (action.requestId < previousState[action.queryId].lastRequestId) {
40 return previousState;
41 }
42 var newState = assign({}, previousState);
43 var resultHasGraphQLErrors = storeUtils_1.graphQLResultHasError(action.result);
44 newState[action.queryId] = assign({}, previousState[action.queryId], {
45 loading: false,
46 networkError: null,
47 graphQLErrors: resultHasGraphQLErrors ? action.result.errors : null,
48 previousVariables: null,
49 });
50 return newState;
51 }
52 else if (actions_1.isQueryErrorAction(action)) {
53 if (!previousState[action.queryId]) {
54 return previousState;
55 }
56 if (action.requestId < previousState[action.queryId].lastRequestId) {
57 return previousState;
58 }
59 var newState = assign({}, previousState);
60 newState[action.queryId] = assign({}, previousState[action.queryId], {
61 loading: false,
62 networkError: action.error,
63 });
64 return newState;
65 }
66 else if (actions_1.isQueryResultClientAction(action)) {
67 if (!previousState[action.queryId]) {
68 return previousState;
69 }
70 var newState = assign({}, previousState);
71 newState[action.queryId] = assign({}, previousState[action.queryId], {
72 loading: action.complete,
73 networkError: null,
74 previousVariables: null,
75 });
76 return newState;
77 }
78 else if (actions_1.isQueryStopAction(action)) {
79 var newState = assign({}, previousState);
80 newState[action.queryId] = assign({}, previousState[action.queryId], {
81 loading: false,
82 stopped: true,
83 });
84 return newState;
85 }
86 else if (actions_1.isStoreResetAction(action)) {
87 return resetQueryState(previousState, action);
88 }
89 return previousState;
90}
91exports.queries = queries;
92function resetQueryState(state, action) {
93 var observableQueryIds = action.observableQueryIds;
94 var newQueries = Object.keys(state).filter(function (queryId) {
95 return (observableQueryIds.indexOf(queryId) > -1);
96 }).reduce(function (res, key) {
97 res[key] = state[key];
98 return res;
99 }, {});
100 return newQueries;
101}
102//# sourceMappingURL=store.js.map
\No newline at end of file