PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
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 1.3.47 1.3.46 1.3.45 All 177 releases
disco / backend / views / components / Main / hooks / useCampaignEdit.js

useCampaignEdit.js in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at backend/views/components/Main/hooks/useCampaignEdit.js

75 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useEffect, useRef, useState } from 'react';
2 import { useDispatch } from 'react-redux';
3 import { useLocation, useSearchParams } from 'react-router';
4 import { useGetCampaignQuery } from '../features/campaigns/campaignsApi';
5 import {
6 useGetBOGOTypesQuery,
7 useGetDiscountBasedOnQuery,
8 useGetDiscountIntentsQuery,
9 useGetDiscountMethodsQuery,
10 useGetDiscountTypesQuery,
11 useGetFiltersQuery,
12 useGetWhatGetsDiscountQuery,
13 } from '../features/discount/discountApi';
14 import { editCampaign } from '../features/discount/discountSlice';
15
16 const useEditCampaign = () => {
17 const [searchParams] = useSearchParams();
18 const [id, setId] = useState('');
19 const [skip, setSkip] = useState(true);
20 const { data: campaign, isLoading } = useGetCampaignQuery(id, { skip });
21 const [uiLoading, setUiLoading] = useState(true);
22 const dispatch = useDispatch();
23 const { isError, error } = useGetDiscountIntentsQuery();
24 const hasCampaignLoaded = useRef(false);
25
26 // Trigger these queries to ensure data is loaded, if necessary
27 useGetFiltersQuery();
28 useGetWhatGetsDiscountQuery();
29 useGetDiscountMethodsQuery();
30 useGetDiscountTypesQuery();
31 useGetDiscountBasedOnQuery();
32 useGetBOGOTypesQuery();
33
34 const { state } = useLocation();
35
36 useEffect(() => {
37 if (searchParams.get('edit') && skip) {
38 if (state) {
39 dispatch(editCampaign(state));
40 hasCampaignLoaded.current = true;
41 } else {
42 setUiLoading(true);
43 const id = searchParams.get('edit');
44 setId(id);
45 setSkip(false);
46 }
47 }
48 }, [searchParams, skip, state, dispatch]);
49
50 useEffect(() => {
51 if (
52 isError &&
53 error.status === 403 &&
54 error.data.code === 'rest_cookie_invalid_nonce'
55 ) {
56 window.location.replace(DISCO.site_url + '/wp-login.php');
57 }
58 }, [isError, error]);
59
60 // Only dispatch editCampaign on initial load, not on refetches
61 useEffect(() => {
62 if (campaign && !hasCampaignLoaded.current) {
63 dispatch(editCampaign(campaign));
64 hasCampaignLoaded.current = true;
65 }
66 setUiLoading(false);
67 }, [campaign, dispatch]);
68
69 return {
70 isLoading: isLoading || uiLoading,
71 };
72 };
73
74 export default useEditCampaign;
75