Added grid

This commit is contained in:
PascalMarchesseault
2024-11-03 23:56:38 -05:00
parent 015ffa9eea
commit 07d435e9d7

View File

@@ -1,12 +1,14 @@
<template> <template>
<div> <div>
<v-infinite-scroll :items="contents" :onLoad="fetchContents">
<v-infinite-scroll :items="contents" <div class="grid gap-2 -mt-4"
:onLoad="fetchContents"> :class="{
'grid-cols-1': isExtraSmallScreen,
<!-- TODO: the -mt-4 is necessary because the v-infinite-scroll has some 'top' panel offsetting the list --> 'grid-cols-2': isSmallScreen,
<div class="flex flex-column gap-4 -mt-4"> 'grid-cols-3': isMediumScreen,
'grid-cols-4': isLargeScreen,
'grid-cols-5': isExtraLargeScreen
}">
<template v-for="content in contents" :key="content.id"> <template v-for="content in contents" :key="content.id">
<component <component
:is="isSmallScreen ? ContentCardSm : ContentCardNormal" :is="isSmallScreen ? ContentCardSm : ContentCardNormal"
@@ -15,7 +17,7 @@
></component> ></component>
</template> </template>
</div> </div>
<template v-slot:empty> <template v-slot:empty>
Il n'y a pas plus de contenus Il n'y a pas plus de contenus
</template> </template>
@@ -23,22 +25,15 @@
<template v-slot:error> <template v-slot:error>
<v-alert type="error">{{ errorMessage }}</v-alert> <v-alert type="error">{{ errorMessage }}</v-alert>
</template> </template>
</v-infinite-scroll> </v-infinite-scroll>
</div> </div>
</template> </template>
<style>
</style>
<script setup> <script setup>
import {useClient} from '@/plugins/api.js'; import { ref, onMounted, onBeforeUnmount, watch } from 'vue';
import {onBeforeUnmount, onMounted, ref, watch} from 'vue';
import ContentCardNormal from "@/views/contents/contentcards/NContentCard.vue"; import ContentCardNormal from "@/views/contents/contentcards/NContentCard.vue";
import ContentCardSm from "@/views/contents/contentcards/SmContentCard.vue"; import ContentCardSm from "@/views/contents/contentcards/SmContentCard.vue";
import { useClient } from '@/plugins/api.js';
const props = defineProps({ const props = defineProps({
creatorId: { creatorId: {
@@ -47,15 +42,24 @@ const props = defineProps({
} }
}); });
const client = useClient() const client = useClient();
const contents = ref([]) const contents = ref([]);
const errorMessage = ref() const errorMessage = ref();
let last_id = null let last_id = null;
const isExtraSmallScreen = ref(false);
const isSmallScreen = ref(false); const isSmallScreen = ref(false);
const isMediumScreen = ref(false);
const isLargeScreen = ref(false);
const isExtraLargeScreen = ref(false);
const updateScreenSize = () => { const updateScreenSize = () => {
isSmallScreen.value = window.matchMedia('(max-width: 600px)').matches; isExtraSmallScreen.value = window.matchMedia('(max-width: 640px)').matches;
isSmallScreen.value = window.matchMedia('(min-width: 641px) and (max-width: 768px)').matches;
isMediumScreen.value = window.matchMedia('(min-width: 769px) and (max-width: 1024px)').matches;
isLargeScreen.value = window.matchMedia('(min-width: 1025px) and (max-width: 1280px)').matches;
isExtraLargeScreen.value = window.matchMedia('(min-width: 1281px)').matches;
}; };
onMounted(() => { onMounted(() => {
@@ -67,51 +71,49 @@ onBeforeUnmount(() => {
window.removeEventListener('resize', updateScreenSize); window.removeEventListener('resize', updateScreenSize);
}); });
async function onContentDeleted(contentId) { async function onContentDeleted(contentId) {
contents.value = contents.value.filter(c => c.id !== contentId) contents.value = contents.value.filter(c => c.id !== contentId);
} }
const creatorIdWatcher = watch( const creatorIdWatcher = watch(
() => props.creatorId, () => props.creatorId,
(newCreatorId) => { (newCreatorId) => {
if (newCreatorId) { if (newCreatorId) {
contents.value = [] contents.value = [];
last_id = null last_id = null;
fetchContents({ fetchContents({ done: () => {} });
done: () => {
}
});
} }
}) }
);
async function fetchContents({done, page_size = 10}) { async function fetchContents({ done, page_size = 10 }) {
if (props.creatorId == null) return if (props.creatorId == null) return;
try { try {
let uri = `/api/contents/creator/${props.creatorId}?page_size=${page_size}` let uri = `/api/contents/creator/${props.creatorId}?page_size=${page_size}`;
if (last_id !== null) uri = uri + `&last_id=${last_id}` if (last_id !== null) uri = uri + `&last_id=${last_id}`;
const response = await client.get(uri) const response = await client.get(uri);
if (response.status >= 200 && response.status < 300) { if (response.status >= 200 && response.status < 300) {
const contentCount = response.data.length;
const contentCount = response.data.length
if (contentCount > 0) { if (contentCount > 0) {
contents.value.push(...response.data) contents.value.push(...response.data);
const [last_content] = response.data.slice(-1) const [last_content] = response.data.slice(-1);
last_id = last_content.id last_id = last_content.id;
} }
if (contentCount < page_size) if (contentCount < page_size)
done('empty') done('empty');
else else
done('ok') done('ok');
} }
} catch (error) { } catch (error) {
console.error("Failed to fetch posts", error); console.error("Failed to fetch posts", error);
errorMessage.value = error.message || "Failed to fetch contents"; errorMessage.value = error.message || "Failed to fetch contents";
done('error') done('error');
} }
} }