Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { ArrowLeftOutlined } from '@ant-design/icons';
- import { pdf } from '@react-pdf/renderer';
- import { Button, Card } from 'antd';
- import React, { useEffect, useMemo, useState } from 'react';
- import { useTranslation } from 'react-i18next';
- import { useDispatch, useSelector } from 'react-redux';
- import { Link, useLocation, useParams } from 'react-router-dom';
- import withLoadStatus from '../../../components/common/withLoadStatus';
- import DocumentExport from '../../../components/DocumentExport';
- import DocumentForm from '../../../components/forms/DocumentForm';
- import useExportToFiles from '../../../hooks/useExportToFiles';
- import {
- getDocumentLocationDetails,
- getCompanyDocuments,
- getNotesForDocumentImport,
- updateCompanyDocument,
- } from '../../../store/company/companyActions';
- import { selectCompanyDocuments, selectCompanyNotes } from '../../../store/company/companySlice';
- import { selectFiles } from '../../../store/file/fileSlice';
- import { selectUser } from '../../../store/user/userSlice';
- import { getFiles } from '../../../store/file/fileActions';
- import { findAssetById } from '../../../utils/groupTreeUtils';
- const Edit = ({ isLoading }) => {
- const { companyId, documentId } = useParams();
- const { t } = useTranslation();
- const location = useLocation();
- const dispatch = useDispatch();
- const companyDocuments = useSelector(selectCompanyDocuments);
- const currentUser = useSelector(selectUser);
- const companyNotes = useSelector(selectCompanyNotes);
- const files = useSelector(selectFiles);
- const assetTree = useSelector((state) => state.company.assetTree);
- const companies = useSelector((state) => state.company.companies);
- const { exportDocument } = useExportToFiles(companyId);
- const [hasChanges, setHasChanges] = useState(false);
- const [locationDetails, setLocationDetails] = useState(null);
- // Funkcija za generiranje hijerarhijskog puta
- const generatePath = () => {
- const pathSnippets = location.pathname.split('/').filter((i) => i);
- // Mapiranje tipova na ljudski čitljive nazive
- const typeMappings = {
- companies: 'Company',
- groups: 'Group',
- assets: 'Asset',
- workspaces: 'Workspace',
- };
- let pathNames = [];
- pathSnippets.forEach((snippet, index, array) => {
- const type = array[index - 1]; // Tip prethodnog segmenta rute
- const id = snippet;
- let name = '';
- switch (type) {
- case 'companies': {
- const company = companies.find((company) => company.id === +id);
- if (company) name = `${company.name} (${typeMappings[type]})`;
- break;
- }
- case 'groups':
- case 'assets':
- case 'workspaces': {
- const asset = findAssetById(assetTree, +id);
- if (asset) name = `${asset.value.name} (${typeMappings[type]})`;
- break;
- }
- default:
- break;
- }
- if (name) {
- pathNames.push(name);
- }
- });
- return pathNames.join(' -> ');
- };
- useEffect(() => {
- dispatch(getFiles({ parentId: companyId, skipParams: true }));
- dispatch(getNotesForDocumentImport(companyId));
- const fetchLocationDetails = async () => {
- const response = await dispatch(getDocumentLocationDetails(companyId)).unwrap();
- setLocationDetails(response.locationDetails);
- };
- fetchLocationDetails();
- }, [companyId, dispatch]);
- const specificDocument = useMemo(() => {
- return companyDocuments.find((document) => document.id === parseInt(documentId));
- }, [companyDocuments, documentId]);
- const translations = {
- updatedBy: t('note.audit.updatedBy'),
- };
- const completePath = generatePath();
- const handleExportNote = async () => {
- const element = (
- <DocumentExport
- document={specificDocument}
- tenant={currentUser.tenant}
- translations={translations}
- locationDetails={locationDetails}
- completePath={completePath} // Prosljeđivanje pročišćenog puta
- />
- );
- const pdfBlob = await pdf(element).toBlob();
- const url = URL.createObjectURL(pdfBlob);
- window.open(url, '_blank');
- exportDocument(specificDocument, pdfBlob, files);
- };
- return (
- <Card
- title={
- <div className='flex items-center justify-between'>
- <div className='flex flex-row gap-3'>
- <Link to={-1}>
- <ArrowLeftOutlined />
- </Link>
- {t('document.edit.title')}
- </div>
- <Button
- type='primary'
- htmlType='button'
- loading={isLoading}
- onClick={handleExportNote}
- disabled={hasChanges}
- >
- {t('button.exportDocument')}
- </Button>
- </div>
- }
- >
- {/* Kontrolni dio - prikaz informacija o lokaciji */}
- {locationDetails && (
- <div style={{ marginBottom: '16px', color: '#6c757d', fontSize: '12px' }}>
- <p>{`${t('document.audit.location')}: ${locationDetails.name}`}</p>
- {locationDetails.image_url && (
- <img
- src={locationDetails.image_url}
- alt='Location'
- style={{ width: '100px', height: 'auto', marginTop: '8px' }}
- />
- )}
- </div>
- )}
- <DocumentForm
- parentId={companyId}
- contextId={documentId}
- document={specificDocument?.content}
- isEditing={true}
- updateAction={updateCompanyDocument}
- contextNotes={companyNotes || []}
- getContextNotes={getNotesForDocumentImport}
- hasChanges={hasChanges}
- setHasChanges={setHasChanges}
- fetchDocuments={getCompanyDocuments}
- />
- </Card>
- );
- };
- export default withLoadStatus(Edit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement