Skip to content

Commit

Permalink
Merge pull request #128 from irontec/CDL-57-downloadable-FSO-object-f…
Browse files Browse the repository at this point in the history
…rom-list

Downloadable file option in column
  • Loading branch information
R0MANDEV authored Aug 20, 2024
2 parents d24c938 + cc1da12 commit c0b5ad1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
2 changes: 1 addition & 1 deletion library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@irontec/ivoz-ui",
"version": "1.4.3",
"version": "1.4.4",
"description": "UI library used in ivozprovider",
"license": "GPL-3.0",
"main": "index.js",
Expand Down
15 changes: 13 additions & 2 deletions library/src/components/List/Content/ListContentValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
StyledCheckBoxOutlineBlankIcon,
StyledTableRowFkLink,
} from './Table/ContentTable.styles';
import DownloadFile from '../DownloadFile';

interface ListContentValueProps {
columnName: string;
Expand All @@ -23,12 +24,12 @@ interface ListContentValueProps {

const ListContentValue = (props: ListContentValueProps): JSX.Element => {
const { column, columnName, row, entityService } = props;

const routes = useStoreState((state) => state.routes.routes);

const ListDecorator = entityService.getListDecorator();
const customComponent = (column as ScalarProperty).component;

const isDownloadable = (column as ScalarProperty).downloadable;
const isFileType = (column as ScalarProperty).type === 'file';
const isFk = isPropertyFk(column);
const loadingFk =
isFk &&
Expand Down Expand Up @@ -97,6 +98,16 @@ const ListContentValue = (props: ListContentValueProps): JSX.Element => {
response = <ListDecorator field={columnName} row={row} property={column} />;
}

if (isDownloadable && isFileType) {
response = (
<DownloadFile
row={row}
path={entityService.getEntity().path}
fileType={columnName}
/>
);
}

const prefix = column?.prefix || '';

return (
Expand Down
21 changes: 21 additions & 0 deletions library/src/components/List/DownloadFile.styles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import styled from '@emotion/styled';
import DownloadIcon from '@mui/icons-material/Download';

export const StyledPdfIcon = styled(DownloadIcon)(() => {
return {
color: '#5b5b5b',
verticalAlign: 'bottom',
fontSize: '1.3em',
cursor: 'pointer',
};
});

export const StyledFileName = styled.span(() => {
return {
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
maxWidth: '60px',
display: 'inline-flex',
};
});
38 changes: 38 additions & 0 deletions library/src/components/List/DownloadFile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useStoreActions } from 'store';
import { StyledPdfIcon, StyledFileName } from './DownloadFile.styles';
import { saveAs } from 'file-saver';

export interface DownloadFileProps {
row: Record<string, any>;
path: string;
fileType: string;
}

export default function DownloadFile(props: DownloadFileProps): JSX.Element {
const { row, path, fileType } = props;

const apiDownload = useStoreActions((actions) => {
return actions.api.download;
});

const download = () => {
apiDownload({
path: `${path}/${row.id}/${fileType}`,
params: {},
successCallback: async (data: any, headers: any) => {
const fileName = headers['content-disposition']
?.split('filename=')
?.pop();

saveAs(data, fileName || 'download');
},
});
};

return (
<>
<StyledPdfIcon onClick={download} />
<StyledFileName>{row[fileType].baseName}</StyledFileName>
</>
);
}
1 change: 1 addition & 0 deletions library/src/services/api/ParsedApiSpecInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface ScalarProperty {
required: boolean;
pattern?: RegExp;
helpText?: string | React.ReactElement<any>;
downloadable?: boolean;
}

export interface EmbeddableProperty {
Expand Down

0 comments on commit c0b5ad1

Please sign in to comment.