balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

Templates.js (3608B)


      1 import create from 'zustand'
      2 import { templates as config } from '../config'
      3 import { createBlocksFromInnerBlocksTemplate } from '../util/blocks'
      4 import { useGlobalStore } from './GlobalState'
      5 
      6 const defaultCategoryForType = (type, tax) => type === 'pattern' && tax === 'tax_categories'
      7     ? 'Default'
      8     : ''
      9 
     10 export const useTemplatesStore = create((set, get) => ({
     11     templates: [],
     12     skipNextFetch: false,
     13     fetchToken: null,
     14     activeTemplate: {},
     15     activeTemplateBlocks: {},
     16     taxonomyDefaultState: {},
     17     searchParams: {
     18         taxonomies: {},
     19         type: config.defaultType,
     20         search: '',
     21     },
     22     // The offset is returned from Airtable.
     23     // It's removed when search params are updated
     24     // Or otherwise updated on each request
     25     nextPage: '',
     26     removeTemplates: () => set({
     27         nextPage: '',
     28         templates: [],
     29     }),
     30     appendTemplates: (templates) => set({
     31         templates: [...new Map([...get().templates, ...templates].map(item => [item.id, item])).values()],
     32     }),
     33     setupDefaultTaxonomies: (taxonomies) => {
     34         // This will transform ['tax_categories', 'tax_another'] to {tax_categories: 'Default', tax_another: ''}
     35         const defaultState = (tax) => defaultCategoryForType(get().searchParams.type, tax)
     36         const taxonomyDefaultState = Object.keys(taxonomies).reduce((theObject, current) => (theObject[current] = defaultState(current), theObject), {})
     37         const tax = {}
     38         tax.taxonomies = Object.assign(
     39             {}, taxonomyDefaultState, get().searchParams.taxonomies,
     40         )
     41 
     42         set({
     43             taxonomyDefaultState: taxonomyDefaultState,
     44             searchParams: {
     45                 ...Object.assign(get().searchParams, tax),
     46             },
     47         })
     48     },
     49     setActive: (template) => {
     50         set({ activeTemplate: template })
     51 
     52         // If we havea  template, we should move that that page
     53         if (Object.keys(template).length > 0) {
     54             useGlobalStore.setState({ currentPage: 'single' })
     55         }
     56 
     57         // This will convert the template to blocks for quick(er) injection
     58         if (template?.fields?.code) {
     59             const { parse } = window.wp.blocks
     60             set({ activeTemplateBlocks: createBlocksFromInnerBlocksTemplate(parse(template.fields.code)) })
     61         }
     62     },
     63     resetTaxonomy: (tax) => {
     64         get().updateTaxonomies({
     65             [tax]: get().taxonomyDefaultState[tax] ?? '',
     66         })
     67     },
     68     updateTaxonomies: (params) => {
     69         const tax = {}
     70         tax.taxonomies = Object.assign(
     71             {}, get().searchParams.taxonomies, params,
     72         )
     73         get().updateSearchParams(tax)
     74     },
     75     updateSearchParams: (params) => {
     76         // If taxonomies are set to {}, lets use the default
     77         if (params?.taxonomies && !Object.keys(params.taxonomies).length) {
     78             params.taxonomies = get().taxonomyDefaultState
     79         }
     80 
     81         // If changing the type, change the hard coded tax cat label
     82         if (params?.type && ['', 'Default'].includes(get().searchParams?.taxonomies?.tax_categories)) {
     83             get().updateTaxonomies({
     84                 tax_categories: defaultCategoryForType(params.type, 'tax_categories'),
     85             })
     86         }
     87 
     88         const searchParams = Object.assign(
     89             {}, get().searchParams, params,
     90         )
     91 
     92         // If the params are the same then don't update
     93         if (JSON.stringify(searchParams) === JSON.stringify(get().searchParams)) {
     94             return
     95         }
     96 
     97         set({
     98             templates: [],
     99             nextPage: '',
    100             searchParams,
    101         })
    102     },
    103 }))