Newer
Older
import React, { useState, useEffect, useContext } from 'react';
import { useNavigate } from 'react-router-dom';
import { UserContext } from '../Usercontext.js';
import axios from 'axios';
10
11
12
13
14
15
16
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
function SearchMap({loc, setLoc}){
// 아주대학교를 기본 위치로 설정
/*
const handleInput = (e) => {
SetSearchAddress(e.target.value);
}
const SearchStart = () => {
console.log('Start Search!');// action test
// 주소를 기반으로 한 검색 구현(제주특별자치도 제주시 첨단로 242 를 입력하면 kakao가 나온다)
const geocoder = new kakao.maps.services.Geocoder();
let callback = function(result, status) {
if (status === kakao.maps.services.Status.OK) {
console.log(result)
const newSearch = result[0]
setLoc({
center: { lat: newSearch.y, lng: newSearch.x }
})
}
};
geocoder.addressSearch(`${searchAddress}`, callback);
<input onChange={handleInput}/>
<button onClick={SearchStart}>검색!</button>
}
*/
return (
<div className="UserInput">
<button onClick={()=>{console.log(loc)}}>위도경도 (디버깅)</button>
<Map // 지도를 표시할 Container
center={loc.center}
isPanto={loc.isPanto}
style={{
// 지도의 크기
width: "450px",
}}
onClick={(_t, mouseEvent) => setLoc({
center: { lat: mouseEvent.latLng.getLat(), lng: mouseEvent.latLng.getLng() },
isPanto: true,
})}
level={3}>
<MapMarker // 마커를 생성합니다
position={{
// 마커가 표시될 위치입니다
lat: loc.center.lat,
lng: loc.center.lng,
}}
/>
</Map>
</div>
)
}
function PostWrite(){
// const [title, setTitle] = useState('');
// const [content, setContent] = useState('');
// const [imageUrls, setImageUrls] = useState([]);
const userContext = useContext(UserContext);
const navigate = useNavigate();
function MoveTo(link){
navigate(link)
}
useEffect(() => {
const session = userContext.CheckSession()
.then((response) => {
if (!response.data) {
alert("세션이 만료되었습니다. 다시 로그인 바랍니다.")
MoveTo('/login')
}
})
.catch((response)=>{
console.log("error!:LogOut")
console.log(response)
})
});
const [location, setLocation] = useState({
center: { lat: 37.28238488648025, lng: 127.04350967609274 },
isPanto: true,
});
const [searchAddress, SetSearchAddress] = useState(); //주소 입력
const [inputs, setInputs] = useState({
title: "",
content: "",
imageUrls: ""
});
const {title, content, imageUrls} = inputs; // 비구조화 할당;
const handleSubmit= async (event) => {
event.preventDefault();
const response = await axios({
url: 'http://localhost:8080/post', // 통신할 웹문서
method: 'post', // 통신할 방식
data: inputs
});
if (response.status === 200) {
return response.data;
}
else {
return null;
}
}
catch (error){
console.log('Error during post creation:', error);
}
}
const onChange = (e) => {
const {value, name} = e.target;
setInputs({
...inputs,
[name]: value
});
}
return(
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<div>
<aside style={{float: "left"}}>
<SearchMap loc={location} setLoc={setLocation}></SearchMap>
</aside>
<main style={{float: "right"}}>
<form onSubmit={handleSubmit}>
<div>
<label>제목</label>
<input
type="text"
name='title'
//value={title}
onChange={onChange}
required
/>
</div>
<div>
<label>Content</label>
<textarea
name='content'
//value={content}
onChange={onChange}
/>
</div>
<button style={{height:"50px"}}>wow</button>
</form>
</main>
</div>