Skip to content

Commit

Permalink
feat: add post status
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeryVerkhoturov committed Jul 1, 2023
1 parent 92aa03a commit 632c748
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 42 deletions.
6 changes: 6 additions & 0 deletions prisma/migrations/20230701110435_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- CreateEnum
CREATE TYPE "LostAndFoundItemStatus" AS ENUM ('ACTIVE', 'EXPIRED', 'BLOCKED');

-- AlterTable
ALTER TABLE "LostAndFoundItem" ADD COLUMN "status" "LostAndFoundItemStatus" NOT NULL DEFAULT 'ACTIVE',
ALTER COLUMN "expires" SET DEFAULT NOW() + interval '1 week';
25 changes: 16 additions & 9 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ model Session {
}

model User {
id String @id @default(cuid())
id String @id @default(cuid())
name String?
nickname String @unique
nickname String @unique
socialNetworks UserSocialNetwork[]
email String? @unique
email String? @unique
emailVerified DateTime?
userInfo String? @db.VarChar(280)
role Role @default(USER)
userInfo String? @db.VarChar(280)
role Role @default(USER)
image String?
isBlocked Boolean @default(false)
isBlocked Boolean @default(false)
blockReason String?
accounts Account[]
sessions Session[]
Expand Down Expand Up @@ -88,6 +88,7 @@ model LostAndFoundItem {
description String @default("") @db.VarChar(512)
campus Campus
reason PostItemReason
status LostAndFoundItemStatus @default(ACTIVE)
images String[]
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
Expand All @@ -98,12 +99,18 @@ model LostAndFoundItem {
@@index([id], type: Hash)
}

enum LostAndFoundItemStatus {
ACTIVE
EXPIRED
BLOCKED
}

model LostAndFoundItemSocialNetworks {
id String @id @default(cuid())
id String @id @default(cuid())
lostAndFoundItemId String
lostAndFoundItem LostAndFoundItem @relation(fields: [lostAndFoundItemId], references: [id], onDelete: Cascade)
lostAndFoundItem LostAndFoundItem @relation(fields: [lostAndFoundItemId], references: [id], onDelete: Cascade)
userSocialNetworkId String
userSocialNetwork UserSocialNetwork @relation(fields: [userSocialNetworkId], references: [id], onDelete: Cascade)
userSocialNetwork UserSocialNetwork @relation(fields: [userSocialNetworkId], references: [id], onDelete: Cascade)
@@unique([lostAndFoundItemId, userSocialNetworkId])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function InfiniteScrollGridWithFilter(props: {
endMessage: ReactNode
}) {
const { enabledSortOption, checkedFilters } = useScrollGridStore((state) => state[props.reason])
const postsQuery = api.posts.infiniteItems.useInfiniteQuery(
const postsQuery = api.posts.infinitePosts.useInfiniteQuery(
{
limit: 12,
reason: props.reason,
Expand Down
6 changes: 5 additions & 1 deletion src/components/posts/my-posts-list/my-posts-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Link from 'next/link'
import { Campus } from '@/lib/campus'
import Image from 'next/image'
import classNames from 'classnames/dedupe'
import PostStatusBadge from '@/components/posts/post-status-badge/post-status-badge'

const pageSize = 25

Expand Down Expand Up @@ -101,7 +102,10 @@ export default function MyPostsList({ reason }: MyPostsListProps) {
<Link href={myPost.id} key={index}>
<li className='flex justify-between gap-x-6 py-5'>
<div className='min-w-0'>
<p className='text-sm font-semibold leading-6 text-gray-900'>{myPost.name}</p>
<div className='flex flex-row items-center space-x-2'>
<p className='text-sm font-semibold leading-6 text-gray-900'>{myPost.name}</p>
<PostStatusBadge status={myPost.status} />
</div>
<p className='mt-1 truncate text-xs leading-5 text-gray-500'>
{myPost.description}
</p>
Expand Down
52 changes: 30 additions & 22 deletions src/components/posts/overview/overview-post/overview-post.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import Window from '@/components/form/window'
import { type OverviewPost as OverviewPostData } from '@/lib/types/overview-post'
import { Campus } from '@/lib/campus'
import { formatDate } from '@/lib/format-date'
import { api } from '@/lib/api'
import { api, type RouterOutputs } from '@/lib/api'
import { useRouter } from 'next/router'
import { type PostItemReason } from '@prisma/client'
import { useState } from 'react'
import errorToast from '@/components/toasts/error-toast'
import DynamicOverviewPostImage from '@/components/posts/overview/overview-post-image/dynamic-overview-post-image'
import PostStatusBadge from '@/components/posts/post-status-badge/post-status-badge'

interface OverviewPostProps {
reason: PostItemReason
}

export default function OverviewPost(props: OverviewPostProps) {
const postId = useRouter().query.postId as string
const [post, setPost] = useState<OverviewPostData>()
api.posts.getPost.useQuery(
const [post, setPost] = useState<RouterOutputs['posts']['getPost']>()

const postQuery = api.posts.getPost.useQuery(
{ postId: postId, reason: props.reason },
{
onSuccess: (data) => {
Expand Down Expand Up @@ -57,25 +58,32 @@ export default function OverviewPost(props: OverviewPostProps) {
return (
<Window>
<div className='mx-auto grid max-w-2xl grid-cols-1 items-center gap-x-8 gap-y-16 px-4 py-24 sm:px-6 sm:py-32 lg:max-w-7xl lg:grid-cols-2 lg:px-8'>
<div>
<h2 className='text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl'>
{post?.name}
</h2>
<p className='mt-4 overflow-hidden text-ellipsis text-gray-500'>{post?.description}</p>
<dl className='mt-16 grid grid-cols-1 gap-x-6 gap-y-10 sm:grid-cols-2 sm:gap-y-16 lg:gap-x-8'>
{features.map((feature) => (
<div key={feature.name} className='border-t border-gray-200 pt-4'>
<dt className='font-medium text-gray-900'>{feature.name}</dt>
<dd className='mt-2 text-sm text-gray-500'>{feature.description}</dd>
{post && (
<>
<div>
<div className='flex flex-row items-center space-x-4'>
<h2 className='text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl'>
{post.name}
</h2>
<PostStatusBadge status={post.status} />
</div>
))}
</dl>
</div>
<div className='grid grid-cols-2 grid-rows-2 gap-4 sm:gap-6 lg:gap-8'>
{post?.images.map((image, index) => (
<DynamicOverviewPostImage key={index} src={image} />
))}
</div>
<p className='mt-4 overflow-hidden text-ellipsis text-gray-500'>{post.description}</p>
<dl className='mt-16 grid grid-cols-1 gap-x-6 gap-y-10 sm:grid-cols-2 sm:gap-y-16 lg:gap-x-8'>
{features.map((feature) => (
<div key={feature.name} className='border-t border-gray-200 pt-4'>
<dt className='font-medium text-gray-900'>{feature.name}</dt>
<dd className='mt-2 text-sm text-gray-500'>{feature.description}</dd>
</div>
))}
</dl>
</div>
<div className='grid grid-cols-2 grid-rows-2 gap-4 sm:gap-6 lg:gap-8'>
{post?.images.map((image, index) => (
<DynamicOverviewPostImage key={index} src={image} />
))}
</div>
</>
)}
</div>
</Window>
)
Expand Down
29 changes: 29 additions & 0 deletions src/components/posts/post-status-badge/post-status-badge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { type LostAndFoundItemStatus } from '@prisma/client'

interface PostStatusBadgeProps {
title?: string
status: LostAndFoundItemStatus
}

export default function PostStatusBadge({ title, status }: PostStatusBadgeProps) {
switch (status) {
case 'ACTIVE':
return (
<div className='inline-flex items-center rounded-md bg-green-50 px-2 py-1 text-xs font-medium text-green-700 ring-1 ring-inset ring-green-600/20'>
{title ?? 'Активен'}
</div>
)
case 'EXPIRED':
return (
<div className='inline-flex items-center rounded-md bg-yellow-50 px-2 py-1 text-xs font-medium text-yellow-800 ring-1 ring-inset ring-yellow-600/20'>
{title ?? 'Просрочен'}
</div>
)
case 'BLOCKED':
return (
<div className='inline-flex items-center rounded-md bg-red-50 px-2 py-1 text-xs font-medium text-red-700 ring-1 ring-inset ring-red-600/10'>
{title ?? 'Заблокирован'}
</div>
)
}
}
8 changes: 0 additions & 8 deletions src/lib/types/overview-post.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/server/api/routers/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SortOption } from '@/lib/types/sort-option'
import { TRPCError } from '@trpc/server'

export const postsRouter = createTRPCRouter({
infiniteItems: publicProcedure
infinitePosts: publicProcedure
.input(
z.object({
limit: z.number().min(1).max(16, 'Превышен лимит запроса').nullish(),
Expand Down Expand Up @@ -97,6 +97,7 @@ export const postsRouter = createTRPCRouter({
description: true,
campus: true,
images: true,
status: true,
created: true,
expires: true,
user: {
Expand Down

0 comments on commit 632c748

Please sign in to comment.