feat : Pagination Implement.

This commit is contained in:
kms
2022-12-04 19:31:37 +09:00
parent 20487f5a46
commit 74451508e9
4 changed files with 17 additions and 25 deletions

View File

@@ -77,9 +77,9 @@ const createArticle = async (article: object | undefined): Promise<AxiosResponse
const listArticles = async (): Promise<AxiosResponse> => { const listArticles = async (): Promise<AxiosResponse> => {
let currentToken = localStorage.getItem("token"); let currentToken = localStorage.getItem("token");
if(currentToken == null){ if(currentToken == null){
return await axiosService.get('/api/articles'); return await axiosService.get('/api/articles?limit=1000&offset=0');
}else{ }else{
return await axiosService.get('/api/articles',{ return await axiosService.get('/api/articles?limit=1000&offset=0',{
headers:{ headers:{
Authorization: "TOKEN " + currentToken, Authorization: "TOKEN " + currentToken,
} }
@@ -102,7 +102,7 @@ const listArticlesByUsername = async (author: string): Promise<AxiosResponse> =>
const feedArticle = async (): Promise<AxiosResponse> => { const feedArticle = async (): Promise<AxiosResponse> => {
let currentToken = localStorage.getItem("token"); let currentToken = localStorage.getItem("token");
return await axiosService.get('/api/articles/feed',{ return await axiosService.get('/api/articles/feed?limit=1000&offset=0',{
headers:{ headers:{
Authorization: "TOKEN " + currentToken, Authorization: "TOKEN " + currentToken,
} }
@@ -120,9 +120,9 @@ const getArticle = async (slug: string | undefined): Promise<AxiosResponse> => {
} }
}); });
} }
} }
const addCommentToArticle = async (slug: string | undefined, comment: object): Promise<AxiosResponse> => { const addCommentToArticle = async (slug: string | undefined, comment: object): Promise<AxiosResponse> => {
let currentToken = localStorage.getItem("token"); let currentToken = localStorage.getItem("token");
return await axiosService.post('/api/articles/' + slug + '/comments',{ return await axiosService.post('/api/articles/' + slug + '/comments',{

View File

@@ -4,7 +4,7 @@ import { listArticles, feedArticle } from "@/api/index";
import { usePagination } from "@/ts/usePagination"; import { usePagination } from "@/ts/usePagination";
export interface ArticleList { export interface Article {
slug: string, slug: string,
title: string, title: string,
description: string, description: string,
@@ -21,12 +21,11 @@ export function usePaginationApi(
currentPage: Ref<number>, currentPage: Ref<number>,
rowsPerPage?: Ref<number> rowsPerPage?: Ref<number>
) { ) {
const articleLists: Ref<ArticleList[]> = ref([]); const articleLists: Ref<Article[]> = ref([]);
const listsAreLoading = ref(false); const listsAreLoading = ref(false);
const isEmpty = ref(false); const isEmpty = ref(false);
const { paginatedArray, numberOfPages } = usePagination<ArticleList>({ const { paginatedArray, numberOfPages } = usePagination<Article>({
rowsPerPage, rowsPerPage,
arrayToPaginate: articleLists, arrayToPaginate: articleLists,
currentPage currentPage
@@ -64,6 +63,7 @@ export function usePaginationApi(
} }
}; };
return { return {
articleLists: paginatedArray, articleLists: paginatedArray,
loadLists, loadLists,

View File

@@ -1,4 +1,4 @@
import { computed, Ref, ref } from "vue"; import {computed, Ref, ref} from "vue";
interface PaginationConfig<T> { interface PaginationConfig<T> {
rowsPerPage?: Ref<number>; rowsPerPage?: Ref<number>;
@@ -15,11 +15,10 @@ export function usePagination<T>(config: PaginationConfig<T>) {
config.currentPage.value * rowsPerPage.value config.currentPage.value * rowsPerPage.value
) )
); );
const numberOfPages = computed(() => {
const numberOfPages = computed(() => return Math.ceil((config.arrayToPaginate.value.length || 0) / rowsPerPage.value)
Math.ceil((config.arrayToPaginate.value.length || 0) / rowsPerPage.value) }
); );
return { return {
paginatedArray, paginatedArray,
numberOfPages numberOfPages

View File

@@ -42,8 +42,6 @@
<div v-else> <div v-else>
<article-list-global v-for="(article,index) in articleLists" <article-list-global v-for="(article,index) in articleLists"
:key="article.slug" :key="article.slug"
:index="index"
@update:Favorite="updateFavorite"
:article="article"> :article="article">
</article-list-global> </article-list-global>
</div> </div>
@@ -55,10 +53,8 @@
</div> </div>
</div> </div>
</div> </div>
<pagination-component v-model="currentPage" :numberOfPages="numberOfPages"></pagination-component>
</div> </div>
<pagination-component v-model="currentPage" :numberOfPages="rowsPerPage"></pagination-component>
</div> </div>
</template> </template>
@@ -101,9 +97,6 @@ export default {
globalActive.value=true; globalActive.value=true;
await loadLists(); await loadLists();
} }
const updateFavorite = async (index: number) => {
console.log("index" , index);
}
onMounted(async () => { onMounted(async () => {
isLogin.value = store.state.token ? true : false; isLogin.value = store.state.token ? true : false;
@@ -116,7 +109,7 @@ export default {
} }
}) })
return { listsAreLoading, isEmpty, isLogin, currentPage, rowsPerPage, numberOfPages, feedActive, globalActive, articleLists, updateFavorite, feedSelect, globalSelect }; return { listsAreLoading, isEmpty, isLogin, currentPage, rowsPerPage, numberOfPages, feedActive, globalActive, articleLists, feedSelect, globalSelect };
} }
} }
</script> </script>