Skip to content

Commit

Permalink
add multiselect and sort options to service list
Browse files Browse the repository at this point in the history
  • Loading branch information
Beniox committed Jul 12, 2024
1 parent 1624a34 commit 49a1af1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 16 deletions.
10 changes: 10 additions & 0 deletions webapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"bootstrap": "5.2.x",
"core-js": "3.x",
"vue": "^3.4.29",
"vue-multiselect": "^3.0.0",
"vue-router": "^4.3.3"
},
"devDependencies": {
Expand Down
95 changes: 79 additions & 16 deletions webapp/src/views/ServiceList.vue
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
<template>
<div class="container">
<h1>Dienstzuteilung</h1>
<multiselect
v-model="selectedServices"
placeholder="Bitte Auswählen"
label="name"
track-by="id"
:options="services"
:multiple="true"
@select="fetchServiceHistory"
@remove="fetchServiceHistory"
v-if="services?.length > 0"
></multiselect>

<select
class="form-select"
aria-label="Default select example"
@change="fetchServiceHistory(($event.target as HTMLSelectElement).value)"
>
<option disabled selected>Bitte auswählen</option>
<option
v-for="(service, index) in services"
:key="index"
:value="service.id"
>
{{ service.name }}
</option>
</select>
<div>
<label class="typo__label">Sortieren nach:</label>
<multiselect
v-model="selectedOption"
:options="sortOptions"
label="text"
track-by="id"
:searchable="false"
:show-labels="false"
:allow-empty="false"
@select="sortServiceHistory"
placeholder="Pick a value"
></multiselect>
</div>

<table class="table table-striped" v-if="serviceHistory">
<thead>
Expand Down Expand Up @@ -46,26 +57,78 @@
</tr>
</tbody>
</table>
<div
class="d-flex justify-content-center"
v-if="!serviceHistory && selectedServices.length > 0"
>
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
</div>
</template>

<script setup lang="ts">
import type { ServiceHistory, Services } from "@/services/service"
import { onMounted, ref } from "vue"
import { getServiceHistory, getServices } from "@/services/service"
import Multiselect from "vue-multiselect"
const services = ref<Services[] | null>(null)
const serviceHistory = ref<ServiceHistory[] | null>(null)
const selectedServices = ref<Services[]>([])
const sortOptions = [
{ id: 0, text: "am längsten ohne Dienst" },
{ id: 1, text: "am wenigsten Dienste" },
]
const selectedOption = ref(sortOptions[0])
onMounted(async () => {
services.value = await getServices()
})
const fetchServiceHistory = async function (id: string) {
serviceHistory.value = await getServiceHistory([parseInt(id)])
const fetchServiceHistory = async function () {
const ids = selectedServices.value.map((service) => service.id)
if (ids.length === 0) {
serviceHistory.value = null
return
}
serviceHistory.value = await getServiceHistory(ids)
sortServiceHistory()
}
const sortServiceHistory = function () {
serviceHistory.value = serviceHistory.value?.sort((a, b) => {
// Helper function to find the most recent past date
const getLastPastServiceDate = (serviceDates) => {
const pastDates = serviceDates
.map((dateObj) => new Date(dateObj.date))
.filter((date) => date <= new Date())
if (pastDates.length === 0) {
return new Date(0) // Return a very old date if there are no past dates
}
return new Date(Math.max(...pastDates))
}
const lastPastDateA = getLastPastServiceDate(a.serviceDates)
const lastPastDateB = getLastPastServiceDate(b.serviceDates)
return lastPastDateA - lastPastDateB
})
if (selectedOption.value.id === 1) {
serviceHistory.value = serviceHistory.value?.sort((a, b) => {
return a.serviceDates.length - b.serviceDates.length
})
}
}
</script>

<style src="node_modules/vue-multiselect/dist/vue-multiselect.css"></style>

<style scoped>
tr.requested td {
color: var(--bs-secondary);
Expand Down

0 comments on commit 49a1af1

Please sign in to comment.