PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.10
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.10
1.4.20 1.4.19 1.4.18 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 All 183 releases
disco / backend / views / components / Main / components / SaveAndUpdateButton.jsx

SaveAndUpdateButton.jsx in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.10, at backend/views/components/Main/components/SaveAndUpdateButton.jsx

168 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { Switch } from '@headlessui/react';
2 import { CheckCircleIcon } from '@heroicons/react/24/outline';
3 import { __ } from '@wordpress/i18n';
4 import { useEffect } from 'react';
5 import { useDispatch, useSelector } from 'react-redux';
6 import { useNavigate } from 'react-router';
7 import { toast } from 'react-toastify';
8 import {
9 useAddCampaignMutation,
10 useUpdateCampaignMutation,
11 } from '../features/campaigns/campaignsApi';
12 import { updateOption } from '../features/discount/discountSlice';
13 import { cleanFilters, validateEmptyField } from '../utilities/utilities';
14 import Button from './Button';
15 import LoadingSpinner from './LoadingSpinner';
16
17 export const SaveButton = ({ disabled = false }) => {
18 const [addCampaign, { data, isLoading, isSuccess, isError }] =
19 useAddCampaignMutation();
20 const navigate = useNavigate();
21
22 const discount = useSelector((state) => state.discount);
23 const { discount_intent } = discount;
24 const handleSave = () => {
25 if (validateEmptyField(discount) === false) {
26 return;
27 }
28
29 const dataForRequest = {
30 ...discount,
31 conditions: cleanFilters(discount.conditions),
32 };
33 addCampaign(dataForRequest);
34 };
35
36 useEffect(() => {
37 if (isSuccess) {
38 toast.success(__('New Campaign Added.', 'disco'));
39 navigate(`?edit=${data.id}`, {
40 state: { ...data },
41 });
42 }
43 if (isError) {
44 toast.success(__('Something Went Wrong.', 'disco'));
45 }
46 }, [data]);
47
48 return (
49 <Button
50 className="!disco-py-2 !disco-px-4 !disco-font-normal disabled:disco-opacity-50 disabled:!disco-cursor-not-allowed"
51 disabled={isLoading || !discount_intent || disabled}
52 onClick={handleSave}
53 type="transparent"
54 iconPositionLeft={false}
55 icon={
56 <CheckCircleIcon className="disco-h-5 disco-w-5 !disco-text-primary" />
57 }
58 >
59 {isLoading ? (
60 <div className="disco-flex disco-gap-2 disco-items-center">
61 <span>{__('Saving', 'disco')}</span>
62 <LoadingSpinner size={4} />
63 </div>
64 ) : (
65 __('Save', 'disco')
66 )}
67 </Button>
68 );
69 };
70
71 export const UpdateButton = ({ disabled = false }) => {
72 const dispatch = useDispatch();
73 const [updateCampaign, { isLoading, isSuccess, isError }] =
74 useUpdateCampaignMutation();
75 const discount = useSelector((state) => state.discount);
76
77 const handleUpdate = () => {
78 if (validateEmptyField(discount) === false) {
79 return;
80 }
81
82 const dataForRequest = { ...discount };
83
84 delete dataForRequest.created_by;
85 delete dataForRequest.modified_by;
86
87 dataForRequest.conditions = cleanFilters(discount.conditions);
88
89 updateCampaign({
90 id: discount.id,
91 data: {
92 ...dataForRequest,
93 priority: String(dataForRequest.priority),
94 },
95 });
96 };
97
98 useEffect(() => {
99 if (isSuccess) {
100 toast.success(__('Campaign Updated.', 'disco'));
101 }
102 if (isError) {
103 toast.success(__('Something Went Wrong.', 'disco'));
104 }
105 }, [isSuccess]);
106
107 const handleToggleStatus = (status, id) => {
108 dispatch(
109 updateOption({
110 option: 'status',
111 value: status === '1' ? '0' : '1',
112 })
113 );
114 updateCampaign({ id, data: { status: status === '1' ? '0' : '1' } });
115 };
116
117 return (
118 <>
119 <div className="disco-flex disco-gap-2 disco-justify-center disco-items-center">
120 <h2 className="disco-text-base disco-font-semibold">
121 {discount.status === '1'
122 ? __('Enabled', 'disco')
123 : __('Disabled', 'disco')}
124 </h2>
125 <Switch
126 disabled={isLoading || disabled}
127 onChange={() =>
128 handleToggleStatus(discount.status, discount.id)
129 }
130 checked={discount.status === '1' ? true : false}
131 className={`${
132 discount.status === '1'
133 ? 'disco-bg-primary'
134 : 'disco-bg-gray-200'
135 }
136 disco-relative disco-inline-flex disco-h-5 disco-w-9 disco-flex-shrink-0 disco-cursor-pointer disco-rounded-full disco-border-2 disco-border-transparent disco-transition-colors disco-duration-200 disco-ease-in-out focus:disco-outline-none
137 `}
138 >
139 <span
140 aria-hidden="true"
141 className={` ${
142 discount.status === '1'
143 ? 'disco-translate-x-4'
144 : 'disco-translate-x-0'
145 } disco-pointer-events-none disco-inline-block disco-h-4 disco-w-4 disco-transform disco-rounded-full disco-bg-white disco-shadow disco-ring-0 disco-transition disco-duration-200 disco-ease-in-out
146 `}
147 />
148 </Switch>
149 </div>
150
151 <Button
152 className="!disco-py-1.5 !disco-px-4 !disco-font-normal"
153 disabled={isLoading || disabled}
154 onClick={handleUpdate}
155 >
156 {isLoading ? (
157 <div className="disco-flex disco-gap-2 disco-items-center">
158 <span> {__('Updating', 'disco')}</span>
159 <LoadingSpinner size={4} />
160 </div>
161 ) : (
162 __('Update', 'disco')
163 )}
164 </Button>
165 </>
166 );
167 };
168