File size: 3,182 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
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
import React, { useEffect, useState } from 'react';
import { Button } from 'react-bootstrap';
import ReviewForm from './ReviewFrom';
import './Review.css';
import { Link } from 'react-router-dom';
import userImg from '../../../../Assets/user.svg';
import axios from 'axios';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faTrashAlt } from '@fortawesome/free-solid-svg-icons';
import { faEdit } from '@fortawesome/free-regular-svg-icons';
import toast from 'react-hot-toast';
import swal from 'sweetalert';
import { useAppContext } from '../../../../context';


const Review = () => {
    const { state: { user: { email, img } } } = useAppContext();
    const [review, setReview] = useState({});
    const [isUpdated, setIsUpdated] = useState(false)
    const {_id, name, address, description} = review || {};
    useEffect(() => {
        axios(`https://immense-river-40491.herokuapp.com/userReview?email=${email}`)
        .then(res => {
            setReview(res.data[0]);
        })
    }, [email, isUpdated])

    const handleDelete = (id) => {
        setIsUpdated(false)
        swal({
            title: "Are you sure?",
            text: "Are you sure! you want to delete the review?",
            icon: "warning",
            buttons: true,
            dangerMode: true,
          })
          .then( wantDelete => {
            if (wantDelete) {
                const loading = toast.loading('deleting...Please wait!')
                axios.delete(`https://immense-river-40491.herokuapp.com/deleteReview/${id}`)
                .then(res => {
                    toast.dismiss(loading)
                    if(res){
                        setIsUpdated(true);
                        toast.success('Your review has been deleted successfully!');
                    }
                    else{
                        toast.error('Something went wrong, please try again');
                    }
                })
                .catch(err => {
                    toast.dismiss(loading)
                    swal({
                        title: "Failed!",
                        text: 'Something went wrong, please try again',
                        icon: "error",
                      });
                })
            } 
          });
    }
    return (
        <div>
            { description ?
            <div className="userReviewBox">
                <div className="review col-md-6 mx-auto">
                    { img ? <img src={img} alt=""/>:
                    <img src={`${userImg}`} alt=""/>}
                    <h5 className="testimonialName">{name}</h5>
                    <h6 className="testimonialAddress">{address}</h6>
                    <p><i>{description}</i></p>
                </div>
                <Button as={Link} to={`/dashboard/review/${_id}`} variant="outline-success"> <FontAwesomeIcon icon={faEdit}/> Edit</Button>
                <Button variant="outline-danger" onClick={() => handleDelete(_id)}> <FontAwesomeIcon icon={faTrashAlt}/> Delete</Button>
            </div>
            :
            <ReviewForm setIsUpdated={setIsUpdated}/> 
            }
        </div>
    );
};

export default Review;