ConceptPortal-public/rsconcept/frontend/src/pages/RSFormPage/RSFormTabs.tsx

139 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-07-15 17:46:19 +03:00
import { Tabs, TabList, TabPanel } from 'react-tabs';
import ConstituentsTable from './ConstituentsTable';
2023-07-20 17:11:03 +03:00
import { IConstituenta } from '../../utils/models';
2023-07-15 17:46:19 +03:00
import { useRSForm } from '../../context/RSFormContext';
2023-07-21 00:09:05 +03:00
import { useEffect, useState } from 'react';
2023-07-15 17:46:19 +03:00
import ConceptTab from '../../components/Common/ConceptTab';
import RSFormCard from './RSFormCard';
import { Loader } from '../../components/Common/Loader';
import BackendError from '../../components/BackendError';
import ConstituentEditor from './ConstituentEditor';
import RSFormStats from './RSFormStats';
import useLocalStorage from '../../hooks/useLocalStorage';
2023-07-20 17:11:03 +03:00
import TablistTools from './TablistTools';
2023-07-15 17:46:19 +03:00
enum TabsList {
2023-07-15 17:46:19 +03:00
CARD = 0,
CST_LIST = 1,
CST_EDIT = 2
}
function RSFormTabs() {
const { setActive, active, error, schema, loading } = useRSForm();
const [tabIndex, setTabIndex] = useLocalStorage('rsform_edit_tab', TabsList.CARD);
2023-07-21 00:09:05 +03:00
const [init, setInit] = useState(false);
2023-07-15 17:46:19 +03:00
const onEditCst = (cst: IConstituenta) => {
console.log(`Set active cst: ${cst.alias}`);
setActive(cst);
setTabIndex(TabsList.CST_EDIT)
2023-07-15 17:46:19 +03:00
};
const onSelectTab = (index: number) => {
setTabIndex(index);
};
useEffect(() => {
2023-07-21 00:09:05 +03:00
if (schema) {
const url = new URL(window.location.href);
const activeQuery = url.searchParams.get('active');
const activeCst = schema?.items?.find((cst) => cst.entityUID === Number(activeQuery)) || undefined;
setActive(activeCst);
setInit(true);
}
}, [setActive, schema, setInit]);
2023-07-18 14:55:40 +03:00
// const [ locationKeys, setLocationKeys ] = useState([])
// const history = useHistory()
// useEffect(() => {
// return history.listen(location => {
// if (history.action === 'PUSH') {
// setLocationKeys([ location.key ])
// }
// if (history.action === 'POP') {
// if (locationKeys[1] === location.key) {
// setLocationKeys(([ _, ...keys ]) => keys)
// // Handle forward event
// } else {
// setLocationKeys((keys) => [ location.key, ...keys ])
// // Handle back event
// }
// }
// })
// }, [ locationKeys, ])
2023-07-18 14:55:40 +03:00
useEffect(() => {
const url = new URL(window.location.href);
const tabQuery = url.searchParams.get('tab');
setTabIndex(Number(tabQuery) || TabsList.CARD);
}, [setTabIndex]);
useEffect(() => {
2023-07-21 00:09:05 +03:00
if (init) {
const url = new URL(window.location.href);
let currentActive = url.searchParams.get('active');
const currentTab = url.searchParams.get('tab');
const saveHistory = tabIndex === TabsList.CST_EDIT && currentActive !== String(active?.entityUID);
if (currentTab !== String(tabIndex)) {
url.searchParams.set('tab', String(tabIndex));
}
2023-07-21 00:09:05 +03:00
if (active) {
if (currentActive !== String(active.entityUID)) {
url.searchParams.set('active', String(active.entityUID));
}
2023-07-21 00:09:05 +03:00
} else {
url.searchParams.delete('active');
}
if (saveHistory) {
window.history.pushState(null, '', url.toString());
} else {
window.history.replaceState(null, '', url.toString());
}
}
2023-07-21 00:09:05 +03:00
}, [tabIndex, active, init]);
2023-07-15 17:46:19 +03:00
return (
2023-07-20 17:11:03 +03:00
<div className='w-full'>
2023-07-15 17:46:19 +03:00
{ loading && <Loader /> }
{ error && <BackendError error={error} />}
{ schema && !loading &&
2023-07-20 17:11:03 +03:00
<Tabs
selectedIndex={tabIndex}
onSelect={onSelectTab}
defaultFocus={true}
selectedTabClassName='font-bold'
>
<TabList className='flex items-start bg-gray-100 w-fit dark:bg-gray-600'>
<TablistTools />
<ConceptTab>Паспорт схемы</ConceptTab>
<ConceptTab className='border-gray-300 border-x-2 dark:border-gray-400 min-w-[10rem] flex justify-between gap-2'>
<span>Конституенты</span>
<span>{`${schema.stats?.count_errors} | ${schema.stats?.count_all}`}</span>
</ConceptTab>
<ConceptTab>Редактор</ConceptTab>
</TabList>
2023-07-15 17:46:19 +03:00
2023-07-20 17:11:03 +03:00
<TabPanel className='flex items-start w-full gap-2'>
<RSFormCard />
{schema.stats && <RSFormStats stats={schema.stats}/>}
</TabPanel>
2023-07-15 17:46:19 +03:00
2023-07-20 17:11:03 +03:00
<TabPanel className='w-full'>
<ConstituentsTable onOpenEdit={onEditCst} />
</TabPanel>
2023-07-15 17:46:19 +03:00
2023-07-20 17:11:03 +03:00
<TabPanel>
<ConstituentEditor />
</TabPanel>
</Tabs>
2023-07-15 17:46:19 +03:00
}
</div>);
}
export default RSFormTabs;