Skip to content

Commit

Permalink
refactor: Use Query Key Factor for Object ACL and Fix Incorrect api-v…
Browse files Browse the repository at this point in the history
…4 type (linode#11369)

* use query key factory

* add changesets

---------

Co-authored-by: Banks Nussman <banks@nussman.us>
  • Loading branch information
bnussman-akamai and bnussman authored Dec 6, 2024
1 parent 5ac188d commit dcf380b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
5 changes: 5 additions & 0 deletions packages/api-v4/.changeset/pr-11369-fixed-1733335872885.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/api-v4": Fixed
---

Incorrect return type of `updateObjectACL` ([#11369](https://github.com/linode/manager/pull/11369))
2 changes: 1 addition & 1 deletion packages/api-v4/src/object-storage/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const updateObjectACL = (
name: string,
acl: Omit<ACLType, 'custom'>
) =>
Request<ObjectStorageObjectACL>(
Request<{}>(
setMethod('PUT'),
setURL(
`${API_ROOT}/object-storage/buckets/${encodeURIComponent(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Tech Stories
---

Update `useObjectAccess` to use a query key factory ([#11369](https://github.com/linode/manager/pull/11369))
37 changes: 27 additions & 10 deletions packages/manager/src/queries/object-storage/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { createQueryKeys } from '@lukemorales/query-key-factory';
import {
keepPreviousData,
queryOptions,
useInfiniteQuery,
useMutation,
useQuery,
Expand Down Expand Up @@ -76,6 +77,17 @@ export const objectStorageQueries = createQueryKeys('object-storage', {
queryKey: null,
},
objects: {
contextQueries: {
acl: (name: string) => ({
queryFn: () =>
getObjectACL({
bucket: bucketName,
clusterId: clusterOrRegion,
params: { name },
}),
queryKey: [name],
}),
},
// This is a placeholder queryFn and QueryKey. View the `useObjectBucketObjectsInfiniteQuery` implementation for details.
queryFn: null,
queryKey: null,
Expand Down Expand Up @@ -209,8 +221,9 @@ export const useObjectAccess = (
) =>
useQuery<ObjectStorageObjectACL, APIError[]>({
enabled: queryEnabled,
queryFn: () => getObjectACL({ bucket, clusterId, params }),
queryKey: [bucket, clusterId, params.name],
...objectStorageQueries
.bucket(clusterId, bucket)
._ctx.objects._ctx.acl(params.name),
});

export const useUpdateBucketAccessMutation = (
Expand Down Expand Up @@ -241,16 +254,20 @@ export const useUpdateObjectAccessMutation = (
name: string
) => {
const queryClient = useQueryClient();

const options = queryOptions(
objectStorageQueries
.bucket(clusterId, bucketName)
._ctx.objects._ctx.acl(name)
);

return useMutation<{}, APIError[], ACLType>({
mutationFn: (data) => updateObjectACL(clusterId, bucketName, name, data),
onSuccess: (_, acl) => {
queryClient.setQueryData<ObjectStorageObjectACL>(
[bucketName, clusterId, name],
(oldData) => ({
acl,
acl_xml: oldData?.acl_xml ?? null,
})
);
onSuccess(_, acl) {
queryClient.setQueryData(options.queryKey, (oldData) => ({
acl,
acl_xml: oldData?.acl_xml ?? null,
}));
},
});
};
Expand Down

0 comments on commit dcf380b

Please sign in to comment.