source: components/admin/Complaints.jsx@ e903234

main
Last change on this file since e903234 was e903234, checked in by anastasovv <simon@…>, 2 years ago

Added an admin panel, and the admin can now answer complaints

  • Property mode set to 100644
File size: 4.9 KB
Line 
1import React from 'react'
2
3import { useEffect } from 'react';
4
5import { useSelector, useDispatch } from 'react-redux'
6
7import axios from 'axios';
8import { setAdminInformation } from '../../redux/reducers/adminInformationSlice';
9import { setAdmin } from '../../redux/reducers/adminSlice';
10import { setStyle } from '../../redux/reducers/styleSlice';
11
12const Complaints = () => {
13 const dispatch = useDispatch();
14
15 const styleState = useSelector(state => state.style);
16 const adminState = useSelector(state => state.admin);
17 const adminInformationState = useSelector(state => state.adminInformation);
18
19 useEffect(() => {
20 axios.get(`/api/postgre?action=get_complaints_as_admin&admin_id=${localStorage.CAESSINO_ADMIN_ID}`).then(res => {
21 if (res.data?.success) {
22 dispatch(setAdminInformation({
23 ...adminInformationState.adminInformation,
24 complaints: res.data?.complaints,
25 }))
26 }
27 })
28 }, [])
29
30 function hideComplaintsScreen() {
31 dispatch(setAdmin({
32 ...adminState.admin,
33 displays: {
34 ...adminState.admin.displays,
35 complaintsScreen: false,
36 }
37 }))
38 }
39
40 function answerComplaint(idx) {
41 dispatch(setAdminInformation({
42 ...adminInformationState.adminInformation,
43 answeringComplaintIndex: idx,
44 answerForComplaint: '',
45 }))
46 }
47
48 function sendAnswer(complaint) {
49 axios.post(`/api/postgre`, {
50 action: 'send_complaint_answer_as_admin',
51 admin_id: localStorage.CAESSINO_ADMIN_ID,
52 complaint: {
53 ...complaint,
54 answer: adminInformationState.adminInformation.answerForComplaint,
55 }
56 }).then(res => {
57 if (res.data?.success) {
58 dispatch(setAdminInformation({
59 ...adminInformationState.adminInformation,
60 complaints: res.data?.complaints,
61 answerForComplaint: '',
62 }))
63
64 dispatch(setStyle({
65 ...styleState.style,
66 notification: {
67 show: true,
68 text: 'Answer sent successfully',
69 status: 'success',
70 },
71 }))
72 }
73 });
74 }
75
76 function onChangeAnswer(e) {
77 dispatch(setAdminInformation({
78 ...adminInformationState.adminInformation,
79 answerForComplaint: e.target.value,
80 }))
81 }
82
83 return (
84 <div className="fullscreen top-to-bottom-centered admin complaintsScreen">
85 <div>
86 <p className="link" onClick={() => hideComplaintsScreen()}>⬅ Go Back</p>
87
88 <h3>These are the complaints sent by players. You can answer them, and an email will be sent to the player so that he knows his complaint has been listened to.</h3>
89
90 { adminInformationState.adminInformation?.complaints?.map(function(complaint, complaintIndex) {
91 if (complaint.answered === "false") return (
92 <div className='complaint' key={complaint.by + complaint.description.substr(0, 20)}>
93 <div>
94 <p>By: {complaint.by}</p>
95 <p>Date: {new Date(complaint.date).toGMTString()}</p>
96 <div>
97 { adminInformationState.adminInformation.answeringComplaintIndex !== complaintIndex &&
98 <button className='primaryButton' onClick={() => answerComplaint(complaintIndex)}>Answer Complaint</button>
99 }
100 </div>
101 </div>
102 <div>
103 <textarea readOnly value={complaint.description}></textarea>
104 </div>
105 { adminInformationState.adminInformation.answeringComplaintIndex === complaintIndex && (
106 <div className="answering">
107 <p>Your answer:</p>
108 <textarea value={adminInformationState.adminInformation.answerForComplaint} onChange={(e) => onChangeAnswer(e)} placeholder="Post your answer here admin."></textarea>
109 <div>
110 <button className='secondaryButton' onClick={() => sendAnswer(complaint)}>Send Your Answer</button>
111 </div>
112 </div>
113 )}
114 </div>
115 )}
116 )}
117
118 { adminInformationState.adminInformation?.complaints?.map(function(complaint, complaintIndex) {
119 if (complaint.answered === "true") return (
120 <div className='complaint' key={complaint.by + complaint.description.substr(0, 20)}>
121 <div>
122 <p>By: {complaint.by}</p>
123 <p>Date: {new Date(complaint.date).toGMTString()}</p>
124 <div>
125 <p>Answered</p>
126 </div>
127 </div>
128 <div>
129 <textarea readOnly value={complaint.description}></textarea>
130 </div>
131 <div className="answering">
132 <p>Your answer:</p>
133 <textarea readOnly value={complaint.answer}></textarea>
134 </div>
135 </div>
136 )}
137 )}
138 </div>
139 </div>
140 )
141}
142
143export default Complaints
Note: See TracBrowser for help on using the repository browser.