Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Update useRequest in Confirm component
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloento committed Dec 6, 2023
1 parent 6bb6a08 commit f43d000
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
26 changes: 13 additions & 13 deletions src/Components/ShopCart/Confirm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Field, Textarea, Toast, ToastBody, ToastTitle, makeStyles, tokens } from "@fluentui/react-components";
import { Drawer, DrawerBody, DrawerHeader, DrawerHeaderTitle } from "@fluentui/react-components/unstable";
import { DismissRegular } from "@fluentui/react-icons";
import { useBoolean, useRequest } from "ahooks";
import { useBoolean } from "ahooks";
import { useState } from "react";
import { ColFlex } from "~/Helpers/Styles";
import { useErrorToast } from "~/Helpers/useToast";
Expand Down Expand Up @@ -31,7 +31,7 @@ const useStyles = makeStyles({
/**
* @author Aloento
* @since 0.1.0
* @version 0.4.0
* @version 0.4.1
*/
export function Confirm() {
const [cmt, setCmt] = useState<string>();
Expand All @@ -43,15 +43,16 @@ export function Confirm() {

const { dispatch, dispatchToast } = useErrorToast();

const { run } = useRequest(Hub.Order.Post.New.bind(Hub.Order.Post), {
onFinally([req], data, e) {
if (e)
return dispatch({
Message: "Failed Create Order",
Request: req,
Error: e
});

const { run } = Hub.Order.Post.useNew({
manual: true,
onError(e, req) {
dispatch({
Message: "Failed Create Order",
Request: req,
Error: e
});
},
onSuccess(data) {
dispatchToast(
<Toast>
<ToastTitle>Order Placed</ToastTitle>
Expand All @@ -64,8 +65,7 @@ export function Confirm() {
toggle();
Nav("History", data);
},
manual: true,
})
});

return <>
<Button appearance="primary" onClick={toggle} disabled={!List.length}>Checkout</Button>
Expand Down
27 changes: 15 additions & 12 deletions src/ShopNet/Order/Post.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useRequest } from "ahooks";
import { Options } from "ahooks/lib/useRequest/src/types";
import { ICartItem } from "~/Components/ShopCart";
import { ShopNet } from "../ShopNet";

Expand All @@ -10,21 +12,22 @@ export abstract class OrderPost extends ShopNet {
/**
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 0.2.0
*/
public static async New(cart: ICartItem[], cmt?: string): Promise<number> {
this.EnsureLogin();
public static useNew(options: Options<number, [ICartItem[], string | undefined]>) {
return useRequest((cart, cmt) => {
this.EnsureLogin();

const req = cart.map(x => {
const { Id, ...rest } = x;
return {
OrderId: Id,
...rest
};
});
const req = cart.map(x => {
const { Id, ...rest } = x;
return {
OrderId: Id,
...rest
};
});

const res = await this.Invoke<number>("OrderPostNew", req, cmt);
return res;
return this.Invoke<number>("OrderPostNew", req, cmt);
}, options);
}

/**
Expand Down

0 comments on commit f43d000

Please sign in to comment.