admin.tsx 5.65 KB
Newer Older
백승민's avatar
백승민 committed
1
2
3
4
import React, { FormEvent, useEffect, useState } from "react";
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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    useEffect(() => {
        imgsData();
    }, []);

    const [addimg, setAddimg] = useState<MainimgType>({
        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
40
    // console.log("asdafsdfs", getimgs)
백승민's avatar
백승민 committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    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("");
        } catch (error) {
            console.log("에러발생");
            catchErrors(error, setError);
        } finally {
            setLoading(false);
        }
    }

    if (success) {
        alert("img 추가되었습니다");
    }
    
백승민's avatar
백승민 committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    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) => (
                <div
                    className={`m-1 shrink-0 bg-gray-200 rounded shadow-md `}
                    key={index}>
                    <img
                        src={pic.url}
                        className="w-full h-10 md:h-20 object-center"
                    />
                    <p className="text-center text-xs">{pic.title}</p>
                </div>
            ))]
        slides.push(k);
    }

백승민's avatar
백승민 committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
    return (
        <div>
            <form
                onSubmit={handleSubmit}>
                <div className="flex justify-center gap-3">
                    <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>
                        <p>url</p>
                        <input name="url" className="border-2 border-sky-500"
                            onChange={handleInputeChange} />
                        {/* type="file"/> */}
                    </div>
                    <div>
                        <p>title</p>
                        <input name="title" className="border-2 border-sky-500"
                            onChange={handleInputeChange} />
                    </div>
                    <div className="my-5 ">
                        <button className="border-2 border-gray-500 rounded">추가</button>
                    </div>
                </div>
            </form>
백승민's avatar
백승민 committed
144
145
146
147
148
            <div className="flex flex-col">
                <MySlide key={Math.random()}
                    slides={slides}
                />
            </div>
백승민's avatar
백승민 committed
149
150
151
        </div>
    );
};