-
Notifications
You must be signed in to change notification settings - Fork 1
useupdatemutation usedeletemutation should include an id #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,12 +12,14 @@ import { usePocketBase } from './usePocketBase'; | |||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @example | ||||||||||||||||||||||||||||||
| * ```tsx | ||||||||||||||||||||||||||||||
| * const { mutate, isPending, isSuccess, error } = useCreateMutation<Post>('posts'); | ||||||||||||||||||||||||||||||
| * const { mutateAsync, isPending, isSuccess, isError, error } = useCreateMutation<Post>('posts'); | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * const handleCreate = async () => { | ||||||||||||||||||||||||||||||
| * const newPost = await mutate({ title: 'Hello', content: 'World' }); | ||||||||||||||||||||||||||||||
| * if (newPost) { | ||||||||||||||||||||||||||||||
| * try { | ||||||||||||||||||||||||||||||
| * const newPost = await mutateAsync({ title: 'Hello', content: 'World' }); | ||||||||||||||||||||||||||||||
| * console.log('Created:', newPost); | ||||||||||||||||||||||||||||||
| * } catch (err) { | ||||||||||||||||||||||||||||||
| * console.error('Failed to create post:', err); | ||||||||||||||||||||||||||||||
| * } | ||||||||||||||||||||||||||||||
| * }; | ||||||||||||||||||||||||||||||
| * ``` | ||||||||||||||||||||||||||||||
|
|
@@ -28,30 +30,42 @@ export function useCreateMutation<Record extends RecordModel>(collectionName: st | |||||||||||||||||||||||||||||
| const [isPending, setIsPending] = useState(false); | ||||||||||||||||||||||||||||||
| const [error, setError] = useState<string | null>(null); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const mutate = useCallback( | ||||||||||||||||||||||||||||||
| async (bodyParams: Partial<Record>, options?: RecordOptions): Promise<Record | null> => { | ||||||||||||||||||||||||||||||
| const mutateAsync = useCallback( | ||||||||||||||||||||||||||||||
| async (bodyParams: Partial<Record>, options?: RecordOptions): Promise<Record> => { | ||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||
| setIsPending(true); | ||||||||||||||||||||||||||||||
| setError(null); | ||||||||||||||||||||||||||||||
| const record = await recordService.create(bodyParams, options); | ||||||||||||||||||||||||||||||
| const record = options ? await recordService.create(bodyParams, options) : await recordService.create(bodyParams); | ||||||||||||||||||||||||||||||
| return record as Record; | ||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||
| setError(err instanceof Error ? err.message : 'Error creating record'); | ||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||
| const errorMessage = err instanceof Error ? err.message : 'Error creating record'; | ||||||||||||||||||||||||||||||
| setError(errorMessage); | ||||||||||||||||||||||||||||||
| throw new Error(errorMessage); | ||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||
| setIsPending(false); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| [recordService], | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const mutate = useCallback( | ||||||||||||||||||||||||||||||
| (bodyParams: Partial<Record>, options?: RecordOptions): void => { | ||||||||||||||||||||||||||||||
| mutateAsync(bodyParams, options).catch(() => { | ||||||||||||||||||||||||||||||
| // Error is already handled in mutateAsync | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| [mutateAsync], | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
|
Comment on lines
+51
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Remove inline comment per coding guidelines. The inline comment violates the "no code comments" guideline. Keep the silent catch but remove the comment. const mutate = useCallback(
(bodyParams: Partial<Record>, options?: RecordOptions): void => {
- mutateAsync(bodyParams, options).catch(() => {
- // Error is already handled in mutateAsync
- });
+ mutateAsync(bodyParams, options).catch(() => {});
},
[mutateAsync],
);As per coding guidelines. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return useMemo( | ||||||||||||||||||||||||||||||
| (): UseCreateMutationResult<Record> => ({ | ||||||||||||||||||||||||||||||
| mutate, | ||||||||||||||||||||||||||||||
| mutateAsync, | ||||||||||||||||||||||||||||||
| isPending, | ||||||||||||||||||||||||||||||
| isError: !!error, | ||||||||||||||||||||||||||||||
| error, | ||||||||||||||||||||||||||||||
| isSuccess: !isPending && !error, | ||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||
| [mutate, isPending, error], | ||||||||||||||||||||||||||||||
| [mutate, mutateAsync, isPending, error], | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { usePocketBase } from './usePocketBase'; | |||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @template Record - The record type extending RecordModel | ||||||||||||||||||||||||||||||
| * @param collectionName - The name of the PocketBase collection | ||||||||||||||||||||||||||||||
| * @param id - The ID of the record to update | ||||||||||||||||||||||||||||||
| * @returns An object containing the mutate function and mutation state | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @example | ||||||||||||||||||||||||||||||
|
|
@@ -22,36 +23,52 @@ import { usePocketBase } from './usePocketBase'; | |||||||||||||||||||||||||||||
| * }; | ||||||||||||||||||||||||||||||
| * ``` | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| export function useUpdateMutation<Record extends RecordModel>(collectionName: string): UseUpdateMutationResult<Record> { | ||||||||||||||||||||||||||||||
| export function useUpdateMutation<Record extends RecordModel>(collectionName: string, id: Record['id'] | null): UseUpdateMutationResult<Record> { | ||||||||||||||||||||||||||||||
| const pb = usePocketBase(); | ||||||||||||||||||||||||||||||
| const recordService = useMemo(() => pb.collection(collectionName), [pb, collectionName]); | ||||||||||||||||||||||||||||||
| const [isPending, setIsPending] = useState(false); | ||||||||||||||||||||||||||||||
| const [error, setError] = useState<string | null>(null); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const mutate = useCallback( | ||||||||||||||||||||||||||||||
| async (id: string, bodyParams: Partial<Record>, options?: RecordOptions): Promise<Record | null> => { | ||||||||||||||||||||||||||||||
| const mutateAsync = useCallback( | ||||||||||||||||||||||||||||||
| async (bodyParams: Partial<Record>, options?: RecordOptions): Promise<Record> => { | ||||||||||||||||||||||||||||||
| if (!id) { | ||||||||||||||||||||||||||||||
| throw new Error('ID is required'); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||
| setIsPending(true); | ||||||||||||||||||||||||||||||
| setError(null); | ||||||||||||||||||||||||||||||
| const record = options ? await recordService.update(id, bodyParams, options) : await recordService.update(id, bodyParams); | ||||||||||||||||||||||||||||||
| return record as Record; | ||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||
| setError(err instanceof Error ? err.message : 'Error updating record'); | ||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||
| const errorMessage = err instanceof Error ? err.message : 'Error updating record'; | ||||||||||||||||||||||||||||||
| setError(errorMessage); | ||||||||||||||||||||||||||||||
| throw new Error(errorMessage); | ||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||
| setIsPending(false); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| [recordService], | ||||||||||||||||||||||||||||||
| [recordService, id], | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const mutate = useCallback( | ||||||||||||||||||||||||||||||
| (bodyParams: Partial<Record>, options?: RecordOptions): void => { | ||||||||||||||||||||||||||||||
| mutateAsync(bodyParams, options).catch(() => { | ||||||||||||||||||||||||||||||
| // Error is already handled in mutateAsync | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| [mutateAsync], | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
|
Comment on lines
+54
to
61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Remove inline comment per coding guidelines. The inline comment violates the "no code comments" guideline. Keep the silent catch but remove the comment. const mutate = useCallback(
(bodyParams: Partial<Record>, options?: RecordOptions): void => {
- mutateAsync(bodyParams, options).catch(() => {
- // Error is already handled in mutateAsync
- });
+ mutateAsync(bodyParams, options).catch(() => {});
},
[mutateAsync],
);As per coding guidelines. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return useMemo( | ||||||||||||||||||||||||||||||
| (): UseUpdateMutationResult<Record> => ({ | ||||||||||||||||||||||||||||||
| mutate, | ||||||||||||||||||||||||||||||
| mutateAsync, | ||||||||||||||||||||||||||||||
| isPending, | ||||||||||||||||||||||||||||||
| isError: !!error, | ||||||||||||||||||||||||||||||
| error, | ||||||||||||||||||||||||||||||
| isSuccess: !isPending && !error, | ||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||
| [mutate, isPending, error], | ||||||||||||||||||||||||||||||
| [mutate, mutateAsync, isPending, error], | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,8 @@ export * from './query-result.type'; | |
| export * from './record-transformer.type'; | ||
| export * from './useAuth.type'; | ||
| export * from './useCollection.type'; | ||
| export * from './useCommon.type'; | ||
| export * from './useCreateMutation.type'; | ||
| export * from './useDeleteMutation.type'; | ||
| export * from './useQueryCommon.type'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainVerify the missing export for UseMutationCommonOptions. The rename from According to the codebase learnings, all exported types must be explicitly defined in Based on learnings. Run the following script to check imports of 🏁 Script executed: #!/bin/bash
# Check if UseMutationCommonOptions is imported anywhere and whether the export is needed
echo "=== Checking imports of UseMutationCommonOptions ==="
rg -n "import.*UseMutationCommonOptions" --type=ts --type=tsx
echo -e "\n=== Checking if useMutationCommon.type is referenced ==="
rg -n "from.*useMutationCommon" --type=ts --type=tsxLength of output: 166 [/run_scripts] 🏁 Script executed: #!/bin/bash
# Check if UseMutationCommonOptions is imported anywhere and whether the export is needed
echo "=== Checking imports of UseMutationCommonOptions ==="
rg -n "import.*UseMutationCommonOptions" --type=ts
echo -e "\n=== Checking if useMutationCommon.type is referenced ==="
rg -n "from.*useMutationCommon" --type=ts
echo -e "\n=== Checking current exports in src/types/index.ts ==="
cat src/types/index.ts
echo -e "\n=== Checking if useMutationCommon.type file exists ==="
fd "useMutationCommon" src/types/Length of output: 1317 Add missing export for The verification confirms the issue: Add 🤖 Prompt for AI Agents |
||
| export * from './useRecord.type'; | ||
| export * from './useUpdateMutation.type'; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Closure bug causes "ID is required" error on every delete.
The delete flow is broken due to closure capture timing:
deletingId=null→ hook initialized withid=null→deletePostwill throwhandleDelete('A')captures currentdeletePost(withid=null)setDeletingId('A')queues state update but doesn't take effect yetawait deletePost()calls the captured function → throws "ID is required" because it usesid=nullThe state update happens after the function reference is captured, so
deletePost()always usesid=null.Solution: Move the ID into the function call rather than hook initialization:
However, this requires updating the hook API to accept the ID as an argument to
mutateAsyncinstead of at initialization. Alternatively, redesign the hook to not require pre-initialization with an ID.🤖 Prompt for AI Agents