| 1 |
import { Modal } from '@wordpress/components' |
| 2 |
import { ToggleControl } from '@wordpress/components' |
| 3 |
import { useSelect } from '@wordpress/data' |
| 4 |
import { unmountComponentAtNode, useState, useEffect } from '@wordpress/element' |
| 5 |
import { __, sprintf } from '@wordpress/i18n' |
| 6 |
import { useSiteSettingsStore } from '@library/state/SiteSettings' |
| 7 |
import { useUserStore } from '@library/state/User' |
| 8 |
|
| 9 |
const LibraryAccessModal = () => { |
| 10 |
const isAdmin = useSelect((select) => |
| 11 |
select('core').canUser('create', 'users'), |
| 12 |
) |
| 13 |
|
| 14 |
const [libraryforMyself, setLibraryforMyself] = useState( |
| 15 |
useUserStore.getState().enabled, |
| 16 |
) |
| 17 |
const [libraryforEveryone, setLibraryforEveryone] = useState( |
| 18 |
useSiteSettingsStore.getState().enabled, |
| 19 |
) |
| 20 |
|
| 21 |
const closeModal = () => { |
| 22 |
const util = document.getElementById('extendify-util') |
| 23 |
unmountComponentAtNode(util) |
| 24 |
} |
| 25 |
|
| 26 |
useEffect(() => { |
| 27 |
hideButton(!libraryforMyself) |
| 28 |
}, [libraryforMyself]) |
| 29 |
|
| 30 |
function hideButton(state = true) { |
| 31 |
const button = document.getElementById( |
| 32 |
'extendify-templates-inserter-btn', |
| 33 |
) |
| 34 |
if (!button) return |
| 35 |
if (state) { |
| 36 |
button.classList.add('hidden') |
| 37 |
} else { |
| 38 |
button.classList.remove('hidden') |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
async function saveUser(value) { |
| 43 |
await useUserStore.setState({ enabled: value }) |
| 44 |
} |
| 45 |
|
| 46 |
async function saveSetting(value) { |
| 47 |
await useSiteSettingsStore.setState({ enabled: value }) |
| 48 |
} |
| 49 |
|
| 50 |
async function saveToggle(state, type) { |
| 51 |
if (type === 'global') { |
| 52 |
await saveSetting(state) |
| 53 |
} else { |
| 54 |
await saveUser(state) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
function handleToggle(type) { |
| 59 |
if (type === 'global') { |
| 60 |
setLibraryforEveryone((state) => { |
| 61 |
saveToggle(!state, type) |
| 62 |
return !state |
| 63 |
}) |
| 64 |
} else { |
| 65 |
setLibraryforMyself((state) => { |
| 66 |
hideButton(!state) |
| 67 |
saveToggle(!state, type) |
| 68 |
return !state |
| 69 |
}) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return ( |
| 74 |
<Modal |
| 75 |
title={sprintf( |
| 76 |
// translators: %s: The name of the plugin, Extendify. |
| 77 |
__('%s Settings', 'extendify'), |
| 78 |
'Extendify', |
| 79 |
)} |
| 80 |
onRequestClose={closeModal}> |
| 81 |
<ToggleControl |
| 82 |
label={ |
| 83 |
isAdmin |
| 84 |
? __('Enable the library for myself', 'extendify') |
| 85 |
: __('Enable the library', 'extendify') |
| 86 |
} |
| 87 |
help={__( |
| 88 |
'Publish with hundreds of patterns & page layouts', |
| 89 |
'extendify', |
| 90 |
)} |
| 91 |
checked={libraryforMyself} |
| 92 |
onChange={() => handleToggle('user')} |
| 93 |
/> |
| 94 |
|
| 95 |
{isAdmin && ( |
| 96 |
<> |
| 97 |
<br /> |
| 98 |
<ToggleControl |
| 99 |
label={__( |
| 100 |
'Allow all users to publish with the library', |
| 101 |
'extendify', |
| 102 |
)} |
| 103 |
help={__( |
| 104 |
'Everyone publishes with patterns & page layouts', |
| 105 |
'extendify', |
| 106 |
)} |
| 107 |
checked={libraryforEveryone} |
| 108 |
onChange={() => handleToggle('global')} |
| 109 |
/> |
| 110 |
</> |
| 111 |
)} |
| 112 |
</Modal> |
| 113 |
) |
| 114 |
} |
| 115 |
|
| 116 |
export default LibraryAccessModal |
| 117 |
|