| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
import { create } from 'zustand'; |
| 3 |
import { createJSONStorage, devtools, persist } from 'zustand/middleware'; |
| 4 |
|
| 5 |
const { siteProfile } = window.extSharedData; |
| 6 |
|
| 7 |
const state = (set) => ({ |
| 8 |
siteProfile, |
| 9 |
setSiteProfile(data) { |
| 10 |
set((state) => { |
| 11 |
const updatedProfile = { |
| 12 |
...state.siteProfile, |
| 13 |
...data, |
| 14 |
}; |
| 15 |
return { siteProfile: updatedProfile }; |
| 16 |
}); |
| 17 |
}, |
| 18 |
resetState() { |
| 19 |
set({ siteProfile }); |
| 20 |
}, |
| 21 |
}); |
| 22 |
|
| 23 |
const path = '/extendify/v1/shared/site-profile'; |
| 24 |
const storage = { |
| 25 |
getItem: async () => await apiFetch({ path }), |
| 26 |
setItem: async (_name, state) => { |
| 27 |
await apiFetch({ |
| 28 |
path, |
| 29 |
method: 'POST', |
| 30 |
data: { siteProfile: JSON.stringify(state) }, |
| 31 |
}); |
| 32 |
}, |
| 33 |
}; |
| 34 |
|
| 35 |
export const useSiteProfileStore = create( |
| 36 |
persist(devtools(state, { name: 'Extendify Site Profile' }), { |
| 37 |
storage: createJSONStorage(() => storage), |
| 38 |
skipHydration: false, |
| 39 |
}), |
| 40 |
state, |
| 41 |
); |
| 42 |
|