@dryv/nextjs-appv0.1.0
Next.js App Router pages from views, typed API calls and schema-driven forms.
dryv pack add @dryv/nextjs-app{#- The generated symbol of another planned file, found by the subject it represents. -#}
{%- macro symbol_for(link) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == link.id -%}{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{#- The operation.client function bound for the operation, if the project binds one. -#}
{%- macro bound_client(operation) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == operation.id and dependency.slot == 'operation.client' -%}{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{#- This pack's own module for the operation: request builder, field metadata and fallback call. -#}
{%- macro own(operation) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == operation.id and dependency.template == 'operation' and dependency.slot is none -%}{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{#- The function that calls the operation: the bound client, else the fallback. -#}
{%- macro call(operation) -%}{{ bound_client(operation) or own(operation) }}{%- endmacro -%}
{#- Field metadata of an operation port, exported by the operation's module. -#}
{%- macro fields_for(port) -%}
{%- if port.direction == 'input' %}{{ own(port.operation) }}InputFields[{{ port.name.camel | tojson }}]{% else %}{{ own(port.operation) }}OutputFields{% endif -%}
{%- endmacro -%}
{#- "yes" when a collect part of this view feeds the operation: its result comes from the form. -#}
{%- macro collected(operation) -%}
{%- for part in view.parts -%}
{%- if part.kind == 'collect' and part.collect.target.port is not none and part.collect.target.port.operation.id == operation.id -%}yes{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{#- "yes" when an earlier present part already shows the operation, so each operation loads once. -#}
{%- macro shown_earlier(operation, position) -%}
{%- for part in view.parts -%}
{%- if loop.index < position and part.kind == 'present' and part.present.source.port is not none and part.present.source.port.operation.id == operation.id -%}yes{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
"use client";
import { useEffect, useState } from "react";
import { DryvForm } from "../../../components/dryv/dryv-form.tsx";
import { DryvResult } from "../../../components/dryv/dryv-result.tsx";
import type { ClientResponse } from "../../../lib/dryv/transport.ts";
{%- for dependency in file.dependencies %}{% if dependency.symbol %}
{%- if dependency.slot == 'operation.client' %}
import { {{ dependency.symbol }} } from "./{{ dependency.path.relative }}";
{%- elif dependency.template == 'operation' %}
import { {% if bound_client(dependency.target) == '' %}{{ dependency.symbol }}, {% endif %}{{ dependency.symbol }}InputFields, {{ dependency.symbol }}OutputFields, {{ dependency.symbol }}Request } from "./{{ dependency.path.relative }}";
{%- endif %}
{%- endif %}{% endfor %}
/**
* {{ view.description or view.name.title }}
*/
export default function {{ file.symbol }}() {
const [results, setResults] = useState<Record<string, ClientResponse<unknown>>>({});
const keep = (operation: string) => (response: ClientResponse<unknown>) => {
setResults((current) => ({ ...current, [operation]: response }));
return response;
};
useEffect(() => {
{%- for part in view.parts %}
{%- if part.kind == 'present' and part.present.source.port is not none %}
{%- set operation = part.present.source.port.operation %}
{%- if collected(operation) == '' and shown_earlier(operation, loop.index) == '' %}
void {{ call(operation) }}({{ own(operation) }}Request() as never).then(keep("{{ own(operation) }}"));
{%- endif %}
{%- endif %}
{%- endfor %}
}, []);
return (
<main className="dryv-page">
<h1>{{ view.name.title }}</h1>
{%- for part in view.parts %}
<section className="dryv-part" data-part="{{ part.name.kebab }}">
<h2>{{ part.name.title }}</h2>
{%- if part.kind == 'collect' and part.collect.target.port is not none %}
{%- set port = part.collect.target.port %}
<DryvForm
fields={ {{- fields_for(port) }} ?? []}
submitLabel={{ port.operation.name.title | tojson }}
onSubmit={async (values) => {
const response = keep("{{ own(port.operation) }}")(
await {{ call(port.operation) }}({{ own(port.operation) }}Request({ {{ port.name.camel }}: values as never }) as never),
);
if (!response.success) throw new Error(response.message);
}}
/>
{%- elif part.kind == 'present' and part.present.source.port is not none %}
{%- set port = part.present.source.port %}
<DryvResult
response={results["{{ own(port.operation) }}"]}
fields={ {{- fields_for(port) -}} }
{%- if part.present.source.select %}
select={[{% for path in part.present.source.select %}"{{ path.dotted }}"{% if not loop.last %}, {% endif %}{% endfor %}]}
{%- endif %}
/>
{%- elif part.kind == 'invoke' and part.invoke.operation is not none %}
<button type="button" onClick={() => void {{ call(part.invoke.operation) }}({{ own(part.invoke.operation) }}Request() as never).then(keep("{{ own(part.invoke.operation) }}"))}>
{{ part.invoke.operation.name.title }}
</button>
{%- endif %}
</section>
{%- endfor %}
</main>
);
}