Skip to content
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

fix: refactor mutation type #2767

Merged
merged 23 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c02b54c
Fixed Mutation Types to include options as undefined
lucaseduoli Jul 9, 2024
5f6a036
Updated mutation function type to not include params if it is undefined
lucaseduoli Jul 17, 2024
4f7e75b
updated useAddUser type to match the new useMutationFunctionType
lucaseduoli Jul 17, 2024
85973b3
updated useDeleteUser type to match the new useMutationFunctionType
lucaseduoli Jul 17, 2024
3f9418f
feat: Update useLoginUser mutation function type
lucaseduoli Jul 17, 2024
6c0d0cb
chore: Update useLogout mutation function type
lucaseduoli Jul 17, 2024
d9d72f7
feat: Update useRefreshAccessToken mutation function type
lucaseduoli Jul 17, 2024
4efec0e
feat: Update useResetPassword mutation function type
lucaseduoli Jul 17, 2024
945f12c
feat: Update useUpdateUser mutation function type
lucaseduoli Jul 17, 2024
3efda63
chore: Update useDeleteMessages mutation function type
lucaseduoli Jul 17, 2024
0975388
refactor: Update useUpdateMessage mutation function type
lucaseduoli Jul 17, 2024
47fff4f
feat: Update usePostLikeComponent mutation function type
lucaseduoli Jul 17, 2024
8233958
chore: Update usePostRetrieveVertexOrder mutation function type
lucaseduoli Jul 17, 2024
88f4900
refactor: Update useGetProfilePicturesQuery function type
lucaseduoli Jul 17, 2024
6b4c0c5
feat: Update profile picture chooser component to use ProfilePictures…
lucaseduoli Jul 17, 2024
8b14879
refactor: Update ProfilePictureForm to use ProfilePicturesQueryResponse
lucaseduoli Jul 17, 2024
96ed699
refactor: Update handleGetProfilePictures to use useGetProfilePicture…
lucaseduoli Jul 17, 2024
8ddabc2
Refactored logout to use the onSuccess and onError inside the API hoo…
lucaseduoli Jul 17, 2024
17edd93
Merge branch 'main' into fix/refactor_mutation_type
lucaseduoli Jul 17, 2024
4616a9b
Merge branch 'main' into fix/refactor_mutation_type
lucaseduoli Jul 17, 2024
b12f091
Merge branch 'main' into fix/refactor_mutation_type
lucaseduoli Jul 17, 2024
03bba13
Merge branch 'main' into fix/refactor_mutation_type
lucaseduoli Jul 17, 2024
38530f5
Merge branch 'main' into fix/refactor_mutation_type
lucaseduoli Jul 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ interface IPostAddApiKey {
}

// add types for error handling and success
export const usePostAddApiKey: useMutationFunctionType<IPostAddApiKey> = (
options,
) => {
export const usePostAddApiKey: useMutationFunctionType<
undefined,
IPostAddApiKey
> = (options) => {
const { mutate } = UseRequestProcessor();

const postAddApiKeyFn = async (payload: IPostAddApiKey): Promise<any> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

export const useAddUser: useMutationFunctionType<UserInputType> = (
export const useAddUser: useMutationFunctionType<undefined, UserInputType> = (
options?,
) => {
const { mutate } = UseRequestProcessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ interface DeleteUserParams {
user_id: string;
}

export const useDeleteMessages: useMutationFunctionType<DeleteUserParams> = (
options?,
) => {
export const useDeleteMessages: useMutationFunctionType<
undefined,
DeleteUserParams
> = (options?) => {
const { mutate } = UseRequestProcessor();

const deleteMessage = async ({ user_id }: DeleteUserParams): Promise<any> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

export const useLoginUser: useMutationFunctionType<LoginType> = (options?) => {
export const useLoginUser: useMutationFunctionType<undefined, LoginType> = (
options?,
) => {
const { mutate } = UseRequestProcessor();

async function updateUser({ password, username }: LoginType): Promise<any> {
Expand Down
18 changes: 16 additions & 2 deletions src/frontend/src/controllers/API/queries/auth/use-logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import {
useMutationFunctionType,
} from "@/types/api";
import { UseMutationResult } from "@tanstack/react-query";
import { useNavigate } from "react-router-dom";
import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

export const useLogout: useMutationFunctionType = (options?) => {
export const useLogout: useMutationFunctionType<undefined, void> = (
options?,
) => {
const { mutate } = UseRequestProcessor();
const navigate = useNavigate();
const logout = useAuthStore((state) => state.logout);

async function logoutUser(): Promise<any> {
const autoLogin = useAuthStore.getState().autoLogin;
Expand All @@ -21,7 +26,16 @@ export const useLogout: useMutationFunctionType = (options?) => {
return res.data;
}

const mutation = mutate(["useLogout"], logoutUser, options);
const mutation = mutate(["useLogout"], logoutUser, {
onSuccess: () => {
logout();
navigate("/login");
},
onError: (error) => {
console.error(error);
},
...options,
});

return mutation;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

export const useRefrshAccessToken: useMutationFunctionType = (options?) => {
export const useRefreshAccessToken: useMutationFunctionType<undefined, any> = (
options?,
) => {
const { mutate } = UseRequestProcessor();

async function refreshAccess(): Promise<any> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ interface resetPasswordParams {
password: resetPasswordType;
}

export const useResetPassword: useMutationFunctionType<resetPasswordParams> = (
options?,
) => {
export const useResetPassword: useMutationFunctionType<
undefined,
resetPasswordParams
> = (options?) => {
const { mutate } = UseRequestProcessor();

async function resetPassword({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ interface UpdateUserParams {
user: changeUser;
}

export const useUpdateUser: useMutationFunctionType<UpdateUserParams> = (
options?,
) => {
export const useUpdateUser: useMutationFunctionType<
undefined,
UpdateUserParams
> = (options?) => {
const { mutate } = UseRequestProcessor();

async function updateUser({ user_id, user }: UpdateUserParams): Promise<any> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

interface ProfilePicturesQueryParams {}

export interface ProfilePicturesQueryResponse {
export interface ProfilePicturesQueryResponse extends Record<string, string[]> {
files: string[];
}

export const useGetProfilePicturesQuery: useQueryFunctionType<
ProfilePicturesQueryParams,
{ [key: string]: string[] }
undefined,
ProfilePicturesQueryResponse
> = () => {
const { query } = UseRequestProcessor();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ interface IPostUploadFile {
id: string;
}

export const usePostUploadFile: useMutationFunctionType<IPostUploadFile> = (
options?,
) => {
export const usePostUploadFile: useMutationFunctionType<
undefined,
IPostUploadFile
> = (options?) => {
const { mutate } = UseRequestProcessor();

const postUploadFileFn = async (payload: IPostUploadFile): Promise<any> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface DeleteMessagesParams {
}

export const useDeleteMessages: useMutationFunctionType<
undefined,
DeleteMessagesParams
> = (options?) => {
const { mutate } = UseRequestProcessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ interface UpdateMessageParams {
message: Message;
}

export const useUpdateMessage: useMutationFunctionType<UpdateMessageParams> = (
options?,
) => {
export const useUpdateMessage: useMutationFunctionType<
undefined,
UpdateMessageParams
> = (options?) => {
const { mutate } = UseRequestProcessor();

const updateMessageApi = async (data: Message) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface IPostLikeComponent {
}

export const usePostLikeComponent: useMutationFunctionType<
undefined,
IPostLikeComponent
> = (options) => {
const { mutate } = UseRequestProcessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface retrieveGetVerticesOrderResponse {

// add types for error handling and success
export const usePostRetrieveVertexOrder: useMutationFunctionType<
undefined,
retrieveGetVerticesOrder,
retrieveGetVerticesOrderResponse
> = (options) => {
Expand Down
19 changes: 0 additions & 19 deletions src/frontend/src/hooks/logout.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ProfilePicturesQueryResponse } from "@/controllers/API/queries/files";
import { useEffect, useRef, useState } from "react";
import { Button } from "../../../../../../../../components/ui/button";
import Loading from "../../../../../../../../components/ui/loading";
Expand All @@ -7,7 +8,7 @@ import { cn } from "../../../../../../../../utils/utils";
import usePreloadImages from "./hooks/use-preload-images";

type ProfilePictureChooserComponentProps = {
profilePictures: { [key: string]: string[] } | undefined;
profilePictures: ProfilePicturesQueryResponse | undefined;
loading: boolean;
value: string;
onChange: (value: string) => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useGetProfilePicturesQuery } from "@/controllers/API/queries/files";
import { ProfilePicturesQueryResponse } from "@/controllers/API/queries/files";
import * as Form from "@radix-ui/react-form";
import { useEffect, useState } from "react";
import { UseQueryResult } from "@tanstack/react-query";
import { Button } from "../../../../../../components/ui/button";
import {
Card,
Expand All @@ -17,7 +17,7 @@ type ProfilePictureFormComponentProps = {
profilePicture: string;
handleInput: (event: any) => void;
handlePatchProfilePicture: (gradient: string) => void;
handleGetProfilePictures: () => undefined;
handleGetProfilePictures: UseQueryResult<ProfilePicturesQueryResponse>;
userData: any;
};
const ProfilePictureFormComponent = ({
Expand All @@ -27,7 +27,7 @@ const ProfilePictureFormComponent = ({
handleGetProfilePictures,
userData,
}: ProfilePictureFormComponentProps) => {
const { data: response, isFetching } = useGetProfilePicturesQuery({});
const { data: response, isFetching } = handleGetProfilePictures;

return (
<Form.Root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ export const GeneralPage = () => {
setErrorData,
);

const handleGetProfilePictures = () => {
const { data } = useGetProfilePicturesQuery({});
return data;
};
const handleGetProfilePictures = useGetProfilePicturesQuery();

const { handlePatchProfilePicture } = usePatchProfilePicture(
setSuccessData,
Expand Down
18 changes: 15 additions & 3 deletions src/frontend/src/types/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,21 @@ export type MutationFunctionType = (
) => UseMutationResult<any, any, any, any>;

export type useMutationFunctionType<
Params,
Variables = any,
Data = any,
Error = any,
> = (
options?: Omit<UseMutationOptions<Data, Error>, "mutationFn" | "mutationKey">,
) => UseMutationResult<Data, Error, Variables>;
> = Params extends undefined
? (
options?: Omit<
UseMutationOptions<Data, Error>,
"mutationFn" | "mutationKey"
>,
) => UseMutationResult<Data, Error, Variables>
: (
params: Params,
options?: Omit<
UseMutationOptions<Data, Error>,
"mutationFn" | "mutationKey"
>,
) => UseMutationResult<Data, Error, Variables>;
Loading