import React, { useState, useEffect } from 'react'; import { Link, useLocation, useNavigate } from 'react-router-dom'; import DatePicker from "react-datepicker"; import EgovAttachFile from 'components/EgovAttachFile'; import RichTextEditor from "../../../../components/editor/RichTextEditor"; import CODE from 'constants/code'; import * as EgovNet from 'api/egovFetch'; import URL from 'constants/url'; import { default as EgovLeftNav } from 'components/leftmenu/EgovLeftNavAdmin'; import styled from "styled-components"; const StyledDiv = styled.div` .board_view2 { margin-bottom: 30px; } .board_btn_area { margin-top: 70px; } `; function PopupWriter(props) { const navigate = useNavigate(); const location = useLocation(); const [modeInfo, setModeInfo] = useState({ mode: props.mode }); const [text, setText] = useState(""); const [popupDetail, setScheduleDetail] = useState({ startDate: new Date(), endDate: new Date() }); const [schdulBgndeHH, setSchdulBgndeHH] = useState(); const [schdulBgndeMM, setSchdulBgndeMM] = useState(); const [schdulEnddeHH, setSchdulEnddeHH] = useState(); const [schdulEnddeMM, setSchdulEnddeMM] = useState(); const [boardAttachFiles, setBoardAttachFiles] = useState(); useEffect(function () { initMode(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const formValidator = (formData) => { if (formData.get('title') === null || formData.get('title') === "") { alert("제목은 필수 값입니다."); return false; } if (formData.get('schdulBgnde') > formData.get('schdulEndde')) { alert("종료일시는 시작일시보다 앞 설 수 없습니다."); return false; } if (formData.get('contents') === null || formData.get('contents') === "") { alert("내용은 필수 값입니다."); return false; } return true; } const initMode = () => { // props.mode 값이 없으면 에러가 발생한다. switch (props.mode) { case CODE.MODE_CREATE: setModeInfo({ ...modeInfo, modeTitle: "등록", method : "POST", editURL: '/contents/api/popup-manage' }); break; case CODE.MODE_MODIFY: setModeInfo({ ...modeInfo, modeTitle: "수정", method : "PUT", editURL: '/contents/api/popup-manage' }); break; default: navigate({pathname: URL.ERROR}, {state: {msg : ""}}); } retrieveDetail(); } const retrieveDetail = () => { if (modeInfo.mode === CODE.MODE_CREATE) {// 조회/등록이면 조회 안함 return; } const retrieveDetailURL = `/contents/api/popup-manage/${location.state?.popupId}`; const requestOptions = { method: "GET", headers: { 'Content-type': 'application/json' } } EgovNet.requestFetch(retrieveDetailURL, requestOptions, function (resp) { let rawDetail = resp.result; //기본값 설정 setScheduleDetail({ ...popupDetail, ...rawDetail, startDate: convertDate(rawDetail.schdulBgnde), endDate: convertDate(rawDetail.schdulEndde), }); setText(rawDetail.contents); } ); } const createPopup = () => { const formData = new FormData(); for (let key in popupDetail) { if ( key === 'startDate' ) { formData.append(key, getDateFourteenDigit( popupDetail[key] )); } else if( key === 'endDate' ) { formData.append(key, getDateFourteenDigit( popupDetail[key] )); } else { formData.append(key, popupDetail[key]); } } //게시글 내용 formData.delete("contents"); formData.append("contents", text); if (formValidator(formData)) { const requestOptions = { method: modeInfo.method, body: formData } if (modeInfo.mode === CODE.MODE_MODIFY) { modeInfo.editURL = `${modeInfo.editURL}/${location.state?.popupId}`; } EgovNet.requestFetch(modeInfo.editURL, requestOptions, (resp) => { if (Number(resp.resultCode) === Number(CODE.RCV_SUCCESS)) { navigate({ pathname: URL.ADMIN__CONTENTS__POP_UP }); } else { navigate({pathname: URL.ERROR}, {state: {msg : resp.resultMessage}}); } } ); } } const onClickDelete = (popupId) => { const deleteBoardURL = `/schedule/${popupId}`; const requestOptions = { method: "DELETE", headers: { 'Content-type': 'application/json', } } EgovNet.requestFetch(deleteBoardURL, requestOptions, (resp) => { console.log("====>>> Schdule delete= ", resp); if (Number(resp.resultCode) === Number(CODE.RCV_SUCCESS)) { alert("게시글이 삭제되었습니다.") navigate(URL.ADMIN__COMMITTEE__SCHEDULES ,{ replace: true }); } else { // alert("ERR : " + resp.message); navigate({pathname: URL.ERROR}, {state: {msg : resp.resultMessage}}); } } ); } const convertDate = (str) => { let year = str.substring(0, 4); let month = str.substring(4, 6); let date = str.substring(6, 8); let hour = str.substring(8, 10); let minute = str.substring(10, 12); return new Date(year, month - 1, date, hour, minute) } const getDateFourteenDigit = (date) => { return `${getYYYYMMDD(date).toString()}${makeTwoDigit(date.getHours())}${makeTwoDigit(date.getMinutes())}${makeTwoDigit(date.getSeconds())}`; } const getYYYYMMDD = (date) => { return date.getFullYear().toString() + makeTwoDigit(Number(date.getMonth() + 1)) + makeTwoDigit(date.getDate()); } const makeTwoDigit = (number) => { return number < 10 ? "0" + number : number.toString(); } const Location = React.memo(function Location() { return (