Admin.js 3.62 KB
Newer Older
kusang96's avatar
kusang96 committed
1
2
import React, { useState, useEffect, useRef } from 'react';
import { Redirect } from 'react-router-dom';
kusang96's avatar
DSAD    
kusang96 committed
3
import AllCard from '../Components/AllCard';
kusang96's avatar
kusang96 committed
4
import Pagination from "../Components/Pagination";
kusang96's avatar
kusang96 committed
5
6
import axios from 'axios';
import catchError from '../utils/catchErrors';
kusang96's avatar
DSAD    
kusang96 committed
7
import { Row, Form, FormControl, Button, Container } from 'react-bootstrap';
Kim, Subin's avatar
Kim, Subin committed
8
9

function Admin() {
kusang96's avatar
kusang96 committed
10
    const INIT_STATUS = { indexOfFirst: 0, indexOfLast: 10 }
kusang96's avatar
kusang96 committed
11
    const [search, setSearch] = useState({ word: '' })
kusang96's avatar
kusang96 committed
12
    const [productlist, setProductlist] = useState([])
kusang96's avatar
kusang96 committed
13
14
15
    const [status, setStatus] = useState(INIT_STATUS)
    const [currentPage, setCurrentPage] = useState(1);
    const [per, setPer] = useState(10);
kusang96's avatar
kusang96 committed
16
    const [error, setError] = useState('')
kusang96's avatar
kusang96 committed
17
18
19
    const searchref = useRef(null)
    const indexOfLast = currentPage * per;
    const indexOfFirst = indexOfLast - per;
kusang96's avatar
kusang96 committed
20
21
22
23
24

    useEffect(() => {
        getProductlist()
    }, [])

kusang96's avatar
kusang96 committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    function paginate(items, index, itemNumber) {
        const posts = [];
        const startIndex = (index - 1) * itemNumber
        for (var i = 0; i < itemNumber; i++) {
            posts.push(items[(startIndex + i)])
        }
        return posts
    }

    function currentPosts(tmp) {
        let currentPosts = 0;
        currentPosts = tmp.slice(indexOfFirst, indexOfLast);
        console.log("postsPerPage=",currentPage)
        return currentPosts;
    }

kusang96's avatar
kusang96 committed
41
42
43
44
45
46
47
48
49
50
    async function getProductlist() {
        try {
            const response = await axios.get(`/api/product/getproduct/all`)
            console.log("response.data=", response.data)
            setProductlist(response.data)
        } catch (error) {
            catchError(error, setError)
        }
    }

kusang96's avatar
kusang96 committed
51
52
    function handleChange(event) {
        setSearch({ word: event.target.value })
kusang96's avatar
kusang96 committed
53
54
    }

kusang96's avatar
kusang96 committed
55
    async function handleSubmit(e) {
kusang96's avatar
kusang96 committed
56
        e.preventDefault()
kusang96's avatar
kusang96 committed
57
58
59
60
61
62
63
        try {
            setError('')
            const response = await axios.get(`/api/product/getproduct/all?product=${search.word}`)
            console.log("response.data=", response.data)
            setProductlist(response.data)
        } catch (error) {
            catchError(error, setError)
kusang96's avatar
kusang96 committed
64
65
        } finally {
            searchref.current.value = ''
kusang96's avatar
kusang96 committed
66
        }
67
    }
Kim, Subin's avatar
Kim, Subin committed
68

kusang96's avatar
kusang96 committed
69
70
71
72
73
74
    if (error) {
        alert(`${error}`)
        setError('')
        searchref.current.value = ''
    }

Kim, Subin's avatar
Kim, Subin committed
75
    return (
kusang96's avatar
kusang96 committed
76
        <Container>
77
78
79
80
81
82
83
84
85
86
87
88
89
            <style type="text/css">
                {`
                .btn {
                    background-color: #CDC5C2;
                    border-color: #CDC5C2;
                }

                .btn:hover, .btn:active, .btn:focus {
                    background-color: #91877F;
                    border-color: #91877F;
                }
                `}
            </style>
kusang96's avatar
kusang96 committed
90
            <Row as={Form} onSubmit={handleSubmit} className="justify-content-end mx-0 my-5">
kusang96's avatar
kusang96 committed
91
                <FormControl ref={searchref} type="text" onChange={handleChange} placeholder="Search" style={{ width: "13rem" }} />
kusang96's avatar
kusang96 committed
92
93
94
95
96
97
                <Button type="submit" className="px-2">
                    <img src="icon/search.svg" width="20" height="20" />
                </Button>
                <Button sm={2} xs={6} type="button" href="/regist" className="ml-1">상품 등록</Button>
            </Row>
            <Row className="justify-content-center m-5">
kusang96's avatar
kusang96 committed
98
                {currentPosts(productlist).map(pro => (
kusang96's avatar
DSAD    
kusang96 committed
99
                    <AllCard id={pro._id} name={pro.pro_name} price={pro.price} main_img={pro.main_imgUrl} />
kusang96's avatar
kusang96 committed
100
101
                ))}
            </Row>
kusang96's avatar
kusang96 committed
102
            <Pagination index={currentPage} totalPosts={Math.ceil(productlist.length / per)} handlePage={setCurrentPage} />
kusang96's avatar
kusang96 committed
103
        </Container>
Kim, Subin's avatar
Kim, Subin committed
104
105
106
107
    )
}

export default Admin