1
- import React , { useState } from 'react' ;
2
- import { Dialog , DialogTitle , DialogContent , DialogActions , Button , Typography , Stepper , Step , StepLabel } from '@mui/material' ;
3
- import { CheckCircle , LocalShipping , Cancel } from '@mui/icons-material' ;
4
- import { useDispatch } from 'react-redux' ;
5
- import { ChangeOrderStatus } from '../../store/feature/orderSlice' ;
1
+ import React , { useState } from "react" ;
2
+ import {
3
+ Dialog ,
4
+ DialogTitle ,
5
+ DialogContent ,
6
+ DialogActions ,
7
+ Button ,
8
+ Typography ,
9
+ Stepper ,
10
+ Step ,
11
+ StepLabel ,
12
+ } from "@mui/material" ;
6
13
7
- const OrderDetails = ( { open , onClose , order , orderStatus } ) => {
8
- const steps = [ 'Ordered' , 'Shipped' , 'Delivered' ] ;
14
+ import { useDispatch , useSelector } from "react-redux" ;
15
+ import { ChangeOrderStatus } from "../../store/feature/orderSlice" ;
9
16
17
+ const OrderDetails = ( { open, onClose, order, orderStatus } ) => {
18
+ const steps = [ "Ordered" , "Shipped" , "Delivered" ] ;
10
19
const dispatch = useDispatch ( ) ;
11
- const [ status , setStatus ] = useState ( 'delivered' )
20
+ const [ status , setStatus ] = useState ( orderStatus ) ;
21
+
22
+ const user = useSelector ( ( state ) => {
23
+ return state . auth . user ;
24
+ } ) ;
25
+
12
26
const getActiveStep = ( status ) => {
13
27
switch ( status ) {
14
28
case 'Ordered' :
@@ -18,36 +32,46 @@ const OrderDetails = ({ open, onClose, order, orderStatus }) => {
18
32
case 'Delivered' :
19
33
return 2 ;
20
34
default :
21
- return - 1 ;
35
+ return - 1 ;
36
+ }
37
+ } ;
38
+
39
+ const handleChangeStatus = ( ) => {
40
+ if ( status !== orderStatus ) {
41
+ dispatch ( ChangeOrderStatus ( { OrderId : order , status } ) ) ;
22
42
}
23
43
} ;
24
44
25
45
return (
26
46
< Dialog open = { open } onClose = { onClose } maxWidth = "sm" fullWidth >
27
- < DialogTitle className = "bg-blue-500 text-white" > Order Details</ DialogTitle >
47
+ < DialogTitle className = "bg-blue-500 text-white" >
48
+ Order Details
49
+ </ DialogTitle >
28
50
< DialogContent >
29
51
< div className = "space-y-4" >
30
52
< Typography variant = "h6" className = "font-bold" >
31
53
Order ID: < span className = "font-normal" > { order } </ span >
32
54
</ Typography >
33
55
< Typography variant = "body1" >
34
- Status: < span className = "font-bold" > { status } </ span >
56
+ Status:
57
+ { user ?. isMerchant ? (
58
+ < select
59
+ value = { status }
60
+ onChange = { ( e ) => setStatus ( e . target . value ) }
61
+ className = "border p-2 rounded"
62
+ >
63
+ < option value = "Ordered" > Ordered</ option >
64
+ < option value = "Shipped" > Shipped</ option >
65
+ < option value = "Delivered" > Delivered</ option >
66
+ </ select >
67
+ ) : (
68
+ < span className = "font-bold" > { orderStatus } </ span >
69
+ ) }
35
70
</ Typography >
36
71
< Stepper activeStep = { getActiveStep ( orderStatus ) } alternativeLabel >
37
72
{ steps . map ( ( label ) => (
38
73
< Step key = { label } >
39
- < StepLabel >
40
- < div className = "flex items-center space-x-4" >
41
- { label === 'Delivered' ? (
42
- < CheckCircle color = "success" />
43
- ) : label === 'Shipped' ? (
44
- < LocalShipping color = "action" />
45
- ) : (
46
- < CheckCircle color = "primary" />
47
- ) }
48
- < Typography variant = "body2" > { label } </ Typography >
49
- </ div >
50
- </ StepLabel >
74
+ < StepLabel > { label } </ StepLabel >
51
75
</ Step >
52
76
) ) }
53
77
</ Stepper >
@@ -57,14 +81,11 @@ const OrderDetails = ({ open, onClose, order, orderStatus }) => {
57
81
< Button onClick = { onClose } color = "primary" >
58
82
Close
59
83
</ Button >
60
- < Button color = "primary" onClick = { ( ) => {
61
- dispatch (
62
- ChangeOrderStatus ( { OrderId :order , status} )
63
- ) ;
64
- } } >
65
- ChangeOrderStatus
66
- </ Button >
67
-
84
+ { user ?. isMerchant && (
85
+ < Button color = "primary" onClick = { handleChangeStatus } >
86
+ Change Order Status
87
+ </ Button >
88
+ ) }
68
89
</ DialogActions >
69
90
</ Dialog >
70
91
) ;
0 commit comments