-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔄 Merge pull request #6 from steeeee0223/feature/ui
Add hooks & fix linting message
- Loading branch information
Showing
13 changed files
with
84 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
packages/ui/src/components/custom/kanban/kanban-container.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
packages/ui/src/components/custom/kanban/kanban-list-header.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/ui/src/components/dnd/single-image-dropzone/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./use-action"; | ||
export * from "./use-fetch"; | ||
export * from "./use-scroll-top"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"use client"; | ||
|
||
import { useCallback, useState } from "react"; | ||
|
||
import { ActionHandler, FieldErrors } from "@/lib"; | ||
|
||
interface UseActionOptions<TOutput> { | ||
onSuccess?: (data: TOutput) => void; | ||
onError?: (error: string) => void; | ||
onComplete?: () => void; | ||
} | ||
|
||
interface UseActionResult<TInput, TOutput> { | ||
execute: (data: TInput) => Promise<void>; | ||
fieldErrors?: FieldErrors<TInput>; | ||
error?: string; | ||
data?: TOutput; | ||
isLoading: boolean; | ||
} | ||
|
||
export const useAction = <TInput, TOutput>( | ||
action: ActionHandler<TInput, TOutput>, | ||
options: UseActionOptions<TOutput>, | ||
): UseActionResult<TInput, 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; | ||
|
||
const { fieldErrors, error, data } = result; | ||
setFieldErrors(fieldErrors); | ||
if (error) { | ||
setError(error); | ||
options.onError?.(error); | ||
} | ||
if (data) { | ||
setData(data); | ||
options.onSuccess?.(data); | ||
} | ||
} finally { | ||
setIsLoading(false); | ||
options.onComplete?.(); | ||
} | ||
}, | ||
[action, options], | ||
); | ||
|
||
return { execute, fieldErrors, error, data, isLoading }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { z } from "zod"; | ||
|
||
import type { ActionHandler, FieldErrors } from "./types"; | ||
|
||
export const createSafeAction = | ||
<TInput, TOutput>( | ||
schema: z.Schema<TInput>, | ||
handler: ActionHandler<TInput, TOutput>, | ||
): ActionHandler<TInput, TOutput> => | ||
async (data) => { | ||
const result = schema.safeParse(data); | ||
return result.success | ||
? await handler(result.data) | ||
: { | ||
fieldErrors: result.error.flatten() | ||
.fieldErrors as FieldErrors<TInput>, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./utils"; | ||
export * from "./action"; | ||
export * from "./types"; | ||
export * from "./utils"; |