Skip to content

Commit

Permalink
Return query response in mutate function
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-piela committed May 21, 2019
1 parent fa7c0ba commit 4d4d8da
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions docs/README.md
Expand Up @@ -420,12 +420,12 @@ const addUserAction = (formValues) => ({
}); });


export const AddUserFormContainer = () => { export const AddUserFormContainer = () => {
const { loading, payload, error, errorObject, mutate } = useMutation(addUserAction); const { loading, payload, mutate, error } = useMutation(addUserAction);


const handleSubmit = async (formValues) => { const handleSubmit = async (formValues) => {
await mutate(formValues); const { error: mutateError } = await mutate(formValues);


if (error) { if (mutateError) {
//show ie. notification //show ie. notification
} }


Expand Down
3 changes: 1 addition & 2 deletions src/components/mutation/Mutation.types.ts
@@ -1,11 +1,10 @@
import { ReactNode } from 'react';
import { Action, QueryResponse } from '../../client/client.types'; import { Action, QueryResponse } from '../../client/client.types';


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


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


export type MutationProps<T, R, S> = { export type MutationProps<T, R, S> = {
Expand Down
6 changes: 4 additions & 2 deletions src/hooks/useMutation/useMutation.ts
@@ -1,6 +1,6 @@
import { useCallback, useContext, useEffect, useReducer, useRef } from 'react'; 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 { QueryError } from '../../client/errors/QueryError';
import { ClientContext } from '../../context/clientContext'; import { ClientContext } from '../../context/clientContext';
import { responseReducer, SET_LOADING, SET_RESPONSE } from '../../reducers/responseReducer'; import { responseReducer, SET_LOADING, SET_RESPONSE } from '../../reducers/responseReducer';
Expand Down Expand Up @@ -28,7 +28,7 @@ export const useMutation = <T = any, R = {}, S = any>(actionCreator: ActionCreat
const handleQuery = useCallback( const handleQuery = useCallback(
async (...params: Parameters<typeof actionCreator>) => { async (...params: Parameters<typeof actionCreator>) => {
if (!isMounted.current) { if (!isMounted.current) {
return; return { error: false } as QueryResponse<T>;
} }


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

return queryResponse;
}, },
[actionCreator], [actionCreator],
); );
Expand Down

0 comments on commit 4d4d8da

Please sign in to comment.