Skip to content

Commit

Permalink
rest
Browse files Browse the repository at this point in the history
  • Loading branch information
sabovyan committed Sep 15, 2024
1 parent 0e971fc commit 2b68db0
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 131 deletions.
File renamed without changes.
45 changes: 43 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
export default function Home() {
import { SpaceList } from '@/components/ListItem';
import { auth } from '@/lib/auth';
import { prisma } from '@/lib/prisma';

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

if (!session) {
return (
<main>
<h1>hello</h1>
</main>
);
}

const lists = await prisma.list.findMany({
where: {
favorite: true,
creatorId: session.user.id
},
include: {
items: {
include: {
creator: {
select: {
name: true
}
}
}
}
}
});

return (
<main>
<h1>hello</h1>
<section className="px-2">
<h2 className="text-4xl">Favorites</h2>
{lists.length > 0 && (
<ul className="flex flex-col gap-4 mt-4 max-w-[450px]">
{lists.map((list) => (
<SpaceList list={list} key={list.id} />
))}
</ul>
)}
</section>
</main>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type ListItems = Array<{

type Props<List extends ListItems> = {
type: 'ONGOING' | 'COMPLETED';
items: List;
items?: List;
listId: string;
spaceId: string;
title: string;
title?: string;
};

export function ListComponent<T extends ListItems>({
Expand All @@ -29,7 +29,7 @@ export function ListComponent<T extends ListItems>({
}: Props<T>) {
return (
<>
<h3 className="text-lg mt-8">{title}</h3>
{title && <h3 className="text-lg mt-8">{title}</h3>}
<ul
className={clsx(
'flex flex-col gap-4 mt-4 max-w-[450px]',
Expand Down
78 changes: 1 addition & 77 deletions app/spaces/[spaceId]/lists/[listId]/@list/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { prisma } from '@/lib/prisma';
import { isCompleted } from '@/utils/listItem';

import { ListComponent } from './components/ListCompoent';
import { SpaceItemParams } from '../../../page';
Expand All @@ -7,21 +8,6 @@ type ListItemParams = SpaceItemParams & {
listId: string;
};

const isCompleted = (currentDate: Date | null) => {
if (currentDate === null) return false;

const diff =
(new Date(currentDate).getTime() - new Date().getTime()) /
1000 /
60 /
60 /
24;

const isCompleted = diff < 1;

return isCompleted;
};

export default async function List({ params }: { params: ListItemParams }) {
const items = await prisma.listItem.findMany({
where: {
Expand Down Expand Up @@ -65,68 +51,6 @@ export default async function List({ params }: { params: ListItemParams }) {
spaceId={params.spaceId}
title="Completed"
/>

{/* <ul className="flex flex-col gap-4 mt-4 max-w-[450px]"> */}
{/* {ongoingItems?.map((item) => { */}
{/* const isDaily = item.type === 'DAILY'; */}
{/**/}
{/* return ( */}
{/* <li */}
{/* key={item.id} */}
{/* className="flex gap-4 items-center justify-between border" */}
{/* > */}
{/* <div className="flex gap-4 items-center"> */}
{/* <Toggle title="toggle recursive state" pressed={isDaily}> */}
{/* <Image */}
{/* src={recurringIcon} */}
{/* alt="recurring icon" */}
{/* width={24} */}
{/* height={24} */}
{/* className={`${isDaily ? 'opacity-100' : 'opacity-20'}`} */}
{/* /> */}
{/* </Toggle> */}
{/* <Label className="text-sm mb-0"> */}
{/* {item.name}{' '} */}
{/* <span className="text-xs text-gray-400"> */}
{/* (By {item.creator.name?.split(' ')[0]}) */}
{/* </span> */}
{/* </Label> */}
{/* </div> */}
{/* <div className="flex gap-2"> */}
{/* <form */}
{/* action={async (formData) => { */}
{/* 'use server'; */}
{/* formData.append('listId', params.listId); */}
{/* formData.append('listItemId', item.id); */}
{/* formData.append('spaceId', params.spaceId); */}
{/**/}
{/* await markItemAsComplete(formData); */}
{/* }} */}
{/* > */}
{/* <Toggle type="submit" aria-label="complete"> */}
{/* &#x2713; */}
{/* </Toggle> */}
{/* </form> */}
{/* <form */}
{/* action={async (formData) => { */}
{/* 'use server'; */}
{/* formData.append('listId', params.listId); */}
{/* formData.append('listItemId', item.id); */}
{/* formData.append('spaceId', params.spaceId); */}
{/* formData.append('completedAt', Date.now().toString()); */}
{/**/}
{/* await deleteListItem(formData); */}
{/* }} */}
{/* > */}
{/* <Toggle type="submit" aria-label="remove"> */}
{/* &#x292B; */}
{/* </Toggle> */}
{/* </form> */}
{/* </div> */}
{/* </li> */}
{/* ); */}
{/* })} */}
{/* </ul> */}
</>
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/spaces/[spaceId]/lists/[listId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default async function List({ params }: { params: ListItemParams }) {
if (!id) {
redirect('/');
}

const list = await prisma.list.findUnique({
where: { id: params.listId }
});
Expand Down
20 changes: 2 additions & 18 deletions app/spaces/[spaceId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Link from 'next/link';
import { notFound, redirect } from 'next/navigation';

import { FavoriteListButton } from '@/components/FavoriteListButton';
import { SpaceList } from '@/components/ListItem';
import { auth } from '@/lib/auth';
import { prisma } from '@/lib/prisma';

Expand Down Expand Up @@ -49,23 +49,7 @@ export default async function SingleSpace({
) : (
<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 flex justify-between"
>
<Link
// @ts-ignore
href={`/spaces/${space.id}/lists/${list.id}`}
className="block flex-grow"
>
{list.name}
</Link>
<FavoriteListButton
spaceId={space.id}
listId={list.id}
isFave={list.favorite}
/>
</li>
<SpaceList key={list.id} list={list} />
))}
</ul>
)}
Expand Down
73 changes: 73 additions & 0 deletions components/ListItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { List, ListItem } from '@prisma/client';
import Link from 'next/link';

import { ListComponent } from '@/app/spaces/[spaceId]/lists/[listId]/@list/components/ListCompoent';
import { isCompleted } from '@/utils/listItem';

import { FavoriteListButton } from './FavoriteListButton';

type Props = {
list: List & {
items?: (ListItem & { creator: { name: string | null } })[];
};
isListItem?: boolean;
};

export function SpaceList({ list, isListItem }: Props) {
const { ongoingItems } = list.items?.reduce<{
ongoingItems: NonNullable<typeof list.items>;
completedItems: typeof list.items;
}>(
(acc, item) => {
if (isCompleted(item.completedAt)) {
acc.completedItems.push(item);

return acc;
}

acc.ongoingItems.push(item);

return acc;
},
{ ongoingItems: [], completedItems: [] }
) || { ongingItems: [], completedItems: [] };

return (
<ItemWrapper key={list.id} asListItem={isListItem}>
<div className="text-xl text-center min-w-[200px] p-2 rounded-lg border flex justify-between">
<Link
href={`/spaces/${list.spaceId}/lists/${list.id}`}
className="block flex-grow"
>
{list.name}
</Link>
<FavoriteListButton
spaceId={list.spaceId}
listId={list.id}
isFave={list.favorite}
/>
</div>
<ListComponent
type="ONGOING"
spaceId={list.spaceId}
listId={list.id}
items={ongoingItems}
/>
</ItemWrapper>
);
}

function ItemWrapper({
children,
asListItem = true
}: {
children: React.ReactNode;
asListItem?: boolean;
className?: string;
}) {
if (asListItem) {
return <li>{children}</li>;
}

return <div>{children}</div>;
}
31 changes: 0 additions & 31 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions utils/listItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const isCompleted = (currentDate: Date | null) => {
if (currentDate === null) return false;

const diff =
(new Date(currentDate).getTime() - new Date().getTime()) /
1000 /
60 /
60 /
24;

const isCompleted = diff < 1;

return isCompleted;
};

0 comments on commit 2b68db0

Please sign in to comment.