summaryrefslogtreecommitdiff
path: root/admin/src/pages/EnrollmentRequests/EnrollmentRequests.jsx
blob: bd2b17b41777a5c95ba972b4d0bf0d5403496e20 (plain)
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
import React from 'react';
import Accordion from '../../components/Accordion/Accordion';
import Button from '../../components/Button/Button';
import Header from '../../components/Header/Header';
import {
  deleteUser,
  getUnverifiedUsers,
  updateUser,
} from '../../services/apiservice';
import {useQuery, useMutation} from 'react-query';
import {useNavigate} from 'react-router-dom';
import BackButton from '../../components/BackButton/BackButton';
import {sendMessage} from '../../services/apiservice';
import Spinner from '../../components/Spinner/Spinner';

import styles from './EnrollmentRequests.module.css';
import MessageModal from '../../components/Modal/MessageModal';

const EnrollmentRequests = () => {
  const navigate = useNavigate();
  const [open, setOpen] = React.useState(false);
  const [message, setMessage] = React.useState('');
  const {data, refetch, isLoading} = useQuery('unverified', getUnverifiedUsers);

  const deleteUse = useMutation((payload) => deleteUser(payload));

  const updateUse = useMutation((id) => updateUser(id, {verified: true}), {
    onSuccess: () => {
      refetch();
    },
  });

  const setConfirm = useMutation((payload) => {
    sendMessage(payload);
  });

  const handleModalClick = (item) => {
    deleteUse.mutate(item._id, {
      onSuccess: () => {
        setConfirm.mutate({
          mobile: `+91${item.mobile}`,
          message: message,
        });
        setOpen(false);
        refetch();
      },
    });
  };

  if (isLoading) {
    return <Spinner heading='Admin' />;
  }

  return (
    <div className={styles.unverified_users}>
      <Header subheading='Admin' />
      <BackButton onClick={() => navigate('/')} />
      <h1 className={styles.unverified_users__heading}>Enrollment Requests</h1>
      <div className={styles.accordion}>
        {data?.data.length !== 0 ? (
          data?.data.map((item) => (
            <div className={styles.unverified_users__accordion} key={item._id}>
              <Accordion name={item.name} user={item} />
              <Button
                title='Accept'
                color='green'
                onClick={() => updateUse.mutate(item._id)}
              />
              <Button
                title='Reject'
                color='red'
                onClick={() => {
                  setOpen(true);
                }}
              />
              <MessageModal
                title='Reason for Reject'
                open={open}
                setOpen={setOpen}
                onChange={(e) => setMessage(e.target.value)}
                onClick={() => handleModalClick(item)}
              />
            </div>
          ))
        ) : (
          <div className={styles.unverified_users__nodata}>No Data Found</div>
        )}
      </div>
    </div>
  );
};

export default EnrollmentRequests;