| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { useEffect, useState } from 'react'; |
| 3 |
import { Link } from 'react-router'; |
| 4 |
import { toast } from 'react-toastify'; |
| 5 |
import Button from '../../components/Button'; |
| 6 |
import CheckBox from '../../components/CheckBox'; |
| 7 |
import ComponentContainer from '../../components/ComponentContainer'; |
| 8 |
import LoadingSpinner from '../../components/LoadingSpinner'; |
| 9 |
import SingleSelect from '../../components/SingleSelect'; |
| 10 |
import { |
| 11 |
useGetSettingsQuery, |
| 12 |
useUpdateSettingsMutation, |
| 13 |
} from '../../features/settings/settingsApi'; |
| 14 |
import SettingsTitle from './components/SettingsTitle'; |
| 15 |
import SingleSelectRadio from './components/SingleSelectRadio'; |
| 16 |
const Settings = () => { |
| 17 |
const { data, isLoading } = useGetSettingsQuery(); |
| 18 |
const [ |
| 19 |
updateSettings, |
| 20 |
{ isLoading: updateLoading, isSuccess: updateSuccess }, |
| 21 |
] = useUpdateSettingsMutation(); |
| 22 |
const [settings, setSettings] = useState({}); |
| 23 |
|
| 24 |
useEffect(() => { |
| 25 |
if (data) { |
| 26 |
setSettings(data); |
| 27 |
} |
| 28 |
}, [data]); |
| 29 |
|
| 30 |
useEffect(() => { |
| 31 |
if (updateSuccess) { |
| 32 |
toast.success('Settings Updated'); |
| 33 |
} |
| 34 |
}, [updateSuccess]); |
| 35 |
|
| 36 |
const handleSettingChange = (key, value) => { |
| 37 |
setSettings((prevState) => ({ |
| 38 |
...prevState, |
| 39 |
[key]: value, |
| 40 |
})); |
| 41 |
}; |
| 42 |
|
| 43 |
const handleSettingSave = () => { |
| 44 |
updateSettings(settings); |
| 45 |
}; |
| 46 |
|
| 47 |
const items = { |
| 48 |
product_price_type: { |
| 49 |
regular_price: 'Regular Price', |
| 50 |
price: 'Sale Price', |
| 51 |
}, |
| 52 |
min_max_discount_amount: { |
| 53 |
min: 'Minimum', |
| 54 |
max: 'Maximum', |
| 55 |
}, |
| 56 |
discount_priority_type: { |
| 57 |
both: 'Both coupons and campaigns', |
| 58 |
disco_campaign: 'Only Disco campaigns', |
| 59 |
}, |
| 60 |
on_sale_badge: { |
| 61 |
show_all: 'Show on sale badge', |
| 62 |
do_not_show: 'Do not show on sale badge', |
| 63 |
// show_for_campaigns: 'Show on sale badge only for campaigns', |
| 64 |
// show_for_wc: 'Show on sale badge only for WooCommerce', |
| 65 |
}, |
| 66 |
show_strike_through: { |
| 67 |
shop_page: true, |
| 68 |
product_pages: true, |
| 69 |
category_page: true, |
| 70 |
cart_page: true, |
| 71 |
}, |
| 72 |
}; |
| 73 |
|
| 74 |
if (isLoading) { |
| 75 |
return ( |
| 76 |
<div className="disco-h-96 disco-w-full disco-flex disco-justify-center disco-items-center"> |
| 77 |
<LoadingSpinner /> |
| 78 |
</div> |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
return ( |
| 83 |
<div className="disco-mt-2.5 disco-mr-4 disco-ml-0.5 disco-flex disco-gap-4"> |
| 84 |
<div className="disco-flex-grow disco-border disco-bg-gray-50 disco-white disco-rounded-xl disco-p-5"> |
| 85 |
<div className="disco-border disco-border-t-0 disco-border-x-0 disco-border-b-white disco-mb-3"> |
| 86 |
<ComponentContainer |
| 87 |
heading={__('Settings', 'disco')} |
| 88 |
className="!disco-mt-0" |
| 89 |
/> |
| 90 |
</div> |
| 91 |
<div className="disco-bg-white disco-p-4 disco-border disco-border-white disco-rounded-xl"> |
| 92 |
<SettingsTitle |
| 93 |
title={__('Product Price Type', 'disco')} |
| 94 |
subtitle={__( |
| 95 |
'Choose product price type by which discount will be calculate.', |
| 96 |
'disco' |
| 97 |
)} |
| 98 |
className="disco-mb-2" |
| 99 |
url="https://discoplugin.com/docs/product-price-type/" |
| 100 |
> |
| 101 |
<SingleSelectRadio |
| 102 |
options={items.product_price_type} |
| 103 |
handleChange={(e) => |
| 104 |
handleSettingChange('product_price_type', e) |
| 105 |
} |
| 106 |
selected={settings.product_price_type} |
| 107 |
/> |
| 108 |
</SettingsTitle> |
| 109 |
|
| 110 |
<SettingsTitle |
| 111 |
title={__('Minimum Maximum Discount Amount', 'disco')} |
| 112 |
subtitle={__( |
| 113 |
"Choose discount's amount to define the maximum and minimum discounts.", |
| 114 |
'disco' |
| 115 |
)} |
| 116 |
className="disco-mb-2" |
| 117 |
url="https://discoplugin.com/docs/minimum-maximum-discount-amount/" |
| 118 |
> |
| 119 |
<SingleSelectRadio |
| 120 |
options={items.min_max_discount_amount} |
| 121 |
handleChange={(e) => |
| 122 |
handleSettingChange( |
| 123 |
'min_max_discount_amount', |
| 124 |
e |
| 125 |
) |
| 126 |
} |
| 127 |
selected={settings.min_max_discount_amount} |
| 128 |
/> |
| 129 |
</SettingsTitle> |
| 130 |
<SettingsTitle |
| 131 |
title={__('Coupons & Campaigns Behavior', 'disco')} |
| 132 |
subtitle={__( |
| 133 |
'Decide how WooCommerce coupons and Disco campaigns should work together.', |
| 134 |
'disco' |
| 135 |
)} |
| 136 |
className="disco-mb-2" |
| 137 |
url="https://discoplugin.com/docs/coupons-campaigns-behavior/" |
| 138 |
> |
| 139 |
<SingleSelect |
| 140 |
className="disco-bg-white disco-test-select-class" |
| 141 |
buttonClass="disco-min-w-[320px]" |
| 142 |
placeholder={__('Select Value', 'disco')} |
| 143 |
items={items?.discount_priority_type} |
| 144 |
selected={settings?.discount_priority_type ?? ''} |
| 145 |
onchange={(e) => { |
| 146 |
handleSettingChange( |
| 147 |
'discount_priority_type', |
| 148 |
e |
| 149 |
); |
| 150 |
}} |
| 151 |
/> |
| 152 |
</SettingsTitle> |
| 153 |
<SettingsTitle |
| 154 |
title={__('WooCommerce On-Sale Badge', 'disco')} |
| 155 |
subtitle={__( |
| 156 |
'Decide how WooCommerce on sale badge shows for any discount rules.', |
| 157 |
'disco' |
| 158 |
)} |
| 159 |
className="disco-mb-2" |
| 160 |
url="https://discoplugin.com/docs/woocommerce-on-sale-badge/" |
| 161 |
> |
| 162 |
<SingleSelect |
| 163 |
className="disco-bg-white disco-test-select-class" |
| 164 |
buttonClass="disco-min-w-[320px]" |
| 165 |
placeholder={__('Select Value', 'disco')} |
| 166 |
items={items?.on_sale_badge} |
| 167 |
selected={settings?.on_sale_badge ?? ''} |
| 168 |
onchange={(e) => { |
| 169 |
handleSettingChange('on_sale_badge', e); |
| 170 |
}} |
| 171 |
/> |
| 172 |
</SettingsTitle> |
| 173 |
<SettingsTitle |
| 174 |
title={__('Strikeout Price', 'disco')} |
| 175 |
subtitle={__( |
| 176 |
'Choose where the strike-through price appears across your store.', |
| 177 |
'disco' |
| 178 |
)} |
| 179 |
className="disco-mb-2 " |
| 180 |
url="https://discoplugin.com/docs/strikeout-price-settings/" |
| 181 |
> |
| 182 |
<div className="disco-flex disco-items-center disco-gap-8"> |
| 183 |
{Object.keys(items.show_strike_through).map( |
| 184 |
(key) => ( |
| 185 |
<CheckBox |
| 186 |
checked={ |
| 187 |
settings.show_strike_through?.[ |
| 188 |
key |
| 189 |
] ?? false |
| 190 |
} |
| 191 |
onChange={(checked) => { |
| 192 |
handleSettingChange( |
| 193 |
'show_strike_through', |
| 194 |
{ |
| 195 |
...settings.show_strike_through, |
| 196 |
[key]: checked, |
| 197 |
} |
| 198 |
); |
| 199 |
}} |
| 200 |
label={key |
| 201 |
?.replace(/_/g, ' ') |
| 202 |
?.replace(/\b\w/g, (char) => |
| 203 |
char.toUpperCase() |
| 204 |
)} |
| 205 |
key={key} |
| 206 |
/> |
| 207 |
) |
| 208 |
)} |
| 209 |
</div> |
| 210 |
</SettingsTitle> |
| 211 |
</div> |
| 212 |
<div className="disco-mt-8 disco-flex disco-justify-end disco-gap-4"> |
| 213 |
<Link to="/"> |
| 214 |
<Button type="transparent"> |
| 215 |
{__('Back', 'disco')} |
| 216 |
</Button> |
| 217 |
</Link> |
| 218 |
<Button |
| 219 |
disabled={updateLoading} |
| 220 |
onClick={handleSettingSave} |
| 221 |
> |
| 222 |
<span>{__('Save', 'disco')}</span> |
| 223 |
{updateLoading && <LoadingSpinner size={4} />} |
| 224 |
</Button> |
| 225 |
</div> |
| 226 |
</div> |
| 227 |
</div> |
| 228 |
); |
| 229 |
}; |
| 230 |
export default Settings; |
| 231 |
|