UNPKG

7.15 kBJavaScriptView Raw
1import { flattenList, normalizeServerList } from './list-transforms';
2import uniqueId from '../../helpers/uniqueId';
3export function loginAction({ loginMutation }) {
4 return (dispatch, getState) => {
5 const state = getState();
6 loginMutation({
7 fetchPolicy: 'no-cache',
8 context: { useNetworkStatusNotifier: false },
9 variables: {
10 id: null,
11 items: flattenList(state.list).map(listItem => ({
12 articleNumber: listItem.articleNumber,
13 quantity: listItem.options.quantity,
14 description: listItem.options.description
15 }))
16 }
17 })
18 .then((response) => {
19 const list = normalizeServerList(response.data.addToCustomerProductList);
20 dispatch({ type: 'LOGIN', payload: list });
21 })
22 .catch((err) => {
23 console.error('Lists could not be merged', err);
24 });
25 };
26}
27export function refreshAction() {
28 return (dispatch) => {
29 dispatch({
30 type: 'REPLACE',
31 payload: JSON.parse(localStorage.getItem('productList'))
32 });
33 };
34}
35export function updateAction({ updateAPI = false, removeMutation, addMutation, options, articleNumber, variantToReplace, variantArticleNumber }) {
36 return (dispatch, getState) => {
37 const { description, quantity } = options;
38 dispatch({
39 type: 'UPDATE',
40 payload: {
41 articleNumber,
42 options,
43 variantArticleNumber,
44 variantToReplace
45 }
46 });
47 if (!updateAPI)
48 return;
49 const [remove] = removeMutation;
50 const [add] = addMutation;
51 const previousState = getState();
52 const requestId = scopedUniqueID();
53 previousState.requestIdRef.current = requestId;
54 remove({
55 // Don't write to the cache. This avoids re-rendering the list.
56 fetchPolicy: 'no-cache',
57 context: { useNetworkStatusNotifier: false },
58 variables: {
59 id: null,
60 articleNumbers: [variantToReplace || articleNumber]
61 }
62 })
63 .then(() => {
64 add({
65 fetchPolicy: 'no-cache',
66 context: { useNetworkStatusNotifier: false },
67 variables: {
68 id: null,
69 items: [
70 {
71 articleNumber: variantArticleNumber || articleNumber,
72 quantity,
73 description
74 }
75 ]
76 }
77 }).then(response => {
78 requestId === previousState.requestIdRef.current &&
79 syncListFromServer(dispatch, response.data.addToCustomerProductList);
80 });
81 })
82 .catch(err => {
83 rollbackList(dispatch, previousState.list);
84 console.error('List could not be updated', err);
85 });
86 };
87}
88export function removeAction({ updateAPI = false, articleNumber, variantArticleNumber, removeMutation }) {
89 return (dispatch, getState) => {
90 dispatch({
91 type: 'REMOVE',
92 payload: {
93 articleNumber,
94 variantArticleNumber
95 }
96 });
97 if (!updateAPI)
98 return;
99 const [remove] = removeMutation;
100 const previousState = getState();
101 const requestId = scopedUniqueID();
102 previousState.requestIdRef.current = requestId;
103 remove({
104 fetchPolicy: 'no-cache',
105 context: { useNetworkStatusNotifier: false },
106 variables: {
107 articleNumbers: [variantArticleNumber || articleNumber],
108 id: null
109 }
110 })
111 .then(response => {
112 requestId === previousState.requestIdRef.current &&
113 syncListFromServer(dispatch, response.data.removeFromCustomerProductList);
114 })
115 .catch(err => {
116 console.error('Product could not be removed from list', err);
117 rollbackList(dispatch, previousState.list);
118 });
119 };
120}
121export function clearAction({ updateAPI = false, removeMutation }) {
122 return (dispatch, getState) => {
123 dispatch({ type: 'REPLACE', payload: {} });
124 if (!updateAPI)
125 return;
126 const [remove] = removeMutation;
127 const state = getState();
128 const articleNumbers = flattenList(state.list).map(listItem => listItem.articleNumber);
129 const requestId = scopedUniqueID();
130 state.requestIdRef.current = requestId;
131 remove({
132 fetchPolicy: 'no-cache',
133 context: { useNetworkStatusNotifier: false },
134 variables: { articleNumbers, id: null }
135 })
136 .then(response => {
137 requestId === state.requestIdRef.current &&
138 syncListFromServer(dispatch, response.data.removeFromCustomerProductList);
139 })
140 .catch(err => {
141 console.error('List could not be cleared', err);
142 rollbackList(dispatch, state.list);
143 });
144 };
145}
146export function addAction({ articleNumber, variantArticleNumber, addMutation, options, updateAPI = false }) {
147 return (dispatch, getState) => {
148 const { quantity, description } = options;
149 dispatch({
150 type: 'ADD',
151 payload: {
152 articleNumber,
153 variantArticleNumber,
154 options: { quantity, description }
155 }
156 });
157 if (!updateAPI)
158 return;
159 const [add] = addMutation;
160 const previousState = getState();
161 const requestId = scopedUniqueID();
162 previousState.requestIdRef.current = requestId;
163 add({
164 fetchPolicy: 'no-cache',
165 context: { useNetworkStatusNotifier: false },
166 variables: {
167 id: null,
168 items: [
169 {
170 articleNumber: variantArticleNumber || articleNumber,
171 quantity,
172 description
173 }
174 ]
175 }
176 })
177 .then(({ data: { addToCustomerProductList } }) => {
178 requestId === previousState.requestIdRef.current &&
179 syncListFromServer(dispatch, addToCustomerProductList);
180 })
181 .catch(err => {
182 console.error('Product could not be added to list', err);
183 rollbackList(dispatch, previousState.list);
184 });
185 };
186}
187function scopedUniqueID() {
188 return uniqueId('_product_list_');
189}
190function syncListFromServer(dispatch, productList) {
191 const normalizedList = normalizeServerList(productList);
192 dispatch({
193 type: 'REPLACE',
194 payload: normalizedList
195 });
196}
197function rollbackList(dispatch, productList) {
198 dispatch({
199 type: 'REPLACE',
200 payload: productList
201 });
202}
203//# sourceMappingURL=action-creators.js.map
\No newline at end of file