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

feat(router): introduce server support for form actions #1346

Open
wants to merge 8 commits into
base: beta
Choose a base branch
from

Conversation

brandonroberts
Copy link
Member

@brandonroberts brandonroberts commented Sep 12, 2024

PR Checklist

Closes #935

What is the new behavior?

In .server.ts files, an action function can be defined to be used with forms to progressively enhance forms and build the foundation for handling form actions on the server.

// newsletter.server.ts
import {
  type PageServerAction,
  redirect,
  json,
  fail,
} from '@analogjs/router/server/actions';
import { readFormData } from 'h3';

export function load() {
  return {
    loaded: true,
  };
}

export async function action({ event }: PageServerAction) {
  const body = await readFormData(event);
  const email = body.get('email') as string;

  if (!email) {
    return fail(422, { email: 'Email is required' });
  }

  if (email.length < 10) {
    return redirect('/');
  }

  console.log({ email: body.get('email') });

  return json({ type: 'success' });
}

A follow-up PR will land to add a form directive to bridge the form to the form server action.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

[optional] What gif best describes this PR or how it makes you feel?

Copy link

netlify bot commented Sep 12, 2024

Deploy Preview for analog-blog ready!

Name Link
🔨 Latest commit 8ecf449
🔍 Latest deploy log https://app.netlify.com/sites/analog-blog/deploys/66e44ad7a21f5500088a2f18
😎 Deploy Preview https://deploy-preview-1346--analog-blog.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Sep 12, 2024

Deploy Preview for analog-docs ready!

Name Link
🔨 Latest commit 8ecf449
🔍 Latest deploy log https://app.netlify.com/sites/analog-docs/deploys/66e44ad7d74296000801877d
😎 Deploy Preview https://deploy-preview-1346--analog-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Sep 12, 2024

Deploy Preview for analog-app ready!

Name Link
🔨 Latest commit 8ecf449
🔍 Latest deploy log https://app.netlify.com/sites/analog-app/deploys/66e44ad72c0ff000088a53d1
😎 Deploy Preview https://deploy-preview-1346--analog-app.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Sep 12, 2024

Deploy Preview for analog-ng-app ready!

Name Link
🔨 Latest commit 8ecf449
🔍 Latest deploy log https://app.netlify.com/sites/analog-ng-app/deploys/66e44ad8812b9700085d1f39
😎 Deploy Preview https://deploy-preview-1346--analog-ng-app.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.


submitted($event: { target: HTMLFormElement } & Event) {
$event.preventDefault();
const body = new FormData($event.target);

This comment was marked as resolved.

fetch(this.action() || `/api/_analog/pages${window.location.pathname}`, {
method: $event.target.method,
body,
}).then((res) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I'm not against fetch, but would it be possible to support/use HttpClient? Does the internal implementation even matter? I genuinely don't know.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its possible to use HttpClient, but I don't think it matters. fetch is more flexible out of the box in terms of determining what to do with the response

const email = body.get('email') as string;

if (!email) {
return fail(422, { errors: { email: 'Email is required' } });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: If JavaScript is disabled, what happens in this case? Can we set up something like [...not-found].page.ts but like [...422].page.ts to handle this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still TBD. Ideally, if JS is off, you come back to this page with errors displayed as part of the rendered HTML

@jdgamble555
Copy link

whahoo!

};
}

export async function action({ event }: PageServerAction) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is copy-pasta so not needed? If this is for demo purposes, can we change the body to something related to the search page instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yea, copy-pasta. Will remove

Copy link
Member

@nartc nartc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with one small nit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Server Form Actions
4 participants