Skip to content

A self-hosted media server and file management system

4.2 KB
Raw Blame
<script setup lang="ts">
import type { GenericSortOption } from '@/types/types';
import type { RecordResource } from '@/types/resources';
import type { SortDir } from '@aminnausin/cedar-ui';

import { computed, onMounted, ref } from 'vue';
import { useRecord, useRecords } from '@/service/records/useRecords';
import { useAppStore } from '@/stores/AppStore';
import { storeToRefs } from 'pinia';
import { sortObject } from '@/service/sort/baseSort';
import { TableBase } from '@/components/cedar-ui/table';
import { ModalBase } from '@/components/cedar-ui/modal';
import { toast } from '@aminnausin/cedar-ui';

import RecordCardDetails from '@/components/cards/data/RecordCardDetails.vue';
import TableSkeleton from '@/components/skeleton/composites/TableSkeleton.vue';
import LayoutBase from '@/layouts/LayoutBase.vue';
import useModal from '@/composables/useModal';

const recordSortingOptions: GenericSortOption<RecordResource>[] = [
    {
        title: 'Date',
        value: 'created_at',
        disabled: false,
    },
    {
        title: 'Title',
        value: 'video_name',
        disabled: false,
    },
    {
        title: 'Folder',
        value: 'folder_name',
        disabled: false,
    },
];

const { pageTitle, selectedSideBar } = storeToRefs(useAppStore());
const { data: stateRecords, isFetching: isLoading } = useRecords();
const { deleteRecord } = useRecord();

const confirmModal = useModal({ title: 'Delete Record?', submitText: 'Confim' });
const cachedID = ref<number | null>(null);

const recordsSortKey = ref<keyof RecordResource>(recordSortingOptions[0].value);
const recordsSortDir = ref<SortDir>(1);
const searchQuery = ref('');

const sortedRecords = computed<RecordResource[]>(() => {
    if (isLoading.value || !stateRecords.value) return [];
    return [...stateRecords.value].sort(sortObject<RecordResource>(recordsSortKey.value, recordsSortDir.value, ['created_at', 'updated_at']));
});

// Filters sorted records by search query
const filteredRecords = computed<RecordResource[]>(() => {
    const tempList = searchQuery.value
        ? sortedRecords.value.filter((record: RecordResource) => {
              try {
                  const strRepresentation = [record.video_name ?? record.file_name, record.folder_name, record.created_at, record.category?.name].join(' ').toLowerCase();
                  return strRepresentation.includes(searchQuery.value.toLowerCase());
              } catch (error) {
                  console.log(error);
                  return false;
              }
          })
        : sortedRecords.value;
    return tempList;
});

const handleDelete = (id: number) => {
    cachedID.value = id;
    confirmModal.toggleModal(true);
};

const submitDelete = async () => {
    if (cachedID.value) {
        const request = await deleteRecord.mutateAsync(cachedID.value);
        if (request) toast.add('Success', { type: 'success', description: 'Record Deleted Successfully!', life: 3000 });
        else toast.add('Error', { type: 'warning', description: 'Unable to delete record. Please try again.', life: 3000 });
    }
};

const handleSort = (sortKey: keyof RecordResource, sortDir: SortDir) => {
    recordsSortDir.value = sortDir;
    recordsSortKey.value = sortKey;
};

onMounted(() => {
    pageTitle.value = 'History';
    selectedSideBar.value = '';
});
</script>

<template>
    <LayoutBase>
        <template v-slot:content>
            <section id="content-history" class="page-height flex flex-col space-y-2">
                <TableBase
                    :class="'flex-1'"
                    :data="filteredRecords"
                    :row="RecordCardDetails"
                    :clickAction="handleDelete"
                    :loading="isLoading"
                    :useToolbar="true"
                    :sort-action="handleSort"
                    :sortingOptions="recordSortingOptions"
                    :loading-placeholder="TableSkeleton"
                    v-model="searchQuery"
                />
            </section>
            <ModalBase :modalData="confirmModal" :action="submitDelete">
                <template #description> Are you sure you want to delete this record? </template>
            </ModalBase>
        </template>
        <template v-slot:sidebar> </template>
    </LayoutBase>
</template>