| 1 |
import { useTasksStore } from '@assist/state/tasks'; |
| 2 |
import apiFetch from '@wordpress/api-fetch'; |
| 3 |
import { useEffect, useRef, useState } from '@wordpress/element'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import classNames from 'classnames'; |
| 6 |
|
| 7 |
export const UpdateSiteDescription = ({ popModal, setModalTitle }) => { |
| 8 |
const [siteDescription, setSiteDescription] = useState(undefined); |
| 9 |
const [initialValue, setInitialValue] = useState(undefined); |
| 10 |
const inputRef = useRef(); |
| 11 |
const { completeTask } = useTasksStore(); |
| 12 |
|
| 13 |
const submitChange = async () => { |
| 14 |
await apiFetch({ |
| 15 |
path: '/wp/v2/settings', |
| 16 |
method: 'POST', |
| 17 |
data: { |
| 18 |
description: siteDescription, |
| 19 |
}, |
| 20 |
}); |
| 21 |
completeTask('site-description'); |
| 22 |
popModal(); |
| 23 |
}; |
| 24 |
|
| 25 |
useEffect(() => { |
| 26 |
setModalTitle(__('Add site description', 'extendify-local')); |
| 27 |
}, [setModalTitle]); |
| 28 |
|
| 29 |
useEffect(() => { |
| 30 |
const controller = new AbortController(); |
| 31 |
|
| 32 |
apiFetch({ |
| 33 |
path: '/wp/v2/settings', |
| 34 |
signal: controller.signal, |
| 35 |
}).then((settings) => { |
| 36 |
setSiteDescription(settings.description); |
| 37 |
setInitialValue(settings.description); |
| 38 |
}); |
| 39 |
|
| 40 |
return () => controller.abort(); |
| 41 |
}, [setSiteDescription]); |
| 42 |
|
| 43 |
useEffect(() => { |
| 44 |
inputRef?.current?.focus(); |
| 45 |
}, [initialValue]); |
| 46 |
|
| 47 |
if (typeof siteDescription === 'undefined') { |
| 48 |
return <div className="h-32">{__('Loading...', 'extendify-local')}</div>; |
| 49 |
} |
| 50 |
|
| 51 |
return ( |
| 52 |
<form className="flex flex-col gap-6" onSubmit={(e) => e.preventDefault()}> |
| 53 |
<div> |
| 54 |
<label |
| 55 |
className="mb-1 block text-sm text-gray-900" |
| 56 |
htmlFor="extendify-site-description-input" |
| 57 |
> |
| 58 |
{__('Site description', 'extendify-local')} |
| 59 |
</label> |
| 60 |
<input |
| 61 |
ref={inputRef} |
| 62 |
type="text" |
| 63 |
name="extendify-site-description-input" |
| 64 |
id="extendify-site-description-input" |
| 65 |
className="input-focus h-12 w-96 max-w-full border border-gray-900 px-2" |
| 66 |
autoComplete="off" |
| 67 |
data-1p-ignore |
| 68 |
onChange={(e) => { |
| 69 |
setSiteDescription(e.target.value); |
| 70 |
}} |
| 71 |
value={siteDescription} |
| 72 |
placeholder={__('Enter a site description...', 'extendify-local')} |
| 73 |
/> |
| 74 |
</div> |
| 75 |
<div> |
| 76 |
<button |
| 77 |
type="button" |
| 78 |
disabled={siteDescription === initialValue} |
| 79 |
className={classNames( |
| 80 |
'button-focus relative w-1/5 rounded-sm border-0 bg-design-main px-4 py-3 text-white', |
| 81 |
{ |
| 82 |
'cursor-default opacity-50': siteDescription === initialValue, |
| 83 |
}, |
| 84 |
)} |
| 85 |
onClick={submitChange} |
| 86 |
> |
| 87 |
{__('Save', 'extendify-local')} |
| 88 |
</button> |
| 89 |
</div> |
| 90 |
</form> |
| 91 |
); |
| 92 |
}; |
| 93 |
|