PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.2
GenerateBlocks v2.0.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / pattern-library / components / manage-libraries.js
generateblocks / src / pattern-library / components Last commit date
add-library.js 2 years ago category-list.js 1 year ago insert-pattern.js 2 years ago library-cache.js 2 years ago library-layout.js 1 year ago library-provider.js 2 years ago library-selector.js 1 year ago manage-libraries.js 2 years ago pattern-details-header.js 1 year ago pattern-details.js 1 year ago pattern-list.js 1 year ago pattern-search.js 2 years ago pattern.js 1 year ago selected-patterns.js 1 year ago
manage-libraries.js
170 lines
1 import { Button, Modal, ToggleControl, Tooltip } from '@wordpress/components';
2 import { useState, useEffect } from '@wordpress/element';
3 import { __ } from '@wordpress/i18n';
4 import { settings, trash } from '@wordpress/icons';
5 import { fetchLibraries, useLibrary } from './library-provider';
6 import apiFetch from '@wordpress/api-fetch';
7
8 async function saveLibraries( newData ) {
9 const response = await apiFetch( {
10 path: 'generateblocks/v1/pattern-library/libraries/save',
11 method: 'POST',
12 data: {
13 data: newData,
14 },
15 } );
16
17 return response;
18 }
19
20 function ManageRow( { library, librariesToManage, setLibrariesToManage, isRemote } ) {
21 const { setLibraries } = useLibrary();
22
23 return (
24 <div key={ library.id } className="gblocks-manage-libraries__library">
25 <div className="gblocks-manage-libraries__library-name">
26 { !! isRemote
27 ? <Tooltip text={ library.domain }><span>{ library.name }</span></Tooltip>
28 : library.name
29 }
30
31 { !! isRemote &&
32 <Button
33 size="small"
34 isDestructive
35 variant="secondary"
36 onClick={ async() => {
37 const newData = [ ...librariesToManage ];
38 const index = newData.findIndex( ( obj ) => obj.id === library.id );
39
40 newData.splice( index, 1 );
41 setLibrariesToManage( newData );
42 const response = await saveLibraries( newData );
43
44 if ( response.success && response?.response?.data.length ) {
45 setLibraries( response?.response?.data.length );
46 }
47 } }
48 icon={ trash }
49 label={ __( 'Delete library', 'generateblocks' ) }
50 showTooltip
51 />
52 }
53 </div>
54 <div className="gblocks-manage-libraries__library-actions">
55 <ToggleControl
56 checked={ !! library.isEnabled }
57 label={ __( 'Enabled', 'generateblocks' ) }
58 onChange={ async() => {
59 const newData = [ ...librariesToManage ];
60 const index = newData.findIndex( ( obj ) => obj.id === library.id );
61
62 newData[ index ] = {
63 ...newData[ index ],
64 isEnabled: librariesToManage[ index ].isEnabled ? false : true,
65 };
66
67 setLibrariesToManage( newData );
68 const response = await saveLibraries( newData );
69
70 if ( response.success && response?.response?.data.length ) {
71 setLibraries();
72 }
73 } }
74 />
75 </div>
76 </div>
77 );
78 }
79
80 export default function ManageLibraries() {
81 const [ showManageLibraries, setShowManageLibraries ] = useState( false );
82 const [ librariesToManage, setLibrariesToManage ] = useState( [] );
83
84 useEffect( () => {
85 ( async function() {
86 if ( ! showManageLibraries ) {
87 return;
88 }
89
90 const { data } = await fetchLibraries( false );
91 setLibrariesToManage( data );
92 }() );
93 }, [ showManageLibraries ] );
94
95 const defaultLibraries = librariesToManage.filter( ( library ) => library.isDefault );
96 const localCollections = librariesToManage.filter( ( library ) => library.isLocal );
97 const remoteLibraries = librariesToManage.filter( ( library ) => ! library.isLocal && ! library.isDefault );
98
99 return (
100 <>
101 <Button
102 variant="tertiary"
103 size="compact"
104 icon={ settings }
105 label={ __( 'Manage Libraries', 'generateblocks' ) }
106 showTooltip
107 onClick={ setShowManageLibraries }
108 />
109
110 { !! showManageLibraries &&
111 <Modal
112 title={ __( 'Manage Libraries', 'generateblocks' ) }
113 onRequestClose={ () => setShowManageLibraries( false ) }
114 >
115 <div className="gblocks-manage-libraries">
116 { !! defaultLibraries.length &&
117 <>
118 <h4>{ __( 'Default', 'generateblocks' ) }</h4>
119 <div className="gblocks-manage-libraries__table">
120 { defaultLibraries.map( ( library ) =>
121 <ManageRow
122 key={ library.id }
123 library={ library }
124 librariesToManage={ librariesToManage }
125 setLibrariesToManage={ setLibrariesToManage }
126 />
127 ) }
128 </div>
129 </>
130 }
131
132 { !! localCollections.length &&
133 <>
134 <h4>{ __( 'Local', 'generateblocks' ) }</h4>
135 <div className="gblocks-manage-libraries__table">
136 { localCollections.map( ( library ) =>
137 <ManageRow
138 key={ library.id }
139 library={ library }
140 librariesToManage={ librariesToManage }
141 setLibrariesToManage={ setLibrariesToManage }
142 />
143 ) }
144 </div>
145 </>
146 }
147
148 { !! remoteLibraries.length &&
149 <>
150 <h4>{ __( 'Remote', 'generateblocks' ) }</h4>
151 <div className="gblocks-manage-libraries__table">
152 { remoteLibraries.map( ( library ) =>
153 <ManageRow
154 key={ library.id }
155 library={ library }
156 librariesToManage={ librariesToManage }
157 setLibrariesToManage={ setLibrariesToManage }
158 isRemote={ true }
159 />
160 ) }
161 </div>
162 </>
163 }
164 </div>
165 </Modal>
166 }
167 </>
168 );
169 }
170