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

B-21049 #14215

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft

B-21049 #14215

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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@opentelemetry/core": "^1.15.1",
"@tanstack/react-query": "^4.29.12",
"@tanstack/react-query-devtools": "^5.17.12",
"@transcom/react-file-viewer": "git+https://github.com/transcom/react-file-viewer#v1.2.4",
"@transcom/react-file-viewer": "git+https://github.com/transcom/react-file-viewer#v1.2.5",
"@trussworks/react-uswds": "3.2.0",
"axe-playwright": "^1.2.3",
"bytes": "^3.1.2",
Expand Down
11 changes: 10 additions & 1 deletion src/components/DocumentViewer/Content/Content.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ import { ReactComponent as ArrowLeft } from 'shared/icon/arrow-left.svg';
import { ReactComponent as ArrowRight } from 'shared/icon/arrow-right.svg';
*/

const DocViewerContent = ({ fileType, filePath, saveRotation, setRotationValue, rotationValue, disableSaveButton }) => (
const DocViewerContent = ({
fileType,
filePath,
saveRotation,
setRotationValue,
rotationValue,
disableSaveButton,
onError,
}) => (
<div data-testid="DocViewerContent" className={styles.DocViewerContent}>
<FileViewer
key={`fileViewer_${filePath}`}
fileType={fileType}
filePath={filePath}
onError={onError}
saveRotation={saveRotation}
rotationValue={rotationValue}
setRotationValue={setRotationValue}
Expand Down
12 changes: 11 additions & 1 deletion src/components/DocumentViewer/DocumentViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import AsyncPacketDownloadLink from 'shared/AsyncPacketDownloadLink/AsyncPacketD
* TODO
* - implement next/previous pages instead of scroll through pages
* - implement rotate left/right
* - handle fetch doc errors
*/

const DocumentViewer = ({ files, allowDownload, paymentRequestId }) => {
const [selectedFileIndex, selectFile] = useState(0);
const [disableSaveButton, setDisableSaveButton] = useState(false);
const [menuIsOpen, setMenuOpen] = useState(false);
const [showContentError, setShowContentError] = useState(false);
const sortedFiles = files.sort((a, b) => moment(b.createdAt) - moment(a.createdAt));
const selectedFile = sortedFiles[parseInt(selectedFileIndex, 10)];

Expand Down Expand Up @@ -73,6 +73,7 @@ const DocumentViewer = ({ files, allowDownload, paymentRequestId }) => {
}, [files.length]);

useEffect(() => {
setShowContentError(false);
setRotationValue(selectedFile?.rotation || 0);
}, [selectedFile]);

Expand Down Expand Up @@ -108,6 +109,11 @@ const DocumentViewer = ({ files, allowDownload, paymentRequestId }) => {

const selectedFileDate = formatDate(moment(selectedFile?.createdAt), 'DD MMM YYYY');

const onContentError = (errorObject) => {
setShowContentError(true);
milmoveLogger.error(errorObject);
};

const saveRotation = () => {
if (fileType.current !== 'pdf' && mountedRef.current === true) {
const uploadBody = {
Expand Down Expand Up @@ -150,13 +156,17 @@ const DocumentViewer = ({ files, allowDownload, paymentRequestId }) => {
)}
{paymentRequestId !== undefined ? paymentPacketDownload : null}
</div>
{showContentError && (
<div className={styles.errorMessage}>If your document does not display, please refresh your browser.</div>
)}
<Content
fileType={fileType.current}
filePath={selectedFile?.url}
rotationValue={rotationValue}
disableSaveButton={disableSaveButton}
setRotationValue={setRotationValue}
saveRotation={saveRotation}
onError={onContentError}
/>
{menuIsOpen && <div className={styles.overlay} />}
<Menu
Expand Down
8 changes: 8 additions & 0 deletions src/components/DocumentViewer/DocumentViewer.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
@include u-margin-left(1);
}
}

.errorMessage {
padding: 5em;
}

.errorMessage ~ div :global(.pdf-loading) {
display: none;
}
}

.overlay {
Expand Down
120 changes: 88 additions & 32 deletions src/components/DocumentViewer/DocumentViewer.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,46 +52,62 @@ const mockFiles = [
},
];

const mockErrorFiles = [
{
id: 1,
filename: 'Test File.pdf',
contentType: 'application/pdf',
url: '404',
createdAt: '2021-06-14T15:09:26.979879Z',
},
];

jest.mock('services/ghcApi', () => ({
...jest.requireActual('services/ghcApi'),
bulkDownloadPaymentRequest: jest.fn(),
}));

jest.mock('./Content/Content', () => ({
__esModule: true,
default: ({ id, filename, contentType, url, createdAt, rotation }) => (
<div>
<div data-testid="documentTitle">
{filename} Uploaded on {createdAt}
</div>
<div>id: {id || 'undefined'}</div>
<div>fileName: {filename || 'undefined'}</div>
<div>contentType: {contentType || 'undefined'}</div>
<div>url: {url || 'undefined'}</div>
<div>createdAt: {createdAt || 'undefined'}</div>
<div>rotation: {rotation || 'undefined'}</div>
<div data-testid="listOfFiles">
<ul>
{mockFiles.map((file) => (
<li key={file.id}>
{file.filename} - Added on {file.createdAt}
</li>
))}
</ul>
</div>
<div data-testid="menuButtonContainer" className="closed">
<button
data-testid="menuButton"
onClick={() => {
toggleMenuClass();
}}
type="button"
>
Toggle
</button>
default: ({ id, filename, contentType, url, createdAt, rotation, filePath, onError }) => {
if (filePath === '404') {
onError('content error happening');
return <div>nothing to see here</div>;
}
return (
<div>
<div data-testid="documentTitle">
{filename} Uploaded on {createdAt}
</div>
<div>id: {id || 'undefined'}</div>
<div>fileName: {filename || 'undefined'}</div>
<div>contentType: {contentType || 'undefined'}</div>
<div>url: {url || 'undefined'}</div>
<div>createdAt: {createdAt || 'undefined'}</div>
<div>rotation: {rotation || 'undefined'}</div>
<div data-testid="listOfFiles">
<ul>
{mockFiles.map((file) => (
<li key={file.id}>
{file.filename} - Added on {file.createdAt}
</li>
))}
</ul>
</div>
<div data-testid="menuButtonContainer" className="closed">
<button
data-testid="menuButton"
onClick={() => {
toggleMenuClass();
}}
type="button"
>
Toggle
</button>
</div>
</div>
</div>
),
);
},
}));

describe('DocumentViewer component', () => {
Expand Down Expand Up @@ -180,6 +196,46 @@ describe('DocumentViewer component', () => {
expect(screen.getByText('id: undefined')).toBeInTheDocument();
});

describe('regarding content errors', () => {
const errorMessageText = 'If your document does not display, please refresh your browser.';
const downloadLinkText = 'Download file';
it('no error message normally', async () => {
render(
<QueryClientProvider client={new QueryClient()}>
<DocumentViewer files={mockFiles} />
</QueryClientProvider>,
);
expect(screen.queryByText(errorMessageText)).toBeNull();
});

it('download link normally', async () => {
render(
<QueryClientProvider client={new QueryClient()}>
<DocumentViewer files={mockFiles} allowDownload />
</QueryClientProvider>,
);
expect(screen.getByText(downloadLinkText)).toBeVisible();
});

it('show message on content error', async () => {
render(
<QueryClientProvider client={new QueryClient()}>
<DocumentViewer files={mockErrorFiles} />
</QueryClientProvider>,
);
expect(screen.getByText(errorMessageText)).toBeVisible();
});

it('download link on content error', async () => {
render(
<QueryClientProvider client={new QueryClient()}>
<DocumentViewer files={mockErrorFiles} allowDownload />
</QueryClientProvider>,
);
expect(screen.getByText(downloadLinkText)).toBeVisible();
});
});

describe('when clicking download Download All Files button', () => {
it('downloads a bulk packet', async () => {
const mockResponse = {
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4583,9 +4583,9 @@
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-2.0.0.tgz#f544a148d3ab35801c1f633a7441fd87c2e484bf"
integrity sha512-XCuKFP5PS55gnMVu3dty8KPatLqUoy/ZYzDzAGCQ8JNFCkLXzmI7vNHCR+XpbZaMWQK/vQubr7PkYq8g470J/A==

"@transcom/react-file-viewer@git+https://github.com/transcom/react-file-viewer#v1.2.4":
version "1.2.4"
resolved "git+https://github.com/transcom/react-file-viewer#d4396dc8bf0acdd646e93ea82d23acf3bc0d5b0f"
"@transcom/react-file-viewer@git+https://github.com/transcom/react-file-viewer#v1.2.5":
version "1.2.5"
resolved "git+https://github.com/transcom/react-file-viewer#4d9924ae58a6acdfcfb353235e034ff98fde5458"
dependencies:
pdfjs-dist "1.8.357"
prop-types "^15.5.10"
Expand Down
Loading