Skip to content

Commit 9154242

Browse files
committed
fix: fix id mismatch on creation if switching forms
1 parent f24140d commit 9154242

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

core/app/c/[communitySlug]/pubs/create/page.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Metadata } from "next";
22

33
import { notFound, redirect } from "next/navigation";
44

5-
import { type PubTypesId } from "db/public";
5+
import { type PubsId, type PubTypesId } from "db/public";
66
import { Button } from "ui/button";
77
import { Label } from "ui/label";
88

@@ -31,12 +31,20 @@ export async function generateMetadata(props: {
3131

3232
export default async function Page(props: {
3333
params: Promise<{ communitySlug: string }>;
34-
searchParams: Promise<Record<string, string> & { pubTypeId: PubTypesId; form?: string }>;
34+
searchParams: Promise<
35+
Record<string, string> & { pubTypeId: PubTypesId; form?: string; pubId?: PubsId }
36+
>;
3537
}) {
3638
const searchParams = await props.searchParams;
3739
const params = await props.params;
3840
const { communitySlug } = params;
3941

42+
if (!searchParams.pubId) {
43+
const sparams = new URLSearchParams(searchParams);
44+
sparams.set("pubId", crypto.randomUUID());
45+
redirect(`/c/${communitySlug}/pubs/create?${sparams.toString()}`);
46+
}
47+
4048
const { user } = await getPageLoginData();
4149

4250
const community = await findCommunityBySlug(communitySlug);

core/app/components/pubs/PubEditor/PubEditor.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ const getRelatedPubData = async ({
103103
};
104104

105105
export type PubEditorProps = {
106-
searchParams: { relatedPubId?: PubsId; slug?: string; pubTypeId?: PubTypesId; form?: string };
106+
searchParams: {
107+
relatedPubId?: PubsId;
108+
slug?: string;
109+
pubTypeId?: PubTypesId;
110+
form?: string;
111+
// used when creating a new pub
112+
pubId?: PubsId;
113+
};
107114
htmlFormId?: string;
108115
formSlug?: string;
109116
} & (
@@ -189,7 +196,7 @@ export async function PubEditor(props: PubEditorProps) {
189196
// Create the pubId before inserting into the DB if one doesn't exist.
190197
// FileUpload needs the pubId when uploading the file before the pub exists
191198
const isUpdating = !!pub?.id;
192-
const pubId = pub?.id ?? (randomUUID() as PubsId);
199+
const pubId = pub?.id ?? props.searchParams.pubId ?? (randomUUID() as PubsId);
193200

194201
if (pub === undefined) {
195202
if (props.searchParams.pubTypeId) {

0 commit comments

Comments
 (0)