index.js
332 lines
| 1 | /* eslint-disable no-undef */ |
| 2 | /* global localStorage, tiTpc */ |
| 3 | import { stringifyUrl } from 'query-string'; |
| 4 | import { v4 as uuidv4 } from 'uuid'; |
| 5 | |
| 6 | import apiFetch from '@wordpress/api-fetch'; |
| 7 | import { dispatch } from '@wordpress/data'; |
| 8 | |
| 9 | const { updateLibrary, updateTemplates } = dispatch( 'tpc/block-editor' ); |
| 10 | const { createNotice } = dispatch( 'core/notices' ); |
| 11 | |
| 12 | const { meta, ...filteredParams } = tiTpc.params; |
| 13 | |
| 14 | const createErrorNotice = ( message ) => { |
| 15 | createNotice( 'warning', message, { |
| 16 | context: 'themeisle-blocks/notices/templates-cloud', |
| 17 | isDismissible: true, |
| 18 | } ); |
| 19 | }; |
| 20 | |
| 21 | export const fetchTemplates = async ( additionalParams = {} ) => { |
| 22 | const params = { |
| 23 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 24 | ...filteredParams, |
| 25 | per_page: 12, |
| 26 | page: 0, |
| 27 | premade: true, |
| 28 | template_site_slug: 'general', |
| 29 | ...additionalParams, |
| 30 | }; |
| 31 | |
| 32 | if ( tiTpc.params.type === 'gutenberg' && params.showFSE ) { |
| 33 | params.type = [ 'gutenberg', 'fse' ]; |
| 34 | } |
| 35 | |
| 36 | const url = stringifyUrl( |
| 37 | { |
| 38 | url: tiTpc.endpoint + 'page-templates', |
| 39 | query: params, |
| 40 | }, |
| 41 | { arrayFormat: 'bracket' } |
| 42 | ); |
| 43 | |
| 44 | try { |
| 45 | const response = await apiFetch( { |
| 46 | url, |
| 47 | method: 'GET', |
| 48 | parse: false, |
| 49 | } ); |
| 50 | |
| 51 | if ( response.ok ) { |
| 52 | const templates = await response.json(); |
| 53 | |
| 54 | if ( templates.message ) { |
| 55 | return createErrorNotice( templates.message ); |
| 56 | } |
| 57 | const totalPages = response.headers.get( 'x-wp-totalpages' ); |
| 58 | const currentPage = params.page; |
| 59 | updateTemplates( templates, currentPage, totalPages ); |
| 60 | } |
| 61 | } catch ( error ) { |
| 62 | if ( error.message ) { |
| 63 | createErrorNotice( error.message ); |
| 64 | } |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | export const fetchLibrary = async ( additionalParams = {} ) => { |
| 69 | const params = { |
| 70 | per_page: 12, |
| 71 | page: 0, |
| 72 | ...additionalParams, |
| 73 | }; |
| 74 | |
| 75 | if ( tiTpc.params.type === 'gutenberg' && params.showFSE ) { |
| 76 | params.type = [ 'gutenberg', 'fse' ]; |
| 77 | } |
| 78 | |
| 79 | const url = stringifyUrl( |
| 80 | { |
| 81 | url: tiTpc.endpoint + 'templates', |
| 82 | query: { |
| 83 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 84 | ...filteredParams, |
| 85 | ...params, |
| 86 | }, |
| 87 | }, |
| 88 | { arrayFormat: 'bracket' } |
| 89 | ); |
| 90 | |
| 91 | try { |
| 92 | const response = await apiFetch( { |
| 93 | url, |
| 94 | method: 'GET', |
| 95 | parse: false, |
| 96 | } ); |
| 97 | |
| 98 | if ( response.ok ) { |
| 99 | const templates = await response.json(); |
| 100 | |
| 101 | if ( templates.message ) { |
| 102 | return createErrorNotice( templates.message ); |
| 103 | } |
| 104 | |
| 105 | const totalPages = response.headers.get( 'x-wp-totalpages' ); |
| 106 | const currentPage = params.page; |
| 107 | |
| 108 | updateLibrary( templates, currentPage, totalPages ); |
| 109 | } |
| 110 | } catch ( error ) { |
| 111 | if ( error.message ) { |
| 112 | createErrorNotice( error.message ); |
| 113 | } |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | export const updateTemplate = async ( params ) => { |
| 118 | const url = stringifyUrl( { |
| 119 | url: `${ tiTpc.endpoint }templates/${ params.template_id }`, |
| 120 | query: { |
| 121 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 122 | ...filteredParams, |
| 123 | meta: JSON.stringify( tiTpc.params.meta ), |
| 124 | ...params, |
| 125 | }, |
| 126 | } ); |
| 127 | |
| 128 | try { |
| 129 | const response = await apiFetch( { |
| 130 | url, |
| 131 | method: 'POST', |
| 132 | parse: false, |
| 133 | } ); |
| 134 | |
| 135 | if ( response.ok ) { |
| 136 | const content = await response.json(); |
| 137 | |
| 138 | if ( content.message ) { |
| 139 | return createErrorNotice( content.message ); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | localStorage.setItem( 'tpcCacheBuster', uuidv4() ); |
| 144 | |
| 145 | await fetchTemplates(); |
| 146 | } catch ( error ) { |
| 147 | if ( error.message ) { |
| 148 | createErrorNotice( error.message ); |
| 149 | } |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | export const getTemplate = async ( template ) => { |
| 154 | const url = stringifyUrl( { |
| 155 | url: `${ window.tiTpc.endpoint }templates/${ template }`, |
| 156 | query: { |
| 157 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 158 | ...filteredParams, |
| 159 | }, |
| 160 | } ); |
| 161 | |
| 162 | try { |
| 163 | const response = await apiFetch( { |
| 164 | url, |
| 165 | method: 'GET', |
| 166 | parse: false, |
| 167 | } ); |
| 168 | |
| 169 | if ( response.ok ) { |
| 170 | const content = await response.json(); |
| 171 | |
| 172 | if ( content.message ) { |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | return content; |
| 177 | } |
| 178 | } catch ( error ) { |
| 179 | return false; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | export const importTemplate = async ( template ) => { |
| 184 | const url = stringifyUrl( { |
| 185 | url: `${ tiTpc.endpoint }templates/${ template }/import`, |
| 186 | query: { |
| 187 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 188 | ...filteredParams, |
| 189 | }, |
| 190 | } ); |
| 191 | |
| 192 | let content = {}; |
| 193 | |
| 194 | try { |
| 195 | const response = await apiFetch( { |
| 196 | url, |
| 197 | method: 'GET', |
| 198 | parse: false, |
| 199 | } ); |
| 200 | |
| 201 | if ( response.ok ) { |
| 202 | content = await response.json(); |
| 203 | |
| 204 | if ( content.message ) { |
| 205 | return createErrorNotice( content.message ); |
| 206 | } |
| 207 | } |
| 208 | } catch ( error ) { |
| 209 | if ( error.message ) { |
| 210 | createErrorNotice( error.message ); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return content; |
| 215 | }; |
| 216 | |
| 217 | export const duplicateTemplate = async ( template ) => { |
| 218 | const url = stringifyUrl( { |
| 219 | url: `${ tiTpc.endpoint }templates/${ template }/clone`, |
| 220 | query: { |
| 221 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 222 | ...filteredParams, |
| 223 | }, |
| 224 | } ); |
| 225 | |
| 226 | try { |
| 227 | const response = await apiFetch( { |
| 228 | url, |
| 229 | method: 'POST', |
| 230 | parse: false, |
| 231 | } ); |
| 232 | |
| 233 | if ( response.ok ) { |
| 234 | const content = await response.json(); |
| 235 | |
| 236 | if ( content.message ) { |
| 237 | return createErrorNotice( content.message ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | localStorage.setItem( 'tpcCacheBuster', uuidv4() ); |
| 242 | |
| 243 | await fetchTemplates(); |
| 244 | await fetchLibrary(); |
| 245 | } catch ( error ) { |
| 246 | if ( error.message ) { |
| 247 | createErrorNotice( error.message ); |
| 248 | } |
| 249 | } |
| 250 | }; |
| 251 | |
| 252 | export const deleteTemplate = async ( template, sortingOrder ) => { |
| 253 | const url = stringifyUrl( { |
| 254 | url: `${ tiTpc.endpoint }templates/${ template }`, |
| 255 | query: { |
| 256 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 257 | _method: 'DELETE', |
| 258 | ...filteredParams, |
| 259 | }, |
| 260 | } ); |
| 261 | |
| 262 | try { |
| 263 | const response = await apiFetch( { url, method: 'POST' } ); |
| 264 | |
| 265 | if ( response.ok ) { |
| 266 | const content = await response.json(); |
| 267 | |
| 268 | if ( content.message ) { |
| 269 | return createErrorNotice( content.message ); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | localStorage.setItem( 'tpcCacheBuster', uuidv4() ); |
| 274 | |
| 275 | await fetchLibrary( sortingOrder ); |
| 276 | } catch ( error ) { |
| 277 | if ( error.message ) { |
| 278 | createErrorNotice( error.message ); |
| 279 | } |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | export const publishTemplate = async ( |
| 284 | template, |
| 285 | siteSlug, |
| 286 | featuredImageURL, |
| 287 | publishStatus, |
| 288 | link |
| 289 | ) => { |
| 290 | const url = stringifyUrl( { |
| 291 | url: `${ tiTpc.endpoint }templates/${ template }/publish`, |
| 292 | query: { |
| 293 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 294 | method: 'POST', |
| 295 | template_site_slug: siteSlug, |
| 296 | template_thumbnail: featuredImageURL, |
| 297 | premade: publishStatus ? 'yes' : 'no', |
| 298 | link, |
| 299 | ...filteredParams, |
| 300 | }, |
| 301 | } ); |
| 302 | |
| 303 | try { |
| 304 | const response = await apiFetch( { |
| 305 | url, |
| 306 | method: 'POST', |
| 307 | headers: { |
| 308 | Authorization: `Bearer ${ tiTpc.bearer || '' } `, |
| 309 | }, |
| 310 | } ); |
| 311 | if ( response.ok ) { |
| 312 | const content = await response.json(); |
| 313 | if ( content.message ) { |
| 314 | createErrorNotice( content.message ); |
| 315 | return { success: false }; |
| 316 | } |
| 317 | } else if ( response.message ) { |
| 318 | createErrorNotice( response.message ); |
| 319 | return { success: false }; |
| 320 | } |
| 321 | |
| 322 | localStorage.setItem( 'tpcCacheBuster', uuidv4() ); |
| 323 | |
| 324 | return { success: true }; |
| 325 | } catch ( error ) { |
| 326 | if ( error.message ) { |
| 327 | createErrorNotice( error.message ); |
| 328 | return { success: false }; |
| 329 | } |
| 330 | } |
| 331 | }; |
| 332 |