Advertisement
bruno83

working for group docs - path

Jan 19th, 2025
17
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, 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 { getFiles } from '../../../store/file/fileActions';
  13. import { selectFiles } from '../../../store/file/fileSlice';
  14. import {
  15.   getGroupDocuments,
  16.   updateGroupDocument,
  17.   getNotesForDocumentImport,
  18.   getDocumentLocationDetails,
  19. } from '../../../store/group/groupActions';
  20. import { selectGroupDocuments, selectGroupNotes } from '../../../store/group/groupSlice';
  21. import { selectUser } from '../../../store/user/userSlice';
  22.  
  23. const Edit = ({ isLoading }) => {
  24.   const { assetId, documentId } = useParams();
  25.   const { t } = useTranslation();
  26.   const groupDocuments = useSelector(selectGroupDocuments);
  27.   const currentUser = useSelector(selectUser);
  28.   const groupNotes = useSelector(selectGroupNotes);
  29.   const { exportDocument } = useExportToFiles(assetId);
  30.   const files = useSelector(selectFiles);
  31.   const dispatch = useDispatch();
  32.   const [hasChanges, setHasChanges] = useState(false);
  33.   const [locationDetails, setLocationDetails] = useState(null);
  34.  
  35.   useEffect(() => {
  36.     dispatch(getFiles({ parentId: assetId, skipParams: true }));
  37.     dispatch(getNotesForDocumentImport(assetId));
  38.     dispatch(getGroupDocuments(assetId));
  39.  
  40.     const fetchLocationDetails = async () => {
  41.       try {
  42.         const response = await dispatch(getDocumentLocationDetails(assetId)).unwrap(); // Dohvaćanje detalja o lokaciji
  43.         setLocationDetails(response.locationDetails || {}); // Čuvanje detalja o lokaciji
  44.       } catch (error) {
  45.         console.error('Greška prilikom preuzimanja lokacijskih detalja:', error);
  46.         setLocationDetails({ name: 'Unknown Location', image_url: null }); // Fallback vrednost
  47.       }
  48.     };
  49.  
  50.     fetchLocationDetails();
  51.   }, [assetId, dispatch]);
  52.  
  53.   const specificDocument = useMemo(() => {
  54.     return groupDocuments.find((document) => document.id === parseInt(documentId));
  55.   }, [groupDocuments, documentId]);
  56.  
  57.   const translations = {
  58.     updatedBy: t('note.audit.updatedBy'),
  59.   };
  60.  
  61.   const handleExportNote = async () => {
  62.     const element = (
  63.       <DocumentExport
  64.         document={specificDocument}
  65.         tenant={currentUser.tenant}
  66.         translations={translations}
  67.         locationDetails={locationDetails}
  68.       />
  69.     );
  70.     const pdfBlob = await pdf(element).toBlob();
  71.  
  72.     const url = URL.createObjectURL(pdfBlob);
  73.     window.open(url, '_blank');
  74.  
  75.     exportDocument(specificDocument, pdfBlob, files);
  76.   };
  77.  
  78.   return (
  79.     <Card
  80.       title={
  81.         <div className='flex items-center justify-between'>
  82.           <div className='flex flex-row gap-3'>
  83.             <Link to={-1}>
  84.               <ArrowLeftOutlined />
  85.             </Link>
  86.             {t('document.edit.title')}
  87.           </div>
  88.           <Button
  89.             type='primary'
  90.             htmlType='button'
  91.             loading={isLoading}
  92.             onClick={handleExportNote}
  93.             disabled={hasChanges}
  94.           >
  95.             {t('button.exportDocument')}
  96.           </Button>
  97.         </div>
  98.       }
  99.     >
  100.  
  101.       {locationDetails?.name && (
  102.         <div style={{ marginBottom: '16px', color: '#6c757d', fontSize: '12px' }}>
  103.           <p>{`${t('document.audit.location')}: ${locationDetails.name}`}</p>
  104.           {locationDetails.image_url && (
  105.             <img
  106.               src={locationDetails.image_url}
  107.               alt='Location'
  108.               style={{ width: '100px', height: 'auto', marginTop: '8px' }}
  109.             />
  110.           )}
  111.         </div>
  112.       )}
  113.       <DocumentForm
  114.         parentId={assetId}
  115.         contextId={documentId}
  116.         document={specificDocument?.content}
  117.         isEditing={true}
  118.         updateAction={updateGroupDocument}
  119.         contextNotes={groupNotes || []}
  120.         getContextNotes={getNotesForDocumentImport}
  121.         hasChanges={hasChanges}
  122.         setHasChanges={setHasChanges}
  123.         fetchDocuments={getGroupDocuments}
  124.       />
  125.     </Card>
  126.   );
  127. };
  128.  
  129. export default withLoadStatus(Edit); */
  130.  
  131. import { ArrowLeftOutlined } from '@ant-design/icons';
  132. import { pdf } from '@react-pdf/renderer';
  133. import { Button, Card } from 'antd';
  134. import React, { useEffect, useMemo, useState } from 'react';
  135. import { useTranslation } from 'react-i18next';
  136. import { useDispatch, useSelector } from 'react-redux';
  137. import { Link, useLocation, useParams } from 'react-router-dom';
  138. import withLoadStatus from '../../../components/common/withLoadStatus';
  139. import DocumentExport from '../../../components/DocumentExport';
  140. import DocumentForm from '../../../components/forms/DocumentForm';
  141. import useExportToFiles from '../../../hooks/useExportToFiles';
  142. import { getFiles } from '../../../store/file/fileActions';
  143. import { selectFiles } from '../../../store/file/fileSlice';
  144. import {
  145.   getGroupDocuments,
  146.   updateGroupDocument,
  147.   getNotesForDocumentImport,
  148.   getDocumentLocationDetails,
  149.   getCompanyAssets,
  150. } from '../../../store/group/groupActions';
  151. import { selectGroupDocuments, selectGroupNotes } from '../../../store/group/groupSlice';
  152. import { selectUser } from '../../../store/user/userSlice';
  153.  
  154. const Edit = ({ isLoading }) => {
  155.   const { assetId, documentId, companyId } = useParams(); // Dohvati i companyId
  156.   const { t } = useTranslation();
  157.   const location = useLocation();
  158.   const dispatch = useDispatch();
  159.  
  160.   const groupDocuments = useSelector(selectGroupDocuments);
  161.   const currentUser = useSelector(selectUser);
  162.   const groupNotes = useSelector(selectGroupNotes);
  163.   const files = useSelector(selectFiles);
  164.   const [assetTree, setAssetTree] = useState([]); // Dodaj stanje za assetTree
  165.   const [hasChanges, setHasChanges] = useState(false);
  166.   const [locationDetails, setLocationDetails] = useState(null);
  167.  
  168.   const { exportDocument } = useExportToFiles(assetId);
  169.  
  170.   // Funkcija za dohvaćanje "assets"
  171.   useEffect(() => {
  172.     const fetchAssets = async () => {
  173.       try {
  174.         const response = await dispatch(getCompanyAssets(companyId)).unwrap();
  175.         console.log('Fetched Assets:', response.assets); // Debug log
  176.         setAssetTree(response.assets);
  177.       } catch (error) {
  178.         console.error('Error fetching assets:', error);
  179.       }
  180.     };
  181.  
  182.     fetchAssets();
  183.   }, [companyId, dispatch]);
  184.  
  185.   // Funkcija za generiranje cijele putanje
  186.   const generatePath = () => {
  187.     const pathSnippets = location.pathname.split('/').filter((i) => i);
  188.  
  189.     const typeMappings = {
  190.       companies: 'Company',
  191.       groups: 'Group',
  192.       assets: 'Asset',
  193.       workspaces: 'Workspace',
  194.     };
  195.  
  196.     let pathNames = [];
  197.  
  198.     pathSnippets.forEach((snippet, index, array) => {
  199.       const type = array[index - 1]; // Tip prethodnog segmenta rute
  200.       const id = snippet;
  201.  
  202.       let name = '';
  203.       switch (type) {
  204.         case 'companies': {
  205.           // Dohvaćanje kompanije iz locationDetails ili companyDetails
  206.           const company = locationDetails?.parent || { name: 'Unknown Company' };
  207.           if (company) name = `${company.name} (${typeMappings[type]})`;
  208.           break;
  209.         }
  210.         case 'groups':
  211.         case 'assets':
  212.         case 'workspaces': {
  213.           const asset = assetTree.find((asset) => asset.id === +id);
  214.           if (asset) name = `${asset.name} (${typeMappings[type]})`;
  215.           break;
  216.         }
  217.         default:
  218.           break;
  219.       }
  220.  
  221.       if (name) {
  222.         pathNames.push(name);
  223.       }
  224.     });
  225.  
  226.     return pathNames.reverse().join(' -> '); // Reverse za hijerarhijski prikaz
  227.   };
  228.  
  229.   // Dohvaćanje datoteka, bilješki i dokumenata
  230.   useEffect(() => {
  231.     dispatch(getFiles({ parentId: assetId, skipParams: true }));
  232.     dispatch(getNotesForDocumentImport(assetId));
  233.     dispatch(getGroupDocuments(assetId));
  234.  
  235.     const fetchLocationDetails = async () => {
  236.       try {
  237.         const response = await dispatch(getDocumentLocationDetails(assetId)).unwrap();
  238.         setLocationDetails(response.locationDetails || {});
  239.       } catch (error) {
  240.         console.error('Greška prilikom preuzimanja lokacijskih detalja:', error);
  241.         setLocationDetails({ name: 'Unknown Location', image_url: null });
  242.       }
  243.     };
  244.  
  245.     fetchLocationDetails();
  246.   }, [assetId, dispatch]);
  247.  
  248.   const specificDocument = useMemo(() => {
  249.     return groupDocuments.find((document) => document.id === parseInt(documentId));
  250.   }, [groupDocuments, documentId]);
  251.  
  252.   const translations = {
  253.     updatedBy: t('note.audit.updatedBy'),
  254.   };
  255.  
  256.   const completePath = generatePath(); // Generiranje cijele putanje
  257.  
  258.   const handleExportNote = async () => {
  259.     const element = (
  260.       <DocumentExport
  261.         document={specificDocument}
  262.         tenant={currentUser.tenant}
  263.         translations={translations}
  264.         locationDetails={locationDetails}
  265.         completePath={completePath} // Prosljeđivanje putanje
  266.       />
  267.     );
  268.     const pdfBlob = await pdf(element).toBlob();
  269.  
  270.     const url = URL.createObjectURL(pdfBlob);
  271.     window.open(url, '_blank');
  272.  
  273.     exportDocument(specificDocument, pdfBlob, files);
  274.   };
  275.  
  276.   return (
  277.     <Card
  278.       title={
  279.         <div className='flex items-center justify-between'>
  280.           <div className='flex flex-row gap-3'>
  281.             <Link to={-1}>
  282.               <ArrowLeftOutlined />
  283.             </Link>
  284.             {t('document.edit.title')}
  285.           </div>
  286.           <Button
  287.             type='primary'
  288.             htmlType='button'
  289.             loading={isLoading}
  290.             onClick={handleExportNote}
  291.             disabled={hasChanges}
  292.           >
  293.             {t('button.exportDocument')}
  294.           </Button>
  295.         </div>
  296.       }
  297.     >
  298.       {/* Prikaz informacija o lokaciji */}
  299.       {locationDetails?.name && (
  300.         <div style={{ marginBottom: '16px', color: '#6c757d', fontSize: '12px' }}>
  301.           <p>{`${t('document.audit.location')}: ${locationDetails.name}`}</p>
  302.           {locationDetails.image_url && (
  303.             <img
  304.               src={locationDetails.image_url}
  305.               alt='Location'
  306.               style={{ width: '100px', height: 'auto', marginTop: '8px' }}
  307.             />
  308.           )}
  309.         </div>
  310.       )}
  311.       {/* Prikaz generirane putanje */}
  312.       {completePath && (
  313.         <div style={{ marginBottom: '16px', color: '#6c757d', fontSize: '12px' }}>
  314.           <p>{`${t('document.audit.path')}: ${completePath}`}</p>
  315.         </div>
  316.       )}
  317.       <DocumentForm
  318.         parentId={assetId}
  319.         contextId={documentId}
  320.         document={specificDocument?.content}
  321.         isEditing={true}
  322.         updateAction={updateGroupDocument}
  323.         contextNotes={groupNotes || []}
  324.         getContextNotes={getNotesForDocumentImport}
  325.         hasChanges={hasChanges}
  326.         setHasChanges={setHasChanges}
  327.         fetchDocuments={getGroupDocuments}
  328.       />
  329.     </Card>
  330.   );
  331. };
  332.  
  333. export default withLoadStatus(Edit);
  334.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement