| 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 |
|