| 1 |
import create from 'zustand' |
| 2 |
import { subscribeWithSelector } from 'zustand/middleware' |
| 3 |
import { useGlobalStore } from './GlobalState' |
| 4 |
import { useSiteSettingsStore } from './SiteSettings' |
| 5 |
import { useTaxonomyStore } from './Taxonomies' |
| 6 |
|
| 7 |
export const useTemplatesStore = create( |
| 8 |
subscribeWithSelector((set, get) => ({ |
| 9 |
templates: [], |
| 10 |
skipNextFetch: false, |
| 11 |
fetchToken: null, |
| 12 |
taxonomyDefaultState: {}, |
| 13 |
nextPage: '', |
| 14 |
searchParams: { |
| 15 |
taxonomies: {}, |
| 16 |
type: 'pattern', |
| 17 |
}, |
| 18 |
initTemplateData() { |
| 19 |
set({ activeTemplate: {} }) |
| 20 |
get().setupDefaultTaxonomies() |
| 21 |
get().updateType(useGlobalStore.getState().currentType) |
| 22 |
}, |
| 23 |
appendTemplates: async (templates) => { |
| 24 |
for (const template of templates) { |
| 25 |
// If we already have this one, ignore it |
| 26 |
if (get().templates.find((t) => t.id === template.id)) { |
| 27 |
continue |
| 28 |
} |
| 29 |
// Add some delay to prevent a batch update. |
| 30 |
await new Promise((resolve) => setTimeout(resolve, 5)) |
| 31 |
requestAnimationFrame(() => { |
| 32 |
const templatesAll = [...get().templates, template] |
| 33 |
set({ templates: templatesAll }) |
| 34 |
}) |
| 35 |
} |
| 36 |
}, |
| 37 |
setupDefaultTaxonomies: () => { |
| 38 |
const taxonomies = useTaxonomyStore.getState().taxonomies |
| 39 |
let taxonomyDefaultState = Object.entries(taxonomies).reduce( |
| 40 |
(state, current) => ( |
| 41 |
(state[current[0]] = { slug: '', title: 'Featured' }), state |
| 42 |
), |
| 43 |
{}, |
| 44 |
) |
| 45 |
const tax = { |
| 46 |
taxonomies: { |
| 47 |
...taxonomyDefaultState, |
| 48 |
// Override with the global state |
| 49 |
...(useGlobalStore.getState()?.currentTaxonomies ?? {}), |
| 50 |
siteType: useSiteSettingsStore.getState().siteType, |
| 51 |
}, |
| 52 |
} |
| 53 |
set((state) => ({ |
| 54 |
taxonomyDefaultState: taxonomyDefaultState, |
| 55 |
searchParams: { ...state.searchParams, ...tax }, |
| 56 |
})) |
| 57 |
// Do this because the siteType could change from the server |
| 58 |
useGlobalStore.getState().updateCurrentTaxonomies(tax.taxonomies) |
| 59 |
}, |
| 60 |
updateTaxonomies: (params) => { |
| 61 |
const data = {} |
| 62 |
data.taxonomies = Object.assign( |
| 63 |
{}, |
| 64 |
get().searchParams.taxonomies, |
| 65 |
params, |
| 66 |
) |
| 67 |
useGlobalStore.getState().updateCurrentTaxonomies(data?.taxonomies) |
| 68 |
get().updateSearchParams(data) |
| 69 |
}, |
| 70 |
updateType(type) { |
| 71 |
useGlobalStore.getState().updateCurrentType(type) |
| 72 |
get().updateSearchParams({ type }) |
| 73 |
}, |
| 74 |
updateSearchParams: (params) => { |
| 75 |
// If taxonomies are set to {}, lets use the default |
| 76 |
if (params?.taxonomies && !Object.keys(params.taxonomies).length) { |
| 77 |
params.taxonomies = get().taxonomyDefaultState |
| 78 |
} |
| 79 |
|
| 80 |
const searchParams = Object.assign({}, get().searchParams, params) |
| 81 |
|
| 82 |
// If the params are not the same, then update |
| 83 |
if ( |
| 84 |
JSON.stringify(searchParams) !== |
| 85 |
JSON.stringify(get().searchParams) |
| 86 |
) { |
| 87 |
set({ templates: [], nextPage: '', searchParams }) |
| 88 |
} |
| 89 |
}, |
| 90 |
resetTemplates: () => set({ templates: [], nextPage: '' }), |
| 91 |
})), |
| 92 |
) |
| 93 |
|