Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloadable file option in column #128

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading