feat(art): switch submit button to ActionButton
This commit is contained in:
@@ -2,10 +2,12 @@
|
|||||||
|
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import posthog from "posthog-js";
|
import posthog from "posthog-js";
|
||||||
import { useState } from "react";
|
import { useRef, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { checkEnkaStatus, submitArtifact } from "@/lib/api";
|
import { checkEnkaStatus, submitArtifact } from "@/lib/api";
|
||||||
import { shared } from "@/lib/comms";
|
import { shared } from "@/lib/comms";
|
||||||
|
import type { ActionSubmitContextData } from "@/components/action-submit-provider";
|
||||||
|
import { ActionSubmitContext } from "@/components/action-submit-provider";
|
||||||
|
|
||||||
export function ArtifactFormWrapper({
|
export function ArtifactFormWrapper({
|
||||||
edit,
|
edit,
|
||||||
@@ -20,6 +22,12 @@ export function ArtifactFormWrapper({
|
|||||||
const [, setWarningUid] = shared.state("warning.uid");
|
const [, setWarningUid] = shared.state("warning.uid");
|
||||||
const [warningSrc, setWarningSrc] = shared.state("warning.src");
|
const [warningSrc, setWarningSrc] = shared.state("warning.src");
|
||||||
const [dataHold, setDataHold] = useState<FormData>();
|
const [dataHold, setDataHold] = useState<FormData>();
|
||||||
|
const callback = useRef<() => void>(() => {});
|
||||||
|
const context: ActionSubmitContextData = {
|
||||||
|
listenForComplete(complete) {
|
||||||
|
callback.current = complete;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
// Disable warning for 60 seconds
|
// Disable warning for 60 seconds
|
||||||
shared.signal(
|
shared.signal(
|
||||||
@@ -53,15 +61,18 @@ export function ArtifactFormWrapper({
|
|||||||
} catch {}
|
} catch {}
|
||||||
router.refresh();
|
router.refresh();
|
||||||
})
|
})
|
||||||
.catch((e) => toast.error(`${e.message || e}`));
|
.catch((e) => toast.error(`${e.message || e}`))
|
||||||
|
.finally(callback.current);
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<form
|
<ActionSubmitContext.Provider value={context}>
|
||||||
{...props}
|
<form
|
||||||
onSubmit={(e) => {
|
{...props}
|
||||||
e.preventDefault();
|
onSubmit={(e) => {
|
||||||
submit(new FormData(e.target as HTMLFormElement));
|
e.preventDefault();
|
||||||
}}
|
submit(new FormData(e.target as HTMLFormElement));
|
||||||
/>
|
}}
|
||||||
|
/>
|
||||||
|
</ActionSubmitContext.Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,11 @@
|
|||||||
import type { MaybePromise } from "bun";
|
import type { MaybePromise } from "bun";
|
||||||
import { Check } from "lucide-react";
|
import { Check } from "lucide-react";
|
||||||
import type { ComponentProps, ReactNode } from "react";
|
import type { ComponentProps, ReactNode } from "react";
|
||||||
import { useEffect, useState } from "react";
|
import { use, useEffect, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Spinner } from "@/components/ui/spinner";
|
import { Spinner } from "@/components/ui/spinner";
|
||||||
|
import { ActionSubmitContext } from "./action-submit-provider";
|
||||||
|
|
||||||
export function ActionButton({
|
export function ActionButton({
|
||||||
action,
|
action,
|
||||||
@@ -15,10 +16,11 @@ export function ActionButton({
|
|||||||
text,
|
text,
|
||||||
...props
|
...props
|
||||||
}: ComponentProps<typeof Button> & {
|
}: ComponentProps<typeof Button> & {
|
||||||
action: () => MaybePromise<void>;
|
action: (() => MaybePromise<void>) | "provider";
|
||||||
text?: string;
|
text?: string;
|
||||||
}) {
|
}) {
|
||||||
const [state, setState] = useState<0 | 1 | 2>(0);
|
const [state, setState] = useState<0 | 1 | 2>(0);
|
||||||
|
const ctx = use(ActionSubmitContext);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (state !== 2) return;
|
if (state !== 2) return;
|
||||||
@@ -33,7 +35,9 @@ export function ActionButton({
|
|||||||
onClick?.(ev);
|
onClick?.(ev);
|
||||||
setState(1);
|
setState(1);
|
||||||
try {
|
try {
|
||||||
await action();
|
if (action === "provider")
|
||||||
|
await new Promise<void>(ctx.listenForComplete);
|
||||||
|
else await action();
|
||||||
setState(2);
|
setState(2);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
toast.error("An error occured!");
|
toast.error("An error occured!");
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { createContext } from "react";
|
||||||
|
|
||||||
|
export type ActionSubmitContextData = {
|
||||||
|
listenForComplete: (complete: () => void) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ActionSubmitContext = createContext<ActionSubmitContextData>({
|
||||||
|
listenForComplete: (r) => {
|
||||||
|
r();
|
||||||
|
},
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user