Skip to content

Commit

Permalink
Merge branch 'main' into 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Feb 8, 2024
2 parents 94843fd + 547a312 commit 35e9afe
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/example-app/src/app/hook/deleteuser-form.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import { useAction } from "next-safe-action/hooks";
import { isExecuting } from "next-safe-action/status";
import { deleteUser } from "./deleteuser-action";

type Props = {
Expand Down Expand Up @@ -57,7 +58,7 @@ const DeleteUserForm = ({ userId }: Props) => {
</form>
<div id="result-container">
<pre>Deleted user ID: {userId}</pre>
<pre>Is executing: {JSON.stringify(status === "executing")}</pre>
<pre>Is executing: {JSON.stringify(isExecuting(status))}</pre>
<div>Action result:</div>
<pre className="result">
{
Expand Down
6 changes: 5 additions & 1 deletion packages/next-safe-action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
],
"exports": {
".": "./dist/index.mjs",
"./hooks": "./dist/hooks.mjs"
"./hooks": "./dist/hooks.mjs",
"./status": "./dist/status.mjs"
},
"typesVersions": {
"*": {
Expand All @@ -20,6 +21,9 @@
],
"hooks": [
"./dist/hooks.d.mts"
],
"status": [
"./dist/status.d.mts"
]
}
},
Expand Down
37 changes: 37 additions & 0 deletions packages/next-safe-action/src/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { HookActionStatus } from "./hooks";

/**
* Returns true if the action is idle.
* @param status
* @returns
*/
export function isIdle(status: HookActionStatus): status is "idle" {
return status === "idle";
}

/**
* Returns true if the action is executing.
* @param status
* @returns
*/
export function isExecuting(status: HookActionStatus): status is "executing" {
return status === "executing";
}

/**
* Returns true if the action has succeeded.
* @param status
* @returns
*/
export function hasSucceeded(status: HookActionStatus): status is "hasSucceeded" {
return status === "hasSucceeded";
}

/**
* Returns true if the action has errored.
* @param status
* @returns
*/
export function hasErrored(status: HookActionStatus): status is "hasErrored" {
return status === "hasErrored";
}
2 changes: 1 addition & 1 deletion packages/next-safe-action/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts", "src/hooks.ts"],
entry: ["src/index.ts", "src/hooks.ts", "src/status.ts"],
format: ["esm"],
clean: true,
splitting: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
sidebar_position: 4
description: Learn how to check the current action status.
---

# Checking action status

There are two ways to get the current action status:

1. Directly via the `status` property returned by `useAction` and `useOptimisticAction` hooks.
2. Using utility functions imported from `next-safe-action/status`.

## Utility functions

You can import `isIdle`, `isExecuting`, `hasSucceeded`, and `hasErrored` from `next-safe-action/status`. This emulates the pre v4 implementation, and it's similar to the [Tanstack Query](https://tanstack.com/query/latest) API.

### Why using single `status` property is better?

The reason for the v4 change, other than debloating the hook result object, is that using a single `status` string property is discriminating, i.e. **only one** status can be active at a specific time. Instead, returning multiple functions to check status as before v4, doesn't guarantee the same behavior.

If you use the utility functions imported from `next-safe-action/status` you get the same discriminated status string, thanks to TypeScript's [type predicates](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates) feature.

0 comments on commit 35e9afe

Please sign in to comment.