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 / OrderStatusMappingSettingsForm.js

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

172 lines 6.1 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 * @typedef ShopOrderStatus
8 * @property {string} id
9 * @property {string} name
10 */
11
12 /**
13 * @typedef OrderStatusMapping
14 * @property {string} sequraStatus
15 * @property {string} shopStatus
16 */
17
18 /**
19 * Handles order status mapping settings form logic.
20 *
21 * @param {{
22 * orderStatusSettings: OrderStatusMapping[],
23 * shopOrderStatuses: ShopOrderStatus[],
24 * }} data
25 * @param {{
26 * getShopOrderStatusesUrl: string,
27 * getOrderStatusMappingSettingsUrl: string,
28 * saveOrderStatusMappingSettingsUrl: string,
29 * page: string,
30 * }} configuration
31 * @constructor
32 */
33 function OrderStatusMappingSettingsForm(data, configuration) {
34 const SEQURA_STATUSES = {
35 PAID: 'approved',
36 IN_REVIEW: 'needs_review',
37 CANCELLED: 'cancelled'
38 }
39
40 const {elementGenerator: generator, utilities} = SequraFE;
41 /** @type AjaxServiceType */
42 const api = SequraFE.ajaxService;
43 /** @type OrderStatusMapping[] */
44 let activeSettings;
45 /** @type OrderStatusMapping[] */
46 let changedSettings;
47
48 /**
49 * Handles form rendering.
50 */
51 this.render = () => {
52 utilities.showLoader();
53
54 if (!activeSettings) {
55 activeSettings = data?.orderStatusSettings?.map((utilities.cloneObject))
56 }
57
58 changedSettings = utilities.cloneObject(activeSettings);
59
60 initForm();
61 utilities.disableFooter(true);
62 utilities.hideLoader();
63 }
64
65 /**
66 * Initializes the form structure.
67 */
68 const initForm = () => {
69 const pageContent = document.querySelector('.sq-content');
70 pageContent?.append(
71 generator.createElement('div', 'sq-content-inner', '', null, [
72 generator.createElement('div', 'sqp-flash-message-wrapper'),
73 generator.createPageHeading({
74 title: 'orderStatusSettings.title',
75 text: 'orderStatusSettings.description'
76 }),
77 generator.createDropdownField({
78 label: 'orderStatusSettings.paid.label',
79 description: 'orderStatusSettings.paid.description',
80 value: changedSettings?.find(
81 (mapping) => mapping.sequraStatus === SEQURA_STATUSES.PAID
82 )?.shopStatus || '',
83 options: getStatusOptions(),
84 variation: 'label-left',
85 onChange: (value) => handleChange(SEQURA_STATUSES.PAID, value)
86 }),
87 generator.createDropdownField({
88 label: 'orderStatusSettings.inReview.label',
89 description: 'orderStatusSettings.inReview.description',
90 value: changedSettings?.find(
91 (mapping) => mapping.sequraStatus === SEQURA_STATUSES.IN_REVIEW
92 )?.shopStatus,
93 options: getStatusOptions(),
94 variation: "label-left",
95 onChange: (value) => handleChange(SEQURA_STATUSES.IN_REVIEW, value)
96 }),
97 generator.createDropdownField({
98 label: 'orderStatusSettings.cancelled.label',
99 description: 'orderStatusSettings.cancelled.description',
100 value: changedSettings.find(
101 (mapping) => mapping.sequraStatus === SEQURA_STATUSES.CANCELLED
102 )?.shopStatus,
103 options: getStatusOptions(),
104 variation: 'label-left',
105 onChange: (value) => handleChange(SEQURA_STATUSES.CANCELLED, value)
106 })
107 ]),
108 generator.createPageFooter({
109 onCancel: () => {
110 const pageContent = document.querySelector('.sq-content');
111 while (pageContent?.firstChild) {
112 pageContent?.removeChild(pageContent.firstChild);
113 }
114
115 this.render();
116 },
117 onSave: handleSave
118 })
119 );
120 }
121
122 /**
123 * Returns shop status options.
124 *
125 * @returns {[{label: string, value: string}]}
126 */
127 const getStatusOptions = () => {
128 const options = [{label: "None", value: ""}];
129 data.shopOrderStatuses.map((shopOrderStatus) => {
130 options.push({
131 label: shopOrderStatus.name.charAt(0).toUpperCase() + shopOrderStatus.name.slice(1),
132 value: shopOrderStatus.id
133 })
134 })
135
136 return options;
137 }
138
139 /**
140 * Handles form input changes.
141 *
142 * @param {string} name
143 * @param {string} value
144 */
145 const handleChange = (name, value) => {
146 const mapping = changedSettings.find((mapping) => mapping.sequraStatus === name)
147 mapping ?
148 mapping.shopStatus = value :
149 changedSettings.push({shopStatus: value, sequraStatus: name});
150
151
152 utilities.disableFooter(false);
153 }
154
155 /**
156 * Handles saving of the form.
157 */
158 const handleSave = () => {
159 utilities.showLoader();
160 api.post(configuration.saveOrderStatusMappingSettingsUrl, changedSettings, SequraFE.customHeader)
161 .then(() => {
162 activeSettings = utilities.cloneObject(changedSettings);
163 SequraFE.state.setData('orderStatusSettings', activeSettings);
164 utilities.disableFooter(true);
165 })
166 .finally(utilities.hideLoader);
167 }
168 }
169
170 SequraFE.OrderStatusMappingSettingsForm = OrderStatusMappingSettingsForm;
171 })();
172