-
Notifications
You must be signed in to change notification settings - Fork 0
DEMOS-1043: Remove debug statements and fix refetchQueries #546
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import React from "react"; | ||
| import { gql, useLazyQuery, useMutation } from "@apollo/client"; | ||
| import { gql, useLazyQuery, useMutation, useApolloClient } from "@apollo/client"; | ||
|
|
||
| import { DocumentType, PhaseName, UploadDocumentInput } from "demos-server"; | ||
| import { DocumentDialog, DocumentDialogFields } from "components/dialog/document/DocumentDialog"; | ||
|
|
@@ -20,8 +20,8 @@ export const DOCUMENT_EXISTS_QUERY = gql` | |
| } | ||
| `; | ||
|
|
||
| const VIRUS_SCAN_MAX_ATTEMPTS = 10; | ||
| const DOCUMENT_POLL_INTERVAL_MS = 1_000; | ||
| export const VIRUS_SCAN_MAX_ATTEMPTS = 10; | ||
| export const DOCUMENT_POLL_INTERVAL_MS = 2_000; | ||
| const LOCALHOST_URL_PREFIX = "http://localhost"; | ||
|
|
||
| /** | ||
|
|
@@ -70,9 +70,8 @@ export const AddDocumentDialog: React.FC<AddDocumentDialogProps> = ({ | |
| onDocumentUploadSucceeded, | ||
| }) => { | ||
| const { showError } = useToast(); | ||
| const [uploadDocumentTrigger] = useMutation(UPLOAD_DOCUMENT_QUERY, { | ||
| refetchQueries, | ||
| }); | ||
| const client = useApolloClient(); | ||
| const [uploadDocumentTrigger] = useMutation(UPLOAD_DOCUMENT_QUERY); | ||
|
|
||
| const [checkDocumentExists] = useLazyQuery(DOCUMENT_EXISTS_QUERY, { | ||
| fetchPolicy: "network-only", | ||
|
|
@@ -89,32 +88,22 @@ export const AddDocumentDialog: React.FC<AddDocumentDialogProps> = ({ | |
| }; | ||
|
|
||
| const waitForVirusScan = async (documentId: string): Promise<void> => { | ||
| console.debug(`[AddDocumentDialog] Starting virus scan polling for document: ${documentId}`); | ||
| for (let attempt = 0; attempt < VIRUS_SCAN_MAX_ATTEMPTS; attempt++) { | ||
| console.debug( | ||
| `[AddDocumentDialog] Polling attempt ${attempt + 1}/${VIRUS_SCAN_MAX_ATTEMPTS}` | ||
| ); | ||
| const { data } = await checkDocumentExists({ | ||
| variables: { documentId }, | ||
| }); | ||
|
|
||
| if (data?.documentExists === true) { | ||
| console.debug(`[AddDocumentDialog] Document exists check passed on attempt ${attempt + 1}`); | ||
| return; | ||
| } | ||
|
|
||
| console.debug( | ||
| `[AddDocumentDialog] Document not yet available, waiting ${DOCUMENT_POLL_INTERVAL_MS}ms before retry` | ||
| ); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, DOCUMENT_POLL_INTERVAL_MS)); | ||
| } | ||
|
|
||
| throw new Error("Waiting for virus scan timed out"); | ||
| }; | ||
|
|
||
| const handleUpload = async (dialogFields: DocumentDialogFields): Promise<void> => { | ||
| console.debug(`[AddDocumentDialog] Starting upload for file: ${dialogFields.file?.name}`); | ||
| if (!dialogFields.file) { | ||
| showError("No file selected"); | ||
| return; | ||
|
|
@@ -146,32 +135,30 @@ export const AddDocumentDialog: React.FC<AddDocumentDialogProps> = ({ | |
| throw new Error("Upload response from the server was empty"); | ||
| } | ||
|
|
||
| console.debug( | ||
| `[AddDocumentDialog] Received presigned URL and documentId: ${uploadResult.documentId}` | ||
| ); | ||
|
|
||
| // Local development mode - skip S3 upload and virus scan | ||
| if (uploadResult.presignedURL.startsWith(LOCALHOST_URL_PREFIX)) { | ||
| onDocumentUploadSucceeded?.(); | ||
| if (refetchQueries) { | ||
| await client.refetchQueries({ include: refetchQueries }); | ||
| } | ||
|
Comment on lines
+141
to
+143
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this feels like there may be a path more intended by apollo for updating frontend data from the result of a polled query, where updates to data arent explicitly caused by mutations. This might be a good example where a direct cache invalidation may be preferred, but its hard to say. I feel like we should avoid direct calls to the apolloclient so far outside the core components, but i cant come up with any concrete reasons why or why not.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seemed to be the most clear route from what I could find but there certainly could be something else out there! I think this pattern is pretty solid for "disjointed" systems where you don't quite know when some sort of data will be available so you just allow the user to call a refresh when they need it 🤷 |
||
| return; | ||
| } | ||
|
|
||
| if (!uploadResult.presignedURL) { | ||
| throw new Error("Could not get presigned URL from the server"); | ||
| } | ||
|
|
||
| console.debug(`[AddDocumentDialog] Starting S3 upload for file: ${dialogFields.file.name}`); | ||
| const response = await tryUploadingFileToS3(uploadResult.presignedURL, dialogFields.file); | ||
| if (!response.success) { | ||
| console.debug(`[AddDocumentDialog] S3 upload failed: ${response.errorMessage}`); | ||
| showError(response.errorMessage); | ||
| throw new Error(response.errorMessage); | ||
| } | ||
|
|
||
| console.debug("[AddDocumentDialog] S3 upload successful, starting virus scan wait"); | ||
| await waitForVirusScan(uploadResult.documentId); | ||
| console.debug("[AddDocumentDialog] Upload and virus scan completed successfully"); | ||
| onDocumentUploadSucceeded?.(); | ||
| if (refetchQueries) { | ||
| await client.refetchQueries({ include: refetchQueries }); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests now reference the real values in case those change the tests will still be accurate