Skip to content

Commit

Permalink
Merge pull request #36 from fastcampus-711/develop
Browse files Browse the repository at this point in the history
PR_20240529 (1)
  • Loading branch information
alreadyme24 authored May 29, 2024
2 parents 0079659 + ece9866 commit b9c6686
Show file tree
Hide file tree
Showing 13 changed files with 559 additions and 238 deletions.
2 changes: 1 addition & 1 deletion app/api/community/[category]/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { NextRequest } from "next/server"

export const GET = async (
request: NextRequest,
context: { params: { category: string; id: string } }
context: { params: { category: string; id: number } }
) => {
const { category, id } = context.params

Expand Down
20 changes: 13 additions & 7 deletions app/api/community/[category]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ export const GET = async (
const { category } = context.params
const { searchParams } = request.nextUrl
// let url = `http://localhost:3001/${category}`
let url = `https://711.ha-ving.store/boards/${category}`

const keyword = searchParams.get("keyword")
const catid =
searchParams.get("catid") === "undefined" ||
searchParams.get("catid") === null
? "0"
: searchParams.get("catid")
console.log(keyword)
console.log(catid)

let url = `https://711.ha-ving.store/boards/${category}?category-id=${catid}`
// searchParams가 비어있지 않고, title이 존재하면서 값이 "undefined"가 아닌 경우에만 title을 URL에 추가합니다.
if (
searchParams &&
searchParams.get("keyowrd") !== undefined &&
searchParams.get("keyword") !== "undefined"
) {
url += `?keyword=${searchParams.get("keyword")}`
if (searchParams && keyword !== undefined && keyword !== "undefined") {
// url += `?keyword=${searchParams.get("keyword")}`
url = `https://711.ha-ving.store/boards/${category}?category-id=${catid}&keyword=${keyword}`
}

try {
Expand Down
6 changes: 3 additions & 3 deletions app/community/[category]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export default async function Page({
searchParams
}: {
params: { category: string }
searchParams: { title: string }
searchParams: { keyword: string; catid: number }
}) {
const { category } = params
const { title } = searchParams
const { keyword, catid } = searchParams

const res = await fetch(
// `${process.env.NEXT_PUBLIC_CLIENT_URL}/api/community/${category}?keyword=${title}`
`https://aptner.ha-ving.store/api/community/${category}?keyword=${title}`
`https://aptner.ha-ving.store/api/community/${category}?catid=${catid}&keyword=${keyword}`
)
const responseData = await res.json()
let componentProps = { responseData, category }
Expand Down
218 changes: 11 additions & 207 deletions app/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -1,138 +1,21 @@
"use client"

import PrimaryButton from "@/components/button/PrimaryButton"
import DropDown from "@/components/dropdown/DropDown"
import { getToday } from "@/utils/todayUtil"
import { useEffect, useState, useMemo } from "react"
import { useSelector, useDispatch } from "react-redux"
import { setCategory } from "@/redux/slices/categorySlice"
import { AppDispatch } from "@/redux/store"

type Options = {
value: number
name: string
}
import EditFrees from "@/components/edit/EditFrees"
import EditMarkets from "@/components/edit/EditMarkets"
import EditQna from "@/components/edit/EditQna"

export default function Edit() {
const category = useSelector((state: any) => state.category)
const dispatch = useDispatch<AppDispatch>()
const [selectedBoard, setSelectedBoard] = useState<number>(0)
const [selectedType, setSelectedType] = useState<number>(0)
const [typeOptions, setTypeOptions] = useState<Options[]>([])

const boardOptions: Options[] = [
{ value: 11, name: "자유게시판" },
{ value: 12, name: "나눔장터" },
{ value: 13, name: "QnA" }
]

const optionsA = useMemo(
() => [
{ value: 1, name: "생활・편의" },
{ value: 2, name: "음식・카페" },
{ value: 3, name: "병원・약국" },
{ value: 4, name: "수리・시공" },
{ value: 5, name: "투자・부동산" },
{ value: 6, name: "교육・육아" },
{ value: 7, name: "아파트・동네소식" },
{ value: 8, name: "여행" },
{ value: 9, name: "살림정보" },
{ value: 10, name: "모임・동호회" },
{ value: 11, name: "기타" }
],
[]
)

const optionsB = useMemo(
() => [
{ value: 999, name: "중고거래" },
{ value: 999, name: "무료나눔" }
],
[]
)

useEffect(() => {
if (category.value === "frees") {
setSelectedBoard(11)
} else if (category.value === "markets") {
setSelectedBoard(12)
} else if (category.value === "qnas") {
setSelectedBoard(13)
}
}, [category.value])

useEffect(() => {
if (selectedBoard === 11) {
setTypeOptions(optionsA)
} else if (selectedBoard === 12) {
setTypeOptions(optionsB)
} else {
setTypeOptions([])
}
}, [selectedBoard, optionsA, optionsB])

const handleBoardChange = (value: number) => {
setSelectedBoard(value)
setSelectedType(0)
if (value === 11) {
dispatch(setCategory("frees"))
} else if (value === 12) {
dispatch(setCategory("markets"))
} else if (value === 13) {
dispatch(setCategory("qnas"))
}
}

const handleTypeChange = (value: number) => {
setSelectedType(value)
}

const handleUpdate = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
const formData = new FormData(e.currentTarget)
// const issaled = "onsale"
// const isnew = true
// const category = formData.get("category")
const category_id = selectedType
const title = formData.get("title")
const content = formData.get("content")
const visible = true
// const price = formData.get("price")
// const nickname = "userNickname"
// const date = getToday()
const updatedItem = {
// issaled: issaled,
// isnew: isnew,
// category: category,
category_id: category_id,
title: title,
content: content,
visible: visible
// price: price,
// nickname: nickname,
// date: date
}
// const url = `http://localhost:3001/${category}`
const url = `https://711.ha-ving.store/boards/${category.value}`
console.log(updatedItem)
try {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(updatedItem)
})
let contentComponent

if (response.ok) {
const responseData = await response.json()
console.log("게시물 등록 성공:", responseData)
} else {
console.error("게시물 등록을 실패했습니다.")
}
} catch (error) {
console.error("에러 발생:", error)
}
if (category.value === "frees") {
contentComponent = <EditFrees />
} else if (category.value === "markets") {
contentComponent = <EditMarkets />
} else if (category.value === "qnas") {
contentComponent = <EditQna />
}

return (
Expand All @@ -145,86 +28,7 @@ export default function Edit() {
게시글 작성 페이지
</p>
</div>
<form onSubmit={handleUpdate}>
<table className="w-full">
<tbody>
<tr>
<td className="w-32 text-grey_900 text-lg font-medium">분류</td>
<td className="flex h-[88px] items-center">
<div className="flex flex-1 gap-4">
<DropDown
label="게시판"
options={boardOptions}
event={handleBoardChange}
initialValue={selectedBoard}
/>
{selectedBoard !== 13 && (
<DropDown
label="타입"
options={typeOptions}
event={handleTypeChange}
/>
)}
</div>
{selectedBoard === 12 && (
<tr>
<td className="w-24 text-justify break-all text-grey_900 text-lg font-medium px-4 before:content-[''] before:inline-block before:w-full after:content-[''] after:inline-block after:w-full">
가 격
</td>
<td>
<input
type="text"
name="price"
placeholder="거래할 물품의 금액을 입력하세요"
className="w-[280px] h-12 p-2 border rounded-lg"
/>
</td>
</tr>
)}
</td>
</tr>
<tr>
<td className="text-grey_900 text-lg font-medium">제목</td>
<td className="py-4">
<textarea
name="title"
placeholder="제목을 입력해주세요 (최대 100자)"
maxLength={100}
className="w-full h-20 bg-grey_50 p-4 rounded-xl"
/>
</td>
</tr>
<tr>
<td className="text-grey_900 text-lg font-medium">내용</td>
<td className="py-4">
<textarea
name="content"
placeholder="내용을 입력해주세요 (최대 150자)"
maxLength={150}
className="w-full h-40 bg-grey_50 p-4 rounded-xl"
/>
</td>
</tr>
<tr>
<td className="text-grey_900 text-lg font-medium">첨부파일</td>
<td className="py-4">
<input type="file" />
</td>
</tr>
</tbody>
</table>
<div className="flex gap-2 justify-end">
<PrimaryButton
label="취소하기"
className="h-[44px] items-center leading-3"
/>
<PrimaryButton
label="등록하기"
className="h-[44px] items-center leading-3"
/>
</div>
</form>
{contentComponent}
</div>
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion components/CommunitySearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function CommunitySearch({
const title = formData.get("title")
console.log(title)
console.log(category)
router.push(`/community/${category}?title=${title}`)
router.push(`/community/${category}?keyword=${title}`)
if (inputRef.current) {
inputRef.current.value = ""
}
Expand Down
Loading

0 comments on commit b9c6686

Please sign in to comment.