Skip to content

Commit

Permalink
feat: add pagination in search popup
Browse files Browse the repository at this point in the history
  • Loading branch information
nxhawk committed Jun 22, 2024
1 parent 43880fd commit b6b48a4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
11 changes: 4 additions & 7 deletions src/components/CustomPagination.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
</template>

<script setup>
import router from "@/router";
import { ref, watch } from "vue";
import { useRoute } from 'vue-router';
const route = useRoute();
const props = defineProps({
perPage: {
type: Number,
Expand All @@ -28,14 +25,14 @@
currentPage: {
type: Number,
required: true,
},
func: {
type: Function,
}
});
const onClickHandler = (page) => {
const classId = route.params.classId;
router.push({ name: "exercise", params: { classId }, query: {
page,
}})
props.func(page);
};
const page = ref(1);
Expand Down
12 changes: 12 additions & 0 deletions src/components/ListBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
:per-page="props.exams.per_page"
:total-items="props.exams.total"
:current-page="props.currentPage"
:func="changePage"
/>
</div>
</template>

<script setup>
import Box from "@/components/Box.vue";
import CustomPagination from "@/components/CustomPagination.vue";
import router from "@/router";
import { useRoute } from 'vue-router';
const route = useRoute();
const props = defineProps({
exams: {
Expand All @@ -31,4 +36,11 @@
required: true,
}
});
function changePage(page) {
const classId = route.params.classId;
router.push({ name: "exercise", params: { classId }, query: {
page,
}})
}
</script>
21 changes: 15 additions & 6 deletions src/components/Search/CardSearchResult.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<template lang="">
<div>
<img
v-lazy="{ src: props.data.avatar }"
class="w-full rounded-lg object-cover h-32 mb-2"
/>
<router-link
:to="{ name: 'do-exercise', params: { exerciseId: props.data.slug } }"
@click.native="onClick"
>
<img
v-lazy="{ src: props.data.avatar }"
class="w-full rounded-lg object-cover h-32 mb-2"
/>
</router-link>
<router-link
v-if="props.data.category"
@click.native="onClick"
Expand All @@ -12,9 +17,13 @@
>
{{ props.data.category.name }}
</router-link>
<div class="font-bold leading-5 mt-2 mb-4 md:mb-2">
<router-link
:to="{ name: 'do-exercise', params: { exerciseId: props.data.slug } }"
@click.native="onClick"
class="font-bold leading-5 mt-2 mb-4 md:mb-2 block"
>
{{ props.data.name }}
</div>
</router-link>
</div>
</template>

Expand Down
21 changes: 18 additions & 3 deletions src/components/Search/SearchHandle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@
/>
</div>

<div class="border-t">Pagination here</div>
<div class="border-t" v-if="searchData.value?.results">
<custom-pagination
:per-page="searchData.value.results.per_page"
:total-items="searchData.value.results.total"
:current-page="searchData.value.results.current_page"
:func="changePage"
/>
</div>
</div>
</template>

Expand All @@ -42,6 +49,8 @@
import { onMounted, reactive } from "vue";
import CardSearchResult from "@/components/Search/CardSearchResult.vue";
import Icon from "@/components/Icon.vue";
import CustomPagination from "@/components/CustomPagination.vue";
const isShowSearchPopup = defineModel("isShowSearchPopup", {
type: Boolean,
required: true,
Expand All @@ -52,13 +61,19 @@
const ExamRepository = RepositoryFactory.get("exams");
const searchText = ref("");
const isLoading = ref(false);
const currentPage = ref(1);
async function handleSubmit() {
isLoading.value = true;
const { data } = await ExamRepository.search(searchText.value, 1);
const { data } = await ExamRepository.search(searchText.value, currentPage.value);
searchData.value = data;
isLoading.value = false;
console.log(searchData.value);
}
function changePage(page) {
currentPage.value = page;
handleSubmit();
}
</script>

0 comments on commit b6b48a4

Please sign in to comment.