Skip to content

Commit

Permalink
Fix: Community Creation Error (#344)
Browse files Browse the repository at this point in the history
* loook in not at

* should turn the other awaits into Promisse.all statements so if any fails this throws instead of still creating

* add better form handling

* put in asChild

* change var name

---------

Co-authored-by: qweliant <[email protected]>
  • Loading branch information
qweliant and qweliant authored May 14, 2024
1 parent 9b56cb5 commit 91b3a91
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 69 deletions.
5 changes: 3 additions & 2 deletions core/app/(user)/communities/AddCommunityDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { AddCommunityForm } from "./AddCommunityForm";

type Props = { user: any };
export const AddCommunity = (props: Props) => {
const [open, setOpen] = React.useState(false);
return (
<Dialog>
<Dialog open={open} onOpenChange={setOpen}>
<TooltipProvider>
<Tooltip>
<TooltipContent> Create a new community</TooltipContent>
Expand All @@ -26,7 +27,7 @@ export const AddCommunity = (props: Props) => {
</Tooltip>
</TooltipProvider>
<DialogContent>
<AddCommunityForm user={props.user} />
<AddCommunityForm user={props.user} setOpen={setOpen} />
</DialogContent>
</Dialog>
);
Expand Down
2 changes: 2 additions & 0 deletions core/app/(user)/communities/AddCommunityForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const communityCreateFormSchema = z.object({

type Props = {
user: any;
setOpen: (open: false) => void;
};

export const AddCommunityForm = (props: Props) => {
Expand All @@ -30,6 +31,7 @@ export const AddCommunityForm = (props: Props) => {
async function onSubmit(data: z.infer<typeof communityCreateFormSchema>) {
const result = await runCreateCommunity({ ...data, user: props.user });
if (didSucceed(result)) {
props.setOpen(false);
toast({
title: "Success",
description: "Community created",
Expand Down
145 changes: 78 additions & 67 deletions core/app/(user)/communities/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { v4 as uuidv4 } from "uuid";

import { expect } from "utils";

import type { TableCommunity } from "./getCommunityTableColumns";
import type { CommunitiesId } from "~/kysely/types/public/Communities";
import type { PubTypesId } from "~/kysely/types/public/PubTypes";
import type { UsersId } from "~/kysely/types/public/Users";
import type { UserAndMemberships } from "~/lib/types";
import { corePubFields } from "~/actions/corePubFields";
import { db } from "~/kysely/database";
import { CommunitiesId } from "~/kysely/types/public/Communities";
import { PubTypesId } from "~/kysely/types/public/PubTypes";
import { UsersId } from "~/kysely/types/public/Users";
import { defineServerAction } from "~/lib/server/defineServerAction";
import { slugifyString } from "~/lib/string";
import { UserAndMemberships } from "~/lib/types";
import prisma from "~/prisma/db";
import { crocCrocId } from "~/prisma/exampleCommunitySeeds/croccroc";
import { unJournalId } from "~/prisma/exampleCommunitySeeds/unjournal";
import { TableCommunity } from "./getCommunityTableColumns";

export const createCommunity = defineServerAction(async function createCommunity({
user,
Expand Down Expand Up @@ -44,7 +43,7 @@ export const createCommunity = defineServerAction(async function createCommunity
try {
const communityExists = await db
.selectFrom("communities")
.selectAll() // or `selectAll()` etc
.selectAll()
.where("slug", "=", `${slug}`)
.executeTakeFirst();

Expand All @@ -66,7 +65,12 @@ export const createCommunity = defineServerAction(async function createCommunity
.executeTakeFirst()
);
const communityUUID = c.id as CommunitiesId;
const member = await db

const pubTypeId: string = uuidv4();

const corePubSlugs = corePubFields.map((field) => field.slug);

const memberPromise = db
.insertInto("members")
.values({
user_id: user.id as UsersId,
Expand All @@ -76,78 +80,79 @@ export const createCommunity = defineServerAction(async function createCommunity
.returning("id")
.executeTakeFirst();

const pubTypeId: string = uuidv4();

const corePubSlugs = corePubFields.map((field) => field.slug);

const [title] = await db
const pubFieldsPromise = db
.selectFrom("pub_fields")
.selectAll()
.where("pub_fields.slug", "=", corePubSlugs)
.where("pub_fields.slug", "in", corePubSlugs)
.execute();

await db
const [fields, member] = await Promise.all([pubFieldsPromise, memberPromise]);
const pubTypesPromise = db
.with("core_pub_type", (db) =>
db
.insertInto("pub_types")
.values({
id: pubTypeId as PubTypesId,
name: "Title Pub That Only List Titles",
name: "Submission ",
community_id: c.id as CommunitiesId,
})
.returning("id")
)
.insertInto("_PubFieldToPubType")
.values((eb) => ({
A: title.id,
B: eb.selectFrom("core_pub_type").select("id"),
}))
.values((eb) =>
fields.map((field) => ({
A: field.id,
B: eb.selectFrom("core_pub_type").select("id"),
}))
)
.execute();
const stages = (
await db
.insertInto("stages")
.values([
{
community_id: communityUUID,
name: "Submitted",
order: "aa",
},
{
community_id: communityUUID,
name: "Ask Author for Consent",
order: "bb",
},
{
community_id: communityUUID,
name: "To Evaluate",
order: "cc",
},
{
community_id: communityUUID,
name: "Under Evaluation",
order: "dd",
},
{
community_id: communityUUID,
name: "In Production",
order: "ff",
},
{
community_id: communityUUID,
name: "Published",
order: "gg",
},
{
community_id: communityUUID,
name: "Shelved",
order: "hh",
},
])
.returning("id")
.execute()
).map((x) => x.id);

await db
const stagesPromise = db
.insertInto("stages")
.values([
{
community_id: communityUUID,
name: "Submitted",
order: "aa",
},
{
community_id: communityUUID,
name: "Ask Author for Consent",
order: "bb",
},
{
community_id: communityUUID,
name: "To Evaluate",
order: "cc",
},
{
community_id: communityUUID,
name: "Under Evaluation",
order: "dd",
},
{
community_id: communityUUID,
name: "In Production",
order: "ff",
},
{
community_id: communityUUID,
name: "Published",
order: "gg",
},
{
community_id: communityUUID,
name: "Shelved",
order: "hh",
},
])
.returning("id")
.execute();

const [_, stagesReturn] = await Promise.all([pubTypesPromise, stagesPromise]);
const stages = stagesReturn.map((stage) => stage.id);

const permissionPromise = db
.with("new_permission", (db) =>
db
.insertInto("permissions")
Expand Down Expand Up @@ -177,7 +182,7 @@ export const createCommunity = defineServerAction(async function createCommunity
])
.execute();

await db
const moveConstraintPromise = db
.insertInto("move_constraint")
.values([
{
Expand All @@ -204,7 +209,7 @@ export const createCommunity = defineServerAction(async function createCommunity
])
.execute();

await db
const createPubPromise = db
.with("new_pubs", (db) =>
db
.insertInto("pubs")
Expand All @@ -226,11 +231,17 @@ export const createCommunity = defineServerAction(async function createCommunity
.values((eb) => [
{
pub_id: eb.selectFrom("new_pubs").select("new_pubs.id"),
field_id: title.id,
field_id: fields.find((field) => field.slug === "pubpub:title")!.id,
value: '"The Activity of Slugs I. The Induction of Activity by Changing Temperatures"',
},
{
pub_id: eb.selectFrom("new_pubs").select("new_pubs.id"),
field_id: fields.find((field) => field.slug === "pubpub:content")!.id,
value: '"LONG LIVE THE SLUGS"',
},
])
.execute();
await Promise.all([permissionPromise, moveConstraintPromise, createPubPromise]);
}
revalidatePath("/");
} catch (error) {
Expand Down
5 changes: 5 additions & 0 deletions core/app/(user)/communities/getCommunityTableColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import type { ColumnDef } from "@tanstack/react-table";

import Link from "next/link";

import { Avatar, AvatarFallback, AvatarImage } from "ui/avatar";
import { Badge } from "ui/badge";
import { Button } from "ui/button";
Expand Down Expand Up @@ -72,6 +74,9 @@ export const getCommunityTableColumns = ({ user }: { user: UserAndMemberships })
{
header: ({ column }) => <DataTableColumnHeader column={column} title="Slug" />,
accessorKey: "slug",
cell: ({ row }) => (
<Link href={`/c/${row.getValue("slug")}/pubs`}>{row.original.slug}</Link>
),
},
{
header: ({ column }) => <DataTableColumnHeader column={column} title="Created" />,
Expand Down

0 comments on commit 91b3a91

Please sign in to comment.