Advertisement
bruno83

edit kompanija path radi

Jan 19th, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { ArrowLeftOutlined } from '@ant-design/icons';
  2. import { pdf } from '@react-pdf/renderer';
  3. import { Button, Card } from 'antd';
  4. import React, { useEffect, useMemo, useState } from 'react';
  5. import { useTranslation } from 'react-i18next';
  6. import { useDispatch, useSelector } from 'react-redux';
  7. import { Link, useLocation, useParams } from 'react-router-dom';
  8. import withLoadStatus from '../../../components/common/withLoadStatus';
  9. import DocumentExport from '../../../components/DocumentExport';
  10. import DocumentForm from '../../../components/forms/DocumentForm';
  11. import useExportToFiles from '../../../hooks/useExportToFiles';
  12. import {
  13.   getDocumentLocationDetails,
  14.   getCompanyDocuments,
  15.   getNotesForDocumentImport,
  16.   updateCompanyDocument,
  17. } from '../../../store/company/companyActions';
  18. import { selectCompanyDocuments, selectCompanyNotes } from '../../../store/company/companySlice';
  19. import { selectFiles } from '../../../store/file/fileSlice';
  20. import { selectUser } from '../../../store/user/userSlice';
  21. import { getFiles } from '../../../store/file/fileActions';
  22. import { findAssetById } from '../../../utils/groupTreeUtils';
  23.  
  24. const Edit = ({ isLoading }) => {
  25.   const { companyId, documentId } = useParams();
  26.   const { t } = useTranslation();
  27.   const location = useLocation();
  28.   const dispatch = useDispatch();
  29.  
  30.   const companyDocuments = useSelector(selectCompanyDocuments);
  31.   const currentUser = useSelector(selectUser);
  32.   const companyNotes = useSelector(selectCompanyNotes);
  33.   const files = useSelector(selectFiles);
  34.   const assetTree = useSelector((state) => state.company.assetTree);
  35.   const companies = useSelector((state) => state.company.companies);
  36.  
  37.   const { exportDocument } = useExportToFiles(companyId);
  38.   const [hasChanges, setHasChanges] = useState(false);
  39.   const [locationDetails, setLocationDetails] = useState(null);
  40.  
  41.   // Funkcija za generiranje hijerarhijskog puta
  42.   const generatePath = () => {
  43.     const pathSnippets = location.pathname.split('/').filter((i) => i);
  44.  
  45.     // Mapiranje tipova na ljudski čitljive nazive
  46.     const typeMappings = {
  47.       companies: 'Company',
  48.       groups: 'Group',
  49.       assets: 'Asset',
  50.       workspaces: 'Workspace',
  51.     };
  52.  
  53.     let pathNames = [];
  54.  
  55.     pathSnippets.forEach((snippet, index, array) => {
  56.       const type = array[index - 1]; // Tip prethodnog segmenta rute
  57.       const id = snippet;
  58.  
  59.       let name = '';
  60.       switch (type) {
  61.         case 'companies': {
  62.           const company = companies.find((company) => company.id === +id);
  63.           if (company) name = `${company.name} (${typeMappings[type]})`;
  64.           break;
  65.         }
  66.         case 'groups':
  67.         case 'assets':
  68.         case 'workspaces': {
  69.           const asset = findAssetById(assetTree, +id);
  70.           if (asset) name = `${asset.value.name} (${typeMappings[type]})`;
  71.           break;
  72.         }
  73.         default:
  74.           break;
  75.       }
  76.  
  77.       if (name) {
  78.         pathNames.push(name);
  79.       }
  80.     });
  81.  
  82.     return pathNames.join(' -> ');
  83.   };
  84.  
  85.   useEffect(() => {
  86.     dispatch(getFiles({ parentId: companyId, skipParams: true }));
  87.     dispatch(getNotesForDocumentImport(companyId));
  88.  
  89.     const fetchLocationDetails = async () => {
  90.       const response = await dispatch(getDocumentLocationDetails(companyId)).unwrap();
  91.       setLocationDetails(response.locationDetails);
  92.     };
  93.  
  94.     fetchLocationDetails();
  95.   }, [companyId, dispatch]);
  96.  
  97.   const specificDocument = useMemo(() => {
  98.     return companyDocuments.find((document) => document.id === parseInt(documentId));
  99.   }, [companyDocuments, documentId]);
  100.  
  101.   const translations = {
  102.     updatedBy: t('note.audit.updatedBy'),
  103.   };
  104.  
  105.   const completePath = generatePath();
  106.  
  107.   const handleExportNote = async () => {
  108.     const element = (
  109.       <DocumentExport
  110.         document={specificDocument}
  111.         tenant={currentUser.tenant}
  112.         translations={translations}
  113.         locationDetails={locationDetails}
  114.         completePath={completePath} // Prosljeđivanje pročišćenog puta
  115.       />
  116.     );
  117.     const pdfBlob = await pdf(element).toBlob();
  118.  
  119.     const url = URL.createObjectURL(pdfBlob);
  120.     window.open(url, '_blank');
  121.  
  122.     exportDocument(specificDocument, pdfBlob, files);
  123.   };
  124.  
  125.   return (
  126.     <Card
  127.       title={
  128.         <div className='flex items-center justify-between'>
  129.           <div className='flex flex-row gap-3'>
  130.             <Link to={-1}>
  131.               <ArrowLeftOutlined />
  132.             </Link>
  133.             {t('document.edit.title')}
  134.           </div>
  135.           <Button
  136.             type='primary'
  137.             htmlType='button'
  138.             loading={isLoading}
  139.             onClick={handleExportNote}
  140.             disabled={hasChanges}
  141.           >
  142.             {t('button.exportDocument')}
  143.           </Button>
  144.         </div>
  145.       }
  146.     >
  147.       {/* Kontrolni dio - prikaz informacija o lokaciji */}
  148.       {locationDetails && (
  149.         <div style={{ marginBottom: '16px', color: '#6c757d', fontSize: '12px' }}>
  150.           <p>{`${t('document.audit.location')}: ${locationDetails.name}`}</p>
  151.           {locationDetails.image_url && (
  152.             <img
  153.               src={locationDetails.image_url}
  154.               alt='Location'
  155.               style={{ width: '100px', height: 'auto', marginTop: '8px' }}
  156.             />
  157.           )}
  158.         </div>
  159.       )}
  160.       <DocumentForm
  161.         parentId={companyId}
  162.         contextId={documentId}
  163.         document={specificDocument?.content}
  164.         isEditing={true}
  165.         updateAction={updateCompanyDocument}
  166.         contextNotes={companyNotes || []}
  167.         getContextNotes={getNotesForDocumentImport}
  168.         hasChanges={hasChanges}
  169.         setHasChanges={setHasChanges}
  170.         fetchDocuments={getCompanyDocuments}
  171.       />
  172.     </Card>
  173.   );
  174. };
  175.  
  176. export default withLoadStatus(Edit);
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement