2024-04-11 21:28:43 -05:00
|
|
|
import { useUserApi } from "~/composables/api";
|
2025-06-20 00:09:12 +07:00
|
|
|
import type { UserRatingSummary } from "~/lib/api/types/user";
|
2024-04-11 21:28:43 -05:00
|
|
|
|
|
|
|
const userRatings = ref<UserRatingSummary[]>([]);
|
|
|
|
const loading = ref(false);
|
|
|
|
const ready = ref(false);
|
|
|
|
|
|
|
|
export const useUserSelfRatings = function () {
|
2025-06-20 00:09:12 +07:00
|
|
|
const $auth = useMealieAuth();
|
2024-09-22 09:59:20 -05:00
|
|
|
const api = useUserApi();
|
2024-04-11 21:28:43 -05:00
|
|
|
|
2024-09-22 09:59:20 -05:00
|
|
|
async function refreshUserRatings() {
|
2025-06-20 00:09:12 +07:00
|
|
|
if (!$auth.user.value || loading.value) {
|
2024-09-22 09:59:20 -05:00
|
|
|
return;
|
2024-04-11 21:28:43 -05:00
|
|
|
}
|
|
|
|
|
2024-09-22 09:59:20 -05:00
|
|
|
loading.value = true;
|
|
|
|
const { data } = await api.users.getSelfRatings();
|
|
|
|
userRatings.value = data?.ratings || [];
|
|
|
|
loading.value = false;
|
|
|
|
ready.value = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setRating(slug: string, rating: number | null, isFavorite: boolean | null) {
|
|
|
|
loading.value = true;
|
2025-06-20 00:09:12 +07:00
|
|
|
const userId = $auth.user.value?.id || "";
|
2024-09-22 09:59:20 -05:00
|
|
|
await api.users.setRating(userId, slug, rating, isFavorite);
|
|
|
|
loading.value = false;
|
|
|
|
await refreshUserRatings();
|
|
|
|
}
|
2024-04-11 21:28:43 -05:00
|
|
|
|
2024-09-22 09:59:20 -05:00
|
|
|
if (!ready.value) {
|
2024-04-11 21:28:43 -05:00
|
|
|
refreshUserRatings();
|
2024-09-22 09:59:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
userRatings,
|
|
|
|
refreshUserRatings,
|
|
|
|
setRating,
|
|
|
|
ready,
|
2025-06-20 00:09:12 +07:00
|
|
|
};
|
|
|
|
};
|