Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create inline action #585

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions blocks/section.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// deno-lint-ignore-file no-explicit-any
import { PartialProps } from "$fresh/src/runtime/Partial.tsx";
import { ComponentType } from "preact";
import { HttpContext } from "../blocks/handler.ts";
import { PropsLoader, propsLoader } from "../blocks/propsLoader.ts";
import { fnContextFromHttpContext, RequestState } from "../blocks/utils.tsx";
import { RequestState, fnContextFromHttpContext } from "../blocks/utils.tsx";
import StubSection, { Empty } from "../components/StubSection.tsx";
import { withSection } from "../components/section.tsx";
import { Context } from "../deco.ts";
Expand All @@ -16,7 +17,6 @@ import {
} from "../engine/block.ts";
import { Resolver } from "../engine/core/resolver.ts";
import { AppManifest, FunctionContext } from "../types.ts";
import { PartialProps } from "$fresh/src/runtime/Partial.tsx";

/**
* @widget none
Expand Down Expand Up @@ -44,9 +44,13 @@ export const isSection = <
return (s as Section)?.metadata?.component === section;
};

export type SectionProps<T> = T extends PropsLoader<any, infer Props> ? Props
type ReturnProps<TFunc> = TFunc extends PropsLoader<any, infer Props> ? Props
: unknown;

export type SectionProps<LoaderFunc, ActionFunc = LoaderFunc> =
| ReturnProps<LoaderFunc>
| ReturnProps<ActionFunc>;

export interface ErrorBoundaryParams<TProps> {
error: any;
props: TProps;
Expand All @@ -55,15 +59,21 @@ export interface ErrorBoundaryParams<TProps> {
export type ErrorBoundaryComponent<TProps> = ComponentFunc<
ErrorBoundaryParams<TProps>
>;
export interface SectionModule<TConfig = any, TProps = any> extends
export interface SectionModule<
TConfig = any,
TProps = any,
TLoaderProps = TProps,
TActionProps = TLoaderProps,
> extends
BlockModule<
ComponentFunc<TProps>,
ReturnType<ComponentFunc<TProps>>,
ComponentFunc<TLoaderProps | TActionProps>,
ReturnType<ComponentFunc<TLoaderProps | TActionProps>>,
PreactComponent
> {
LoadingFallback?: ComponentType;
ErrorFallback?: ComponentType<{ error?: Error }>;
loader?: PropsLoader<TConfig, TProps>;
loader?: PropsLoader<TConfig, TLoaderProps>;
action?: PropsLoader<TConfig, TActionProps>;
partialMode?: PartialProps["mode"];
}

Expand Down Expand Up @@ -101,7 +111,10 @@ export const createSectionBlock = (
type: "sections" | "pages",
): Block<SectionModule> => ({
type,
introspect: { funcNames: ["loader", "default"], includeReturn: true },
introspect: {
funcNames: ["loader", "action", "default"],
includeReturn: true,
},
adapt: <TConfig = any, TProps = any>(
mod: SectionModule<TConfig, TProps>,
resolver: string,
Expand All @@ -123,8 +136,8 @@ export const createSectionBlock = (
mod.ErrorFallback,
mod.partialMode,
);
const loader = mod.loader;
if (!loader) {

if (!mod.action && !mod.loader) {
return (
props: TProps,
ctx: HttpContext<RequestState>,
Expand All @@ -142,17 +155,26 @@ export const createSectionBlock = (
resolve,
} = httpCtx;

const loaderSectionProps = request.method === "POST"
? mod.action ?? mod.loader
: mod.loader;

if (!loaderSectionProps) {
return componentFunc(props as unknown as TProps, httpCtx);
}

const ctx = {
...context,
state: { ...context.state, $live: props, resolve },
} as FunctionContext;

const fnContext = fnContextFromHttpContext(httpCtx);
const p = await wrapCaughtErrors(() =>
propsLoader(
loader,
loaderSectionProps,
ctx.state.$live,
request,
fnContextFromHttpContext(httpCtx),
fnContext,
), props ?? {});

return componentFunc(p, httpCtx);
Expand Down
8 changes: 8 additions & 0 deletions hooks/useAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { usePartialSection } from "./usePartialSection.ts";

/**
* @returns the action url of the current section.
*/
export const useAction = () => {
return usePartialSection()["f-partial"];
};
Loading