|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { |
| 3 | + getAllMentorshipRequests, |
| 4 | + sendStaledRequestEmail, |
| 5 | +} from '../../api/admin'; |
| 6 | +import Card from '../components/Card'; |
| 7 | +import { MentorshipRequest } from '../../types/models'; |
| 8 | +import { formatRequestTime } from '../../helpers/mentorship'; |
| 9 | +import Button from '../components/Button'; |
| 10 | +import { toast } from 'react-toastify'; |
| 11 | + |
| 12 | +const Admin = () => { |
| 13 | + const [mentorshipLoading, setMentorshipLoading] = useState<string | null>(); |
| 14 | + const [mentorshipRequests, setMentorshipRequests] = useState< |
| 15 | + MentorshipRequest[] |
| 16 | + >([]); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + getAllMentorshipRequests().then(response => { |
| 20 | + if (response?.success) { |
| 21 | + setMentorshipRequests( |
| 22 | + response.data.filter(({ mentor, mentee }) => !!mentor && !!mentee) |
| 23 | + ); |
| 24 | + } |
| 25 | + }); |
| 26 | + }, []); |
| 27 | + |
| 28 | + const sendEmail = async (mentorshipId: string) => { |
| 29 | + setMentorshipLoading(mentorshipId); |
| 30 | + await sendStaledRequestEmail(mentorshipId); |
| 31 | + setMentorshipRequests( |
| 32 | + mentorshipRequests.map(mentorship => { |
| 33 | + if (mentorship.id === mentorshipId) { |
| 34 | + return { |
| 35 | + ...mentorship, |
| 36 | + reminderSentAt: new Date().toString(), |
| 37 | + }; |
| 38 | + } |
| 39 | + return mentorship; |
| 40 | + }) |
| 41 | + ); |
| 42 | + setMentorshipLoading(null); |
| 43 | + toast.success('Email sent'); |
| 44 | + }; |
| 45 | + |
| 46 | + const columns = [ |
| 47 | + 'Mentor', |
| 48 | + 'Mentee', |
| 49 | + 'Status', |
| 50 | + 'Created', |
| 51 | + 'Sent', |
| 52 | + ].map(column => <td key={column}>{column}</td>); |
| 53 | + |
| 54 | + const rows = mentorshipRequests |
| 55 | + .sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()) |
| 56 | + .map(({ mentor, mentee, status, date, id, reminderSentAt }) => { |
| 57 | + const pending = status === 'New' || status === 'Viewed'; |
| 58 | + return ( |
| 59 | + <tr key={id}> |
| 60 | + <td>{mentor.name}</td> |
| 61 | + <td>{mentee.name}</td> |
| 62 | + <td>{status}</td> |
| 63 | + <td>{pending ? formatRequestTime(new Date(date)) : 0}</td> |
| 64 | + <td> |
| 65 | + {reminderSentAt ? formatRequestTime(new Date(reminderSentAt)) : 0} |
| 66 | + </td> |
| 67 | + <td> |
| 68 | + {pending && ( |
| 69 | + <Button |
| 70 | + disabled={!!reminderSentAt} |
| 71 | + title={reminderSentAt && `Sent at ${new Date(reminderSentAt)}`} |
| 72 | + isLoading={id === mentorshipLoading} |
| 73 | + onClick={() => sendEmail(id)} |
| 74 | + > |
| 75 | + Send Email |
| 76 | + </Button> |
| 77 | + )} |
| 78 | + </td> |
| 79 | + </tr> |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + return ( |
| 84 | + <Card> |
| 85 | + {mentorshipRequests.length ? ( |
| 86 | + <table> |
| 87 | + <thead> |
| 88 | + <tr>{columns}</tr> |
| 89 | + </thead> |
| 90 | + <tbody>{rows}</tbody> |
| 91 | + </table> |
| 92 | + ) : ( |
| 93 | + <>No requests</> |
| 94 | + )} |
| 95 | + </Card> |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +export default Admin; |
0 commit comments