dryvPacks

@dryv/react-native-appv0.1.0

An Expo app from views — feature screens, expo-router routes, TanStack Query hooks, a connectivity gate, calls through ts-api-client.

React Native AppFrontendUnitPublished 7 days ago2 downloads

dryv pack add @dryv/react-native-app
3.8 KBRaw ↗
jinja
{%- macro client_for(link) -%}
{%- for dependency in file.dependency_groups.operation_client %}{% if dependency.target.id == link.id %}{{ dependency.symbol }}{% endif %}{% endfor -%}
{%- endmacro -%}
{%- macro fed_by_form(link) -%}
{%- for part in view.parts %}{% if part.kind == 'collect' and part.collect.target.port is not none and part.collect.target.port.operation.id == link.id %}yes{% endif %}{% endfor -%}
{%- endmacro -%}
{%- macro form_part(link) -%}
{%- for part in view.parts %}{% if part.kind == 'collect' and part.collect.target.port is not none and part.collect.target.port.operation.id == link.id %}{{ part.name.camel }}{% endif %}{% endfor -%}
{%- endmacro -%}
{%- macro field_kind(value) -%}
{%- if value is none -%}text
{%- elif value.is.boolean -%}boolean
{%- elif value.is.number -%}number
{%- elif value.is.enum -%}choice
{%- elif value.is.date or value.is.date_time -%}date
{%- else -%}text
{%- endif -%}
{%- endmacro -%}
import { useMutation, useQuery } from "@tanstack/react-query";
import * as api from "{{ application.name.kebab }}-api";

import type { DryvField } from "../../widgets/dryv-form";

// State and calls of the {{ view.name.title }} screen. Generated by Dryv; regenerated when the view changes.
{%- for part in view.parts %}
{%- if part.kind == 'collect' and part.collect.target.port is not none %}
{%- set port = part.collect.target.port %}

/** Fields of the {{ part.name.title }} form. */
export const {{ view.name.camel }}{{ part.name.pascal }}Fields: readonly DryvField[] = [
{%- if port.value.schema is not none %}
{%- for field in port.value.schema.fields %}
  {
    name: "{{ field.name.raw }}",
    label: {{ field.name.title | tojson }},
    kind: "{{ field_kind(field.value) }}",
    required: {{ 'false' if field.optional else 'true' }},
{%- if field.value is not none and field.value.is.enum %}
    options: [{% for item in field.value.property.enum.values %}{{ item.value_json }}{% if not loop.last %}, {% endif %}{% endfor %}],
{%- endif %}
  },
{%- endfor %}
{%- endif %}
];
{%- endif %}
{%- endfor %}

/** Queries and mutations of every part of the {{ view.name.title }} view. */
export function use{{ view.name.pascal }}() {
{%- for part in view.parts %}
{%- if part.kind == 'collect' and part.collect.target.port is not none %}
{%- set port = part.collect.target.port %}
  const {{ part.name.camel }} = useMutation({
    mutationFn: (values: Record<string, unknown>) =>
      api.{{ client_for(port.operation) }}({% if port.field is none %}values as unknown as api.{{ port.value.generated.schema_types.symbol }}{% else %}{ {{ port.field.camel }}: values as unknown as api.{{ port.value.generated.schema_types.symbol }} }{% endif %}),
  });
{%- elif part.kind == 'invoke' and part.invoke.operation is not none %}
  const {{ part.name.camel }} = useMutation({ mutationFn: () => api.{{ client_for(part.invoke.operation) }}() });
{%- endif %}
{%- endfor %}
{%- for part in view.parts %}
{%- if part.kind == 'present' and part.present.source.port is not none and not fed_by_form(part.present.source.port.operation) %}
  const {{ part.name.camel }} = useQuery({
    queryKey: ["{{ view.name.camel }}", "{{ part.name.camel }}"],
    queryFn: () => api.{{ client_for(part.present.source.port.operation) }}(),
  });
{%- endif %}
{%- endfor %}
  return {
{%- for part in view.parts %}
{%- if part.kind == 'present' and part.present.source.port is not none and fed_by_form(part.present.source.port.operation) %}
    /** The latest submission's result. */
    {{ part.name.camel }}: {{ form_part(part.present.source.port.operation) }}.data,
{%- elif part.kind == 'present' and part.present.source.port is not none %}
    {{ part.name.camel }}: {{ part.name.camel }}.data,
    {{ part.name.camel }}Loading: {{ part.name.camel }}.isLoading,
{%- elif part.kind == 'collect' or part.kind == 'invoke' %}
    {{ part.name.camel }},
{%- endif %}
{%- endfor %}
  };
}