Skip to content

Commit

Permalink
prepare all useMutation hooks for react-query upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyNenashev committed Oct 26, 2023
1 parent 090f1db commit 350eef8
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const IntegrationPreviewList: FC = () => {
const filteredIntegrations =
data?.items.filter(integration =>
integration.name.toLowerCase().includes(query.toLowerCase())
) || [];
) ?? [];

return (
<Grid container flexDirection='column' alignItems='center'>
Expand Down
15 changes: 6 additions & 9 deletions odd-platform-ui/src/lib/hooks/api/dataEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function useDataEntityMetrics({
}: UseDataEntityMetricsProps) {
return useQuery({
queryKey: ['dataEntityMetrics', dataEntityId],
queryFn: () =>
queryFn: async () =>
dataEntityApi.getDataEntityMetrics({ dataEntityId }).catch(err => {
showServerErrorToast(err as Response, {
additionalMessage: 'while loading metrics',
Expand Down Expand Up @@ -143,12 +143,9 @@ export function useGetDataEntityGroupItems({
}

export function useUpdateDataEntityStatus() {
return useMutation(
(params: DataEntityApiUpdateStatusRequest) => dataEntityApi.updateStatus(params),
{
onSuccess: () => {
showSuccessToast({ message: 'Status successfully updated!' });
},
}
);
return useMutation({
mutationFn: async (params: DataEntityApiUpdateStatusRequest) =>
dataEntityApi.updateStatus(params),
onSuccess: () => showSuccessToast({ message: 'Status successfully updated!' }),
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ interface UseSaveDataEntityFileParams {
export function useSaveDataEntityFile() {
const client = useQueryClient();

return useMutation(
async ({ dataEntityId, file }: UseSaveDataEntityFileParams) => {
return useMutation({
mutationFn: async ({ dataEntityId, file }: UseSaveDataEntityFileParams) => {
const dataEntityUploadFormData = { fileName: file.name };
const initialUploadParams = { dataEntityId, dataEntityUploadFormData };
const { id: uploadId } = await dataEntityAttachmentApi.initiateFileUpload(
Expand All @@ -74,13 +74,11 @@ export function useSaveDataEntityFile() {
uploadId,
});
},
{
onSuccess: () => {
showSuccessToast({ message: 'File successfully saved!' });
client.invalidateQueries(['dataEntityAttachments']);
},
}
);
onSuccess: async () => {
showSuccessToast({ message: 'File successfully saved!' });
await client.invalidateQueries(['dataEntityAttachments']);
},
});
}

export function useDownloadDataEntityFile({
Expand All @@ -97,16 +95,14 @@ export function useDownloadDataEntityFile({
export function useDeleteDataEntityFile() {
const client = useQueryClient();

return useMutation(
(params: DataEntityAttachmentApiDeleteFileRequest) =>
return useMutation({
mutationFn: (params: DataEntityAttachmentApiDeleteFileRequest) =>
dataEntityAttachmentApi.deleteFile(params),
{
onSuccess: () => {
showSuccessToast({ message: 'File successfully deleted!' });
client.invalidateQueries(['dataEntityAttachments']);
},
}
);
onSuccess: async () => {
showSuccessToast({ message: 'File successfully deleted!' });
await client.invalidateQueries(['dataEntityAttachments']);
},
});
}

export function useGetUploadOptions({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,48 @@ export function useDataEntityAttachments({
DataEntityAttachments,
ErrorState,
Array<DataEntityFile | DataEntityLink>
>(
['dataEntityAttachments'],
() => dataEntityAttachmentApi.getAttachments({ dataEntityId }),
{
select: data => [...data.files, ...data.links],
}
);
>({
queryKey: ['dataEntityAttachments'],
queryFn: () => dataEntityAttachmentApi.getAttachments({ dataEntityId }),
select: data => [...data.files, ...data.links],
});
}

export function useSaveDataEntityLinks() {
const client = useQueryClient();

return useMutation(
(params: DataEntityAttachmentApiSaveLinksRequest) =>
dataEntityAttachmentApi.saveLinks(params),
{
onSuccess: () => {
showSuccessToast({ message: 'Links successfully saved!' });
client.invalidateQueries(['dataEntityAttachments']);
},
}
);
return useMutation({
mutationFn: async (params: DataEntityAttachmentApiSaveLinksRequest) =>
await dataEntityAttachmentApi.saveLinks(params),
onSuccess: async () => {
showSuccessToast({ message: 'Links successfully saved!' });
await client.invalidateQueries(['dataEntityAttachments']);
},
});
}

export function useUpdateDataEntityLink() {
const client = useQueryClient();

return useMutation(
(params: DataEntityAttachmentApiUpdateLinkRequest) =>
dataEntityAttachmentApi.updateLink(params),
{
onSuccess: () => {
showSuccessToast({ message: 'Link successfully updated!' });
client.invalidateQueries(['dataEntityAttachments']);
},
}
);
return useMutation({
mutationFn: async (params: DataEntityAttachmentApiUpdateLinkRequest) =>
await dataEntityAttachmentApi.updateLink(params),
onSuccess: async () => {
showSuccessToast({ message: 'Link successfully updated!' });
await client.invalidateQueries(['dataEntityAttachments']);
},
});
}

export function useDeleteDataEntityLink() {
const client = useQueryClient();

return useMutation(
(params: DataEntityAttachmentApiDeleteLinkRequest) =>
return useMutation({
mutationFn: (params: DataEntityAttachmentApiDeleteLinkRequest) =>
dataEntityAttachmentApi.deleteLink(params),
{
onSuccess: () => {
showSuccessToast({ message: 'Link successfully deleted!' });
client.invalidateQueries(['dataEntityAttachments']);
},
}
);
onSuccess: async () => {
showSuccessToast({ message: 'Link successfully deleted!' });
await client.invalidateQueries(['dataEntityAttachments']);
},
});
}
56 changes: 22 additions & 34 deletions odd-platform-ui/src/lib/hooks/api/datasetField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ interface UseDataEntityMetricsProps {
export function useDataSetFieldMetrics({ datasetFieldId }: UseDataEntityMetricsProps) {
return useQuery({
queryKey: ['dataSetFieldMetrics', datasetFieldId],
queryFn: () =>
datasetFieldApiClient.getDatasetFieldMetrics({ datasetFieldId }).catch(err => {
showServerErrorToast(err as Response, {
additionalMessage: 'while loading field metrics',
});
}),
queryFn: async () =>
datasetFieldApiClient
.getDatasetFieldMetrics({ datasetFieldId })
.catch((err: Response) => {
showServerErrorToast(err, { additionalMessage: 'while loading field metrics' });
}),
retry: false,
refetchOnWindowFocus: false,
});
Expand All @@ -30,43 +30,31 @@ interface UseAddDatasetFieldTermParams {
}

export function useAddDatasetFieldTerm() {
return useMutation(
({ datasetFieldId, termId }: UseAddDatasetFieldTermParams) => {
const params = { datasetFieldId, datasetFieldTermFormData: { termId } };

return datasetFieldApiClient.addDatasetFieldTerm(params);
},
{
onSuccess: () => {
showSuccessToast({ message: 'Term successfully added!' });
},
}
);
return useMutation({
mutationFn: async ({ termId, datasetFieldId }: UseAddDatasetFieldTermParams) =>
datasetFieldApiClient.addDatasetFieldTerm({
datasetFieldId,
datasetFieldTermFormData: { termId },
}),
onSuccess: () => showSuccessToast({ message: 'Term successfully added!' }),
});
}

export function useDeleteDatasetFieldTerm() {
return useMutation(
async (params: DatasetFieldApiDeleteTermFromDatasetFieldRequest) => {
return useMutation({
mutationFn: async (params: DatasetFieldApiDeleteTermFromDatasetFieldRequest) => {
await datasetFieldApiClient.deleteTermFromDatasetField(params);

return params.termId;
},
{
onSuccess: () => {
showSuccessToast({ message: 'Term successfully deleted!' });
},
}
);
onSuccess: () => showSuccessToast({ message: 'Term successfully deleted!' }),
});
}

export function useUpdateDatasetFieldInternalName() {
return useMutation(
async (params: DatasetFieldApiUpdateDatasetFieldInternalNameRequest) =>
return useMutation({
mutationFn: async (params: DatasetFieldApiUpdateDatasetFieldInternalNameRequest) =>
datasetFieldApiClient.updateDatasetFieldInternalName(params),
{
onSuccess: () => {
showSuccessToast({ message: 'Internal name successfully updated!' });
},
}
);
onSuccess: () => showSuccessToast({ message: 'Internal name successfully updated!' }),
});
}
49 changes: 25 additions & 24 deletions odd-platform-ui/src/lib/hooks/api/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,34 @@ export function useIntegrationPreviews() {
{ items: GeneratedIntegrationPreview[] },
ErrorState,
{ items: IntegrationPreview[] }
>(['integrationPreviews'], () => integrationApi.getIntegrationPreviews());
>({
queryKey: ['integrationPreviews'],
queryFn: () => integrationApi.getIntegrationPreviews(),
});
}

export function useIntegration({ integrationId }: IntegrationApiGetIntegrationRequest) {
return useQuery<GeneratedIntegration, ErrorState, Integration>(
['integration', integrationId],
() => integrationApi.getIntegration({ integrationId }),
{
select: ({ id, installed, name, description, contentBlocks }) => {
const contentByTitle = contentBlocks.reduce<Integration['contentByTitle']>(
(memo, block) => {
const lowerCasedTitle = block.title.toLowerCase();
return useQuery<GeneratedIntegration, ErrorState, Integration>({
queryKey: ['integration', integrationId],
queryFn: () => integrationApi.getIntegration({ integrationId }),
select: ({ id, installed, name, description, contentBlocks }) => {
const contentByTitle = contentBlocks.reduce<Integration['contentByTitle']>(
(memo, block) => {
const lowerCasedTitle = block.title.toLowerCase();

return {
...memo,
[lowerCasedTitle]: {
...memo[lowerCasedTitle],
content: block.content,
codeSnippets: block.codeSnippets,
},
};
},
{}
);
return {
...memo,
[lowerCasedTitle]: {
...memo[lowerCasedTitle],
content: block.content,
codeSnippets: block.codeSnippets,
},
};
},
{}
);

return { id: id as DatasourceName, name, description, installed, contentByTitle };
},
}
);
return { id: id as DatasourceName, name, description, installed, contentByTitle };
},
});
}

0 comments on commit 350eef8

Please sign in to comment.