| 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 args = Object.assign( |
| 13 |
{ |
| 14 |
filterByFormula: prepareFilterFormula( |
| 15 |
searchParams, |
| 16 |
taxonomyType, |
| 17 |
), |
| 18 |
pageSize: defaultpageSize, |
| 19 |
categories: searchParams.taxonomies, |
| 20 |
search: searchParams.search, |
| 21 |
type: searchParams.type, |
| 22 |
offset: '', |
| 23 |
initial: count === 1, |
| 24 |
request_count: count, |
| 25 |
sdk_partner: useUserStore.getState()?.sdkPartner ?? '', |
| 26 |
}, |
| 27 |
options, |
| 28 |
) |
| 29 |
return await api.post('templates', args) |
| 30 |
}, |
| 31 |
|
| 32 |
// TODO: Refactor this later to combine the following three |
| 33 |
maybeImport(template) { |
| 34 |
return api.post(`templates/${template.id}`, { |
| 35 |
template_id: template?.id, |
| 36 |
maybe_import: true, |
| 37 |
type: template.fields?.type, |
| 38 |
pageSize: '1', |
| 39 |
template_name: template.fields?.title, |
| 40 |
}) |
| 41 |
}, |
| 42 |
import(template) { |
| 43 |
return api.post(`templates/${template.id}`, { |
| 44 |
template_id: template.id, |
| 45 |
imported: true, |
| 46 |
basePattern: |
| 47 |
template.fields?.basePattern ?? |
| 48 |
template.fields?.baseLayout ?? |
| 49 |
'', |
| 50 |
type: template.fields.type, |
| 51 |
pageSize: '1', |
| 52 |
template_name: template.fields?.title, |
| 53 |
}) |
| 54 |
}, |
| 55 |
} |
| 56 |
|
| 57 |
const prepareFilterFormula = ({ taxonomies }, type) => { |
| 58 |
const siteType = taxonomies?.siteType?.slug?.length |
| 59 |
? taxonomies.siteType.slug |
| 60 |
: 'default' |
| 61 |
const formula = [ |
| 62 |
`{type}="${type.replace('Type', '')}"`, |
| 63 |
`{siteType}="${siteType}"`, |
| 64 |
] |
| 65 |
if (taxonomies[type]?.slug) { |
| 66 |
formula.push(`{${type}}="${taxonomies[type].slug}"`) |
| 67 |
} |
| 68 |
return `AND(${formula.join(', ')})`.replace(/\r?\n|\r/g, '') |
| 69 |
} |
| 70 |
|