PluginProbe ʕ •ᴥ•ʔ
Starter Sites & Templates by Neve / trunk
Starter Sites & Templates by Neve vtrunk
1.4.1 1.4.0 1.3.0 1.2.29 1.2.28 1.2.6 1.2.7 1.2.8 1.2.9 trunk 1.0.10 1.0.11 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.27 1.1.28 1.1.29 1.1.3 1.1.30 1.1.31 1.1.32 1.1.33 1.1.34 1.1.35 1.1.36 1.1.37 1.1.38 1.1.39 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.3 1.2.4 1.2.5
templates-patterns-collection / beaver / src / data / templates-cloud / index.js
templates-patterns-collection / beaver / src / data / templates-cloud Last commit date
index.js 2 years ago
index.js
223 lines
1 /* global localStorage, lodash, FLBuilder */
2 import { stringifyUrl } from 'query-string';
3 import { v4 as uuidv4 } from 'uuid';
4
5 import apiFetch from '@wordpress/api-fetch';
6 import { dispatch, select } from '@wordpress/data';
7
8 const dispatchNotification = ( message ) => FLBuilder.alert( message );
9
10 const { setFetching } = dispatch( 'tpc/beaver' );
11
12 export const fetchTemplates = async ( additionalParams = {} ) => {
13 const { isScroll, ...filteredAdditionalParams } = additionalParams;
14 const params = {
15 cache: localStorage.getItem( 'tpcCacheBuster' ),
16 ...window.tiTpc.params,
17 per_page: 20,
18 page: 0,
19 premade: true,
20 template_site_slug: 'general',
21 ...filteredAdditionalParams,
22 };
23
24 const url = stringifyUrl( {
25 url: window.tiTpc.endpoint + 'page-templates',
26 query: params,
27 } );
28
29 try {
30 setFetching( true );
31 const response = await apiFetch( {
32 url,
33 method: 'GET',
34 parse: false,
35 } );
36 setFetching( false );
37
38 if ( response.ok ) {
39 const templates = await response.json();
40
41 if ( templates.message ) {
42 return dispatchNotification( templates.message );
43 }
44
45 let items = templates;
46
47 if ( additionalParams.isScroll ) {
48 const library = select( 'tpc/beaver' ).getTemplates();
49 items = [ ...library.items, ...templates ];
50 }
51
52 const totalPages = response.headers.get( 'x-wp-totalpages' );
53 const currentPage = params.page;
54
55 dispatch( 'tpc/beaver' ).updateTemplates(
56 items,
57 currentPage,
58 totalPages
59 );
60 }
61 } catch ( error ) {
62 if ( error.message ) {
63 dispatchNotification( error.message );
64 }
65 }
66 };
67
68 export const fetchLibrary = async ( additionalParams = {} ) => {
69 const { isScroll, ...filteredAdditionalParams } = additionalParams;
70 const params = {
71 per_page: 20,
72 page: 0,
73 ...filteredAdditionalParams,
74 };
75
76 const url = stringifyUrl( {
77 url: window.tiTpc.endpoint + 'templates',
78 query: {
79 cache: localStorage.getItem( 'tpcCacheBuster' ),
80 ...window.tiTpc.params,
81 ...params,
82 },
83 } );
84
85 try {
86 setFetching( true );
87 const response = await apiFetch( {
88 url,
89 method: 'GET',
90 parse: false,
91 } );
92 setFetching( false );
93
94 if ( response.ok ) {
95 const templates = await response.json();
96
97 if ( templates.message ) {
98 return dispatchNotification( templates.message );
99 }
100
101 let items = templates;
102
103 if ( additionalParams.isScroll ) {
104 const library = select( 'tpc/beaver' ).getLibrary();
105 items = [ ...library.items, ...templates ];
106 }
107
108 const totalPages = response.headers.get( 'x-wp-totalpages' );
109 const currentPage = params.page;
110
111 dispatch( 'tpc/beaver' ).updateLibrary(
112 items,
113 currentPage,
114 totalPages
115 );
116 }
117 } catch ( error ) {
118 if ( error.message ) {
119 dispatchNotification( error.message );
120 }
121 }
122 };
123
124 export const importTemplate = async ( template ) => {
125 const url = stringifyUrl( {
126 url: `${ window.tiTpc.endpoint }templates/${ template }/import`,
127 query: {
128 cache: localStorage.getItem( 'tpcCacheBuster' ),
129 ...window.tiTpc.params,
130 },
131 } );
132
133 let content = {};
134
135 try {
136 const response = await apiFetch( {
137 url,
138 method: 'GET',
139 parse: false,
140 } );
141
142 if ( response.ok ) {
143 content = await response.json();
144
145 if ( content.message ) {
146 return dispatchNotification( content.message );
147 }
148 }
149 } catch ( error ) {
150 if ( error.message ) {
151 dispatchNotification( error.message );
152 }
153 }
154
155 return content;
156 };
157
158 export const updateTemplate = async ( params ) => {
159 const url = stringifyUrl( {
160 url: `${ window.tiTpc.endpoint }templates/${ params.template_id }`,
161 query: {
162 cache: localStorage.getItem( 'tpcCacheBuster' ),
163 ...window.tiTpc.params,
164 ...params,
165 },
166 } );
167
168 try {
169 const response = await apiFetch( {
170 url,
171 method: 'POST',
172 parse: false,
173 } );
174
175 if ( response.ok ) {
176 const content = await response.json();
177
178 if ( content.message ) {
179 return dispatchNotification( content.message );
180 }
181 }
182
183 localStorage.setItem( 'tpcCacheBuster', uuidv4() );
184
185 await fetchLibrary();
186 } catch ( error ) {
187 if ( error.message ) {
188 dispatchNotification( error.message );
189 }
190 }
191 };
192
193 export const deleteTemplate = async ( template ) => {
194 const url = stringifyUrl( {
195 url: `${ window.tiTpc.endpoint }templates/${ template }`,
196 query: {
197 cache: localStorage.getItem( 'tpcCacheBuster' ),
198 _method: 'DELETE',
199 ...window.tiTpc.params,
200 },
201 } );
202
203 try {
204 const response = await apiFetch( { url, method: 'POST' } );
205
206 if ( response.ok ) {
207 const content = await response.json();
208
209 if ( content.message ) {
210 return dispatchNotification( content.message );
211 }
212 }
213
214 localStorage.setItem( 'tpcCacheBuster', uuidv4() );
215
216 await fetchLibrary();
217 } catch ( error ) {
218 if ( error.message ) {
219 dispatchNotification( error.message );
220 }
221 }
222 };
223