Skip to content

Commit

Permalink
feat: introducing support for csv files download
Browse files Browse the repository at this point in the history
  • Loading branch information
mahamdan committed Jan 15, 2024
1 parent 2031675 commit 76d8306
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/components/Operation/Operation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export class Operation extends React.Component<OperationProps, OperationState> {
type: mapStatusCodeToType(statusCode),
code: statusCode || 0,
content: formatResponseContent(data, contentType),
format: contentType,
},
});
});
Expand Down
12 changes: 10 additions & 2 deletions src/components/ResponseSamples/DownloadResponse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ import { OperationModel } from '../../services';
interface DownloadResponseProps {
operation?: OperationModel;
content: any;
format?: string;
}

export const DownloadResponse = ({ operation, content }: DownloadResponseProps) => {
const getHrefTextFormat = (format?: string) => {
if (format === 'text/csv') {
return format;
}
return 'text/plain';
};

export const DownloadResponse = ({ operation, content, format }: DownloadResponseProps) => {
content = typeof content === 'object' && content !== null ? JSON.stringify(content) : content;
const downloadItAnchor = (
<a
style={{ color: '#326CD1', cursor: 'pointer', fontWeight: 600 }}
href={`data:text/plain;charset=utf-8,${encodeURIComponent(content)}`}
href={`data:${getHrefTextFormat(format)};charset=utf-8,${encodeURIComponent(content)}`}
download={`${operation?.id || ''}Response`}
>
download it
Expand Down
4 changes: 2 additions & 2 deletions src/components/ResponseSamples/ResponseSamples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ResponseSamples extends React.Component<ResponseSamplesProps> {
return response.content && response.content.hasSample;
});
const hasResponseSamples = responses && responses.length > 0;
const content = customResponse?.content;
const { content, format } = customResponse || {};
const contentLength = content?.length || JSON.stringify(content)?.length || 0;
const reachedMaxPayloadLength = contentLength > MAX_CONTENT_LENGTH;

Expand All @@ -36,7 +36,7 @@ export class ResponseSamples extends React.Component<ResponseSamplesProps> {
<RightPanelHeader> {l('response')} </RightPanelHeader>
<div>
{reachedMaxPayloadLength ? (
<DownloadResponse operation={operation} content={content} />
<DownloadResponse operation={operation} content={content} format={format} />
) : (
<PayloadSamples customData={customResponse} />
)}
Expand Down

0 comments on commit 76d8306

Please sign in to comment.