diff --git a/.gitignore b/.gitignore index fd3dbb5..9aa4395 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. +/public/uploads + # dependencies /node_modules /.pnp diff --git a/app/api/upload/route.ts b/app/api/upload/route.ts new file mode 100644 index 0000000..3a48c95 --- /dev/null +++ b/app/api/upload/route.ts @@ -0,0 +1,46 @@ +import { writeFile } from 'fs/promises' +import { NextRequest, NextResponse } from 'next/server' +import path from 'path' +import fs from 'fs' + +export const globalProducts: any[] = [] + +export async function POST(request: NextRequest) { + const data = await request.formData() + const file: File | null = data.get('file') as unknown as File + const measure: string = data.get('measure') as string + const lastPrice: string = data.get('lastPrice') as string + const title: string = data.get('title') as string + const weight: string = data.get('weight') as string + const image: string = data.get('image') as string + const category: string = data.get('category') as string + const currentPrice: string = data.get('currentPrice') as string + + if (!file) { + return NextResponse.json({ success: false }) + } + + const bytes = await file.arrayBuffer() + const buffer = Buffer.from(bytes) + + + + let fileName = '/uploads/' + new Date().valueOf() + file.name + + const dir = path.resolve('./public') + if (!fs.existsSync(path.join(dir, './uploads'))) { + fs.mkdirSync(path.join(dir, './uploads')); + } + const filePath = path.join(dir, fileName) + await writeFile(filePath, buffer) + + console.log(`open ${filePath} to see the uploaded file`) + console.log(filePath) + globalProducts.push({ title, measure, image: fileName, lastPrice, weight, category, currentPrice }) + + return NextResponse.json(globalProducts) +} + +export async function GET(params: NextRequest) { + return NextResponse.json(globalProducts) +} \ No newline at end of file diff --git a/components/Catalog.tsx b/components/Catalog.tsx index d0446c6..b409abb 100644 --- a/components/Catalog.tsx +++ b/components/Catalog.tsx @@ -1,26 +1,27 @@ -import '../src/css/catalog.css' -import onigiri from '../src/img/Onigiri.jpeg' -import Product from './Product' +import { useState } from 'react' +import '../src/css/catalog.css'; +import Product, {ProductProps} from './Product' -export default function Index() { +export interface CatalogProps { + products: ProductProps[] +} + +export default function Index(props: CatalogProps) { + const [category, setCategory] = useState('All') return
Products
You can sort by category
- setCategory(e.target.value) }> + + {Array.from(new Set(props.products.map(x => x.category))).map(x => ())}
- - - - - + {category === 'All' ? + props.products.map(x => ) : + props.products.filter(x => x.category == category).map(x => )}
} diff --git a/components/Product.tsx b/components/Product.tsx index 22e7ab8..eed5ec2 100644 --- a/components/Product.tsx +++ b/components/Product.tsx @@ -2,19 +2,20 @@ import Image from 'next/image' import { StaticImport } from 'next/dist/shared/lib/get-img-props' -interface ProductProps { - currentPrice: number, +export interface ProductProps { + category: string | undefined + currentPrice: string, measure: string, - lastPrice: number | undefined, + lastPrice: string | undefined, title: string, weight: string, - imgSrc: string | StaticImport + image: string | StaticImport } export default function Product(props: ProductProps) { return
- {'company + {'company

diff --git a/pages/index.tsx b/pages/index.tsx index 49b4c77..d641a26 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,13 +1,80 @@ +'use client' + import Layout from '../components/Layout'; import Hero from '../components/Hero'; import Catalog from '../components/Catalog'; import About from "@/components/About"; +import { useEffect, useState } from 'react'; + +const Home = () => { + const [file, setFile] = useState() + const [category, setCategory] = useState() + const [measure, setMeasure] = useState() + const [lastPrice, setLastPrice] = useState() + const [currentPrice, setCurrentPrice] = useState() + const [title, setTitle] = useState() + const [weight, setWeight] = useState() + const [image, setImage] = useState() + const [products, setProducts] = useState([]) + + + useEffect(() => { + fetch('/api/upload', { + method: 'GET', + }).then(resp => resp.json()) + .then(resp => setProducts(resp)) + }, []) + + const onSubmit = async (e: React.FormEvent) => { + e.preventDefault() + if (!file) return -const Home = () => ( - + try { + const data = new FormData() + data.set('file', file) + data.set('category', category as string) + data.set('currentPrice', currentPrice as string) + data.set('measure', measure as string) + data.set('lastPrice', lastPrice as string) + data.set('title', title as string) + data.set('weight', weight as string) + data.set('image', image as string) + + + const res = await fetch('/api/upload', { + method: 'POST', + body: data + }) + // handle the error + if (!res.ok) throw new Error(await res.text()) + setProducts(await res.json()) + } catch (e: any) { + // Handle errors here + console.error(e) + } + } + console.log(products) + return - + +

+ + setCategory(e.target.value)} name='category' type='text' /> + setCurrentPrice(e.target.value)} name='currentPrice' type='text' /> + setMeasure(e.target.value)} name='measure' type='text' /> + setLastPrice(e.target.value)} name='lastPrice' type='text' /> + setTitle(e.target.value)} name='title' type='text' /> + setWeight(e.target.value)} name='weight' type='text' /> + setImage(e.target.value)} name='image' type='text' /> + + setFile(e.target.files?.[0])} + /> + +
-); +}; export default Home; \ No newline at end of file