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

function Admin() {
kusang96's avatar
kusang96 committed
9
    const [search, setSearch] = useState({ word: '' })
kusang96's avatar
kusang96 committed
10
    const [productlist, setProductlist] = useState([])
11
    const [length, setLength] = useState(0)
kusang96's avatar
kusang96 committed
12
    const [currentPage, setCurrentPage] = useState(1)
kusang96's avatar
kusang96 committed
13
    const [error, setError] = useState('')
kusang96's avatar
kusang96 committed
14
    const searchref = useRef(null)
15
    const per = 9;
kusang96's avatar
kusang96 committed
16
17
18
19
20

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

kusang96's avatar
kusang96 committed
21
    useEffect(() => {
22
23
24
25
26
        if (search.word == '') {
            getProductlist()
        } else {
            handleSearch()
        }
kusang96's avatar
kusang96 committed
27
    }, [currentPage])
kusang96's avatar
kusang96 committed
28

kusang96's avatar
kusang96 committed
29
30
    async function getProductlist() {
        try {
kusang96's avatar
kusang96 committed
31
            setError('')
32
33
34
35
            setSearch({ word: '' })
            const response = await axios.get(`/api/product/getproduct/all?page=${currentPage}`)
            setProductlist(response.data.productPiece)
            setLength(response.data.length)
kusang96's avatar
kusang96 committed
36
37
38
39
40
        } catch (error) {
            catchError(error, setError)
        }
    }

41
42
43
44
45
46
47
48
    async function handleSearch() {
        try {
            setError('')
            const response = await axios.get(`/api/product/getproduct/all?product=${search.word}&page=${currentPage}`)
            setProductlist(response.data.productPiece)
            setLength(response.data.length)
        } catch (error) {
            catchError(error, setError)
Kim, Subin's avatar
정리    
Kim, Subin committed
49
        }
50
51
    }

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

kusang96's avatar
kusang96 committed
56
    async function handleSubmit(e) {
kusang96's avatar
kusang96 committed
57
        e.preventDefault()
kusang96's avatar
kusang96 committed
58
59
        try {
            setError('')
60
61
62
63
64
65
            if (currentPage != 1) {
                setCurrentPage(1)
            }
            const response = await axios.get(`/api/product/getproduct/all?product=${search.word}&page=${currentPage}`)
            setProductlist(response.data.productPiece)
            setLength(response.data.length)
kusang96's avatar
kusang96 committed
66
67
        } catch (error) {
            catchError(error, setError)
kusang96's avatar
kusang96 committed
68
69
        } finally {
            searchref.current.value = ''
kusang96's avatar
kusang96 committed
70
        }
71
    }
Kim, Subin's avatar
Kim, Subin committed
72

kusang96's avatar
kusang96 committed
73
74
75
76
77
78
    if (error) {
        alert(`${error}`)
        setError('')
        searchref.current.value = ''
    }

Kim, Subin's avatar
Kim, Subin committed
79
    return (
kusang96's avatar
kusang96 committed
80
        <Container>
81
82
            <style type="text/css">
                {`
박상호's avatar
css    
박상호 committed
83
84
85
86
87
88
89
90
                @font-face {
                    font-family: 'Jal_Onuel';
                    src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_20-10-21@1.0/Jal_Onuel.woff') format('woff');
                    font-weight: normal;
                    font-style: normal;
                }
                body{font-family:'Jal_Onuel'}

91
92
93
94
                .btn {
                    background-color: #CDC5C2;
                    border-color: #CDC5C2;
                }
kusang96's avatar
kusang96 committed
95
96
97
98
99
100
101
102
103
                .btn:hover {
                    background-color: #91877F;
                    border-color: #91877F;
                }
                .btn-primary.focus, .btn-primary:focus {
                    background-color: #91877F;
                    border-color: #91877F;
                    box-shadow: 0 0 0 0;
                }
kusang96's avatar
css    
kusang96 committed
104
                .btn-primary:not(:disabled):not(.disabled):active, .show>.btn-primary.dropdown-toggle {
105
106
107
108
109
                    background-color: #91877F;
                    border-color: #91877F;
                }
                `}
            </style>
kusang96's avatar
kusang96 committed
110
            <Row as={Form} onSubmit={handleSubmit} className="justify-content-end mx-0 my-5">
kusang96's avatar
kusang96 committed
111
                <FormControl ref={searchref} type="text" onChange={handleChange} placeholder="Search" style={{ width: "13rem" }} />
kusang96's avatar
kusang96 committed
112
113
114
115
116
117
                <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">
118
                {productlist.map(pro => (
kusang96's avatar
DSAD    
kusang96 committed
119
                    <AllCard id={pro._id} name={pro.pro_name} price={pro.price} main_img={pro.main_imgUrl} />
kusang96's avatar
kusang96 committed
120
121
                ))}
            </Row>
122
            <Pagination index={currentPage} totalPages={Math.ceil(length / per)} handlePage={setCurrentPage} />
kusang96's avatar
kusang96 committed
123
        </Container>
Kim, Subin's avatar
Kim, Subin committed
124
125
126
127
    )
}

export default Admin