Skip to content

Add form switcher #1160

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

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open

Add form switcher #1160

wants to merge 28 commits into from

Conversation

kalilsn
Copy link
Member

@kalilsn kalilsn commented Apr 15, 2025

Issue(s) Resolved

Resolves #1037
Resolves #1036

High-level Explanation of PR

This PR adds the form switcher component to forms and the pub details page, and fully implements the new form permissions introduced in #1134.

  • Forms can now only be used to edit pubs of the same pub type as the form
  • Contributors can only edit and view pubs using the forms attached to their membership
  • Contributors to a pub without any forms can only view the pub, not edit it

Test Plan

Screenshots (if applicable)

Notes

@kalilsn kalilsn changed the title Remove extra character from croccrocid to fix api validation errors Add form switcher Apr 15, 2025
@kalilsn kalilsn changed the base branch from main to kalilsn/memberships-have-forms April 15, 2025 14:30
@kalilsn kalilsn force-pushed the kalilsn/form-switcher branch from 13ee081 to 8948d60 Compare April 21, 2025 03:56
@kalilsn kalilsn force-pushed the kalilsn/memberships-have-forms branch from f3c9d0e to 87cecc5 Compare April 21, 2025 04:57
@kalilsn kalilsn force-pushed the kalilsn/form-switcher branch from a6adb69 to ff6774d Compare April 21, 2025 04:58
@kalilsn kalilsn added the preview Auto-deploys a preview application label Apr 21, 2025
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 April 21, 2025 05:06 Inactive
@kalilsn kalilsn force-pushed the kalilsn/form-switcher branch from 5719a72 to 28d6c2a Compare April 22, 2025 14:31
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 April 22, 2025 14:36 Inactive
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 April 22, 2025 19:35 Inactive
@kalilsn kalilsn force-pushed the kalilsn/form-switcher branch from 3252290 to 27d51b4 Compare April 22, 2025 20:10
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 April 22, 2025 20:15 Inactive
Base automatically changed from kalilsn/memberships-have-forms to main April 22, 2025 20:34
@kalilsn kalilsn force-pushed the kalilsn/form-switcher branch from 27d51b4 to 05289b7 Compare April 22, 2025 20:36
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 April 22, 2025 20:41 Inactive
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 May 6, 2025 17:10 Inactive
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 May 6, 2025 21:20 Inactive
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 May 7, 2025 14:15 Inactive
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 May 7, 2025 21:49 Inactive
@kalilsn kalilsn force-pushed the kalilsn/form-switcher branch from 1fcd173 to 607b6ab Compare May 7, 2025 22:09
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 May 7, 2025 22:14 Inactive
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 May 12, 2025 22:03 Inactive
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 May 12, 2025 22:28 Inactive
@kalilsn kalilsn force-pushed the kalilsn/form-switcher branch from 42efc45 to 1d4431c Compare May 12, 2025 22:42
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 May 12, 2025 22:47 Inactive
@kalilsn kalilsn force-pushed the kalilsn/form-switcher branch from 1d4431c to 9e4205f Compare May 12, 2025 23:12
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 May 12, 2025 23:17 Inactive
@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 May 14, 2025 11:52 Inactive
@tefkah tefkah force-pushed the kalilsn/form-switcher branch from ca09ebf to f24140d Compare May 14, 2025 12:10
@tefkah
Copy link
Member

tefkah commented May 14, 2025

sorry, i have some suggested changes i wanted to push up to a separate branch, but accidentally pushed them here, hence the force push revert

@3mcd 3mcd temporarily deployed to gh-654103159-pr-1160 May 14, 2025 12:23 Inactive
Copy link
Member

@tefkah tefkah left a comment

Choose a reason for hiding this comment

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

other than the bug with switching the view while creating the form it works well!

even though i know its temporary, i do feel like the behavior/look of the switcher could be slightly nicer. i put up this pr with some suggested changes: #1246

merge it in if you want, or not!

otherwise lgtm!

Comment on lines 176 to 178
}

const result = await handleFormToken({
params,
searchParams,
onExpired: ({ params, searchParams, result }) => {
return;
},
});

return (
Copy link
Member

Choose a reason for hiding this comment

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

nice that we can get rid of this logic, i totally forgot it was there! good that we can just rely on the magic link/invite functionality now rather than this weird edge case

Comment on lines +1 to +33
"use client";

import type { Dispatch, PropsWithChildren, SetStateAction } from "react";

import { createContext, useContext, useMemo, useState } from "react";

import type { Form } from "~/lib/server/form";

export type FormSwitcherContext = {
selectedForm?: Form;
setSelectedForm: Dispatch<SetStateAction<Form | undefined>>;
};

const FormSwitcherContext = createContext<FormSwitcherContext>({
setSelectedForm: () => {},
});

type Props = PropsWithChildren<{
defaultForm?: Form;
}>;

export const FormSwitcherProvider = ({ defaultForm, children }: Props) => {
const [selectedForm, setSelectedForm] = useState(defaultForm);

const value = useMemo(
() => ({ selectedForm, setSelectedForm }),
[selectedForm, setSelectedForm]
);

return <FormSwitcherContext.Provider value={value}>{children}</FormSwitcherContext.Provider>;
};

export const useFormSwitcherContext = () => useContext(FormSwitcherContext);
Copy link
Member

Choose a reason for hiding this comment

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

this doesn't seem to be used anywhere, maybe we can remove it?

Comment on lines 34 to 47
searchParams: Promise<
Record<string, string> & { pubTypeId: PubTypesId; form?: string; pubId?: PubsId }
>;
}) {
const searchParams = await props.searchParams;
const params = await props.params;
const { communitySlug } = params;

if (!searchParams.pubId) {
const sparams = new URLSearchParams(searchParams);
sparams.set("pubId", crypto.randomUUID());
redirect(`/c/${communitySlug}/pubs/create?${sparams.toString()}`);
}

Copy link
Member

Choose a reason for hiding this comment

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

I added this to get around the following issue (see vid):

  1. Go to create a Pub.
  2. A pubId is internally initialized using crypto.randomUUID() in bc the UploadElement needs to know about it
  3. Switch form
  4. Content is reloaded! therefore crypto.randomUUID() is called again!
  5. Create Pub
  6. Redirect fails, bc clientside pubid is different from server one

To solve this, I madet this page always have a UUID through the searchParam. This is not optimal, if you navigate back from the edit page to this page it will behave somewhat strangely. That could be solved by doing an additional check if a UUID is provided, namely to check whether that Pub already exists. If it does, try once more.

I'm not a big fan of this solution, as it forces an extra redirect.

Another solution is to properly propagate the changes in UUID on the server to the client. The problem with this is the following:

  1. Go to create a Pub.
  2. Upload a File.
  3. Switch the view
  4. Create Pub
  5. Upload file "PubId" no longer matches the pub it belongs to.

Maybe that's not a big deal and we should just do that, ill leave it to you!

Copy link
Member

Choose a reason for hiding this comment

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

The bug

Screen.Recording.2025-05-14.at.14.16.47.mov

@@ -188,7 +196,7 @@ export async function PubEditor(props: PubEditorProps) {
// Create the pubId before inserting into the DB if one doesn't exist.
// FileUpload needs the pubId when uploading the file before the pub exists
const isUpdating = !!pub?.id;
const pubId = pub?.id ?? (randomUUID() as PubsId);
const pubId = pub?.id ?? props.searchParams.pubId ?? (randomUUID() as PubsId);
Copy link
Member

Choose a reason for hiding this comment

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

this is the other place where the pubId searchParam is used, see above comment

@tefkah tefkah force-pushed the kalilsn/form-switcher branch from 9154242 to f24140d Compare May 14, 2025 13:04
@tefkah
Copy link
Member

tefkah commented May 14, 2025

and again...

i made this (#1247) a separate PR as it was messing with the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Auto-deploys a preview application
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update contributor role permissions to include form view/edit layer Form view switcher
3 participants