1 | import React, { useState, useEffect } from 'react';
|
---|
2 | import { useNavigate } from 'react-router-dom';
|
---|
3 | import StarRating from '../RecipesComponents/StarRating';
|
---|
4 | import styles from '../../css/ShoppingCartCss/delivery-review-style.module.css';
|
---|
5 |
|
---|
6 | const DeliveryReview = () => {
|
---|
7 | const [rating, setRating] = useState(0);
|
---|
8 | const [comment, setComment] = useState('');
|
---|
9 | const [showModal, setShowModal] = useState(false);
|
---|
10 | const navigate = useNavigate();
|
---|
11 |
|
---|
12 | useEffect(() => {
|
---|
13 | const orderId = localStorage.getItem('orderId');
|
---|
14 | if (!orderId) {
|
---|
15 | navigate('/');
|
---|
16 | }
|
---|
17 | }, [navigate]);
|
---|
18 |
|
---|
19 | const handleConfirm = () => {
|
---|
20 | const orderId = localStorage.getItem('orderId');
|
---|
21 | if (orderId && (rating > 0 || comment.trim() !== '')) {
|
---|
22 | const token = localStorage.getItem('token');
|
---|
23 | fetch(`http://localhost:8080/api/orders/${orderId}/review`, {
|
---|
24 | method: 'PUT',
|
---|
25 | headers: {
|
---|
26 | 'Content-Type': 'application/json',
|
---|
27 | 'Authorization': `Bearer ${token}`
|
---|
28 | },
|
---|
29 | body: JSON.stringify({
|
---|
30 | review: comment,
|
---|
31 | rating: rating
|
---|
32 | })
|
---|
33 | })
|
---|
34 | .then(response => response.json())
|
---|
35 | .then(data => {
|
---|
36 | if (data) {
|
---|
37 | setShowModal(true);
|
---|
38 | setTimeout(() => {
|
---|
39 | navigate('/');
|
---|
40 | localStorage.removeItem('orderId');
|
---|
41 | window.location.reload();
|
---|
42 | }, 2000);
|
---|
43 | }
|
---|
44 | })
|
---|
45 | .catch(error => {
|
---|
46 | console.error('Error submitting review:', error);
|
---|
47 | });
|
---|
48 | } else {
|
---|
49 | alert('Please provide a rating or a comment!');
|
---|
50 | }
|
---|
51 | };
|
---|
52 |
|
---|
53 | return (
|
---|
54 | <div className={styles.reviewContainer}>
|
---|
55 | <h2>Rate Your Delivery Experience</h2>
|
---|
56 |
|
---|
57 | <StarRating rating={rating} setRating={setRating} />
|
---|
58 |
|
---|
59 | <textarea
|
---|
60 | className={styles.reviewComment}
|
---|
61 | placeholder="Leave a comment (optional)..."
|
---|
62 | value={comment}
|
---|
63 | onChange={(e) => setComment(e.target.value)}
|
---|
64 | />
|
---|
65 |
|
---|
66 | <button onClick={handleConfirm} className={styles.confirmButton}>
|
---|
67 | Confirm Review
|
---|
68 | </button>
|
---|
69 |
|
---|
70 | {showModal && (
|
---|
71 | <div className={styles.modal}>
|
---|
72 | <div className={styles.modalContent}>
|
---|
73 | <h3>Thank you for the review!</h3>
|
---|
74 | </div>
|
---|
75 | </div>
|
---|
76 | )}
|
---|
77 | </div>
|
---|
78 | );
|
---|
79 | };
|
---|
80 |
|
---|
81 | export default DeliveryReview;
|
---|