mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-22 11:29:22 -05:00
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import useSWR from "swr";
|
|
import { fetcher } from "./utils";
|
|
|
|
export const useSubmissionSessions = (formId: string) => {
|
|
const { data, error, mutate } = useSWR(
|
|
() => `/api/forms/${formId}/submissionSessions`,
|
|
fetcher
|
|
);
|
|
|
|
return {
|
|
submissionSessions: data,
|
|
isLoadingSubmissionSessions: !error && !data,
|
|
isErrorSubmissionSessions: error,
|
|
mutateSubmissionSessions: mutate,
|
|
};
|
|
};
|
|
|
|
// fill the schema with the values provided by the user
|
|
export const getSubmission = (submissionSession, schema) => {
|
|
if (!schema) return {};
|
|
// create new submission
|
|
const submission = {
|
|
id: submissionSession.id,
|
|
createdAt: submissionSession.createdAt,
|
|
pages: [],
|
|
};
|
|
if (submissionSession.events.length > 0) {
|
|
// iterate through schema pages to fill submission
|
|
for (const page of schema.pages) {
|
|
// new submission page
|
|
const submissionPage = {
|
|
name: page.name,
|
|
type: page.type,
|
|
elements: page.elements
|
|
? JSON.parse(JSON.stringify(page.elements))
|
|
: [],
|
|
};
|
|
// search for elements in schema pages of type "form" and fill their value into the submission
|
|
if (page.type === "form") {
|
|
const pageSubmission = submissionSession.events.find(
|
|
(s) => s.type === "pageSubmission" && s.data?.pageName === page.name
|
|
);
|
|
if (typeof pageSubmission !== "undefined") {
|
|
for (const [elementIdx, element] of page.elements.entries()) {
|
|
if (element.type !== "submit") {
|
|
if (element.name in pageSubmission.data?.submission) {
|
|
submissionPage.elements[elementIdx].value =
|
|
pageSubmission.data.submission[element.name];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
submission.pages.push(submissionPage);
|
|
}
|
|
}
|
|
return submission;
|
|
};
|