nuxt-live-editor
Version:
My new Nuxt module
46 lines (45 loc) • 1.52 kB
JavaScript
import { useDirectusItems, useLazyAsyncData, useNuxtApp } from "#imports";
import { ref } from "vue";
import { useLiveEditorStore } from "../stores/useLiveEditorStore.mjs";
export const useFetchLiveEditorValues = async (edit, user) => {
const { getItems } = useDirectusItems();
const liveEditorValues = ref([]);
const nuxtApp = useNuxtApp();
const storeData = useLiveEditorStore();
const { data: dataLive, refresh } = await useLazyAsyncData("live_editor", () => fetchDataLive(), {
transform(data) {
return { data, fetchedAt: /* @__PURE__ */ new Date() };
},
getCachedData(key) {
if (edit) return;
const data = nuxtApp.payload.data[key];
if (!data) {
return;
}
const expirationDate = new Date(data.fetchedAt);
const timeCache = 24 * 60 * 60 * 1e3 * 7;
expirationDate.setTime(expirationDate.getTime() + timeCache);
const isExpired = expirationDate.getTime() < Date.now();
if (isExpired) {
return;
}
return data;
}
});
liveEditorValues.value = dataLive.value?.data || [];
storeData.setLiveEditor({ user, is_edit: edit, data_live_editor: dataLive.value?.data });
return { fetchDataLive, liveEditorValues, refresh };
async function fetchDataLive() {
let fields = edit ? ["*"] : ["option_key", "option_value"];
return await getItems({
collection: "live_editor",
params: {
limit: -1,
fields,
filter: {
language: "vi"
}
}
});
}
};