Skip to content
Permalink
Browse files

Return query response in mutate function

  • Loading branch information...
marcin-piela committed May 21, 2019
1 parent fa7c0ba commit 4d4d8da9c062b6d77ff8c84fe34d47b18a190b25
Showing with 8 additions and 7 deletions.
  1. +3 −3 docs/README.md
  2. +1 −2 src/components/mutation/Mutation.types.ts
  3. +4 −2 src/hooks/useMutation/useMutation.ts
@@ -420,12 +420,12 @@ const addUserAction = (formValues) => ({
});
export const AddUserFormContainer = () => {
const { loading, payload, error, errorObject, mutate } = useMutation(addUserAction);
const { loading, payload, mutate, error } = useMutation(addUserAction);
const handleSubmit = async (formValues) => {
await mutate(formValues);
const { error: mutateError } = await mutate(formValues);
if (error) {
if (mutateError) {
//show ie. notification
}
@@ -1,11 +1,10 @@
import { ReactNode } from 'react';
import { Action, QueryResponse } from '../../client/client.types';

type ActionCreator<S, R> = (action: S) => Action<R>;

export type MutationApi<T, S> = {
loading: boolean;
mutate: (action: S) => Promise<void>;
mutate: (action: S) => Promise<QueryResponse<T>>;
} & QueryResponse<T>;

export type MutationProps<T, R, S> = {
@@ -1,6 +1,6 @@
import { useCallback, useContext, useEffect, useReducer, useRef } from 'react';

import { Action } from '../../client/client.types';
import { Action, QueryResponse } from '../../client/client.types';
import { QueryError } from '../../client/errors/QueryError';
import { ClientContext } from '../../context/clientContext';
import { responseReducer, SET_LOADING, SET_RESPONSE } from '../../reducers/responseReducer';
@@ -28,7 +28,7 @@ export const useMutation = <T = any, R = {}, S = any>(actionCreator: ActionCreat
const handleQuery = useCallback(
async (...params: Parameters<typeof actionCreator>) => {
if (!isMounted.current) {
return;
return { error: false } as QueryResponse<T>;
}

dispatch({ type: SET_LOADING });
@@ -38,6 +38,8 @@ export const useMutation = <T = any, R = {}, S = any>(actionCreator: ActionCreat
if (isMounted.current) {
dispatch({ type: SET_RESPONSE, response: queryResponse });
}

return queryResponse;
},
[actionCreator],
);

0 comments on commit 4d4d8da

Please sign in to comment.
You can’t perform that action at this time.