Skip to content

Commit

Permalink
[spaces] add lists
Browse files Browse the repository at this point in the history
  • Loading branch information
sabovyan committed Dec 3, 2023
1 parent de3ca09 commit 3a1befd
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 52 deletions.
13 changes: 13 additions & 0 deletions app/spaces/[spaceId]/lists/[listId]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { SpaceItemParams } from '../../page';

type ListItemParams = SpaceItemParams & {
listId: string;
};
export default function ListItem({ params }: { params: ListItemParams }) {
console.log(params);
return (
<div>
<h1>{params.spaceId}</h1>
</div>
);
}
60 changes: 60 additions & 0 deletions app/spaces/[spaceId]/lists/add/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { revalidateTag } from 'next/cache';
import { RedirectType, redirect } from 'next/navigation';
import { z } from 'zod';

import { FormField } from '@/components/formField';
import { Button } from '@/components/ui/button';
import { auth } from '@/lib/auth';
import { prisma } from '@/lib/prisma';

const schema = z.object({
name: z.string()
});

export default function AddList({
params: { spaceId }
}: {
params: { spaceId: string };
}) {
return (
<div>
<form
action={async (formData: FormData) => {
'use server';
try {
const parsed = schema.parse({
name: formData.get('name')
});
const session = await auth();

const newList = await prisma.list.create({
data: {
name: parsed.name,
Space: {
connect: {
id: spaceId
}
},
creator: {
connect: {
id: session?.user.id
}
}
}
});

revalidateTag(`/spaces/${spaceId}`);

redirect(`/spaces/${spaceId}/lists/${newList.id}`);
} catch (err) {
console.log(err);
console.error('OOOPS');
}
}}
>
<FormField type="text" label="Name" name="name" />
<Button className="w-full mt-4">Submit</Button>
</form>
</div>
);
}
58 changes: 58 additions & 0 deletions app/spaces/[spaceId]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Link from 'next/link';
import { notFound } from 'next/navigation';

import { prisma } from '@/lib/prisma';

export type SpaceItemParams = { spaceId: string };

export default async function SingleSpace({
params: { spaceId: paramsSpaceId }
}: {
params: SpaceItemParams;
}) {
const space = await prisma.space.findUnique({
where: { id: paramsSpaceId },
include: { Lists: true }
});

if (!space) {
notFound();
}

return (
<section>
<div className="flex gap-4 mb-4 mx-4 items-center justify-start">
<h2 className="text-3xl border max-w-fit px-4 py-2 rounded-2xl bg-primary text-primary-foreground">
<Link href={`/spaces/${space.id}`}>☀︎ {space.name}</Link>
</h2>
<Link
href={`/spaces/${space.id}/lists/add`}
className="text-3xl border px-4 py-2 rounded-2xl"
>
+
</Link>
</div>
{!space.Lists.length ? (
<div>
<h2 className="text-2xl">No List was Found</h2>
</div>
) : (
<ul className="flex flex-wrap gap-2">
{space.Lists.map((list) => (
<li
key={list.id}
className="text-xl text-center min-w-[200px] p-2 rounded-lg border"
>
<Link
href={`/spaces/${space.id}/lists/${list.id}`}
className="block"
>
{list.name}
</Link>
</li>
))}
</ul>
)}
</section>
);
}
7 changes: 0 additions & 7 deletions app/spaces/edit/page.tsx

This file was deleted.

23 changes: 0 additions & 23 deletions app/spaces/layout.tsx

This file was deleted.

54 changes: 35 additions & 19 deletions app/spaces/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,42 @@ export default async function Spaces() {
}
});

console.log(spaces);

return (
<main className="flex">
{spaces.length ? (
<section>
<ul className="flex gap-2">
{spaces.map((sp) => (
<li
key={sp.id}
className="text-xl text-center min-w-[200px] p-2 rounded-lg border"
>
<Link href={`/spaces/${sp.id}`} className="block">
{sp.name}
</Link>
</li>
))}
</ul>
</section>
) : (
<div>no space was found</div>
)}
<main>
<header className="mb-8 flex gap-4">
<h2 className="text-2xl underline">
<Link href={`/spaces`}>Your Spaces</Link>
</h2>

<Link
href={`/spaces/add`}
className="text-xl text-center border rounded-lg min-w-[50px]"
>
+
</Link>
</header>
<div className="flex">
{spaces.length ? (
<section>
<ul className="flex flex-wrap gap-2">
{spaces.map((sp) => (
<li
key={sp.id}
className="text-xl text-center min-w-[200px] p-2 rounded-lg border"
>
<Link href={`/spaces/${sp.id}`} className="block">
{sp.name}
</Link>
</li>
))}
</ul>
</section>
) : (
<section>no space was found</section>
)}
</div>
</main>
);
}
17 changes: 17 additions & 0 deletions prisma/migrations/20231203122846_add_lists/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- CreateTable
CREATE TABLE "List" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"spaceId" TEXT NOT NULL,
"creatorId" TEXT NOT NULL,

CONSTRAINT "List_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "List" ADD CONSTRAINT "List_spaceId_fkey" FOREIGN KEY ("spaceId") REFERENCES "Space"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "List" ADD CONSTRAINT "List_creatorId_fkey" FOREIGN KEY ("creatorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
8 changes: 8 additions & 0 deletions prisma/migrations/20231203125043_list_to_lists/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `favorite` to the `List` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "List" ADD COLUMN "favorite" BOOLEAN NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- A unique constraint covering the columns `[creatorId,name,spaceId]` on the table `List` will be added. If there are existing duplicate values, this will fail.
*/
-- AlterTable
ALTER TABLE "List" ALTER COLUMN "favorite" DROP NOT NULL,
ALTER COLUMN "favorite" SET DEFAULT false;

-- CreateIndex
CREATE UNIQUE INDEX "List_creatorId_name_spaceId_key" ON "List"("creatorId", "name", "spaceId");
27 changes: 24 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ model User {
accounts Account[]
sessions Session[]
spaces Space[]
Lists List[]
}

model VerificationToken {
Expand All @@ -57,12 +58,32 @@ model VerificationToken {
}

model Space {
id String @id @default(cuid())
name String
id String @id @default(cuid())
name String
Lists List[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
creator User @relation(fields: [creatorId], references: [id])
creator User @relation(fields: [creatorId], references: [id])
creatorId String
@@unique([creatorId, name], name: "SpaceIdentifier")
}

model List {
id String @id @default(cuid())
name String
favorite Boolean? @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Space Space @relation(fields: [spaceId], references: [id])
spaceId String
creator User @relation(fields: [creatorId], references: [id])
creatorId String
@@unique([creatorId, name, spaceId], name: "ListIdentifier")
}

0 comments on commit 3a1befd

Please sign in to comment.