From f7ad8fb45abced0bbd5fdc78cfac9b47cce4082d Mon Sep 17 00:00:00 2001 From: ElyasMehraein Date: Tue, 30 Apr 2024 18:40:09 +0330 Subject: [PATCH] last test --- app/all/page.js | 113 +++++++++++++++--- app/api/allBusinesses/route.js | 21 ++++ .../templates/allBusinesses/AllBusinesses.jsx | 87 -------------- 3 files changed, 117 insertions(+), 104 deletions(-) create mode 100644 app/api/allBusinesses/route.js delete mode 100644 components/templates/allBusinesses/AllBusinesses.jsx diff --git a/app/all/page.js b/app/all/page.js index 08fb550..c95abf9 100644 --- a/app/all/page.js +++ b/app/all/page.js @@ -1,23 +1,102 @@ -import connectToDB from "@/configs/db"; -import BusinessModel from "@/models/Business"; -import dynamic from 'next/dynamic' -const AllBusinesses = dynamic(() => import("@/components/templates/allBusinesses/AllBusinesses")) +"use client" +import { AppBar, Avatar, Box, Button, Container, List, ListItem, ListItemAvatar, ListItemButton, ListItemIcon, ListItemText, TextField, Toolbar, Typography } from '@mui/material' +import { useEffect, useState } from 'react' +import { useRouter } from 'next/navigation' +import { AllBusinessesText, selectGuild } from '@/components/typoRepo'; +import { Accordion, AccordionDetails, Chip } from "@mui/material"; +import QuestionMarkOutlinedIcon from '@mui/icons-material/QuestionMarkOutlined'; +import ArrowForwardIcon from '@mui/icons-material/ArrowForward'; +import ItsAvatar from '@/components/modules/ItsAvatar' +// import dynamic from 'next/dynamic' +// const ShowMyLocation = dynamic(() => import('@/components/modules/ShowMyLocation'), { ssr: false }) -const page = async function () { - await connectToDB(); - const businesses = JSON.parse(JSON.stringify(await BusinessModel.find({}, - "businessName bio businessBrand isAvatar").catch(err => { - console.error(err); - }))); +export default function AllBusinesses() { + const router = useRouter() + const [expanded, setExpanded] = useState(false); + const [mounted, setMounted] = useState(false); + const [businesses, setBusinesses] = useState(false); + useEffect(() => { + const getBusinesses = async () => { + try { + const res = await fetch("/api/allBusinesses", { method: "GET" }) + if (res.status === 200) { + const data = await res.json() + setBusinesses(data.data) + } else if ((res.status === 403)) { + console.log("unauthorized access"); + } + } catch (error) { + console.error("Error fetching Businesses:", error); + } + } + getBusinesses() + setMounted(true) + }, []) - return ( - - //

تست

- - ); + const goToIndex = () => { + router.push("/") + } + return (mounted && + <> + + + + + + + + + + + setExpanded(!expanded)} + icon={} + /> + + + + + {businesses ? businesses.map((business) => { + return ( + + + router.push(`/${business.businessName}`)}> + + + + + + + + + + ) + }) : + کسب و کاری برای نمایش وجود ندارد + } + + + + ) } - -export default page \ No newline at end of file diff --git a/app/api/allBusinesses/route.js b/app/api/allBusinesses/route.js new file mode 100644 index 0000000..9d41f46 --- /dev/null +++ b/app/api/allBusinesses/route.js @@ -0,0 +1,21 @@ +import connectToDB from "@/configs/db"; +import BusinessModel from "@/models/Business"; + + +export async function GET(req) { + try { + await connectToDB(); + const businesses = JSON.parse(JSON.stringify(await BusinessModel.find({}, "businessName bio businessBrand isAvatar"))); + + + return Response.json( + { message: 'get businesses successfully', data: businesses }, + { status: 200 } + ); +} catch (error) { + console.error(`Error get businesses`, error); + return Response.json( + { message: `Error get businesses`, error }, + { status: 500 }) +} +} \ No newline at end of file diff --git a/components/templates/allBusinesses/AllBusinesses.jsx b/components/templates/allBusinesses/AllBusinesses.jsx deleted file mode 100644 index 63f9e45..0000000 --- a/components/templates/allBusinesses/AllBusinesses.jsx +++ /dev/null @@ -1,87 +0,0 @@ -"use client" -import { AppBar, Avatar, Box, Button, Container, List, ListItem, ListItemAvatar, ListItemButton, ListItemIcon, ListItemText, TextField, Toolbar, Typography } from '@mui/material' -import { useEffect, useState } from 'react' -import { useRouter } from 'next/navigation' -import { AllBusinessesText, selectGuild } from '@/components/typoRepo'; -import { Accordion, AccordionDetails, Chip } from "@mui/material"; -import QuestionMarkOutlinedIcon from '@mui/icons-material/QuestionMarkOutlined'; -import ArrowForwardIcon from '@mui/icons-material/ArrowForward'; -import ItsAvatar from '@/components/modules/ItsAvatar' -// import dynamic from 'next/dynamic' -// const ShowMyLocation = dynamic(() => import('@/components/modules/ShowMyLocation'), { ssr: false }) - - -export default function AllBusinesses({ businesses }) { - const router = useRouter() - const [expanded, setExpanded] = useState(false); - const [mounted, setMounted] = useState(false); - useEffect(() => { - setMounted(true) - }, []) - - const goToIndex = () => { - router.push("/") - } - - return (mounted && - <> - - - - - - - - - - - setExpanded(!expanded)} - icon={} - /> - - - - - {businesses ? businesses.map((business) => { - return ( - - - router.push(`/${business.businessName}`)}> - - - - - - - - - - ) - }) : - کسب و کاری برای نمایش وجود ندارد - } - - - - ) - -}