♻️ Refactor frontend

This commit is contained in:
2022-12-10 16:10:10 +01:00
parent 568bfb9782
commit f243137145
12 changed files with 64 additions and 46 deletions

View File

@ -37,8 +37,8 @@ const isDataView = computed(() => {
<DateSelector v-show="isDataView" />
<RouterView />
</main>
<BottomNavbar v-show="$route.name != 'title.login'" />
</div>
<BottomNavbar v-show="$route.name != 'title.login'" />
</div>
</template>

View File

@ -32,18 +32,16 @@ import { RouterLink } from "vue-router";
.bottomnav {
width: calc(90% - 40px);
height: 50px;
background-color: var(--bottomnav-color);
border-radius: 10px;
max-width: 800px;
padding: 0px 20px;
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: grid;
grid-auto-columns: 1fr;
grid-auto-flow: column;
padding: 0px 20px;
max-width: 800px;
z-index: 100;
border-radius: 10px;
background-color: var(--bottomnav-color);
box-shadow: var(--bottomnav-shadow);
}

View File

@ -2,6 +2,7 @@
import { selectedDate, selectedDay, changeDay, changeDate } from "../store";
import ArrowIcon from "./icons/arrow-icon.vue";
import dayjs from "dayjs";
import { getDateSkippingWeekend } from "../util";
const dayNames = [
"days.sunday",
@ -16,15 +17,15 @@ const dayNames = [
<template>
<div class="selector">
<ArrowIcon @click="() => (changeDay -= 1)" />
<span class="day" @click="changeDate = dayjs().toDate()">
<ArrowIcon @click="changeDay--" />
<span
class="day"
@click="changeDate = getDateSkippingWeekend(dayjs().toDate(), true)"
>
{{ $t(dayNames[selectedDay + 1]) }},
{{ dayjs(selectedDate).format("DD.MM.YYYY") }}
</span>
<ArrowIcon
style="transform: rotate(180deg)"
@click="() => (changeDay += 1)"
/>
<ArrowIcon style="transform: rotate(180deg)" @click="changeDay++" />
</div>
</template>

View File

@ -3,6 +3,7 @@ import { selectedDate, changeDate, changeDay } from "../store";
import { Swiper, SwiperSlide } from "swiper/vue";
import { Virtual } from "swiper";
import { ref, watch } from "vue";
import { getDateSkippingWeekend } from "../util";
defineProps({
element: {
@ -26,20 +27,25 @@ const loadedDates = ref([
// Load left or right date on slide change
function slideChange(swiper) {
const activeSlide = swiper.activeIndex;
// Only trigger data refresh if date is different
const newSelectedDate = loadedDates.value[swiper.activeIndex];
const newSelectedDate = loadedDates.value[activeSlide];
if (selectedDate.value.getTime() != newSelectedDate.getTime())
selectedDate.value = newSelectedDate;
const activeSlide = swiper.activeIndex;
// Check if current slide is the last one
if (activeSlide == loadedDates.value.length - 1) {
const lastDate = loadedDates.value[loadedDates.value.length - 1];
// Append next day to loadedDates
const lastDate = loadedDates.value.at(-1);
loadedDates.value.push(getDateSkippingWeekend(calculateDate(lastDate, 1)));
}
}
function slideChangeEnd(swiper) {
const activeSlide = swiper.activeIndex;
// Check if the current slide is the first one
if (activeSlide == 0) {
// Prepend the previous day to loadedDates
// and slide to the next slide without transition
// so the same day as before is shown
const lastDate = loadedDates.value[0];
loadedDates.value = [
getDateSkippingWeekend(calculateDate(lastDate, -1)),
@ -48,6 +54,12 @@ function slideChangeEnd(swiper) {
swiper.slideTo(activeSlide + 1, 0);
}
}
// Also check if a day needs to be prepended
// if the user releases the card later because
// a different event is fired for that
function slideResetEnd(swiper) {
slideChangeEnd(swiper);
}
// Helper functions
function calculateDate(date, offset) {
@ -60,6 +72,8 @@ function slideToDate(date) {
const slide = calculateSlide(date);
if (slide != -1) swiperElement.value.slideTo(slide);
else {
// If the slide is not loaded yet reset
// loaded dates and load that date
loadedDates.value = [calculateDate(date, -1), date, calculateDate(date, 1)];
swiperElement.value.slideTo(1, 0);
}
@ -76,16 +90,6 @@ watch(changeDay, (change) => {
watch(changeDate, (date) => {
slideToDate(new Date(date.setUTCHours(0, 0, 0, 0)));
});
function getDateSkippingWeekend(date) {
var calculatedDate = date;
// Skip weekend
if (calculatedDate.getDay() == 6)
calculatedDate = new Date(calculatedDate.getTime() + 86400000 * 2);
if (calculatedDate.getDay() == 0)
calculatedDate = new Date(calculatedDate.getTime() - 86400000 * 2);
return calculatedDate;
}
</script>
<template>
@ -96,6 +100,7 @@ function getDateSkippingWeekend(date) {
:initial-slide="1"
:virtual="true"
@slide-change-transition-end="slideChangeEnd"
@slide-reset-transition-end="slideResetEnd"
@slide-change="slideChange"
@swiper="setSwiper"
>

View File

@ -15,6 +15,7 @@ const props = defineProps({
},
});
const loadingElement = ref(null);
const computedProgress = ref(0);
const visible = ref(true);
var hideTimeout;
@ -38,6 +39,7 @@ watch(
hideTimeout = setTimeout(() => {
if (!props.active) {
visible.value = false;
loadingElement.value.classList.remove("transition");
computedProgress.value = 0;
}
}, 500);
@ -48,7 +50,8 @@ watch(
<template>
<div
class="bar"
class="bar transition"
ref="loadingElement"
:class="(active ? 'active' : '') + (error ? ' error' : '')"
:style="`width: ${computedProgress * 100}%;`"
></div>
@ -57,9 +60,12 @@ watch(
<style scoped>
.bar {
width: 0%;
opacity: 0;
opacity: 0.2;
background-color: var(--loader-color);
height: 5px;
}
.bar.transition {
transition: 0.5s;
transition-property: width opacity background-color;
}
@ -67,6 +73,7 @@ watch(
.bar.active {
opacity: 1;
}
.bar.error {
background-color: var(--loader-error-color);
}

View File

@ -38,7 +38,6 @@ const substitutions = computed(() => {
<style scoped>
.substitution {
display: grid;
background-color: var(--substitution-background-unchanged);
min-height: 50px;
border-radius: 11px;
margin-bottom: 10px;

View File

@ -41,7 +41,7 @@ defineProps({
width: max-content;
height: max-content;
max-width: 100%;
z-index: 10;
z-index: 1;
}
.content {
@ -76,7 +76,7 @@ span.description {
select {
margin-top: 15px;
padding: 5px 0px;
padding: 5px 5px;
outline: none;
border: 2px solid var(--element-border-input);
border-radius: 5px;

View File

@ -1,5 +1,4 @@
import { computed } from "@vue/reactivity";
import { ref, watch } from "vue";
import { ref, watch, computed } from "vue";
import router from "./router";
import i18n from "./main";

View File

@ -1,11 +1,4 @@
import {
classFilter,
fetchSubstitutions,
fetchHistory,
loading,
selectedDate,
} from "./store";
import dayjs from "dayjs";
import { classFilter } from "./store";
export function getSubstitutionText(substitution) {
const includeClass = !classFilter.value || classFilter.value == "none";
@ -29,3 +22,14 @@ export function getSubstitutionColor(substitution) {
return "background-color: var(--substitution-background-cancellation);";
}
}
export function getDateSkippingWeekend(date, onlyNext) {
// Go from saturday to next monday
if (date.getDay() == 6) return new Date(date.getTime() + 86400000 * 2);
// Go from sunday to last friday or next monday
if (date.getDay() == 0)
return new Date(
onlyNext ? date.getTime() + 86400000 * 1 : date.getTime() - 86400000 * 2
);
return date;
}

View File

@ -15,7 +15,7 @@
<style scoped>
.login {
padding: 90px 10px 80px 10px;
padding: 50px 10px;
}
.login {
@ -31,13 +31,18 @@ form {
display: flex;
flex-direction: column;
gap: 10px;
width: 250px;
}
form * {
height: 30px;
}
input {
border: 0px;
outline: none;
background-color: var(--element-color);
padding: 5px 5px;
padding: 0px 5px;
font-size: 15px;
margin: 5px 0;
border: 2px solid var(--element-border-input);

View File

@ -1,5 +1,5 @@
<script setup>
import TimetableSetup from "../components/initial-setup.vue";
import TimetableSetup from "../components/timetable-setup.vue";
import { classList, classFilter } from "../store";
import DayCarousel from "../components/day-carousel.vue";
import TimetableList from "../components/timetable-list.vue";