-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (96 loc) · 2.97 KB
/
Copy pathindex.js
File metadata and controls
118 lines (96 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import apiFetch from '@wordpress/api-fetch'
import { doAction, applyFilters } from '@wordpress/hooks'
const LATEST_API_VERSION = 'v4'
let designLibrary = {}
let designs = {}
let pages = {}
export const getBlockName = block => block.replace( /^[\w-]+\//, '' )
export const fetchDesignLibrary = async ( forceReset = false, version = '', type = 'patterns' ) => {
if ( forceReset ) {
doAction( 'stackable.design-library.reset-cache' )
designLibrary = {}
designs = {}
pages = {}
}
if ( ( type === 'patterns' && ! Object.keys( designs ).length ) ||
( type === 'pages' && ! Object.keys( pages ).length )
) {
const results = await apiFetch( {
path: `/stackable/v2/design_library/${ type }${ forceReset ? '/reset' : '' }`,
method: 'GET',
} )
const designsPerType = results
designLibrary[ type ] = designsPerType
if ( type === 'patterns' ) {
designs = designsPerType[ LATEST_API_VERSION ] ?? {}
} else {
pages = designsPerType[ LATEST_API_VERSION ] ?? {}
}
}
return designLibrary[ type ]?.[ version || LATEST_API_VERSION ] ?? designLibrary[ type ] ?? {}
}
export const fetchDesign = async designId => {
if ( ! designs[ designId ] ) {
await fetchDesignLibrary()
}
return designs[ designId ] || {}
}
// TODO: to remove
export const setDevModeDesignLibrary = async ( devMode = false ) => {
const results = await apiFetch( {
path: `/stackable/v2/design_library_dev_mode/`,
method: 'POST',
data: {
devmode: devMode,
},
} )
return await results
}
export const getDesigns = async ( {
reset = false,
type = 'patterns',
} ) => {
const designLibrary = await fetchDesignLibrary( reset, LATEST_API_VERSION, type )
if ( designLibrary.wp_remote_get_error || designLibrary.content_error ) {
const error = designLibrary.wp_remote_get_error ?? designLibrary.content_error
// eslint-disable-next-line no-console
console.error( 'Stackable: ', error )
return { error }
}
// pre-fetch patterns
if ( type === 'pages' ) {
await fetchDesignLibrary()
}
return Object.values( designLibrary )
}
export const filterDesigns = async ( {
library = [],
plan: isPlan = '',
category: isCategory = '',
type = 'patterns',
} ) => {
if ( isPlan && type !== 'saved' ) {
library = library.filter( ( { plan } ) => plan === isPlan )
}
if ( isCategory ) {
library = library.filter( ( { category } ) => category === isCategory )
}
return library
}
/**
*
* @param {string} designId The name of the design
* @param {string} version The version of the design library API to use.
*
* @return {Object} The design object.
*/
export const getDesign = async ( designId, version = '' ) => {
const library = await fetchDesignLibrary( false, version )
const meta = library[ designId ]
let design = await applyFilters( 'stackable.design-library.get-design', null, designId, meta, version )
// Every design has their own template file which contains the entire design, get that.
if ( ! design && meta.template ) {
design = await fetchDesign( designId, version )
}
return design
}