-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
181 lines (158 loc) · 6.29 KB
/
index.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { hex2bin } from '@shopgate/pwa-common/helpers/data';
import showModal from '@shopgate/pwa-common/actions/modal/showModal';
import {
getThemeSettings, historyPop, historyPush, routeWillEnter$, routeWillLeave$,
} from '@shopgate/engage/core';
import ToastProvider from '@shopgate/pwa-common/providers/toast';
import { getSearchRoute } from '@shopgate/pwa-common-commerce/search';
import fetchProduct from '../actions/fetchProduct';
import fetchProductDescription from '../actions/fetchProductDescription';
import fetchProductProperties from '../actions/fetchProductProperties';
import fetchProductImages from '../actions/fetchProductImages';
import fetchProductShipping from '../actions/fetchProductShipping';
import fetchProductVariants from '../actions/fetchProductVariants';
import fetchProductOptions from '../actions/fetchProductOptions';
import fetchProductMedia from '../actions/fetchProductMedia';
import { productImageFormats } from '../collections';
import {
productWillEnter$,
galleryWillEnter$,
productReceived$,
cachedProductReceived$,
productRelationsReceived$,
receivedVisibleProduct$,
errorProductResourcesNotFound$,
visibleProductNotFound$,
productNotAvailable$,
} from '../streams';
import fetchProductsById from '../actions/fetchProductsById';
import { getProductRelationsByHash } from '../selectors/relations';
import { checkoutSucceeded$ } from '../../checkout/streams';
import expireProductById from '../action-creators/expireProductById';
import { ITEM_PATTERN, NOT_AVAILABLE_EFFECTIVITY_DATES } from '../constants';
import { getProductName } from '../selectors/product';
/**
* Product subscriptions.
* @param {Function} subscribe The subscribe function.
*/
function product(subscribe) {
const processProduct$ = productReceived$.merge(cachedProductReceived$);
subscribe(productWillEnter$, ({ action, dispatch }) => {
const { productId } = action.route.params;
const { productId: variantId } = action.route.state;
const id = variantId || hex2bin(productId);
dispatch(fetchProduct(id));
dispatch(fetchProductDescription(id));
dispatch(fetchProductProperties(id));
dispatch(fetchProductImages(id, productImageFormats.getAllUniqueFormats()));
dispatch(fetchProductShipping(id));
/**
* This feature is currently in BETA testing.
* It should only be used for approved BETA Client Projects
*/
dispatch(fetchProductMedia(id));
});
subscribe(galleryWillEnter$, ({ action, dispatch }) => {
const { productId } = action.route.params;
dispatch(fetchProductImages(hex2bin(productId), productImageFormats.getAllUniqueFormats()));
});
subscribe(processProduct$, ({ action, dispatch }) => {
const {
id,
flags = {
hasVariants: false,
hasOptions: false,
},
baseProductId,
} = action.productData;
if (baseProductId) {
dispatch(fetchProduct(baseProductId));
dispatch(fetchProductImages(baseProductId, productImageFormats.getAllUniqueFormats()));
}
if (flags.hasVariants) {
dispatch(fetchProductVariants(id));
}
if (flags.hasOptions) {
dispatch(fetchProductOptions(id));
}
});
const errorProductResourcesNotFoundFirst$ = receivedVisibleProduct$
.switchMap(() => errorProductResourcesNotFound$.first());
/** Refresh product data after some of resources has ENOTFOUND code */
subscribe(errorProductResourcesNotFoundFirst$, ({ action, dispatch }) => {
const { productId } = action;
dispatch(fetchProduct(productId, true));
});
const productNotFound$ = visibleProductNotFound$.withLatestFrom((
receivedVisibleProduct$.startWith({ action: { productData: {} } })
));
/** Visible product is no more available */
subscribe(productNotFound$, ([{ action, dispatch }, { action: { productData } }]) => {
const { productId } = action;
const name = productData.id === productId ? productData.name : productId;
dispatch(showModal({
confirm: null,
dismiss: 'modal.ok',
title: 'modal.title_error',
message: 'product.no_more_found',
params: {
name,
},
}));
dispatch(historyPop());
dispatch(expireProductById(productId));
});
subscribe(productRelationsReceived$, ({ dispatch, getState, action }) => {
const { hash } = action;
const productIds = getProductRelationsByHash(hash)(getState());
dispatch(fetchProductsById(productIds));
});
/**
* Expire products after checkout, fetch updated data
*/
subscribe(checkoutSucceeded$, ({ dispatch, action }) => {
const { products } = action;
const productIds = products.map(p => p.product.id);
productIds.forEach(id => dispatch(expireProductById(id)));
dispatch(fetchProductsById(productIds));
});
const productNotAvailableEffDates$ = productNotAvailable$
.filter(({ action }) => action.reason === NOT_AVAILABLE_EFFECTIVITY_DATES);
/** PDP expired effectivity dates */
const productNotAvailableEffDatesPDP$ = productNotAvailableEffDates$
.withLatestFrom(routeWillEnter$)
.filter(([notAvailable, willEnter]) => (
willEnter.action.route.pattern === ITEM_PATTERN
&& notAvailable.action.productId === willEnter.action.route.state.productId
)).map(([notAvailable]) => notAvailable);
subscribe(productNotAvailableEffDatesPDP$, ({
action, getState, dispatch, events,
}) => {
const { effectivityDates: { accessExpired } = {} } = getThemeSettings('product') || {};
if (accessExpired === false) {
const { productId } = action;
dispatch(historyPop());
const name = getProductName(getState(), { productId });
events.emit(ToastProvider.ADD, {
id: 'product.available.not_search_similar',
message: 'product.available.not_search_similar',
messageParams: {
name,
},
action: () => dispatch(historyPush({
pathname: getSearchRoute(name),
})),
});
}
});
/** Expired effectivity dates products after PLP leave */
const productNotAvailableEffDatesPLP$ = productNotAvailableEffDates$
.buffer(routeWillLeave$)
.filter(buffered => buffered.length);
subscribe(productNotAvailableEffDatesPLP$, (buffered) => {
const [{ dispatch }] = buffered;
const productIds = buffered.map(params => params.action.productId);
dispatch(expireProductById(productIds, true));
});
}
export default product;