Skip to content

Commit 1cb1976

Browse files
committed
Add cron trigger button to landing page
1 parent 72e1841 commit 1cb1976

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

landing/src/components/Button.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ export interface ButtonProps {
55
type?: string;
66
onClick?: any;
77
disabled?: boolean;
8+
className?: string;
89
}
910

10-
export const Button = ({ children, onClick, type, disabled }: ButtonProps) => {
11+
export const Button = ({
12+
children,
13+
onClick,
14+
type,
15+
disabled,
16+
className = "",
17+
}: ButtonProps) => {
1118
const color = disabled ? "bg-slate-400" : "bg-blue-500";
1219

1320
return (
14-
<button class={`${color} text-white px-5 py-1 font-semibold rounded`} onClick={onClick} type={type} disabled={disabled}>
21+
<button
22+
className={`${color} text-white px-5 py-1 font-semibold rounded ${className}`}
23+
onClick={onClick}
24+
type={type}
25+
disabled={disabled}
26+
>
1527
{children}
1628
</button>
1729
);

landing/src/components/Content.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import { Spinner } from "./Loading";
1010
export const Content = () => {
1111
return (
1212
<IntrospectConsumer>
13-
{value => <ContentUI introspect={value} /> }
13+
{(value) => <ContentUI introspect={value} />}
1414
</IntrospectConsumer>
1515
);
16-
}
16+
};
1717

1818
/**
1919
* A messy catch-all component to render almost the entire landing page.
@@ -152,7 +152,7 @@ export const ContentUI = ({ introspect }: { introspect: IntrospectValue }) => {
152152

153153
{fns?.functions.length ? (
154154
<div class="flex flex-col">
155-
<div class="w-full grid grid-cols-[1fr_1fr_1fr] font-semibold border-b-2 border-slate-300 pb-1">
155+
<div class="w-full grid grid-cols-[1fr_1fr_1fr_100px] font-semibold border-b-2 border-slate-300 pb-1">
156156
<div>Name</div>
157157
<div>ID</div>
158158
<div>Event / Cron</div>

landing/src/components/FunctionBlock.tsx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { useMemo } from "preact/hooks";
22
import { FunctionConfig } from "../types";
33
import { classNames } from "../utils/classnames";
44
import { configErrors } from "./ConfigErrors";
5+
import { Button } from "./Button";
6+
import useToast from "./Toast";
57

68
interface Props {
79
config: FunctionConfig;
@@ -12,6 +14,7 @@ interface Props {
1214
* Renders a single entry for a found function.
1315
*/
1416
export const FunctionBlock = ({ config, altBg }: Props) => {
17+
const { push } = useToast();
1518
/**
1619
* Figure out here what kind of function it is so that we can approriately
1720
* label it.
@@ -42,11 +45,39 @@ export const FunctionBlock = ({ config, altBg }: Props) => {
4245
[config.errors]
4346
);
4447

48+
/**
49+
* Trigger a cron function
50+
*/
51+
const triggerCron = async () => {
52+
const url = new URL(window.location.href);
53+
54+
// Default to the default step name for crons
55+
url.searchParams.set("fnId", config.id);
56+
url.searchParams.set("stepId", config.steps.step.id);
57+
58+
push({
59+
type: "default",
60+
message: "Scheduled function triggered",
61+
});
62+
63+
const res = await fetch(url, {
64+
method: "POST",
65+
});
66+
const json = await res.json();
67+
const status =
68+
typeof json === "object" && json.status ? json.status : res.status;
69+
70+
push({
71+
type: status === 200 ? "success" : "error",
72+
message: `Function complete: ${JSON.stringify(json)}`,
73+
});
74+
};
75+
4576
return (
4677
<>
4778
<div
4879
class={classNames({
49-
"w-full grid grid-cols-[1fr_1fr_1fr] p-2 items-center": true,
80+
"w-full grid grid-cols-[1fr_1fr_1fr_100px] p-2 items-center": true,
5081
"bg-slate-200/30": Boolean(altBg),
5182
"bg-red-400/30": hasErrors,
5283
})}
@@ -83,6 +114,13 @@ export const FunctionBlock = ({ config, altBg }: Props) => {
83114
<code class="bg-white">Invalid or no expression</code>
84115
)}
85116
</span>
117+
<div>
118+
{type === "cron" && (
119+
<Button className="text-sm" onClick={triggerCron}>
120+
Trigger
121+
</Button>
122+
)}
123+
</div>
86124
</div>
87125
{hasErrors ? (
88126
<div class="w-full p-2 bg-red-400/30">

0 commit comments

Comments
 (0)