Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/lukebennett88/luke-ui. ui.lukebennett.dev
Something went wrong. Try again.
961 B · 59 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960export type AvatarUploadState = | { status: 'idle'; } | { status: 'reading'; file: File; } | { status: 'readFailed'; error: string; };
export type AvatarUploadAction = | { type: 'selected'; file: File; } | { type: 'rejected'; error: string; } | { type: 'readFailed'; error: string; } | { type: 'reset'; };
export const initialAvatarUploadState: AvatarUploadState = { status: 'idle',};
export function avatarUploadReducer( state: AvatarUploadState, action: AvatarUploadAction,): AvatarUploadState { switch (action.type) { case 'selected': return { file: action.file, status: 'reading', }; case 'rejected': return { error: action.error, status: 'readFailed', }; case 'readFailed': if (state.status !== 'reading') return state; return { error: action.error, status: 'readFailed', }; case 'reset': return initialAvatarUploadState; }}