95 lines
2.5 KiB
JavaScript
95 lines
2.5 KiB
JavaScript
import { defineStore } from 'pinia';
|
|
import { a as getStories, i as isStoryInLocalSeen } from './media-CiMHc-Ka.mjs';
|
|
import { ref } from 'vue';
|
|
import { groupBy } from 'lodash-es';
|
|
import { aA as getProducts } from './server.mjs';
|
|
|
|
const useMediaStore = defineStore("media", () => {
|
|
const users = ref([]);
|
|
const posts = ref([]);
|
|
const limit = ref(10);
|
|
const endPage = ref(false);
|
|
const loading = ref(false);
|
|
const currentPage = ref(0);
|
|
const isLoaded = ref(false);
|
|
const totalCount = ref(0);
|
|
const getList = async () => {
|
|
users.value = [];
|
|
const res = await getStories();
|
|
const group = groupBy(res, "user_id");
|
|
Object.keys(group).forEach((userId) => {
|
|
if (group[userId]?.length) {
|
|
const user = group[userId][0].users;
|
|
const media = group[userId].filter((item) => item?.mediaitems?.length).map((item) => {
|
|
let storyView = item.storyviews;
|
|
if (isStoryInLocalSeen(item.id)) {
|
|
storyView = [...storyView ?? [], { user_id: "me" }];
|
|
}
|
|
return {
|
|
id: item.id,
|
|
caption: item.caption,
|
|
storyviews: storyView,
|
|
url: item.mediaitems[0].media_url,
|
|
type: item.mediaitems[0].media_type
|
|
};
|
|
});
|
|
users.value.push({ id: userId, name: user?.name, avatar: user?.avatar, stories: media });
|
|
}
|
|
});
|
|
};
|
|
const loadPosts = async (page) => {
|
|
if (page === 1 && isLoaded.value) {
|
|
return { data: posts.value, count: totalCount.value };
|
|
}
|
|
try {
|
|
loading.value = true;
|
|
const res = await getProducts(page, limit.value);
|
|
if (page === 1) {
|
|
posts.value = res.data;
|
|
isLoaded.value = true;
|
|
currentPage.value = 1;
|
|
} else {
|
|
posts.value.push(...res.data);
|
|
currentPage.value = page;
|
|
}
|
|
totalCount.value = res.count;
|
|
if (res.data.length < limit.value) {
|
|
endPage.value = true;
|
|
}
|
|
return res;
|
|
} catch (e) {
|
|
console.error(e);
|
|
return { data: [], count: 0 };
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
};
|
|
const reset = () => {
|
|
posts.value = [];
|
|
endPage.value = false;
|
|
loading.value = false;
|
|
currentPage.value = 0;
|
|
isLoaded.value = false;
|
|
totalCount.value = 0;
|
|
};
|
|
const request = async (page = 1) => {
|
|
return await loadPosts(page);
|
|
};
|
|
return {
|
|
users,
|
|
posts,
|
|
limit,
|
|
endPage,
|
|
loading,
|
|
currentPage,
|
|
isLoaded,
|
|
totalCount,
|
|
getList,
|
|
loadPosts,
|
|
request,
|
|
reset
|
|
};
|
|
});
|
|
|
|
export { useMediaStore as u };
|