Skip to content

Commit

Permalink
add better form handling
Browse files Browse the repository at this point in the history
  • Loading branch information
qweliant committed May 9, 2024
1 parent 5168d67 commit 0cac468
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 53 deletions.
7 changes: 4 additions & 3 deletions core/app/(user)/communities/AddCommunityDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ 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>
<TooltipTrigger asChild>
<TooltipTrigger>
<DialogTrigger asChild>
<Button variant="outline" className="flex items-center gap-x-2">
<ListPlus size="16" /> Create Community
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
98 changes: 50 additions & 48 deletions core/app/(user)/communities/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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 Down Expand Up @@ -88,7 +88,7 @@ export const createCommunity = defineServerAction(async function createCommunity
.execute();

const [fields, member] = await Promise.all([pubFieldsPromise, memberPromise]);
await db
const pubTypesPromise = db
.with("core_pub_type", (db) =>
db
.insertInto("pub_types")
Expand All @@ -108,51 +108,52 @@ export const createCommunity = defineServerAction(async function createCommunity
)
.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);
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);

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

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

await db
const createPubPromise = db
.with("new_pubs", (db) =>
db
.insertInto("pubs")
Expand Down Expand Up @@ -241,6 +242,7 @@ export const createCommunity = defineServerAction(async function createCommunity
},
])
.execute();
await Promise.all([permissionPromise, moveConstraintPromise, createPubPromise]);
}
revalidatePath("/");
} catch (error) {
Expand Down
7 changes: 5 additions & 2 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 All @@ -18,7 +20,6 @@ import { MoreVertical } from "ui/icon";

import { UserAndMemberships } from "~/lib/types";
import { RemoveCommunityButton } from "./RemoveCommunityButton";
import Link from "next/link";

export type TableCommunity = {
id: string;
Expand Down Expand Up @@ -73,7 +74,9 @@ export const getCommunityTableColumns = ({ user }: { user: UserAndMemberships })
{
header: ({ column }) => <DataTableColumnHeader column={column} title="Slug" />,
accessorKey: "slug",
cell: ({ row }) => <Link href={`/communities/${row.getValue("slug")}`}>{row.original.slug}</Link>,
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 0cac468

Please sign in to comment.