File size: 2,071 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios from 'axios';
import React from 'react';
import { Col, Form, Row } from 'react-bootstrap';
import { useForm } from 'react-hook-form';
import toast from 'react-hot-toast';
import swal from 'sweetalert';
import { useAppContext } from '../../../context';

const MakeAdmin = () => {
    const { state:{ user: {email}}} = useAppContext()
    const { register, handleSubmit, formState: { errors }, reset} = useForm();

    const onSubmit = data => {
        const loading = toast.loading('Please wait...')
        if(email === "test@admin.com"){
            toast.dismiss(loading)
            swal("Permission restriction!", "As a test admin, You haven't permission to add a new admin", "info");
        } else {
            axios.post('https://immense-river-40491.herokuapp.com/addAdmin',{email: data.email})
            .then(res => {
                toast.dismiss(loading)
                toast.success('One admin added successfully')
                reset();
            })
            .catch(err => {
                toast.dismiss(loading)
                toast.error(err.message)
            })
        }
    };
    return (
        <div className="px-2">
            <Form onSubmit={handleSubmit(onSubmit)} className="makeAdmin">
                <Row>
                    <Col>
                        <Form.Group>
                            <Form.Label style={{ fontWeight: "bold" }}>Email</Form.Label>
                            <Form.Control
                                type="text"
                                {...register("email", { required: true })}
                                placeholder="email"
                            />
                            {errors.email && <span className="text-danger">This field is required</span>}
                        </Form.Group>
                    </Col>
                    <Col>
                        <button type="submit" className="mainBtn adminBtn">Submit</button>
                    </Col>
                </Row>
            </Form>
        </div>
    )
};

export default MakeAdmin;