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

feat(UI): Add query/hook for forms API to UI #183

Merged
merged 17 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
32 changes: 12 additions & 20 deletions src/services/api/handlers/forms.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,44 @@
import { response } from "../libs/handler";

import * as fs from "fs";
import { APIGatewayEvent } from "aws-lambda";

export const forms = async (event: APIGatewayEvent) => {
try {
const body = event.body ? JSON.parse(event.body) : {};
const formId = body.formId;
const formVersion = body.formVersion;
const formId = event.queryStringParameters.formId;
const formVersion = event.queryStringParameters.formVersion;

if (!formId) {
return {
return response({
statusCode: 400,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ error: "File ID was not provided" }),
};
});
}

const filePath = getFilepathForIdAndVersion(formId, formVersion);
const jsonData = await fs.promises.readFile(filePath, "utf-8");

if (!jsonData) {
return {
return response({
statusCode: 404,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
error: "No file was found with provided formId and formVersion",
}),
};
});
}
console.log(jsonData);
return {
return response({
statusCode: 200,
headers: {
"Content-Type": "application/json",
},
body: jsonData,
};
});
} catch (error) {
console.error("Error:", error);
return {
return response({
statusCode: 500,
body: JSON.stringify({
error: error.message ? error.message : "Internal server error",
}),
};
});
}
};

Expand Down
1 change: 1 addition & 0 deletions src/services/ui/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./useSearch";
export * from "./useGetForm";
export * from "./useGetItem";
export * from "./getAttachmentUrl";
20 changes: 20 additions & 0 deletions src/services/ui/src/api/useGetForm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useQuery } from "@tanstack/react-query";
import { API } from "aws-amplify";
import { ReactQueryApiError } from "shared-types";

export const getForm = async (
formId: string,
formVersion?: string
): Promise<any> => {
charmcitygavin marked this conversation as resolved.
Show resolved Hide resolved
const form = await API.get("os", "/forms", {
queryStringParameters: { formId, formVersion },
});

return form;
};

export const useGetForm = (formId: string, formVersion?: string) => {
return useQuery<any, ReactQueryApiError>(["formID"], () =>
charmcitygavin marked this conversation as resolved.
Show resolved Hide resolved
getForm(formId, formVersion)
);
};
1 change: 0 additions & 1 deletion src/services/ui/src/pages/form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useForm } from "react-hook-form";
import { Button, Form } from "@/components/Inputs";

import { RHFDocument } from "@/components/RHF";
import { ABP1 } from "./proto";
import { documentInitializer } from "@/components/RHF";
Expand Down