Skip to content

Commit

Permalink
🧑‍💻 - chore: introduce useSubmitAction() providing easier interaction…
Browse files Browse the repository at this point in the history
… with actions
  • Loading branch information
svenvandescheur committed Jul 9, 2024
1 parent 1621187 commit 3902d0f
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 24 deletions.
1 change: 1 addition & 0 deletions frontend/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./useSubmitAction";
69 changes: 69 additions & 0 deletions frontend/src/hooks/useSubmitAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { SubmitOptions, useSubmit } from "react-router-dom";

// From React Router
type JsonObject = {
[Key in string]: JsonValue;
} & {
[Key in string]?: JsonValue | undefined;
};
type JsonArray = JsonValue[] | readonly JsonValue[];
type JsonPrimitive = string | number | boolean | null;
type JsonValue = JsonPrimitive | JsonObject | JsonArray;

/**
* Can be used when using (React Router) actions to explicitly set the type
* of action, and it's payload. This is similar to a Redux action.
*
* @example
*
* const increment: TypedAction = {
* type: "INCREMENT_VALUE",
* payload: 1
* }
*
* NOTE: Using complex objects in (React Router) `submit()` calls require a method
* to be set and an encType of "application/json" to be used.
*
* @example
* const submit = useSubmit()
*
* submit(increment, {
* method: "POST", // NOTE: Not needed when using `submitAction()`.
* encType: "application/json", // NOTE: Not needed when using `submitAction()`.
* })
*/
export type TypedAction<T = string, P = JsonValue> = {
type: T;
payload: P;
};

/**
* A small wrapper around React Routers' `useSubmit()` that takes a `TypedAction`
* as data. The `SubmitOptions` default to method of "POST" and an encType of "application/json".
*
* @example
* const submitAction = useSubmitAction();
*
* const increment: TypedAction = {
* type: "INCREMENT_VALUE",
* payload: 1
* }
*
* submitAction(increment);
*
* NOTE: In the (React Router) action handler `request.clone().json()` should be
* used to retrieve the `TypedAction`. If multiple actions should be handled the
* type of the action can be used to determine the applicable logic.
*/
export function useSubmitAction() {
const submit = useSubmit();

return (typedAction: TypedAction, options: SubmitOptions = {}) => {
const targetOptions: SubmitOptions = {
method: "POST",
encType: "application/json",
};
Object.assign(targetOptions, options);
return submit(typedAction, targetOptions);
};
}
8 changes: 5 additions & 3 deletions frontend/src/pages/destructionlist/detail/Assignees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
SerializedFormData,
} from "@maykin-ui/admin-ui";
import React, { FormEvent, useState } from "react";
import { useActionData, useSubmit } from "react-router-dom";
import { useActionData } from "react-router-dom";

import { useSubmitAction } from "../../../hooks";
import { DestructionListAssignee } from "../../../lib/api/destructionLists";
import { formatUser } from "../utils";
import { UpdateDestructionListAction } from "./DestructionListDetail";
Expand All @@ -18,7 +19,8 @@ export function AssigneesEditable({
reviewers,
}: AssigneesEditableProps) {
const errors = useActionData() || {};
const submit = useSubmit();
const submitAction = useSubmitAction();

const reviewerAssignees = [...assignees].splice(1);
const [confirmationModalState, setConfirmationModalState] = useState<{
open: boolean;
Expand Down Expand Up @@ -84,7 +86,7 @@ export function AssigneesEditable({
>;
Object.assign(action.payload, { comment: String(comment) });

submit(action, { method: "PATCH", encType: "application/json" });
submitAction(action);
setConfirmationModalState({ ...confirmationModalState, open: false });
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import {
import { ActionFunctionArgs } from "@remix-run/router/utils";
import { redirect, useLoaderData } from "react-router-dom";

import { TypedAction } from "../../../hooks/useSubmitAction";
import { User } from "../../../lib/api/auth";
import {
DestructionList,
DestructionListItemUpdate,
DestructionListUpdateData,
getDestructionList,
updateDestructionList,
} from "../../../lib/api/destructionLists";
Expand Down Expand Up @@ -111,7 +111,7 @@ export function DestructionListDetailPage() {
);
}

export type UpdateDestructionListAction<T> = StateMutationAction<
export type UpdateDestructionListAction<T> = TypedAction<
"PROCESS_REVIEW" | "UPDATE_ASSIGNEES" | "UPDATE_ZAKEN",
T
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import {
useNavigation,
useRevalidator,
useSearchParams,
useSubmit,
} from "react-router-dom";
import { useAsync } from "react-use";

import { useSubmitAction } from "../../../hooks/useSubmitAction";
import { ReviewItem } from "../../../lib/api/review";
import {
ReviewItemResponse,
Expand Down Expand Up @@ -73,7 +73,8 @@ interface ProcessZaakReviewSelectionDetail {
export function DestructionListItems() {
const { state } = useNavigation();
const [urlSearchParams, setUrlSearchParams] = useSearchParams();
const submit = useSubmit();
const submitAction = useSubmitAction();

const {
storageKey,
destructionList,
Expand Down Expand Up @@ -134,7 +135,7 @@ export function DestructionListItems() {
zaakUrls,
},
};
submit(action, { method: "PATCH", encType: "application/json" });
submitAction(action);
};

// Selection actions allowing the user to add/remove zaken to/from the destruction list or escape such flow.
Expand Down Expand Up @@ -274,7 +275,7 @@ export function DestructionListItems() {
},
};

submit(actionData, { method: "POST", encType: "application/json" });
submitAction(actionData);
};

// Whether the user is processing a review.
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions frontend/src/types/index.d.ts

This file was deleted.

13 changes: 0 additions & 13 deletions frontend/src/types/state.d.ts

This file was deleted.

0 comments on commit 3902d0f

Please sign in to comment.