PluginProbe
seQura / 3.2.2
seQura v3.2.2
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / assets / js / src / core / SettingsController.js

SettingsController.js in seQura 3.2.2, at assets/js/src/core/SettingsController.js

319 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 if (!window.SequraFE) {
2 window.SequraFE = {};
3 }
4
5 (function () {
6 /**
7 * Handles settings page logic.
8 *
9 * @param {{
10 * getConnectionDataUrl: string,
11 * getWidgetSettingsUrl: string,
12 * getGeneralSettingsUrl: string,
13 * getOrderStatusMappingSettingsUrl: string,
14 * getShopOrderStatusesUrl: string,
15 * getShopCategoriesUrl: string,
16 * getShopPaymentMethodsUrl: string,
17 * getSellingCountriesUrl: string,
18 * getCountrySettingsUrl: string,
19 * getPaymentMethodsUrl: string,
20 * getAllPaymentMethodsUrl: string,
21 * validateConnectionDataUrl: string,
22 * disconnectUrl: string,
23 * page: string
24 * }} configuration
25 * @constructor
26 */
27 function SettingsController(configuration) {
28 const {templateService, elementGenerator: generator, utilities, formFactory} = SequraFE;
29
30 /** @type AjaxServiceType */
31 const api = SequraFE.ajaxService;
32 let currentStoreId = '';
33 /** @type Version */
34 let version;
35 /** @type Store[] */
36 let stores;
37 /** @type CountrySettings[] **/
38 let countrySettings;
39 /** @type ConnectionSettings **/
40 let connectionSettings;
41 /** @type WidgetSettings **/
42 let widgetSettings;
43
44 /**
45 * Displays page content.
46 *
47 * @param {{ state?: string, storeId: string }} config
48 */
49 this.display = ({storeId}) => {
50 utilities.showLoader();
51 currentStoreId = storeId;
52 templateService.clearMainPage();
53 stores = SequraFE.state.getData('stores');
54 version = SequraFE.state.getData('version');
55 connectionSettings = SequraFE.state.getData('connectionSettings');
56 countrySettings = SequraFE.state.getData('countrySettings');
57 widgetSettings = SequraFE.state.getData('widgetSettings');
58
59 initializePage();
60 renderPage();
61 };
62
63 /**
64 * Handles rendering of a form based on state.
65 */
66 const renderPage = () => {
67 utilities.showLoader();
68 let page = SequraFE.state.getPage();
69 let renderer;
70 let promises;
71
72 if (!SequraFE.pages.settings.includes(page)) {
73 page = SequraFE.pages.settings[0];
74 }
75
76 switch (page) {
77 case SequraFE.appPages.SETTINGS.CONNECTION:
78 renderer = renderConnectionSettingsForm;
79 promises = Promise.all([])
80 break;
81 case SequraFE.appPages.SETTINGS.ORDER_STATUS:
82 renderer = renderOrderStatusMappingSettingsForm;
83 promises = Promise.all([
84 SequraFE.isPromotional ? [] :
85 SequraFE.state.getData('orderStatusSettings') ?? api.get(configuration.getOrderStatusMappingSettingsUrl, null, SequraFE.customHeader),
86 SequraFE.isPromotional ? [] :
87 SequraFE.state.getData('shopOrderStatuses') ?? api.get(configuration.getShopOrderStatusesUrl, null, SequraFE.customHeader)
88 ])
89 break;
90 case SequraFE.appPages.SETTINGS.WIDGET:
91 renderer = renderWidgetSettingsForm;
92 promises = Promise.all([
93 SequraFE.state.getData('paymentMethods') ?? api.get(configuration.getPaymentMethodsUrl.replace('{merchantId}', countrySettings[0].merchantId), null, SequraFE.customHeader),
94 SequraFE.state.getData('allPaymentMethods') ?? api.get(configuration.getAllPaymentMethodsUrl, null, SequraFE.customHeader)
95 ])
96 break;
97 default:
98 renderer = renderGeneralSettingsForm;
99 promises = Promise.all([
100 SequraFE.isPromotional ? [] :
101 SequraFE.state.getData('generalSettings') ?? api.get(configuration.getGeneralSettingsUrl, null, SequraFE.customHeader),
102 SequraFE.isPromotional ? [] :
103 SequraFE.state.getData('shopCategories') ?? api.get(configuration.getShopCategoriesUrl, null, SequraFE.customHeader),
104 SequraFE?.generalSettings?.useReplacementPaymentMethod && !SequraFE.isPromotional ?
105 SequraFE.state.getData('shopPaymentMethods') ?? api.get(configuration.getShopPaymentMethodsUrl, null, SequraFE.customHeader) : [],
106 SequraFE.state.getData('sellingCountries') ?? api.get(configuration.getSellingCountriesUrl, null, SequraFE.customHeader),
107 ])
108 }
109
110 promises
111 .then((array) => renderer(...array))
112 };
113
114 /**
115 * Renders the connection settings form.
116 */
117 const renderConnectionSettingsForm = () => {
118 const form = formFactory.getInstance(
119 'connectionSettings',
120 {connectionSettings, countrySettings},
121 {...configuration, appState: SequraFE.appStates.SETTINGS}
122 );
123
124 form?.render();
125 }
126
127 /**
128 * Renders the order status mappings settings form.
129 *
130 * @param orderStatusSettings
131 * @param shopOrderStatuses
132 */
133 const renderOrderStatusMappingSettingsForm = (orderStatusSettings, shopOrderStatuses) => {
134 if (!SequraFE.state.getData('orderStatusSettings')) {
135 SequraFE.state.setData('orderStatusSettings', orderStatusSettings)
136 }
137
138 if (!SequraFE.state.getData('shopOrderStatuses')) {
139 SequraFE.state.setData('shopOrderStatuses', shopOrderStatuses)
140 }
141
142 const form = formFactory.getInstance(
143 'orderStatusMappingSettings',
144 {orderStatusSettings, shopOrderStatuses},
145 {...configuration}
146 );
147
148 form?.render();
149 }
150
151 /**
152 * Renders the widget settings form.
153 *
154 * @param paymentMethods
155 * @param allPaymentMethods
156 */
157 const renderWidgetSettingsForm = (paymentMethods, allPaymentMethods) => {
158 if (!SequraFE.state.getData('paymentMethods')) {
159 SequraFE.state.setData('paymentMethods', paymentMethods)
160 }
161
162 if (!SequraFE.state.getData('allPaymentMethods')) {
163 SequraFE.state.setData('allPaymentMethods', allPaymentMethods)
164 }
165
166 const form = formFactory.getInstance(
167 'widgetSettings',
168 {widgetSettings, connectionSettings, countrySettings, paymentMethods, allPaymentMethods},
169 {...configuration, appState: SequraFE.appStates.SETTINGS}
170 );
171
172 form?.render();
173 }
174
175 /**
176 * Renders the general settings form.
177 *
178 * @param generalSettings
179 * @param shopCategories
180 * @param shopPaymentMethods
181 * @param sellingCountries
182 */
183 const renderGeneralSettingsForm = (
184 generalSettings,
185 shopCategories,
186 shopPaymentMethods,
187 sellingCountries,
188 ) => {
189 saveFetchedDataToDataStore(generalSettings, shopCategories, shopPaymentMethods, sellingCountries);
190
191 const form = formFactory.getInstance(
192 'generalSettings',
193 {
194 generalSettings,
195 shopCategories,
196 shopPaymentMethods,
197 sellingCountries,
198 connectionSettings,
199 countrySettings
200 },
201 {...configuration, appState: SequraFE.appStates.SETTINGS}
202 );
203
204 form?.render();
205 }
206
207 /**
208 * Saves data to data store if fetched on render.
209 *
210 * @param generalSettings
211 * @param shopCategories
212 * @param shopPaymentMethods
213 * @param sellingCountries
214 */
215 const saveFetchedDataToDataStore = (generalSettings, shopCategories, shopPaymentMethods, sellingCountries) => {
216 if (!SequraFE.state.getData('generalSettings')) {
217 SequraFE.state.setData('generalSettings', generalSettings)
218 }
219
220 if (!SequraFE.state.getData('shopCategories')) {
221 SequraFE.state.setData('shopCategories', shopCategories)
222 }
223
224 if (!SequraFE.state.getData('shopPaymentMethods')) {
225 SequraFE.state.setData('shopPaymentMethods', shopPaymentMethods)
226 }
227
228 if (!SequraFE.state.getData('sellingCountries')) {
229 SequraFE.state.setData('sellingCountries', sellingCountries)
230 }
231 }
232
233 /**
234 * Get sidebar link options.
235 *
236 * @returns {unknown[]}
237 */
238 const getLinkConfiguration = () => {
239 return SequraFE.pages.settings.map((link) => {
240 const activePage = SequraFE.state.getPage() ?? SequraFE.pages.settings[0]
241 switch (link) {
242 case SequraFE.appPages.SETTINGS.GENERAL:
243 return {
244 label: 'sidebar.generalSettings',
245 href: '#settings-general',
246 icon: 'general',
247 isActive: activePage === SequraFE.appPages.SETTINGS.GENERAL
248 }
249 case SequraFE.appPages.SETTINGS.CONNECTION:
250 return {
251 label: 'sidebar.connectionSettings',
252 href: '#settings-connection',
253 icon: 'connection',
254 isActive: activePage === SequraFE.appPages.SETTINGS.CONNECTION
255 }
256 // case SequraFE.appPages.SETTINGS.ORDER_STATUS:
257 // return {
258 // label: 'sidebar.orderStatusSettings',
259 // href: '#settings-order_status',
260 // icon: 'order',
261 // isActive: activePage === SequraFE.appPages.SETTINGS.ORDER_STATUS,
262 // }
263 case SequraFE.appPages.SETTINGS.WIDGET:
264 return {
265 label: 'sidebar.widgetSettings',
266 href: '#settings-widget',
267 icon: 'widget',
268 isActive: activePage === SequraFE.appPages.SETTINGS.WIDGET
269 }
270 default:
271 return null
272 }
273 });
274 }
275
276 /**
277 * Initializes general settings state content.
278 */
279 const initializePage = () => {
280 const pageWrapper = document.getElementById('sq-page-wrapper')
281
282 pageWrapper.append(
283 generator.createElement('div', 'sq-page-content-wrapper sqv--settings', '', null, [
284 SequraFE.components.PageHeader.create(
285 {
286 currentVersion: version?.current,
287 newVersion: {
288 versionLabel: version?.new,
289 versionUrl: version?.downloadNewVersionUrl
290 },
291 mode: connectionSettings.environment === 'live' ? connectionSettings.environment : 'test',
292 activeStore: currentStoreId,
293 stores: stores.map((store) => ({label: store.storeName, value: store.storeId})),
294 onChange: (storeId) => {
295 if (storeId !== SequraFE.state.getStoreId()) {
296 SequraFE.state.setStoreId(storeId);
297 window.location.hash = '';
298 SequraFE.state.display();
299 }
300 },
301 menuItems: SequraFE.isPromotional ? [] : SequraFE.utilities.getMenuItems(SequraFE.appStates.SETTINGS)
302 }
303 ),
304 generator.createElement('div', 'sq-page-content', '', null, [getSidebarRow()]),
305 generator.createSupportLink()
306 ]))
307 }
308
309 const getSidebarRow = () => {
310 return generator.createElement('div', 'sq-content-row', '', null, [
311 generator.createSettingsSidebar({links: getLinkConfiguration()}),
312 generator.createElement('main', 'sq-content')
313 ]);
314 }
315 }
316
317 SequraFE.SettingsController = SettingsController;
318 })();
319