Card.js 2.84 KB
Newer Older
1
2
3
4
import React, { useContext } from 'react';
import { Link } from 'react-router-dom';
import axios from 'axios';
import { Card, Accordion, Col, AccordionContext, useAccordionToggle, Button } from 'react-bootstrap';
Ha YeaJin's avatar
Ha YeaJin committed
5
6
7
8
9
10
11
12
13
14
15
import styled from 'styled-components';

const Text = styled(Card.Body)`
    & .WRAP {
        display: inline-block;
        text-overflow: ellipsis;
        width: 100%;
        white-space: initial;
    }
`

16
function Notice({ card_id, card_index, title, date, content, admin }) {
Ha YeaJin's avatar
Ha YeaJin committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    function ContextAwareToggle({ children, eventKey, callback }) {
        const currentEventKey = useContext(AccordionContext);

        const decoratedOnClick = useAccordionToggle(
            eventKey,
            () => callback && callback(eventKey),
        );

        const isCurrentEventKey = currentEventKey === eventKey;
        return (
            <div
                className={isCurrentEventKey ? "text-wrap whiteSpace-initial" : "text-truncate"}
                onClick={decoratedOnClick}
            >
                {children}
            </div>
        );
    }

    function dateForm(day) {
        const post_day = new Date(day);
        let year = post_day.getFullYear();
        let month = post_day.getMonth() + 1;
        let date = post_day.getDate();

        month = month < 10 ? '0' + month : month;
        date = date < 10 ? '0' + date : date;

        const new_date = year + "-" + month + "-" + date;
        return new_date
    }

49
50
51
52
53
54
55
56
57
58
59
60
    function remove (card_id) {
        axios.delete(`/notices/${card_id}`)
            .then(res => {
                if (res.status === 404) return alert(res.data.error)
                alert("삭제되었습니다!");
                window.location.reload();
            })
            .catch(err => {
                alert(err.error)
            });
    }

Ha YeaJin's avatar
Ha YeaJin committed
61
62
63
64
65
66
67
68
69
    return (
        <Card className="w-100">
            <Card.Header className="row flex-row py-3">
                <Col md={10} xs={8} >
                    <ContextAwareToggle variant="link" eventKey={card_index + 1}>{title}</ContextAwareToggle>
                </Col>
                <Col md={2} xs={4} className="p-0" >{dateForm(date)}</Col>
            </Card.Header>
            <Accordion.Collapse eventKey={card_index + 1}>
70
71
72
73
74
75
                <Text>
                    {content.split("\n").map((i, key) => {
                        return <div key={key}>{i}</div>;
                    })}
                    {admin === "admin" ? (
                        <div className="d-flex justify-content-end">
Kim, Subin's avatar
Kim, Subin committed
76
                            <Button variant="primary" size="sm" as={Link} to={`/modify/${card_id}`}>수정</Button>
77
78
79
                            <Button variant="danger" size="sm" onClick={() => remove(card_id)}>삭제</Button>
                        </div>) : null}
                </Text>
Ha YeaJin's avatar
Ha YeaJin committed
80
81
82
83
84
85
            </Accordion.Collapse>
        </Card >
    )
}

export default Notice;