Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show when data was last updated #244

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions src/components/FilterMenu/FilterMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
<fieldset class="filter-menu__filter-group">
<legend class="h3">
{{ $t("occupancy") }}
<span
v-if="updatedAt"
class="filter-menu__legend-meta"
>
{{ $t('updated') }}:
{{ updatedAt }}
</span>
</legend>
<FilterMenuItem
v-for="occupancy in OCCUPANCY_RATES"
Expand Down Expand Up @@ -133,7 +140,9 @@
</template>

<script setup lang="ts">
const { $locale } = useNuxtApp();
import { storeToRefs } from "pinia";
import { useIntervalFn } from "@vueuse/core"
import { useSpacesStore } from "~/stores/spaces";
import { OCCUPANCY_RATES } from "~/types/Filters";

Expand All @@ -149,11 +158,35 @@ const { filters, buildings } = storeToRefs(spacesStore);
const spaceCount = computed(() =>
spacesStore.currentSelection?.level == "building"
? spacesStore.filteredSpaces.filter(
(space) => space.buildingNumber === spacesStore.currentBuilding!.number
).length
(space) => space.buildingNumber === spacesStore.currentBuilding!.number
).length
: spacesStore.filteredSpaces.length
);

function getRelativeTimeString(date: Date): string {
const deltaSeconds = Math.round((date.getTime() - Date.now()) / 1000);

const cutoffs = [60, 3600, 86400, Infinity];
// Array equivalent to the above but in the string representation of the units
const units: Intl.RelativeTimeFormatUnit[] = ["second", "minute", "hour"];

const unitIndex = cutoffs.findIndex(cutoff => cutoff > Math.abs(deltaSeconds));
// Get the divisor to divide from the seconds. E.g. if our unit is "day" our divisor
// is one day in seconds, so we can divide our seconds by this to get the # of days
const divisor = unitIndex ? cutoffs[unitIndex - 1] : 1;

const rtf = new Intl.RelativeTimeFormat($locale.value, { numeric: "auto" });
return rtf.format(Math.floor(deltaSeconds / divisor), units[unitIndex]);
}

const updatedAt = ref();
const { resume, pause } = useIntervalFn(() => {
updatedAt.value = getRelativeTimeString(spacesStore.updatedAt)
}, 5000);

onMounted(() => { resume(); })
onUnmounted(() => { pause(); })

function clearFilters() {
spacesStore.clearFilters();
}
Expand Down Expand Up @@ -262,4 +295,10 @@ function clearFilters() {
.filter-menu__buttons .button--primary {
margin: 0 0 0 var(--spacing-half);
}

.filter-menu__legend-meta {
display: block;
font-family: var(--font-body);
margin-bottom: var(--spacing-quarter);
}
</style>
3 changes: 2 additions & 1 deletion src/data/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,6 @@
"occupancy.quiet": "quiet",
"occupancy.busy": "busy",
"occupancy.crowded": "crowded",
"occupancy.unknown": "unknown"
"occupancy.unknown": "unknown",
"updated": "Updated"
}
3 changes: 2 additions & 1 deletion src/data/nl/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,6 @@
"occupancy.quiet": "rustig",
"occupancy.busy": "druk",
"occupancy.crowded": "vol",
"occupancy.unknown": "onbekend"
"occupancy.unknown": "onbekend",
"updated": "Laatst bijgewerkt"
}
3 changes: 3 additions & 0 deletions src/stores/spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type AssociatedSpace = {
export const useSpacesStore = defineStore("spaces", () => {
const currentSelection = ref<Selection>(undefined);
const currentAssociatedSpaces = ref<AssociatedSpace[]>([]);
const updatedAt = ref();

const currentSpace = computed(() =>
currentSelection.value && currentSelection.value.level == "space"
Expand Down Expand Up @@ -114,6 +115,7 @@ export const useSpacesStore = defineStore("spaces", () => {
return { ...building, activeDevices, occupancy };
});
mapStore.updateData();
updatedAt.value = new Date();
}

function setBuildingOccupancy(
Expand Down Expand Up @@ -294,5 +296,6 @@ export const useSpacesStore = defineStore("spaces", () => {
buildingsI18n, //These need to be exported to be passed as payload from server to client
roomsI18n,
spacesI18n,
updatedAt,
};
});