Skip to content

Commit

Permalink
[spaces] add routes and navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
sabovyan committed Nov 26, 2023
1 parent ea02e7a commit 2ff2693
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default function Home() {
return (
<main>
<h1>main content</h1>
<h1>hello</h1>
</main>
);
}
22 changes: 22 additions & 0 deletions app/spaces/add/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { FormField } from '@/components/formField';
import { Button } from '@/components/ui/button';

export default function NewSpace() {
return (
<div>
<h1>ADD NEW SPACE</h1>

<form>
<FormField type="text" label="Name" name="name" />
<FormField type="number" label="Price" name="price" />
<FormField
type="date"
label="Date"
name="date"
defaultValue={new Date().toLocaleDateString('en-CA')}
/>
<Button className="w-full mt-3">Submit</Button>
</form>
</div>
);
}
7 changes: 7 additions & 0 deletions app/spaces/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function NewSpace() {
return (
<div>
<h1>EDIT</h1>
</div>
);
}
41 changes: 41 additions & 0 deletions app/spaces/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { redirect } from 'next/navigation';

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

export default async function Groups() {
const session = await auth();

const email = session?.user?.email;

if (!email) {
redirect('/');
}

const user = await prisma.user.findUnique({
where: {
email
},
include: {
spaces: true
}
});

if (!user) {
// TODO handle it - either redirect it to sign it page or show not sign in view
throw new Error('handle it');
}

return (
<main>
<header>
<h2>Your Space Management</h2>
</header>
{user.spaces.length ? (
<div>your spaces</div>
) : (
<div>no space was found</div>
)}
</main>
);
}
5 changes: 4 additions & 1 deletion components/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { userAgent } from 'next/server';
import { auth } from '@/lib/auth';

// import { AddExpense } from './AddExpense/AddExpense';
import { SpacesNav } from './SpacesNav';
import { UserAccount } from './UserAccount/UserAccount';

export async function Nav() {
Expand All @@ -14,9 +15,11 @@ export async function Nav() {
const session = await auth();

return (
<nav className="fixed bottom-0 w-full justify-end items-center flex px-4 py-2 border">
<nav className="fixed bottom-0 w-full justify-end items-center flex px-4 py-2 border gap-3">
{/* TODO move to general place */}
{/* <AddExpense deviceType={deviceType || 'desktop'} /> */}
{session ? <SpacesNav /> : null}

<UserAccount session={session} />
</nav>
);
Expand Down
36 changes: 36 additions & 0 deletions components/SpacesNav.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use client';

import { Anchor } from './Anchor';
import { Button } from './ui/button';
import {
DropdownMenu,
DropdownMenuTrigger,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem
} from './ui/dropdown-menu';

export function SpacesNav() {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" size="default">
Spaces
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuGroup>
<DropdownMenuItem>
<Anchor href="/spaces/add">Add New Space</Anchor>
</DropdownMenuItem>
</DropdownMenuGroup>

<DropdownMenuGroup>
<DropdownMenuItem>
<Anchor href="/spaces">Go To Main Spaces Page</Anchor>
</DropdownMenuItem>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
);
}
8 changes: 3 additions & 5 deletions lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { PrismaClient } from '@prisma/client';

// export const prisma = new PrismaClient({
// log: ['query']
// });

export const prisma = new PrismaClient();
export const prisma = new PrismaClient({
log: ['query']
});
48 changes: 48 additions & 0 deletions prisma/migrations/20231126110633_spaces/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Warnings:
- You are about to drop the `UserGroup` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `_UserToUserGroup` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE "_UserToUserGroup" DROP CONSTRAINT "_UserToUserGroup_A_fkey";

-- DropForeignKey
ALTER TABLE "_UserToUserGroup" DROP CONSTRAINT "_UserToUserGroup_B_fkey";

-- DropTable
DROP TABLE "UserGroup";

-- DropTable
DROP TABLE "_UserToUserGroup";

-- CreateTable
CREATE TABLE "Spaces" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"userId" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "_SpacesToUser" (
"A" INTEGER NOT NULL,
"B" TEXT NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "_SpacesToUser_AB_unique" ON "_SpacesToUser"("A", "B");

-- CreateIndex
CREATE INDEX "_SpacesToUser_B_index" ON "_SpacesToUser"("B");

-- AddForeignKey
ALTER TABLE "Spaces" ADD CONSTRAINT "Spaces_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_SpacesToUser" ADD CONSTRAINT "_SpacesToUser_A_fkey" FOREIGN KEY ("A") REFERENCES "Spaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_SpacesToUser" ADD CONSTRAINT "_SpacesToUser_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Warnings:
- You are about to drop the column `userId` on the `Spaces` table. All the data in the column will be lost.
- Added the required column `adminId` to the `Spaces` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "Spaces" DROP CONSTRAINT "Spaces_userId_fkey";

-- AlterTable
ALTER TABLE "Spaces" DROP COLUMN "userId",
ADD COLUMN "adminId" TEXT NOT NULL;

-- AddForeignKey
ALTER TABLE "Spaces" ADD CONSTRAINT "Spaces_adminId_fkey" FOREIGN KEY ("adminId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
11 changes: 11 additions & 0 deletions prisma/migrations/20231126111343_remove_admin/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- You are about to drop the column `adminId` on the `Spaces` table. All the data in the column will be lost.
*/
-- DropForeignKey
ALTER TABLE "Spaces" DROP CONSTRAINT "Spaces_adminId_fkey";

-- AlterTable
ALTER TABLE "Spaces" DROP COLUMN "adminId";
44 changes: 44 additions & 0 deletions prisma/migrations/20231126112121_spaces_typo/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Warnings:
- You are about to drop the `Spaces` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `_SpacesToUser` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE "_SpacesToUser" DROP CONSTRAINT "_SpacesToUser_A_fkey";

-- DropForeignKey
ALTER TABLE "_SpacesToUser" DROP CONSTRAINT "_SpacesToUser_B_fkey";

-- DropTable
DROP TABLE "Spaces";

-- DropTable
DROP TABLE "_SpacesToUser";

-- CreateTable
CREATE TABLE "Space" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "_SpaceToUser" (
"A" INTEGER NOT NULL,
"B" TEXT NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "_SpaceToUser_AB_unique" ON "_SpaceToUser"("A", "B");

-- CreateIndex
CREATE INDEX "_SpaceToUser_B_index" ON "_SpaceToUser"("B");

-- AddForeignKey
ALTER TABLE "_SpaceToUser" ADD CONSTRAINT "_SpaceToUser_A_fkey" FOREIGN KEY ("A") REFERENCES "Space"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_SpaceToUser" ADD CONSTRAINT "_SpaceToUser_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
14 changes: 4 additions & 10 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ model Session {
}

model User {
id String @id @default(cuid())
id String @id @default(cuid())
name String?
email String? @unique
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
UserGroup UserGroup[]
spaces Space[]
}

model VerificationToken {
Expand All @@ -56,14 +56,8 @@ model VerificationToken {
@@unique([identifier, token])
}

model UserGroup {
model Space {
id Int @id @default(autoincrement())
name String
users User[]
}

// model Expense {
// id Int @id @default(autoincrement())
// name String
// price
// }

0 comments on commit 2ff2693

Please sign in to comment.