167 lines
6.0 KiB
TypeScript
167 lines
6.0 KiB
TypeScript
import { mdiChartTimelineVariant } from '@mdi/js'
|
|
import Head from 'next/head'
|
|
import { uniqueId } from 'lodash';
|
|
import React, { ReactElement, useState } from 'react'
|
|
import CardBox from '../../components/CardBox'
|
|
import LayoutAuthenticated from '../../layouts/Authenticated'
|
|
import SectionMain from '../../components/SectionMain'
|
|
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton'
|
|
import LegalOpsPageIntro from '../../components/LegalOpsPageIntro'
|
|
import { getPageTitle } from '../../config'
|
|
import TableTraining_courses from '../../components/Training_courses/TableTraining_courses'
|
|
import BaseButton from '../../components/BaseButton'
|
|
import axios from "axios";
|
|
import {useAppDispatch, useAppSelector} from "../../stores/hooks";
|
|
import CardBoxModal from "../../components/CardBoxModal";
|
|
import DragDropFilePicker from "../../components/DragDropFilePicker";
|
|
import {setRefetch, uploadCsv} from '../../stores/training_courses/training_coursesSlice';
|
|
|
|
|
|
import {hasPermission} from "../../helpers/userPermissions";
|
|
|
|
|
|
|
|
const Training_coursesTablesPage = () => {
|
|
const [filterItems, setFilterItems] = useState([]);
|
|
const [csvFile, setCsvFile] = useState<File | null>(null);
|
|
const [isModalActive, setIsModalActive] = useState(false);
|
|
const [showTableView, setShowTableView] = useState(false);
|
|
|
|
|
|
const { currentUser } = useAppSelector((state) => state.auth);
|
|
|
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
|
|
const [filters] = useState([{label: 'Course name', title: 'name'},{label: 'Description', title: 'description'},{label: 'Link', title: 'link'},
|
|
{label: 'Duration minutes', title: 'duration_minutes', number: 'true'},{label: 'Validity days', title: 'validity_days', number: 'true'},
|
|
|
|
|
|
|
|
|
|
{label: 'Delivery type', title: 'delivery_type', type: 'enum', options: ['video','live','document','lms_link']},{label: 'Status', title: 'status', type: 'enum', options: ['draft','active','archived']},
|
|
]);
|
|
|
|
const hasCreatePermission = currentUser && hasPermission(currentUser, 'CREATE_TRAINING_COURSES');
|
|
|
|
|
|
const addFilter = () => {
|
|
const newItem = {
|
|
id: uniqueId(),
|
|
fields: {
|
|
filterValue: '',
|
|
filterValueFrom: '',
|
|
filterValueTo: '',
|
|
selectedField: '',
|
|
},
|
|
};
|
|
newItem.fields.selectedField = filters[0].title;
|
|
setFilterItems([...filterItems, newItem]);
|
|
};
|
|
|
|
const getTraining_coursesCSV = async () => {
|
|
const response = await axios({url: '/training_courses?filetype=csv', method: 'GET',responseType: 'blob'});
|
|
const type = response.headers['content-type']
|
|
const blob = new Blob([response.data], { type: type })
|
|
const link = document.createElement('a')
|
|
link.href = window.URL.createObjectURL(blob)
|
|
link.download = 'training_coursesCSV.csv'
|
|
link.click()
|
|
};
|
|
|
|
const onModalConfirm = async () => {
|
|
if (!csvFile) return;
|
|
await dispatch(uploadCsv(csvFile));
|
|
dispatch(setRefetch(true));
|
|
setCsvFile(null);
|
|
setIsModalActive(false);
|
|
};
|
|
|
|
const onModalCancel = () => {
|
|
setCsvFile(null);
|
|
setIsModalActive(false);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<Head>
|
|
<title>{getPageTitle('Training courses table')}</title>
|
|
</Head>
|
|
<SectionMain>
|
|
<SectionTitleLineWithButton icon={mdiChartTimelineVariant} title="Training courses table" main>
|
|
{''}
|
|
</SectionTitleLineWithButton>
|
|
<LegalOpsPageIntro
|
|
eyebrow='AI adoption training'
|
|
title='Training courses as an editable governance table'
|
|
description='Use table view to scan delivery type, validity, status, links, and attachments across the training catalog.'
|
|
metrics={[
|
|
{ label: 'View mode', value: 'Table', helper: 'Best for comparing course duration, status, and validity quickly.' },
|
|
{ label: 'Access control', value: 'Training evidence', helper: 'Courses support user readiness for approved AI tools.' },
|
|
{ label: 'Lifecycle', value: 'Draft / active', helper: 'Keep archived courses separate from active governance content.' },
|
|
]}
|
|
>
|
|
{hasCreatePermission && <BaseButton href={'/training_courses/training_courses-new'} color='info' label='New course'/>}
|
|
<BaseButton
|
|
color='info'
|
|
label='Add filter'
|
|
onClick={addFilter}
|
|
/>
|
|
<BaseButton color='whiteDark' label='Export CSV' onClick={getTraining_coursesCSV} />
|
|
|
|
{hasCreatePermission && (
|
|
<BaseButton
|
|
color='whiteDark'
|
|
label='Upload CSV'
|
|
onClick={() => setIsModalActive(true)}
|
|
/>
|
|
)}
|
|
|
|
<div className='md:inline-flex items-center ms-auto'>
|
|
<div id='delete-rows-button'></div>
|
|
</div>
|
|
<BaseButton href={'/training_courses/training_courses-list'} color='whiteDark' label='Card view'/>
|
|
</LegalOpsPageIntro>
|
|
<CardBox className="mb-6" hasTable>
|
|
<TableTraining_courses
|
|
filterItems={filterItems}
|
|
setFilterItems={setFilterItems}
|
|
filters={filters}
|
|
showGrid={true}
|
|
/>
|
|
</CardBox>
|
|
</SectionMain>
|
|
<CardBoxModal
|
|
title='Upload CSV'
|
|
buttonColor='info'
|
|
buttonLabel={'Confirm'}
|
|
// buttonLabel={false ? 'Deleting...' : 'Confirm'}
|
|
isActive={isModalActive}
|
|
onConfirm={onModalConfirm}
|
|
onCancel={onModalCancel}
|
|
>
|
|
<DragDropFilePicker
|
|
file={csvFile}
|
|
setFile={setCsvFile}
|
|
formats={'.csv'}
|
|
/>
|
|
</CardBoxModal>
|
|
</>
|
|
)
|
|
}
|
|
|
|
Training_coursesTablesPage.getLayout = function getLayout(page: ReactElement) {
|
|
return (
|
|
<LayoutAuthenticated
|
|
|
|
permission={'READ_TRAINING_COURSES'}
|
|
|
|
>
|
|
{page}
|
|
</LayoutAuthenticated>
|
|
)
|
|
}
|
|
|
|
export default Training_coursesTablesPage
|