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;