From 8230e6549bf6bea4c92028d81ee9de5eb8c978d7 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sun, 24 Sep 2023 11:59:44 +0800 Subject: [PATCH] Implement adjustments to handlers Allow ways to send methods other than get/post on server-side. --- .../src/pages/a/notes/[noteId].ts | 12 ++ .../src/pages/notes/[noteId].tsx | 56 +++++- .../src/pages/notes/index.tsx | 6 +- .../src/styles/globals.css | 4 + packages/iceform-next/.gitignore | 1 + .../src/client/components/Form.tsx | 13 +- packages/iceform-next/src/common/constants.ts | 2 + packages/iceform-next/src/server/action.ts | 141 +++++++++++++ packages/iceform-next/src/server/constants.ts | 1 - .../iceform-next/src/server/destination.ts | 103 ++++++++++ packages/iceform-next/src/server/index.ts | 189 +----------------- .../iceform-next/src/utils/serialization.ts | 2 +- 12 files changed, 330 insertions(+), 200 deletions(-) create mode 100644 packages/iceform-next/src/common/constants.ts create mode 100644 packages/iceform-next/src/server/action.ts create mode 100644 packages/iceform-next/src/server/destination.ts diff --git a/packages/iceform-next-sandbox/src/pages/a/notes/[noteId].ts b/packages/iceform-next-sandbox/src/pages/a/notes/[noteId].ts index 9f7d994..4986fea 100644 --- a/packages/iceform-next-sandbox/src/pages/a/notes/[noteId].ts +++ b/packages/iceform-next-sandbox/src/pages/a/notes/[noteId].ts @@ -6,6 +6,18 @@ const ActionNotesResourcePage: NextPage = () => null; const getServerSideProps = Iceform.action.getServerSideProps({ fn: noteResource, + onAction: async (context) => { + if (context.req.method?.toLowerCase() === 'delete') { + return { + redirect: { + destination: '/notes', + permanent: false, + }, + }; + } + + // use default behavior + }, }); export { diff --git a/packages/iceform-next-sandbox/src/pages/notes/[noteId].tsx b/packages/iceform-next-sandbox/src/pages/notes/[noteId].tsx index 18efec1..7d94bcf 100644 --- a/packages/iceform-next-sandbox/src/pages/notes/[noteId].tsx +++ b/packages/iceform-next-sandbox/src/pages/notes/[noteId].tsx @@ -1,11 +1,21 @@ import * as Iceform from '@modal-sh/iceform-next'; import * as React from 'react'; -const NotesItemPage: Iceform.NextPage = ({ +export interface NotesItemPageProps { + note: { + id: string; + title: string; + content: string; + image: string; + } +} + +const NotesItemPage: Iceform.NextPage = ({ req, res, + note, }) => { - const body = (res.body ?? {}) as Record; + const body = (res.body ?? note ?? {}) as Record; const {response, loading, ...isoformProps} = Iceform.useResponse({ res }); @@ -26,22 +36,23 @@ const NotesItemPage: Iceform.NextPage = ({ method="post" action={`/a/notes/${req.query.noteId}`} clientAction={`/api/notes/${req.query.noteId}`} + clientMethod="put" >