BottleInfoContainer.tsx
5.99 KB
1
2
3
4
5
6
7
8
9
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import React, { useState, useEffect } from 'react';
import { RouteComponentProps } from 'react-router-dom';
import { useRecoilValue } from 'recoil';
import * as recoilUtil from '../../util/recoilUtil';
import * as makeChart from '../../util/makeChart';
import * as Alert from '../../util/alertMessage';
import moment from 'moment';
import Header from '../../components/Header';
import Footer from '../../components/Footer';
import BottleInfoPresenter from './BottleInfoPresenter';
import { doctorApi } from '../../api';
interface RouteParmas {
bottleId : string;
}
type BottleInfoProps = RouteComponentProps<RouteParmas>
const BottleInfoContainer = (props : BottleInfoProps) => {
const { bottleId } = props.match.params;
const token = useRecoilValue(recoilUtil.token);
const userTypeCd = useRecoilValue(recoilUtil.userTypeCd);
const [bottleInfo, setBottleInfo] = useState<any>({
feedbackList : [],
medicine : {},
bottleId : 0,
takeMedicineHist : [],
});
//차트에 표시되는 행의 개수
const numberOfChartItem = 7;
const [chartOption, setChartOption] = useState<any>({
chart : {
type : 'line',
height : 300,
width : 450,
},
title : {
text : '',
style : { 'color' : '#343434', 'fontSize' : '15px', 'fontWeight' : '700' },
margin : 10,
},
xAxis : {
categories : [],
},
series : [{
name : '약 복용 회분',
color : '#337DFF',
data : [],
}],
});
const [takeMedicineHist, setTakeMedicineHist] = useState<any[]>([]);
const [feedback, setFeedback] = useState<string>('');
const [fdbType, setFdbType] = useState<string>('RECOMMEND');
const [medicineInfoModal, setMedicineInfoModal] = useState<boolean>(false);
const [modalType, setModalType] = useState<string>('hist'); //hist , info
const fetchData = async () => {
setModalType('hist');
setFeedback('');
setFdbType('RECOMMEND');
setMedicineInfoModal(false);
try {
const result = await doctorApi.getPatientBottleDetail(token, bottleId);
if (result.statusText === 'OK') {
setTakeMedicineHist(result.data.takeMedicineHist.map((takeMedicine : any) => {
return ({
...takeMedicine,
takeDate : moment(takeMedicine.takeDate).format('YYYY년 MM월 DD일 hh시 mm분'),
});
}));
const { categories, data } = makeChart.make(result.data.takeMedicineHist, numberOfChartItem);
setBottleInfo({
...result.data,
feedbackList : result.data.feedbackList.map((feedback : any) => {
return {
...feedback,
fdbDtm : moment(feedback.fdbDtm).format('YYYY-MM-DD hh:mm'),
}
}),
});
setChartOption({
...chartOption,
title : {
text : result.data.medicine.name,
},
xAxis : {
categories,
},
series : [{
data,
}]
});
} else {
Alert.onError('접근 권한이 없습니다.', () => props.history.push('/'));
}
} catch(e : any) {
console.log(e);
Alert.onError('알 수 없는 에러가 발생했습니다.', () => props.history.push('/'));
}
};
const onSetFeedback = (e : React.ChangeEvent<HTMLTextAreaElement>) => {
setFeedback(e.target.value);
};
const onSubmitFeedback = () => {
const register = async () => {
if(feedback.length) {
try {
const res = await doctorApi.writeBottleFeedback(token, {
bottleId,
fdbType,
feedback
});
if(res.statusText === 'OK') {
Alert.onSuccess('피드백이 등록되었습니다.', () => fetchData());
} else {
console.log(res);
Alert.onError('피드백 등록에 실패했습니다.', () => null);
}
} catch(e : any) {
Alert.onError('알 수 없는 에러가 발생했습니다.', () => fetchData());
}
} else {
Alert.onError('피드백 내용을 입력하세요.', () => null);
}
}
Alert.onCheck('피드백을 등록하시겠습니까?', register, () => null);
};
const onViewTakeHist = () => {
if(modalType === 'info') setModalType('hist');
};
const onViewMedicineInfo = () => {
if(modalType === 'hist') setModalType('info');
};
useEffect(() => {
if(userTypeCd !== 'DOCTOR') {
Alert.onError('접근 권한이 없습니다.', () => props.history.push('/'));
}
fetchData();
}, [userTypeCd]);
return (
<>
<Header {...props} />
<BottleInfoPresenter
bottleInfo = {bottleInfo}
chartOption = {chartOption}
takeMedicineHist = {takeMedicineHist}
medicineInfoModal = {medicineInfoModal}
modalType = {modalType}
onViewTakeHist = {onViewTakeHist}
onViewMedicineInfo = {onViewMedicineInfo}
setMedicineInfoModal = {setMedicineInfoModal}
feedback = {feedback}
onSetFeedback = {onSetFeedback}
fdbType = {fdbType}
setFdbType = {setFdbType}
onSubmitFeedback = {onSubmitFeedback}
/>
<Footer {...props}/>
</>
);
};
export default BottleInfoContainer;