Skip to content
Snippets Groups Projects
Commit d7aed17d authored by Haakon Tideman Kanter's avatar Haakon Tideman Kanter
Browse files

Fixed api call

parents 15a604d6 7ad2e244
No related branches found
No related tags found
1 merge request!87Community request
Pipeline #179965 failed
<template>
<div v-if="totalPages() > 0">
<span v-if="showPreviousLink()"
class="cursor-pointer inline-flex items-center p-2 text-sm font-medium text-gray-500 bg-white rounded-lg border border-gray-300 hover:bg-gray-100 hover:text-gray-700"
@click="updatePage(currentPage - 1)">
Forrige
</span>
<label class="mx-2">{{ currentPage + 1 }} av {{ totalPages() }}</label>
<span
v-if="showNextLink()"
class="cursor-pointer inline-flex items-center p-2 text-sm font-medium text-gray-500 bg-white rounded-lg border border-gray-300 hover:bg-gray-100 hover:text-gray-700"
@click="updatePage(currentPage + 1)">
Neste
</span>
</div>
</template>
<script>
export default {
name: 'paginationTemplate',
props: ['items', 'currentPage', 'pageSize'],
methods: {
updatePage(pageNumber) {
this.$emit('page:update', pageNumber);
},
totalPages() {
return Math.ceil(this.items.length / this.pageSize);
},
showPreviousLink() {
return this.currentPage == 0 ? false : true;
},
showNextLink() {
return this.currentPage == (this.totalPages() - 1) ? false : true;
}
}
}
</script>
...@@ -22,19 +22,39 @@ ...@@ -22,19 +22,39 @@
class="w-full py-3 pl-10 pr-4 text-gray-700 bg-white border rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-primary-medium dark:focus:border-primary-medium focus:outline-none focus:ring" class="w-full py-3 pl-10 pr-4 text-gray-700 bg-white border rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-primary-medium dark:focus:border-primary-medium focus:outline-none focus:ring"
placeholder="Search" placeholder="Search"
v-model="search" v-model="search"
@change="searchWritten"
/> />
</div> </div>
<!-- Item cards -->
<div class="absolute inset-x-0 px-6 py-3"> <div class="absolute inset-x-0 px-5 py-3">
<div
class="grid grid-flow-row-dense grid-cols-2 md:grid-cols-4 lg:grid-cols-5 w-full place-items-center" <!-- ItemCards -->
> <div class="flex items-center justify-center w-screen">
<ItemCard <!-- Shows items based on pagination -->
v-for="item in searchedItems" <div
:key="item" class="grid grid-flow-row-dense grid-cols-2 md:grid-cols-4 lg:grid-cols-5 w-full"
:item="item" v-if="showItems">
@click="goToItemInfoPage(item.listingID)" <ItemCard v-for="item in visibleItems" :key="item" :item="item" @click="goToItemInfoPage(item.listingID)" />
</div>
<!-- Shows items based on search field input -->
<div
class="grid grid-flow-row-dense grid-cols-2 md:grid-cols-4 lg:grid-cols-5 w-full place-items-center"
v-if="showSearchedItems">
<ItemCard v-for="item in searchedItems" :key="item" :item="item" @click="goToItemInfoPage(item.listingID)" />
</div>
</div>
<!-- pagination -->
<div class="flex justify-center" v-if="showItems">
<PaginationTemplate
v-bind:items="items"
v-on:page:update="updatePage"
v-bind:currentPage="currentPage"
v-bind:pageSize="pageSize"
class="mt-10"
/> />
</div> </div>
</div> </div>
...@@ -42,8 +62,10 @@ ...@@ -42,8 +62,10 @@
</template> </template>
<script> <script>
import CommunityHeader from "@/components/CommunityComponents/CommunityHeader.vue";
import ItemCard from "@/components/ItemComponents/ItemCard"; import ItemCard from "@/components/ItemComponents/ItemCard";
import CommunityHeader from "@/components/CommunityComponents/CommunityHeader";
import PaginationTemplate from "@/components/BaseComponents/PaginationTemplate";
import { import {
GetCommunity, GetCommunity,
GetListingsInCommunity, GetListingsInCommunity,
...@@ -54,6 +76,7 @@ export default { ...@@ -54,6 +76,7 @@ export default {
components: { components: {
CommunityHeader, CommunityHeader,
ItemCard, ItemCard,
PaginationTemplate,
}, },
computed: { computed: {
searchedItems() { searchedItems() {
...@@ -87,6 +110,14 @@ export default { ...@@ -87,6 +110,14 @@ export default {
search: "", search: "",
communityID: -1, communityID: -1,
community: {}, community: {},
showItems: true,
showSearchedItems: false,
//Variables connected to pagination
currentPage: 0,
pageSize: 12,
visibleItems: [],
}; };
}, },
methods: { methods: {
...@@ -114,10 +145,36 @@ export default { ...@@ -114,10 +145,36 @@ export default {
let res = await getItemPictures(itemid); let res = await getItemPictures(itemid);
return res; return res;
}, },
searchWritten: function (){
//This method triggers when search input field is changed
if(this.search.length > 0){
this.showItems = false;
this.showSearchedItems = true;
}
else{
this.showItems = true;
this.showSearchedItems = false;
}
},
//Pagination
updatePage(pageNumber) {
this.currentPage = pageNumber;
this.updateVisibleTodos();
},
updateVisibleTodos() {
this.visibleItems = this.items.slice(this.currentPage * this.pageSize, (this.currentPage * this.pageSize) + this.pageSize);
// if we have 0 visible items, go back a page
if (this.visibleItems.length === 0 && this.currentPage > 0) {
this.updatePage(this.currentPage -1);
}
},
}, },
beforeMount() { async beforeMount() {
this.getCommunityFromAPI(); //To get the id of the community before mounting the view await this.getCommunityFromAPI(); //To get the id of the community before mounting the view
this.getListingsOfCommunityFromAPI(); await this.getListingsOfCommunityFromAPI();
this.updateVisibleTodos();
}, },
}; };
</script> </script>
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<!-- Description --> <!-- Description -->
<div class="mt-6" :class="{ error: v$.group.description.$errors.length }"> <div class="mt-6" :class="{ error: v$.description.$errors.length }">
<label <label
class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-400" class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-400"
id="descriptionLabel" id="descriptionLabel"
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
<textarea <textarea
id="description" id="description"
rows="4" rows="4"
v-model="v$.group.description.$model" v-model="description"
class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light" class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
required required
></textarea> ></textarea>
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
<!-- error message for description --> <!-- error message for description -->
<div <div
class="text-error" class="text-error"
v-for="(error, index) of v$.group.description.$errors" v-for="(error, index) of v$.description.$errors"
:key="index" :key="index"
> >
<div class="text-error text-sm"> <div class="text-error text-sm">
...@@ -49,6 +49,7 @@ import axios from "axios"; ...@@ -49,6 +49,7 @@ import axios from "axios";
import useVuelidate from "@vuelidate/core"; import useVuelidate from "@vuelidate/core";
import { required, helpers, maxLength } from "@vuelidate/validators"; import { required, helpers, maxLength } from "@vuelidate/validators";
import Button from "@/components/BaseComponents/ColoredButton"; import Button from "@/components/BaseComponents/ColoredButton";
import {tokenHeader} from "@/utils/token-utils";
import {GetCommunity} from "@/utils/apiutil"; import {GetCommunity} from "@/utils/apiutil";
export default { export default {
...@@ -63,7 +64,6 @@ export default { ...@@ -63,7 +64,6 @@ export default {
validations() { validations() {
return { return {
group: {
description: { description: {
required: helpers.withMessage( required: helpers.withMessage(
() => "Meldingen kan ikke være tom", () => "Meldingen kan ikke være tom",
...@@ -74,7 +74,7 @@ export default { ...@@ -74,7 +74,7 @@ export default {
maxLength(200) maxLength(200)
), ),
}, },
},
}; };
}, },
data() { data() {
...@@ -87,9 +87,12 @@ export default { ...@@ -87,9 +87,12 @@ export default {
computed: { computed: {
}, },
methods: { methods: {
//TODO fix so that community id is set (not null)
async saveClicked() { async saveClicked() {
await axios.post(process.env.VUE_APP_BASEURL+ `community/${this.communityId}/private/join`); this.communityID = await this.$router.currentRoute.value.params
.communityID;
await axios.post(process.env.VUE_APP_BASEURL+ `communities/${this.communityID}/private/join`, {message: this.description, }, {headers: tokenHeader()} );
}, },
getCommunityFromAPI: async function () { getCommunityFromAPI: async function () {
this.communityID = await this.$router.currentRoute.value.params this.communityID = await this.$router.currentRoute.value.params
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment