mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-09 08:42:15 +01:00
178 lines
7.3 KiB
Vue
178 lines
7.3 KiB
Vue
<script setup lang="ts">
|
|
import AppLayout from '@/Layouts/AppLayout.vue';
|
|
import TimeTracker from '@/Components/TimeTracker.vue';
|
|
import { computed, onMounted, ref, watch } from 'vue';
|
|
import MainContainer from '@/Pages/MainContainer.vue';
|
|
import {
|
|
type TimeEntriesGroupedByType,
|
|
useTimeEntriesStore,
|
|
} from '@/utils/useTimeEntries';
|
|
import { storeToRefs } from 'pinia';
|
|
import type { TimeEntry } from '@/utils/api';
|
|
import TimeEntryRowHeading from '@/Components/Common/TimeEntry/TimeEntryRowHeading.vue';
|
|
import TimeEntryRow from '@/Components/Common/TimeEntry/TimeEntryRow.vue';
|
|
import { useElementVisibility } from '@vueuse/core';
|
|
import { ClockIcon } from '@heroicons/vue/20/solid';
|
|
import { getDayJsInstance, getLocalizedDateFromTimestamp } from '@/utils/time';
|
|
import SecondaryButton from '@/Components/SecondaryButton.vue';
|
|
import { PlusIcon } from '@heroicons/vue/16/solid';
|
|
import TimeEntryCreateModal from '@/Components/Common/TimeEntry/TimeEntryCreateModal.vue';
|
|
import TimeEntryAggregateRow from '@/Components/Common/TimeEntry/TimeEntryAggregateRow.vue';
|
|
|
|
const timeEntriesStore = useTimeEntriesStore();
|
|
const { timeEntries, allTimeEntriesLoaded } = storeToRefs(timeEntriesStore);
|
|
|
|
const loading = ref(false);
|
|
const loadMoreContainer = ref<HTMLDivElement | null>(null);
|
|
const isLoadMoreVisible = useElementVisibility(loadMoreContainer);
|
|
|
|
watch(isLoadMoreVisible, async (isVisible) => {
|
|
if (
|
|
isVisible &&
|
|
timeEntries.value.length > 0 &&
|
|
!allTimeEntriesLoaded.value
|
|
) {
|
|
loading.value = true;
|
|
await timeEntriesStore.fetchMoreTimeEntries();
|
|
}
|
|
});
|
|
|
|
onMounted(async () => {
|
|
await timeEntriesStore.fetchTimeEntries();
|
|
});
|
|
|
|
const groupedTimeEntries = computed(() => {
|
|
const groupedEntriesByDay: Record<string, TimeEntry[]> = {};
|
|
for (const entry of timeEntries.value) {
|
|
const oldEntries =
|
|
groupedEntriesByDay[getLocalizedDateFromTimestamp(entry.start)];
|
|
groupedEntriesByDay[getLocalizedDateFromTimestamp(entry.start)] = [
|
|
...(oldEntries ?? []),
|
|
entry,
|
|
];
|
|
}
|
|
const groupedEntriesByDayAndType: Record<
|
|
string,
|
|
TimeEntriesGroupedByType[]
|
|
> = {};
|
|
for (const dailyEntriesKey in groupedEntriesByDay) {
|
|
const dailyEntries = groupedEntriesByDay[dailyEntriesKey];
|
|
const newDailyEntries: TimeEntriesGroupedByType[] = [];
|
|
|
|
for (const entry of dailyEntries) {
|
|
// check if same entry already exists
|
|
const oldEntriesIndex = newDailyEntries.findIndex(
|
|
(e) =>
|
|
e.project_id === entry.project_id &&
|
|
e.task_id === entry.task_id &&
|
|
e.billable === entry.billable &&
|
|
e.description === entry.description
|
|
);
|
|
if (oldEntriesIndex !== -1 && newDailyEntries[oldEntriesIndex]) {
|
|
newDailyEntries[oldEntriesIndex].timeEntries.push(entry);
|
|
|
|
// Add up durations for time entries of the same type
|
|
newDailyEntries[oldEntriesIndex].duration =
|
|
(newDailyEntries[oldEntriesIndex].duration ?? 0) +
|
|
(entry?.duration ?? 0);
|
|
|
|
// adapt start end times so they show the earliest start and latest end time
|
|
if (
|
|
getDayJsInstance()(entry.start).isBefore(
|
|
getDayJsInstance()(
|
|
newDailyEntries[oldEntriesIndex].start
|
|
)
|
|
)
|
|
) {
|
|
newDailyEntries[oldEntriesIndex].start = entry.start;
|
|
}
|
|
if (
|
|
getDayJsInstance()(entry.end).isAfter(
|
|
getDayJsInstance()(newDailyEntries[oldEntriesIndex].end)
|
|
)
|
|
) {
|
|
newDailyEntries[oldEntriesIndex].end = entry.end;
|
|
}
|
|
} else {
|
|
newDailyEntries.push({ ...entry, timeEntries: [entry] });
|
|
}
|
|
}
|
|
|
|
groupedEntriesByDayAndType[dailyEntriesKey] = newDailyEntries;
|
|
}
|
|
return groupedEntriesByDayAndType;
|
|
});
|
|
const showManualTimeEntryModal = ref(false);
|
|
</script>
|
|
|
|
<template>
|
|
<TimeEntryCreateModal
|
|
v-model:show="showManualTimeEntryModal"></TimeEntryCreateModal>
|
|
<AppLayout title="Dashboard" data-testid="time_view">
|
|
<MainContainer
|
|
class="pt-5 sm:pt-8 pb-4 sm:pb-6 border-b border-default-background-separator">
|
|
<div
|
|
class="sm:flex items-end sm:divide-x divide-default-background-separator divide-y sm:divide-y-0 space-y-2 sm:space-y-0 sm:space-x-2">
|
|
<div class="flex-1">
|
|
<TimeTracker></TimeTracker>
|
|
</div>
|
|
<div class="pb-2 pt-2 sm:pt-0 sm:pl-4 flex justify-center">
|
|
<SecondaryButton
|
|
class="w-full text-center flex justify-center"
|
|
@click="showManualTimeEntryModal = true"
|
|
:icon="PlusIcon"
|
|
>Manual time entry
|
|
</SecondaryButton>
|
|
</div>
|
|
</div>
|
|
</MainContainer>
|
|
<div v-for="(value, key) in groupedTimeEntries" :key="key">
|
|
<TimeEntryRowHeading :date="key"></TimeEntryRowHeading>
|
|
<template v-for="entry in value" :key="entry.id">
|
|
<TimeEntryAggregateRow
|
|
v-if="
|
|
'timeEntries' in entry && entry.timeEntries.length > 1
|
|
"
|
|
:time-entry="entry"></TimeEntryAggregateRow>
|
|
<TimeEntryRow v-else :time-entry="entry"></TimeEntryRow>
|
|
</template>
|
|
</div>
|
|
<div
|
|
v-if="Object.keys(groupedTimeEntries).length === 0"
|
|
class="text-center pt-12">
|
|
<ClockIcon class="w-8 text-icon-default inline pb-2"></ClockIcon>
|
|
<h3 class="text-white font-semibold">No time entries found</h3>
|
|
<p class="pb-5">Create your first time entry now!</p>
|
|
</div>
|
|
<div ref="loadMoreContainer">
|
|
<div
|
|
v-if="loading && !allTimeEntriesLoaded"
|
|
class="flex justify-center items-center py-5 text-white font-medium">
|
|
<svg
|
|
class="animate-spin -ml-1 mr-3 h-5 w-5 text-white"
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
fill="none"
|
|
viewBox="0 0 24 24">
|
|
<circle
|
|
class="opacity-25"
|
|
cx="12"
|
|
cy="12"
|
|
r="10"
|
|
stroke="currentColor"
|
|
stroke-width="4"></circle>
|
|
<path
|
|
class="opacity-75"
|
|
fill="currentColor"
|
|
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
|
</svg>
|
|
<span> Loading more time entries... </span>
|
|
</div>
|
|
<div
|
|
v-else-if="allTimeEntriesLoaded"
|
|
class="flex justify-center items-center py-5 text-muted font-medium">
|
|
All time entries are loaded!
|
|
</div>
|
|
</div>
|
|
</AppLayout>
|
|
</template>
|