diff --git a/src/Components/ShopCart/Confirm.tsx b/src/Components/ShopCart/Confirm.tsx index 830a3cc5..b66b3d07 100644 --- a/src/Components/ShopCart/Confirm.tsx +++ b/src/Components/ShopCart/Confirm.tsx @@ -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"; @@ -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(); @@ -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( Order Placed @@ -64,8 +65,7 @@ export function Confirm() { toggle(); Nav("History", data); }, - manual: true, - }) + }); return <> diff --git a/src/ShopNet/Order/Post.ts b/src/ShopNet/Order/Post.ts index 8e35add9..24006af6 100644 --- a/src/ShopNet/Order/Post.ts +++ b/src/ShopNet/Order/Post.ts @@ -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"; @@ -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 { - this.EnsureLogin(); + public static useNew(options: Options) { + 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("OrderPostNew", req, cmt); - return res; + return this.Invoke("OrderPostNew", req, cmt); + }, options); } /**