AllCard.js 1.44 KB
Newer Older
kusang96's avatar
DSAD    
kusang96 committed
1
import React, { useState, useEffect, useRef } from 'react';
kusang96's avatar
kusang96 committed
2
3
4
import { Redirect } from 'react-router-dom';
import axios from 'axios';
import catchError from '../utils/catchErrors';
kusang96's avatar
DSAD    
kusang96 committed
5
6
7
import { Card, Button } from 'react-bootstrap';

function AllCard({ id, name, price, main_img }) {
kusang96's avatar
kusang96 committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    const [success, setSuccess] = useState(false)
    const [error, setError] = useState('')
    const cardRef = useRef(null)

    async function handleDelete() {
        const pro_id = cardRef.current.id
        try {
            setError('')
            const response = await axios.delete(`/api/product/delete?pro_id=${pro_id}`)
            alert('해당 상품을 성공적으로 삭제하였습니다.')
            setSuccess(true)
        } catch (error) {
            catchError(error, setError)
            setSuccess(false)
        }
    }

    if (success) {
        return <Redirect to="/admin" />
kusang96's avatar
DSAD    
kusang96 committed
27
28
29
    }

    return (
kusang96's avatar
kusang96 committed
30
        <Card id={id} ref={cardRef} className="m-3" style={{ width: "18rem" }}>
kusang96's avatar
DSAD    
kusang96 committed
31
32
33
34
35
36
37
38
39
40
41
            <Card.Img variant="top" src={main_img && `/images/${main_img}`} style={{ objectFit: "contain", height: "22rem" }} />
            <Card.Body>
                <Card.Title style={{ whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{name}</Card.Title>
                <Card.Text>{price} </Card.Text>
                <Button className="float-right" onClick={handleDelete}>삭제</Button>
            </Card.Body>
        </Card>
    )
}

export default AllCard