Skip to content

Commit

Permalink
Merge pull request #22 from atomdmac/allow-clear-state
Browse files Browse the repository at this point in the history
feat: Initialize complete state, allow state to be cleared.
  • Loading branch information
zackify authored Sep 24, 2020
2 parents 01540fa + 9aa8195 commit e339e6a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
32 changes: 24 additions & 8 deletions src/upload-reducer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,54 @@ export const SET_ERROR = 'SET_ERROR';
export const START_UPLOADING = 'START_UPLOADING';
export const SET_UPLOAD_PROGRESS = 'SET_UPLOAD_PROGRESS';
export const FINISH_UPLOADING = 'FINISH_UPLOADING';
export const RESET = 'RESET';

export type UploadState = {
loading?: boolean;
progress?: number;
error?: string;
done?: boolean;
loading: boolean;
progress: number;
error: string | null;
done: boolean;
response?: any;
};

export const initialState: UploadState = {
loading: false,
progress: 0,
error: null,
done: false,
response: null
};

export type Action =
| { type: 'START_UPLOADING' }
| { type: 'SET_UPLOAD_PROGRESS'; payload: number }
| { type: 'SET_ERROR'; payload: string }
| { type: 'FINISH_UPLOADING'; payload: any };
| { type: 'FINISH_UPLOADING'; payload: any }
| { type: 'RESET'; };

export type dispatchType = (action: Action) => void;

// The possible state changes that take place during a file upload
export function reducer(state: UploadState, action: Action) {
export function reducer(state: UploadState, action: Action): UploadState {
switch (action.type) {
case START_UPLOADING:
return { loading: true };
return { ...initialState, loading: true };
case SET_UPLOAD_PROGRESS:
return { ...state, progress: action.payload };
case SET_ERROR:
return { loading: false, error: action.payload, done: true };
return { ...state, loading: false, error: action.payload, done: true };
case FINISH_UPLOADING:
return {
...state,
done: true,
loading: false,
response: action.payload,
error: action.payload.error ? action.payload.response : false,
};
case RESET:
return {
...initialState
};
default:
return state;
}
Expand Down
15 changes: 12 additions & 3 deletions src/use-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
SET_UPLOAD_PROGRESS,
FINISH_UPLOADING,
dispatchType,
initialState,
RESET,
} from './upload-reducer';
import { XHRClient, XHROptions, createXhrClient } from './clients/xhr';
import { FileOrFileList } from './';
Expand Down Expand Up @@ -40,12 +42,16 @@ const handleUpload = async ({
if (response) dispatch({ type: FINISH_UPLOADING, payload: response });
};

type UseUploadResult = UploadState & {
reset: () => void
}

export const useUpload = (
files: FileOrFileList,
options: XHROptions | GraphQLOptions,
): UploadState => {
): UseUploadResult => {
let client = useContext<XHRClient | GraphQLClient | null>(UploadContext);
const [state, dispatch] = useReducer(reducer, {});
const [state, dispatch] = useReducer(reducer, initialState);

useEffect(() => {
if (!files) return;
Expand All @@ -57,5 +63,8 @@ export const useUpload = (
});
}, [files]);

return state;
return {
...state,
reset: () => dispatch({ type: RESET })
};
};

0 comments on commit e339e6a

Please sign in to comment.