Skip to content

Commit

Permalink
Fix/improve cancel order refunds and edit address, and updated UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Winston-Hsiao committed Nov 18, 2024
1 parent c7f5972 commit eb99923
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 91 deletions.
41 changes: 13 additions & 28 deletions frontend/src/components/modals/CancelOrderModal.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import React, { useState } from "react";

import Modal from "@/components/ui/Modal";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Label } from "@/components/ui/label";
import { paths } from "@/gen/api";
import { useAlertQueue } from "@/hooks/useAlertQueue";
Expand Down Expand Up @@ -43,9 +38,9 @@ const CancelOrderModal: React.FC<CancelOrderModalProps> = ({
const MAX_REASON_LENGTH = 500;

const [cancellation, setCancellation] = useState<RefundRequest>({
payment_intent_id: "",
payment_intent_id: order.order.stripe_payment_intent_id,
cancel_reason: { reason: "", details: "" },
amount: 0,
amount: order.order.price_amount,
});
const [customReason, setCustomReason] = useState("");

Expand Down Expand Up @@ -81,11 +76,6 @@ const CancelOrderModal: React.FC<CancelOrderModalProps> = ({
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();

if (!cancellation.payment_intent_id) {
addErrorAlert("Invalid payment information");
return;
}

try {
const { data, error } = await client.PUT("/stripe/refunds/{order_id}", {
params: { path: { order_id: order.order.id } },
Expand All @@ -110,15 +100,13 @@ const CancelOrderModal: React.FC<CancelOrderModalProps> = ({
};

return (
<Dialog open={isOpen} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[425px] bg-gray-1 text-gray-12 border border-gray-3 rounded-lg shadow-lg">
<DialogHeader>
<DialogTitle>Cancel Order</DialogTitle>
</DialogHeader>
<Modal isOpen={isOpen} onClose={() => onOpenChange(false)}>
<div className="p-6">
<h2 className="text-xl font-semibold mb-4">Cancel Order</h2>
<form onSubmit={handleSubmit}>
<div className="grid gap-4 py-4">
<div className="grid gap-4 py-4 mb-4">
<div className="grid gap-2">
<Label htmlFor="cancel_reason">Cancel Reason</Label>
<Label htmlFor="cancel_reason">Reason for cancelling</Label>
<select
id="cancel_reason"
name="cancel_reason"
Expand All @@ -127,7 +115,7 @@ const CancelOrderModal: React.FC<CancelOrderModalProps> = ({
className="bg-gray-2 border-gray-3 text-gray-12 rounded-md p-2"
>
<option value="" disabled>
Select a reason for cancellation
Select a reason for cancelling
</option>
{cancellationReasons.map((reason) => (
<option key={reason} value={reason}>
Expand Down Expand Up @@ -156,21 +144,18 @@ const CancelOrderModal: React.FC<CancelOrderModalProps> = ({
<div className="flex justify-end gap-2">
<Button
type="button"
variant="outline"
variant="default"
onClick={() => onOpenChange(false)}
>
Cancel
</Button>
<Button
type="submit"
className="bg-primary-9 text-gray-1 hover:bg-gray-12"
>
<Button type="submit" variant="outline">
Save Changes
</Button>
</div>
</form>
</DialogContent>
</Dialog>
</div>
</Modal>
);
};

Expand Down
30 changes: 10 additions & 20 deletions frontend/src/components/modals/EditAddressModal.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import React, { useState } from "react";

import Modal from "@/components/ui/Modal";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useAlertQueue } from "@/hooks/useAlertQueue";
Expand Down Expand Up @@ -47,7 +42,7 @@ const EditAddressModal: React.FC<EditAddressModalProps> = ({
e.preventDefault();
try {
const { data, error } = await client.PUT(
"/orders/{order_id}/shipping-address",
"/orders/shipping-address/{order_id}",
{
params: { path: { order_id: order.order.id } },
body: address,
Expand All @@ -72,13 +67,11 @@ const EditAddressModal: React.FC<EditAddressModalProps> = ({
};

return (
<Dialog open={isOpen} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[425px] bg-gray-1 text-gray-12 border border-gray-3 rounded-lg shadow-lg">
<DialogHeader>
<DialogTitle>Edit Delivery Address</DialogTitle>
</DialogHeader>
<Modal isOpen={isOpen} onClose={() => onOpenChange(false)}>
<div className="p-6">
<h2 className="text-xl font-semibold mb-4">Edit Delivery Address</h2>
<form onSubmit={handleSubmit}>
<div className="grid gap-4 py-4">
<div className="grid gap-3 py-4">
<div className="grid gap-2">
<Label htmlFor="shipping_name">Name</Label>
<Input
Expand Down Expand Up @@ -153,21 +146,18 @@ const EditAddressModal: React.FC<EditAddressModalProps> = ({
<div className="flex justify-end gap-2">
<Button
type="button"
variant="outline"
variant="default"
onClick={() => onOpenChange(false)}
>
Cancel
</Button>
<Button
type="submit"
className="bg-primary-9 text-gray-1 hover:bg-gray-12"
>
<Button type="submit" variant="outline">
Save Changes
</Button>
</div>
</form>
</DialogContent>
</Dialog>
</div>
</Modal>
);
};

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/ui/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const Modal: React.FC<ModalProps> = ({ isOpen, onClose, children }) => {
return (
<div className="fixed inset-0 z-50 flex items-center justify-center">
<div
className="absolute inset-0 bg-gray-1 opacity-50"
className="absolute inset-0 bg-gray-11 opacity-50"
onClick={onClose}
></div>
<div className="bg-gray-2 rounded-lg z-10 max-w-md w-full">
<div className="bg-gray-12 rounded-lg z-10 max-w-md w-full">
{children}
</div>
</div>
Expand Down
54 changes: 27 additions & 27 deletions frontend/src/gen/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,15 +652,15 @@ export interface paths {
patch?: never;
trace?: never;
};
"/orders/{order_id}": {
"/orders/me": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
/** Get Order */
get: operations["get_order_orders__order_id__get"];
/** Get User Orders */
get: operations["get_user_orders_orders_me_get"];
put?: never;
post?: never;
delete?: never;
Expand All @@ -669,15 +669,15 @@ export interface paths {
patch?: never;
trace?: never;
};
"/orders/me": {
"/orders/{order_id}": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
/** Get User Orders */
get: operations["get_user_orders_orders_me_get"];
/** Get Order */
get: operations["get_order_orders__order_id__get"];
put?: never;
post?: never;
delete?: never;
Expand All @@ -686,7 +686,7 @@ export interface paths {
patch?: never;
trace?: never;
};
"/orders/{order_id}/shipping-address": {
"/orders/shipping-address/{order_id}": {
parameters: {
query?: never;
header?: never;
Expand All @@ -695,7 +695,7 @@ export interface paths {
};
get?: never;
/** Update Order Shipping Address */
put: operations["update_order_shipping_address_orders__order_id__shipping_address_put"];
put: operations["update_order_shipping_address_orders_shipping_address__order_id__put"];
post?: never;
delete?: never;
options?: never;
Expand Down Expand Up @@ -1700,7 +1700,7 @@ export interface components {
/** Stripe Price Id */
stripe_price_id: string;
/** Stripe Payment Intent Id */
stripe_payment_intent_id?: string | null;
stripe_payment_intent_id: string;
/** Preorder Release Date */
preorder_release_date?: number | null;
/** Preorder Deposit Amount */
Expand Down Expand Up @@ -3291,13 +3291,11 @@ export interface operations {
};
};
};
get_order_orders__order_id__get: {
get_user_orders_orders_me_get: {
parameters: {
query?: never;
header?: never;
path: {
order_id: string;
};
path?: never;
cookie?: never;
};
requestBody?: never;
Expand All @@ -3308,25 +3306,18 @@ export interface operations {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["OrderWithProduct"];
};
};
/** @description Validation Error */
422: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["HTTPValidationError"];
"application/json": components["schemas"]["OrderWithProduct"][];
};
};
};
};
get_user_orders_orders_me_get: {
get_order_orders__order_id__get: {
parameters: {
query?: never;
header?: never;
path?: never;
path: {
order_id: string;
};
cookie?: never;
};
requestBody?: never;
Expand All @@ -3337,12 +3328,21 @@ export interface operations {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["OrderWithProduct"][];
"application/json": components["schemas"]["OrderWithProduct"];
};
};
/** @description Validation Error */
422: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["HTTPValidationError"];
};
};
};
};
update_order_shipping_address_orders__order_id__shipping_address_put: {
update_order_shipping_address_orders_shipping_address__order_id__put: {
parameters: {
query?: never;
header?: never;
Expand Down
2 changes: 1 addition & 1 deletion store/app/crud/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OrderDataCreate(TypedDict):
stripe_product_id: str
stripe_price_id: str
stripe_connect_account_id: str
stripe_payment_intent_id: NotRequired[str | None]
stripe_payment_intent_id: str
preorder_release_date: NotRequired[int | None]
preorder_deposit_amount: NotRequired[int | None]
stripe_preorder_deposit_id: NotRequired[str | None]
Expand Down
4 changes: 2 additions & 2 deletions store/app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ class Order(StoreBaseModel):
stripe_connect_account_id: str
stripe_product_id: str
stripe_price_id: str
stripe_payment_intent_id: str | None = None
stripe_payment_intent_id: str
preorder_release_date: int | None = None
preorder_deposit_amount: int | None = None
stripe_preorder_deposit_id: str | None = None
Expand Down Expand Up @@ -619,7 +619,7 @@ def create(
quantity: int,
price_amount: int,
currency: str,
stripe_payment_intent_id: str | None = None,
stripe_payment_intent_id: str,
preorder_release_date: int | None = None,
preorder_deposit_amount: int | None = None,
stripe_preorder_deposit_id: str | None = None,
Expand Down
5 changes: 2 additions & 3 deletions store/app/routers/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def get_product_info(order: Order) -> OrderWithProduct:
results = await asyncio.gather(*[get_product_info(order) for order in orders])
return results

except OrdersNotFoundError as e:
except OrdersNotFoundError:
logger.info(f"No orders found for user: {user.id}")
return []
except Exception as e:
Expand All @@ -89,7 +89,6 @@ async def get_order(
if not order or order.user_id != user.id:
raise HTTPException(status_code=404, detail="Order not found")

# Get product info from Stripe
product = await stripe.get_product(order.stripe_product_id, crud)

# Convert ProductResponse to ProductInfo
Expand All @@ -115,7 +114,7 @@ class UpdateOrderAddressRequest(BaseModel):
shipping_country: str


@router.put("/{order_id}/shipping-address", response_model=Order)
@router.put("/shipping-address/{order_id}", response_model=Order)
async def update_order_shipping_address(
order_id: str,
address_update: UpdateOrderAddressRequest,
Expand Down
Loading

0 comments on commit eb99923

Please sign in to comment.