diff --git a/package-lock.json b/package-lock.json index e5ed72f7..b1a32d53 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11045,15 +11045,8 @@ }, "node_modules/symbol-tree": { "version": "3.2.4", - "resolved": "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.4.tgz", - "integrity": "sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==", - "dev": true - }, - "node_modules/synchronous-promise": { - "version": "2.0.17", - "resolved": "https://registry.npmjs.org/synchronous-promise/-/synchronous-promise-2.0.17.tgz", - "integrity": "sha512-AsS729u2RHUfEra9xJrE39peJcc2stq2+poBXX8bcM08Y6g9j/i/PUzwNQqkaJde7Ntg1TO7bSREbR5sdosQ+g==", - "dev": true + "dev": true, + "license": "MIT" }, "node_modules/synckit": { "version": "0.9.1", diff --git a/src/components/Orders/OrderDetailsModal.tsx b/src/components/Orders/OrderDetailsModal.tsx index d774310d..e7142fd8 100644 --- a/src/components/Orders/OrderDetailsModal.tsx +++ b/src/components/Orders/OrderDetailsModal.tsx @@ -1,19 +1,30 @@ +import { useState } from 'react'; import Order from '@/interfaces/order'; +import { updateOrderStatus } from '@/features/Orders/ordersSlice'; +import { useAppDispatch } from '@/app/hooks'; +interface ModalPropsb { + order: Order; + close: () => void; + cancel: (id: number) => void; + edit: () => void; + status: string; +} interface ModalProps { order: Order; close: () => void; + cancel: (id: number) => void; } -function OrderDetailsModal({ close, order }: ModalProps) { +function Modal({ close, order, cancel, edit, status }: ModalPropsb) { const billigDetails = order.deliveryInfo; return ( -
+

Order #234

- {order.status} + {status}
@@ -96,12 +93,14 @@ function OrderDetailsModal({ close, order }: ModalProps) { @@ -110,4 +109,95 @@ function OrderDetailsModal({ close, order }: ModalProps) { ); } -export default OrderDetailsModal; +function Edit({ + edit, + order, + status, +}: { + edit: () => void; + order: Order; + status: (nstat: string) => void; +}) { + const validStatuses = [ + 'Pending', + 'Failed', + 'Canceled', + 'Paid', + 'Shipping', + 'Delivered', + 'Returned', + 'Completed', + ]; + const dispatch = useAppDispatch(); + return ( +
+
+

+ Edit Order Status +

+ + +
+ + +
+
+
+ ); +} + +export default function OrderDetailsModal({ + close, + order, + cancel, +}: ModalProps) { + const [edit, setEdit] = useState(false); + const [status, setStatus] = useState(order.status); + return edit ? ( + setEdit(false)} + order={order} + status={(nstat) => setStatus(nstat)} + /> + ) : ( + setEdit(true)} + status={status} + /> + ); +} diff --git a/src/components/Orders/Orders.tsx b/src/components/Orders/Orders.tsx index 437bdf4c..f0bc430a 100644 --- a/src/components/Orders/Orders.tsx +++ b/src/components/Orders/Orders.tsx @@ -125,6 +125,7 @@ export function Orders() { setSelected(-1)} order={orders.find((order) => order.id === selected) as Order} + cancel={(id: number) => dispatch(cancelOrder(id))} />
)} diff --git a/src/features/Cart/cartSlice.tsx b/src/features/Cart/cartSlice.tsx index bef23aab..fcb19117 100644 --- a/src/features/Cart/cartSlice.tsx +++ b/src/features/Cart/cartSlice.tsx @@ -139,7 +139,9 @@ const cartSlice = createSlice({ const update = state.cartItems[index] as Cart; update.quantity = action.payload.quantity; state.loading = false; - state.cartItems = state.cartItems.splice(index, 1, update); + const { cartItems } = state; + cartItems.splice(index, 1, update); + state.cartItems = cartItems.slice(); }) .addCase(updateCartItemQuantity.rejected, (state, action) => { state.loading = false; diff --git a/src/features/Orders/ordersSlice.ts b/src/features/Orders/ordersSlice.ts index 1c01b27c..4fcdbfe3 100644 --- a/src/features/Orders/ordersSlice.ts +++ b/src/features/Orders/ordersSlice.ts @@ -80,6 +80,26 @@ const ordersSlice = createSlice({ }); } }, + updateOrderStatus: ( + state, + action: PayloadAction<{ id: number; status: string }> + ) => { + const { id, status } = action.payload; + const orderToUpdate = state.orders.find((order) => order.id === id); + + if (orderToUpdate) { + const tokenFromStorage = localStorage.getItem('token') || ''; + axios.put( + `${baseUrl}/order/${id}`, + { status }, + { + headers: { + Authorization: `Bearer ${tokenFromStorage}`, + }, + } + ); + } + }, }, extraReducers: (builder) => { builder @@ -98,7 +118,7 @@ const ordersSlice = createSlice({ }, }); -export const { cancelOrder } = ordersSlice.actions; +export const { cancelOrder, updateOrderStatus } = ordersSlice.actions; export const selectOrders = (state: RootState) => state.orders; export default ordersSlice.reducer;