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

[BitBuilder] Add "Close form" functionality #48

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion src/pages/forms/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ export default function FormDetailPage() {
<Link href={'/forms/fill/' + form.id}>
<button className="rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">
View live form
</button>
<button className="rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" onClick={closeForm} disabled={!form.is_open}>
Close form
</button>
</Link>
</div>
{form.created_at && (
Expand Down Expand Up @@ -152,3 +154,14 @@ function respectNewLines(text: string | null | undefined) {
</span>
));
}
async function closeForm() {
const { data, error } = await supabase
.from('forms')
.update({ is_open: false })
.eq('id', form.id)
if (error) {
console.error('Error: ', error)
} else {
setForm({ ...form, is_open: false })
}
}
10 changes: 8 additions & 2 deletions src/pages/forms/fill/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@ export function CreateFormInner(props: { formId: string }) {
console.error(maybeForm.message);
setError(maybeForm);
} else {
getFormFromSupabase(formId, supabase).then((maybeForm) => {
if (maybeForm instanceof Error) {
console.error(maybeForm.message);
setError(maybeForm);
} else if (!maybeForm.is_open) {
setError(new Error('This form is closed and cannot be filled.'));
} else {
setForm(maybeForm);
}
});
}
}, []); // The empty array ensures this effect runs only once on mount
return form ? (
<InnerChat form={form} supabase={supabase} />
Expand Down Expand Up @@ -239,4 +245,4 @@ function SubmissionBox(submission: object): React.ReactNode {
</p>
</div>
);
}
}
22 changes: 21 additions & 1 deletion src/pages/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,39 @@ export default function HomePage() {
const { isLoading: isSessionLoading, session, error } = useSessionContext();
const supabase = createClientComponentClient<Database>();
const [user, setUser] = useState<null | User>(null);
const [openForms, setOpenForms] = useState(0);
const [closedForms, setClosedForms] = useState(0);

useEffect(() => {
if (!isSessionLoading && !session) {
push('/auth');
}
if (!isSessionLoading && session) {
getUserFromSupabase(session, supabase, setUser);
getFormCounts();
}
}, [isSessionLoading, session]);

async function getFormCounts() {
let { data: forms, error } = await supabase
.from('forms')
.select('*');
if (error) console.log('Error: ', error);
else {
let openCount = forms.filter(form => form.is_open).length;
let closedCount = forms.length - openCount;
setOpenForms(openCount);
setClosedForms(closedCount);
}
}

return (
<Page pageTitle="Home" user={user}>
<DashboardMode user={user} />
<div>
<span>Open Forms: {openForms}</span>
<span>Closed Forms: {closedForms}</span>
</div>
</Page>
);
}
}
Loading