PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.3.10
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.3.10
1.4.17 1.4.16 1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 All 180 releases
disco / backend / views / components / Main / features / discount / designAutoSaveMiddleware.js

designAutoSaveMiddleware.js in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.3.10, at backend/views/components/Main/features/discount/designAutoSaveMiddleware.js

68 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { campaignsApi } from '../campaigns/campaignsApi';
2 import {
3 updateBadge,
4 updateCartPage,
5 updateCountdown,
6 updateTable,
7 updateTextHighlight,
8 } from '../discount/discountSlice';
9
10 import { setSaveStatus } from '../interaction/interactionSlice';
11
12 let abortController = null;
13 let debounceTimer = null;
14
15 const designAutoSaveMiddleware = (store) => (next) => async (action) => {
16 const designActions = [
17 updateBadge.type,
18 updateTextHighlight.type,
19 updateCountdown.type,
20 updateTable.type,
21 updateCartPage.type,
22 ];
23
24 const result = next(action);
25 const state = store.getState().discount;
26
27 // 🔹 WHEN SAVE REQUEST SUCCESSFULLY COMPLETED
28 if (campaignsApi.endpoints.patchCampaign.matchFulfilled(action)) {
29 store.dispatch(setSaveStatus('success'));
30 }
31
32 // Auto-save only on design tab
33 if (!designActions.includes(action.type) || state.ui[0] !== 1) {
34 return result;
35 }
36
37 // Debounce
38 if (debounceTimer) clearTimeout(debounceTimer);
39
40 debounceTimer = setTimeout(() => {
41 // Abort previous pending request
42 if (abortController) {
43 abortController.abort();
44 }
45
46 abortController = new AbortController();
47 const signal = abortController.signal;
48
49 //Before saving, set status = "saving"
50 store.dispatch(setSaveStatus('saving'));
51
52 // Send mutation request
53 store.dispatch(
54 campaignsApi.endpoints.patchCampaign.initiate(
55 {
56 id: state.id,
57 data: { design_blocks: state.design_blocks },
58 },
59 { signal }
60 )
61 );
62 }, 1000);
63
64 return result;
65 };
66
67 export default designAutoSaveMiddleware;
68