Skip to content

Commit

Permalink
fixed override bug
Browse files Browse the repository at this point in the history
  • Loading branch information
armintalaie committed Dec 31, 2024
1 parent 5341d63 commit 497e7c0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/app/portal/admin/forms/[id]/support/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export default function SubmissionAsUser() {
</div>
<div className="flex border border-background-600 flex-col p-2 px-4 w-full">
<ApplicationForm
userOverride={submissions.find((s) => s.email === search).user_id}
application={app as unknown as Application}
applicationForm={form as unknown as Form}
/>
Expand Down
8 changes: 6 additions & 2 deletions src/app/portal/forms/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,24 @@ export async function submitApplication({
export async function updateApplication({
application,
formId,
otherUser,
}: {
application: Obj;
formId: bigint;
otherUser?: string;
}) {
const supabase = createClient();
const userId = otherUser || (await supabase.auth.getUser()).data?.user?.id;
const { data, error } = await supabase.auth.getUser();
if (!data.user || error) {
return null;
}

const res = await db.submissions.findUnique({
where: {
user_id_form_id: {
form_id: formId,
user_id: data.user.id,
user_id: userId,
},
},
});
Expand All @@ -155,7 +159,7 @@ export async function updateApplication({
where: {
user_id_form_id: {
form_id: formId,
user_id: data.user.id,
user_id: userId,
},
},
data: {
Expand Down
9 changes: 5 additions & 4 deletions src/components/forms/applications/applicationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ type FormContext = {
const formContext = createContext<FormContext>({} as FormContext);

export default function ApplicationForm({
userOverride,
application,
applicationForm,
}: {
userOverride?: string;
application: Application;
applicationForm: Form;
}) {
Expand Down Expand Up @@ -85,6 +87,7 @@ export default function ApplicationForm({
<BeforeSubmitTab
goToPreviousTab={goToPreviousTab}
formId={applicationForm.id}
otherUser={userOverride}
/>
);
}
Expand Down Expand Up @@ -113,11 +116,9 @@ export default function ApplicationForm({
return;
}
const formIdAsBigInt = BigInt(applicationForm.id);
saveApplication(formAnswers, formIdAsBigInt).then(() => {
saveApplication(formAnswers, formIdAsBigInt, userOverride).then(() => {
setCurrentStep((prev) => prev + 1);
toast.success(
"Application progress until now has been saved; you can continue now or later",
);
toast.success("All changes saved.");
});
} catch (e) {
toast.error(
Expand Down
3 changes: 3 additions & 0 deletions src/components/forms/applications/beforeSubmitTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { Button } from "@/components/primitives/button";
export default function BeforeSubmitTab({
goToPreviousTab,
formId,
otherUser,
}: {
goToPreviousTab: () => void;
formId: number | bigint;
otherUser?: string;
}) {
const [submitted, setSubmitted] = useState<
"not submitted" | "submitting" | "submitted"
Expand Down Expand Up @@ -54,6 +56,7 @@ export default function BeforeSubmitTab({
onClick={() =>
submitApplication({
formId: BigInt(formId),
otherUser: otherUser,
}).then(
() => {
toast.success("Your application has been submitted!");
Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/forms/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Form } from "@/lib/types/application";
export async function saveApplication(
formAnswers: FormDetails,
formId: bigint,
otherUser?: string,
) {
const fields: Obj = {};
Object.keys(formAnswers).forEach((key) => {
Expand All @@ -28,7 +29,7 @@ export async function saveApplication(

if (Object.keys(fields).length !== 0) {
const formIdNumber = BigInt(formId);
updateApplication({ application: fields, formId: formIdNumber })
updateApplication({ application: fields, formId: formIdNumber, otherUser })
.then((res) => {
// console.log(res);
})
Expand Down

0 comments on commit 497e7c0

Please sign in to comment.