# extendify/0.2.0/src/api/Templates.js

Extendify, version 0.2.0. 64 lines.

- Page: https://pluginprobe.com/plugins/extendify/0.2.0/code/src/api/Templates.js
- Raw: https://pluginprobe.com/plugins/extendify/0.2.0/raw/src/api/Templates.js
- Modified: 2022-01-06T18:08:32+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/extendify/0.2.0/code/src/api/Templates.js#L10-L20`.

```javascript
import { Axios as api } from './axios'
import { useUserStore } from '../state/User'

let count = 0

export const Templates = {
    async get(searchParams, options = {}) {
        count++
        const defaultpageSize = searchParams.type === 'pattern' ? '8' : '4'
        const taxonomyType =
            searchParams.type === 'pattern' ? 'patternType' : 'layoutType'
        const templates = await api.post('templates', {
            filterByFormula: prepareFilterFormula(searchParams, taxonomyType),
            pageSize: options?.pageSize ?? defaultpageSize,
            categories: searchParams.taxonomies,
            search: searchParams.search,
            type: searchParams.type,
            offset: options.offset ?? '',
            initial: count === 1,
            request_count: count,
            sdk_partner: useUserStore.getState()?.sdkPartner ?? '',
        })
        return templates
    },

    // TODO: Refactor this later to combine the following three
    maybeImport(template) {
        return api.post(`templates/${template.id}`, {
            template_id: template?.id,
            maybe_import: true,
            type: template.fields?.type,
            pageSize: '1',
            template_name: template.fields?.title,
        })
    },
    import(template) {
        return api.post(`templates/${template.id}`, {
            template_id: template.id,
            imported: true,
            basePattern:
                template.fields?.basePattern ??
                template.fields?.baseLayout ??
                '',
            type: template.fields.type,
            pageSize: '1',
            template_name: template.fields?.title,
        })
    },
}

const prepareFilterFormula = ({ taxonomies }, type) => {
    const siteType = taxonomies?.siteType?.slug?.length
        ? taxonomies.siteType.slug
        : 'default'
    const formula = [
        `{type}="${type.replace('Type', '')}"`,
        `{siteType}="${siteType}"`,
    ]
    if (taxonomies[type]?.slug) {
        formula.push(`{${type}}="${taxonomies[type].slug}"`)
    }
    return `AND(${formula.join(', ')})`.replace(/\r?\n|\r/g, '')
}

```
