admin.tsx 6.61 KB
Newer Older
백승민's avatar
백승민 committed
1
import React, { FormEvent, useEffect, useState, MouseEvent } from "react";
백승민's avatar
백승민 committed
2
3
4
import { mainimgApi } from "../apis";
import { catchErrors } from "../helpers";
import { MainimgType } from "../types";
백승민's avatar
백승민 committed
5
import { MySlide } from "./adminslide";
백승민's avatar
백승민 committed
6
7
8
9

export default function Admin() {

    const [getimgs, setGetimgs] = useState<MainimgType[]>([]);
백승민's avatar
백승민 committed
10

백승민's avatar
백승민 committed
11
12
    async function imgsData() {
        const imgs = await mainimgApi.getmainimg();
백승민's avatar
백승민 committed
13
        // console.log("ㅑㅡㅎ", imgs)
백승민's avatar
백승민 committed
14
15
        setGetimgs(imgs)
    };
백승민's avatar
백승민 committed
16

백승민's avatar
백승민 committed
17
18
19
20
21
    useEffect(() => {
        imgsData();
    }, []);

    const [addimg, setAddimg] = useState<MainimgType>({
백승민's avatar
백승민 committed
22
        _id: "",
백승민's avatar
백승민 committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        theme: "",
        city: "",
        url: "",
        title: "",
    });
    const [loading, setLoading] = useState(false);
    const [error, setError] = useState("");
    const [success, setSuccess] = useState(false);

    function handleSelectChange(event: React.ChangeEvent<HTMLSelectElement>) {
        const { name, value } = event.currentTarget;
        setAddimg({ ...addimg, [name]: value });
    }
    function handleInputeChange(event: React.ChangeEvent<HTMLInputElement>) {
        const { name, value } = event.currentTarget;
        setAddimg({ ...addimg, [name]: value });
    }

백승민's avatar
백승민 committed
41
    // console.log("asdafsdfs", getimgs)
백승민's avatar
백승민 committed
42
43
44
45
46
47
48
49
50
51
    async function handleSubmit(event: FormEvent) {
        event.preventDefault();
        try {
            setError("");
            console.log("img data", addimg);
            setLoading(true);
            const res = await mainimgApi.mainimg(addimg);
            console.log("서버연결됬나요", res);
            setSuccess(true);
            setError("");
백승민's avatar
백승민 committed
52
            alert("img 추가되었습니다");
백승민's avatar
백승민 committed
53
54
55
56
57
58
59
60
        } catch (error) {
            console.log("에러발생");
            catchErrors(error, setError);
        } finally {
            setLoading(false);
        }
    }

백승민's avatar
백승민 committed
61
62
63
64
65
66
67
68
69
70
71
72
    // if (success) {
    //     alert("img 추가되었습니다");
    // }

    async function handleDeleteClick(event: MouseEvent<HTMLButtonElement>) {
        const picId = event.currentTarget.id;
        console.log(picId)
        const res = await mainimgApi.delmainimg(picId);
        console.log("delete img", res);
        alert("img 삭제되었습니다.")
    };

백승민's avatar
백승민 committed
73
74
75
76
77
78
79
    let limit = 15;
    const numPages = Math.ceil(getimgs.length / 15);

    const slides = []
    for (let i = 0; i < numPages; i++) {
        const k = [
            getimgs.slice(i * limit, i * limit + limit).map((pic, index: number) => (
백승민's avatar
백승민 committed
80
81
82
83
84
85
86
87
88
89
90
                <div key={index}>
                    <div className={`m-1 shrink-0 bg-gray-200 rounded shadow-md `}>
                        <img
                            src={pic.url}
                            className="w-full h-10 md:h-20 object-center"
                        />
                        <p className="text-center text-xs">{pic.title}</p>
                    </div>
                    <button id={pic._id} onClick={handleDeleteClick}>
                        삭제
                    </button>
백승민's avatar
백승민 committed
91
92
93
94
95
                </div>
            ))]
        slides.push(k);
    }

백승민's avatar
백승민 committed
96
97
98
99
    return (
        <div>
            <form
                onSubmit={handleSubmit}>
백승민's avatar
백승민 committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
                <div className="flex flex-wrap justify-center gap-3">
                    <div className="gap-3 md:flex ">
                        <select
                            name="city"
                            id="Questions"
                            className="border border-3 border-black w-20 my-5"
                            defaultValue="질문종류"
                            onChange={handleSelectChange}
                        >
                            <option value="질문종류">도시</option>
                            <option value="Seoul">서울</option>
                            <option value="Busan">부산</option>
                            <option value="Incheon">인천</option>
                            <option value="Daegoo">대구</option>
                            <option value="Kwangjoo">광주</option>
                            <option value="Daejeon">대전</option>
                            <option value="Woolsan">울산</option>
                            <option value="Sejong">세종</option>
                            <option value="Dokdo">독도</option>
                            <option value="Jeju">제주</option>
                        </select>
                        <select
                            name="theme"
                            id="Questions"
                            className="border border-3 border-black w-20 my-5"
                            defaultValue="질문종류"
                            onChange={handleSelectChange}
                        >
                            <option value="질문종류">테마</option>
                            <option value="cycling">사이클링</option>
                            <option value="surfing">서핑</option>
                            <option value="activity">액티비티</option>
                            <option value="camping">캠핑</option>
                            <option value="sking">스키</option>
                            <option value="boat">보트</option>
                            <option value="desert">사막</option>
                            <option value="golf">골프</option>
                            <option value="cave">동굴</option>
                            <option value="history">문화재</option>
                            <option value="zoo">동물원</option>
                            <option value="cycling">사이클링</option>
                        </select>
                        <div className="flex items-center justify-end gap-3">
                            <p>url :</p>
                            <input name="url" className="border-2 border-sky-500"
                                onChange={handleInputeChange} />
                            {/* type="file"/> */}
                        </div>
                        <div className="flex items-center justify-end gap-3 mt-2 md:mt-0">
                            <p>title :</p>
                            <input name="title" className="border-2 border-sky-500"
                                onChange={handleInputeChange} />
                        </div>
백승민's avatar
백승민 committed
153
                    </div>
백승민's avatar
백승민 committed
154
155
                    <div className="my-5 flex items-center">
                        <button className="border-2 border-gray-500 rounded ">추가</button>
백승민's avatar
백승민 committed
156
157
158
                    </div>
                </div>
            </form>
백승민's avatar
백승민 committed
159
            <div className="flex justify-center">
백승민's avatar
백승민 committed
160
161
162
                <MySlide key={Math.random()}
                    slides={slides}
                />
백승민's avatar
백승민 committed
163

백승민's avatar
백승민 committed
164
            </div>
백승민's avatar
백승민 committed
165
166
167
        </div>
    );
};