Skip to content

Commit

Permalink
download button for xml (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninarchive committed Dec 4, 2023
1 parent ee79473 commit 33cd816
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 36 deletions.
50 changes: 35 additions & 15 deletions frontend/src/actions/downloadActions.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
import request from 'superagent-bluebird-promise';
import moment from 'moment';
import {catchHandler} from './utils/fetch';
import {buildUrl} from './utils/buildUrl';
import { catchHandler } from './utils/fetch';
import { buildUrl } from './utils/buildUrl';
import InvoiceModel from '../components/invoice/models/InvoiceModel';
import {Attachment} from '../models';
import {ClientModel} from '../components/client/models/ClientModels';
import {getInvoiceFileName, getDownloadUrl, previewPdf, downloadAttachment} from './utils/download-helpers';
import {ProjectMonthOverviewModel} from '../components/project/models/ProjectMonthModel';
import {FullProjectMonthModel} from '../components/project/models/FullProjectMonthModel';
import {authService} from '../components/users/authService';
import { Attachment } from '../models';
import { ClientModel } from '../components/client/models/ClientModels';
import { getInvoiceFileName, getDownloadUrl, previewPdf, downloadAttachment, getMyInvoiceFileName } from './utils/download-helpers';
import { ProjectMonthOverviewModel } from '../components/project/models/ProjectMonthModel';
import { FullProjectMonthModel } from '../components/project/models/FullProjectMonthModel';
import { authService } from '../components/users/authService';


export function getInvoiceDownloadUrl(
fileNameTemplate: string,
invoice: InvoiceModel,
attachment: 'pdf' | Attachment = 'pdf',
attachment: string | Attachment = 'pdf',
downloadType?: 'preview' | 'download',
fullProjectMonth?: FullProjectMonthModel,
): string {

const fileType = invoice.isQuotation ? 'quotation' : 'invoice';
const fileName = attachment === 'pdf' || attachment.type === 'pdf'
? getInvoiceFileName(fileNameTemplate, invoice, fullProjectMonth)
: attachment.fileName;
const attachmentType = attachment === 'pdf' ? 'pdf' : attachment.type;
/* const fileName = typeof attachment === 'string' || attachment.type === 'pdf'
? getMyInvoiceFileName(fileNameTemplate, invoice, 'pdf', fullProjectMonth)
: attachment.fileName; */

const filename = () => {
if (typeof attachment === 'string') {
return getMyInvoiceFileName(fileNameTemplate, invoice, attachment, fullProjectMonth);
}
if (attachment.type === 'pdf') {
return getMyInvoiceFileName(fileNameTemplate, invoice, 'pdf', fullProjectMonth);
}
return attachment.fileName;
}

const attachmentType = typeof attachment === 'string' ? attachment : attachment.type;
// return buildUrl(`/attachments/${fileType}/${invoice._id}/${attachmentType}/${encodeURIComponent(fileName)}${query}`);

return getDownloadUrl(fileType, invoice._id, attachmentType, fileName, downloadType);
/* const myType = () => {
if (attachment === 'xml') {
return 'xml';
}
if (typeof attachment !== 'string') {
return attachment.type;
}
return 'pdf';
} */

return getDownloadUrl(fileType, invoice._id, attachmentType, filename(), downloadType);
}


Expand Down
13 changes: 9 additions & 4 deletions frontend/src/actions/utils/download-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import InvoiceModel from '../../components/invoice/models/InvoiceModel';
import {buildUrl} from './buildUrl';
import {invoiceReplacements} from '../../components/invoice/invoice-replacements';
import {FullProjectMonthModel} from '../../components/project/models/FullProjectMonthModel';
import {authService} from '../../components/users/authService';
import { buildUrl } from './buildUrl';
import { invoiceReplacements } from '../../components/invoice/invoice-replacements';
import { FullProjectMonthModel } from '../../components/project/models/FullProjectMonthModel';
import { authService } from '../../components/users/authService';

export type DownloadAttachmentModelTypes = {
invoice: string;
Expand Down Expand Up @@ -36,6 +36,11 @@ export function downloadAttachment(fileName: string, content: Blob): void {



export function getMyInvoiceFileName(fileName: string, invoice: InvoiceModel, extension: string, fullProjectMonth?: FullProjectMonthModel): string {
console.log(`${invoiceReplacements(fileName, invoice, fullProjectMonth)}.${extension}`);
return `${invoiceReplacements(fileName, invoice, fullProjectMonth)}.${extension}`;
}

export function getInvoiceFileName(fileName: string, invoice: InvoiceModel, fullProjectMonth?: FullProjectMonthModel): string {
return `${invoiceReplacements(fileName, invoice, fullProjectMonth)}.pdf`;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React from 'react';
import {useSelector} from 'react-redux';
import {Icon, IconProps} from '../Icon';
import {getInvoiceDownloadUrl} from '../../../actions/index';
import { useSelector } from 'react-redux';
import { Icon, IconProps } from '../Icon';
import { getInvoiceDownloadUrl } from '../../../actions/index';
import t from '../../../trans';
import {InvoiceModelProps} from '../../invoice/models/InvoiceModel';
import {Attachment} from '../../../models';
import {getAwesomeFileType} from '../../invoice/models/getAwesomeFileType';
import {ConfacState} from '../../../reducers/app-state';
import { InvoiceModelProps } from '../../invoice/models/InvoiceModel';
import { Attachment } from '../../../models';
import { getAwesomeFileType } from '../../invoice/models/getAwesomeFileType';
import { ConfacState } from '../../../reducers/app-state';


export const InvoiceDownloadIcon = ({invoice, ...props}: InvoiceModelProps) => {
export const InvoiceDownloadPdfIcon = ({ invoice, ...props }: InvoiceModelProps) => {
const configInvoiceFileName = useSelector((state: ConfacState) => state.config.invoiceFileName);

const defaultInvoiceFileName = invoice.client.invoiceFileName || configInvoiceFileName;
Expand All @@ -23,8 +23,24 @@ export const InvoiceDownloadIcon = ({invoice, ...props}: InvoiceModelProps) => {
);
};

export const InvoiceDownloadXmlIcon = ({ invoice, ...props }: InvoiceModelProps) => {
const configInvoiceFileName = useSelector((state: ConfacState) => state.config.invoiceFileName);

const defaultInvoiceFileName = invoice.client.invoiceFileName || configInvoiceFileName;
const url = getInvoiceDownloadUrl(defaultInvoiceFileName, invoice, 'xml', 'download');
console.log('url: ' + url);
console.log('filename: ' + defaultInvoiceFileName);
return (
<AttachmentDownloadIcon
downloadUrl={url}
attachment={invoice.attachments.find(a => a.type === 'xml')}
{...props}
/>
);
};


export const InvoicePreviewIcon = ({invoice, ...props}: InvoiceModelProps & IconProps) => {
export const InvoicePreviewIcon = ({ invoice, ...props }: InvoiceModelProps & IconProps) => {
const configInvoiceFileName = useSelector((state: ConfacState) => state.config.invoiceFileName);
const defaultInvoiceFileName = invoice.client.invoiceFileName || configInvoiceFileName;
const fileType = invoice.isQuotation ? 'quotation' : 'invoice';
Expand All @@ -46,7 +62,7 @@ type AttachmentPreviewIconProps = {
title?: string,
}

export const AttachmentPreviewIcon = ({previewUrl, attachment, ...props}: AttachmentPreviewIconProps) => (
export const AttachmentPreviewIcon = ({ previewUrl, attachment, ...props }: AttachmentPreviewIconProps) => (
<Icon title={t(props.title || 'invoice.viewPdf')} href={previewUrl} size={1} fa="far fa-eye" {...props} />
);

Expand All @@ -56,12 +72,12 @@ type AttachmentDownloadIconProps = IconProps & {
downloadUrl: string;
}

export const AttachmentDownloadIcon = ({downloadUrl, attachment, ...props}: AttachmentDownloadIconProps) => (
export const AttachmentDownloadIcon = ({ downloadUrl, attachment, ...props }: AttachmentDownloadIconProps) => (
<Icon
fa={`${getAwesomeFileType(attachment)} fa-2x`}
title={t('invoice.downloadAttachment', attachment && {type: attachment.fileName || attachment.type})}
title={t('invoice.downloadAttachment', attachment && { type: attachment.fileName || attachment.type })}
{...props}
href={downloadUrl}
labelStyle={{fontSize: 16}}
labelStyle={{ fontSize: 16 }}
/>
);
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {useDispatch} from 'react-redux';
import InvoiceModel from '../models/InvoiceModel';
import {EditIcon} from '../../controls/Icon';
import {InvoiceVerifyIconToggle} from '../invoice-list/InvoiceVerifyIconToggle';
import {InvoiceDownloadIcon, InvoicePreviewIcon} from '../../controls/attachments/AttachmentDownloadIcon';
import { InvoiceDownloadPdfIcon, InvoiceDownloadXmlIcon, InvoicePreviewIcon} from '../../controls/attachments/AttachmentDownloadIcon';
import {t} from '../../utils';
import {deleteInvoice} from '../../../actions';
import {ConfirmedDeleteIcon} from '../../controls/icons/DeleteIcon';
Expand All @@ -30,7 +30,8 @@ export const InvoiceListRowActions = ({invoice, toggleValid, small = false}: Inv
/>
)}
<InvoiceVerifyIconToggle claim={Claim.ValidateInvoices} invoice={invoice} toggleValid={toggleValid} />
{!small && <InvoiceDownloadIcon invoice={invoice} />}
{!small && <InvoiceDownloadPdfIcon invoice={invoice} />}
{!small && <InvoiceDownloadXmlIcon invoice={invoice} />}
<InvoicePreviewIcon invoice={invoice} />
{!small && (
<ConfirmedDeleteIcon
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/components/invoice/models/getAwesomeFileType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Attachment} from '../../../models';
import { Attachment } from '../../../models';

export function getAwesomeFileType(att: Attachment | undefined): string {
console.log(att);
if (!att) {
return 'fa fa-file-upload';
}
Expand All @@ -10,6 +11,9 @@ export function getAwesomeFileType(att: Attachment | undefined): string {
// --> Looks too busy in the InvoiceList
// return 'fa fa-file-invoice-dollar';
}
if (att.type === 'xml') {
return 'far fa-file-invoice';
}
if (att.fileType === 'application/pdf') {
return 'far fa-file-pdf';
}
Expand Down Expand Up @@ -46,6 +50,9 @@ export function getAwesomeFileType(att: Attachment | undefined): string {
case 'application/x-zip-compressed':
return 'far fa-file-archive';

case 'application/xml':
return 'far fa-file-invoice';

default:
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export function getInvoiceColumns(includeFields: string[], transPrefix: string):
}, {
key: 'buttons',
header: '',
style: {width: 240},
style: {width: 270},
className: 'icons-cell',
value: (i: InvoiceModel) => <InvoiceListRowActions invoice={i} />,
}];
Expand Down

0 comments on commit 33cd816

Please sign in to comment.