mirror of
https://github.com/ahmadk953/tasko.git
synced 2025-05-04 04:33:10 +00:00
Initial Commit
This commit is contained in:
commit
f3e2f01bd7
150 changed files with 13612 additions and 0 deletions
58
hooks/use-action.ts
Normal file
58
hooks/use-action.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
import { useState, useCallback } from "react";
|
||||
|
||||
import { ActionState, FieldErrors } 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;
|
||||
}
|
||||
|
||||
export const useAction = <TInput, TOutput>(
|
||||
action: Action<TInput, TOutput>,
|
||||
options: UseActionOptions<TOutput> = {}
|
||||
) => {
|
||||
const [fieldErrors, setFieldErrors] = useState<
|
||||
FieldErrors<TInput> | undefined
|
||||
>(undefined);
|
||||
const [error, setError] = useState<string | undefined>(undefined);
|
||||
const [data, setData] = useState<TOutput | undefined>(undefined);
|
||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||
|
||||
const execute = useCallback(
|
||||
async (input: TInput) => {
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
const result = await action(input);
|
||||
|
||||
if (!result) return;
|
||||
setFieldErrors(result.fieldErrors);
|
||||
if (result.error) {
|
||||
setError(result.error);
|
||||
options.onError?.(result.error);
|
||||
}
|
||||
if (result.data) {
|
||||
setData(result.data);
|
||||
options.onSuccess?.(result.data);
|
||||
}
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
options.onComplete?.();
|
||||
}
|
||||
},
|
||||
[action, options]
|
||||
);
|
||||
|
||||
return {
|
||||
execute,
|
||||
fieldErrors,
|
||||
error,
|
||||
data,
|
||||
isLoading,
|
||||
};
|
||||
};
|
15
hooks/use-card-modal.ts
Normal file
15
hooks/use-card-modal.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { create } from "zustand";
|
||||
|
||||
type CardModalStore = {
|
||||
id?: string;
|
||||
isOpen: boolean;
|
||||
onOpen: (id: string) => void;
|
||||
onClose: () => void;
|
||||
};
|
||||
|
||||
export const useCardModal = create<CardModalStore>((set) => ({
|
||||
id: undefined,
|
||||
isOpen: false,
|
||||
onOpen: (id: string) => set({ isOpen: true, id }),
|
||||
onClose: () => set({ isOpen: false, id: undefined }),
|
||||
}));
|
13
hooks/use-mobile-sidebar.ts
Normal file
13
hooks/use-mobile-sidebar.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { create } from "zustand";
|
||||
|
||||
type MobileSidebarStore = {
|
||||
isOpen: boolean;
|
||||
onOpen: () => void;
|
||||
onClose: () => void;
|
||||
};
|
||||
|
||||
export const useMobileSidebar = create<MobileSidebarStore>((set) => ({
|
||||
isOpen: false,
|
||||
onOpen: () => set({ isOpen: true }),
|
||||
onClose: () => set({ isOpen: false }),
|
||||
}));
|
13
hooks/use-pro-modal.ts
Normal file
13
hooks/use-pro-modal.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { create } from "zustand";
|
||||
|
||||
type ProModalStore = {
|
||||
isOpen: boolean;
|
||||
onOpen: () => void;
|
||||
onClose: () => void;
|
||||
};
|
||||
|
||||
export const useProModal = create<ProModalStore>((set) => ({
|
||||
isOpen: false,
|
||||
onOpen: () => set({ isOpen: true }),
|
||||
onClose: () => set({ isOpen: false }),
|
||||
}));
|
Loading…
Add table
Add a link
Reference in a new issue