Skip to content

Instantly share code, notes, and snippets.

View shaanardev's full-sized avatar

Shaan Arora shaanardev

  • Australia
  • 04:33 (UTC +11:00)
View GitHub Profile
@shaanardev
shaanardev / useAction.tsx
Created January 26, 2024 01:04
Type Safe Server Action Hook
import { useState, useCallback } from "react";
import { ActionState, FormFieldErrors } from "@/lib/create-safe-action";
type Action<TInput, TOutput> = (data: TInput) => Promise<ActionState<TInput, TOutput>>;
interface UseActionOptions<TOutput> {
onSuccess?: (data: TOutput) => void;
onError?: (error: string) => void;
onComplete?: () => void;
@shaanardev
shaanardev / createSafeAction.ts
Created January 26, 2024 01:06
Server Action Wrapper with Validation
import { z } from "zod";
export type FormFieldErrors<T> = {
[Key in keyof T]?: string[];
}
export type ActionState<TInput, TOutput> = {
fieldErrors?: FormFieldErrors<TInput>;
error?: string | null;
data?: TOutput;