PluginProbe
Extendify / 0.2.0
Extendify v0.2.0
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / api / Templates.js

Templates.js in Extendify 0.2.0, at src/api/Templates.js

64 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { Axios as api } from './axios'
2 import { useUserStore } from '../state/User'
3
4 let count = 0
5
6 export const Templates = {
7 async get(searchParams, options = {}) {
8 count++
9 const defaultpageSize = searchParams.type === 'pattern' ? '8' : '4'
10 const taxonomyType =
11 searchParams.type === 'pattern' ? 'patternType' : 'layoutType'
12 const templates = await api.post('templates', {
13 filterByFormula: prepareFilterFormula(searchParams, taxonomyType),
14 pageSize: options?.pageSize ?? defaultpageSize,
15 categories: searchParams.taxonomies,
16 search: searchParams.search,
17 type: searchParams.type,
18 offset: options.offset ?? '',
19 initial: count === 1,
20 request_count: count,
21 sdk_partner: useUserStore.getState()?.sdkPartner ?? '',
22 })
23 return templates
24 },
25
26 // TODO: Refactor this later to combine the following three
27 maybeImport(template) {
28 return api.post(`templates/${template.id}`, {
29 template_id: template?.id,
30 maybe_import: true,
31 type: template.fields?.type,
32 pageSize: '1',
33 template_name: template.fields?.title,
34 })
35 },
36 import(template) {
37 return api.post(`templates/${template.id}`, {
38 template_id: template.id,
39 imported: true,
40 basePattern:
41 template.fields?.basePattern ??
42 template.fields?.baseLayout ??
43 '',
44 type: template.fields.type,
45 pageSize: '1',
46 template_name: template.fields?.title,
47 })
48 },
49 }
50
51 const prepareFilterFormula = ({ taxonomies }, type) => {
52 const siteType = taxonomies?.siteType?.slug?.length
53 ? taxonomies.siteType.slug
54 : 'default'
55 const formula = [
56 `{type}="${type.replace('Type', '')}"`,
57 `{siteType}="${siteType}"`,
58 ]
59 if (taxonomies[type]?.slug) {
60 formula.push(`{${type}}="${taxonomies[type].slug}"`)
61 }
62 return `AND(${formula.join(', ')})`.replace(/\r?\n|\r/g, '')
63 }
64