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

fix(RV-428): Fix change upload flow to reflect UI #494

Merged
merged 1 commit into from
Dec 27, 2024

Conversation

knguyenrise8
Copy link
Collaborator

Description

fix change upload button

Screenshots (if applicable)

Dec-27-2024 09-53-55

Related Issues

[Link any related issues or tasks from your project management system.]

Checklist

  • The title of this PR is descriptive and concise.
  • My changes follow the style guidelines of this project.
  • I have added or updated test cases to cover my changes.
  • I've let the team know about this PR by linking it in the review channel

Copy link
Collaborator

@marycrawford marycrawford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG, just have a question regarding the error handling logic in case the file that is uploaded is not a pdf. Are we considering that?

if (files.length > 0) {
setUploadedFile([]);
clearFiles();
setHasError(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setHasError is set to false here. Is there a try/catch logic that will handle errors in case the file that is uploaded is not a pdf?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handled outside in the extractProcess page

    try {
      setIsLoading(true);
      const newQueries = pdfs
        .map((pdf) => {
          return pdf.images.map((page, index) => {
            const selectedTemplateName = selectedTemplates.find(
              (temp) => temp.fileName === pdf.file,
            )?.templateName;
            return {
              pdfName: pdf.file,
              fieldNames: templates.find(
                (template) => template.name === selectedTemplateName,
              )?.pages[index].fieldNames as Field[],
              sourceImage: page,
              templateImage: templates.find(
                (template) => template.name === selectedTemplateName,
              )?.pages[index].templateImage as string,
            };
          });
        })
        .map((perBatch) =>
          perBatch.map(async (args) => ({
            name: args.pdfName,
            resp: await ImageToText({
              sourceImage: args.sourceImage,
              templateImage: args.templateImage,
              fieldNames: args.fieldNames,
            }),
          })),
        )
        .flat();
      const new_responses = await Promise.all(
        newQueries.map(async (query) => ({
          name: (await query).name,
          resp: (await query).resp,
        })),
      );
      const arrResults: { [key: string]: IResults } = {};

      new_responses.forEach((response) => {
        if (response.name in arrResults) {
          Object.keys(response.resp as ImageToTextResponse).forEach((key) => {
            arrResults[response.name][key] = {
              text: response.resp ? response.resp[key][0] : "",
              confidence: response.resp ? response.resp[key][1] : 0,
            };
          });
        } else {
          arrResults[response.name] = {};
          Object.keys(response.resp as ImageToTextResponse).forEach((key) => {
            arrResults[response.name][key] = {
              text: response.resp ? response.resp[key][0] : "",
              confidence: response.resp ? response.resp[key][1] : 0,
            };
          });
        }
      });

      const arrTransformedResponses: Submission[] = Object.keys(arrResults).map(
        (key) => {
          const transformedResponses: Submission = {
            template_name: templates[0].name,
            template_image: templates[0].pages[0].templateImage,
            file_name: key,
            file_image: pdfs.find((pdf) => pdf.file === key)
              ?.images[0] as string,
            results: arrResults[key],
          };
          return transformedResponses;
        },
      );

      localStorage.setItem(
        "arr_submissions",
        JSON.stringify(arrTransformedResponses),
      );

      // Update loading state and navigate
      setIsLoading(false);
      navigate("/extract/review");
    } catch (error) {
      console.error("Error processing templates:", error);
      setIsLoading(false);
      navigate("/extract/upload");
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [navigate]);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we navigate back to the start of the flow

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we def need to get design/product for the app when the user uploads not a pdf

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now i just gated the input to only take in pdfs

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@knguyenrise8 knguyenrise8 force-pushed the fix/RV-428-change-upload branch from 1258741 to 6972ebd Compare December 27, 2024 16:50
Copy link
Collaborator

@marycrawford marycrawford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if (files.length > 0) {
setUploadedFile([]);
clearFiles();
setHasError(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@knguyenrise8 knguyenrise8 added this pull request to the merge queue Dec 27, 2024
Merged via the queue into main with commit 3b04b3b Dec 27, 2024
3 checks passed
@knguyenrise8 knguyenrise8 deleted the fix/RV-428-change-upload branch December 27, 2024 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants