| 1 |
import React, { useState, useEffect } from 'react' |
| 2 |
import { BlockPicker } from 'react-color'; |
| 3 |
const { apiFetch } = wp; |
| 4 |
const Colors = () => { |
| 5 |
const [success, setSuccess] = useState(false); |
| 6 |
const [primary, setPrimary] = useState('#afafaf'); |
| 7 |
const [secondary, setSecondary] = useState('#dedede'); |
| 8 |
|
| 9 |
useEffect(() => { |
| 10 |
getColors() |
| 11 |
|
| 12 |
}, []) |
| 13 |
|
| 14 |
const getColors = async (value) => { |
| 15 |
let { primary, secondary } = await apiFetch({ |
| 16 |
path: 'blockspare-dashboard/v1/getcolorsettings', |
| 17 |
method: "GET" |
| 18 |
}) |
| 19 |
setPrimary(primary); |
| 20 |
setSecondary(secondary) |
| 21 |
} |
| 22 |
const handlePrimaryChangeComplete = async (color) => { |
| 23 |
setPrimary(color.hex); |
| 24 |
|
| 25 |
const success = await apiFetch({ |
| 26 |
path: `blockspare-dashboard/v1/updatecolorsettings`, |
| 27 |
method: 'POST', |
| 28 |
data: { |
| 29 |
name: 'primary', |
| 30 |
primary: color.hex, |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
}); |
| 35 |
setSuccess(true) |
| 36 |
successMsg() |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
const handleSecondaryChangeComplete = async (color) => { |
| 43 |
setSecondary(color.hex) |
| 44 |
|
| 45 |
const success = await apiFetch({ |
| 46 |
path: `blockspare-dashboard/v1/updatecolorsettings`, |
| 47 |
method: 'POST', |
| 48 |
data: { |
| 49 |
name: 'secondary', |
| 50 |
secondary: color.hex, |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
}); |
| 55 |
setSuccess(true) |
| 56 |
successMsg() |
| 57 |
|
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
function successMsg() { |
| 62 |
setTimeout(function () { |
| 63 |
setSuccess(false) |
| 64 |
}, 3000) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
return ( |
| 69 |
<div className="content-wrapper settings"> |
| 70 |
<div className="settings-tab-container"> |
| 71 |
<div className="setting-header"> |
| 72 |
<h1>Color Settings</h1> |
| 73 |
{success && |
| 74 |
<span>Color Updated</span> |
| 75 |
} |
| 76 |
</div> |
| 77 |
<div className="setting-body"> |
| 78 |
<div className="blockspare__settings"> |
| 79 |
<div className="blockspare__settings-info"> |
| 80 |
<h3 className="blockspare__settings-title">Primary Color</h3> |
| 81 |
<div className="blockspare__input-wrap"> |
| 82 |
|
| 83 |
<BlockPicker |
| 84 |
color={primary} |
| 85 |
colors={['#afafaf', '#F47373', '#697689', '#37D67A', '#2CCCE4', '#555555', '#dce775', '#ff8a65', '#ba68c8']} |
| 86 |
onChangeComplete={handlePrimaryChangeComplete} |
| 87 |
triangle="hide" |
| 88 |
/> |
| 89 |
|
| 90 |
</div> |
| 91 |
|
| 92 |
</div> |
| 93 |
<div className="blockspare__settings-info"> |
| 94 |
<h3 className="blockspare__settings-title">Secondary Color</h3> |
| 95 |
<div className="blockspare__input-wrap"> |
| 96 |
<BlockPicker |
| 97 |
color={secondary} |
| 98 |
colors={['#afafaf', '#F47373', '#697689', '#37D67A', '#2CCCE4', '#555555', '#dce775', '#ff8a65', '#ba68c8']} |
| 99 |
onChangeComplete={handleSecondaryChangeComplete} |
| 100 |
triangle="hide" |
| 101 |
/> |
| 102 |
|
| 103 |
</div> |
| 104 |
|
| 105 |
</div> |
| 106 |
</div> |
| 107 |
</div> |
| 108 |
</div> |
| 109 |
</div> |
| 110 |
|
| 111 |
) |
| 112 |
|
| 113 |
} |
| 114 |
export default Colors |