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