Last active
December 1, 2016 15:22
-
-
Save ruffle1986/768e610b0f8ead8e42525a2aec8240e0 to your computer and use it in GitHub Desktop.
YMCA actions, reducers, selectors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Dispatch = (action: Action | ThunkAction) => any; | |
type GetState = () => Object; | |
type ThunkAction = (dispatch: Dispatch, getState: GetState) => any; | |
type Image = { | |
publicId: string, | |
tags: Array<string> | |
// ... | |
}; | |
// actions | |
type Action = | |
{ type: 'LOAD_TOTAL_COUNT' } | |
| { type: 'LOAD_TOTAL_COUNT_SUCCESS', payload: { count: number } } | |
| { type: 'LOAD_TOTAL_COUNT_ERROR', payload: { err: Error } } | |
| { type: 'LOAD_IMAGES' } | |
| { type: 'LOAD_IMAGES_SUCCESS', payload: { images: Array<Image> } } | |
| { type: 'LOAD_IMAGES_ERROR', payload: { err: Error } } | |
| { type: 'UPLOAD_IMAGE', payload: { file: File } } | |
| { type: 'UPLOAD_IMAGE_PROGRESS', payload: { progress: number } } | |
| { type: 'UPLOAD_IMAGE_SUCCESS', payload: { image: Image } } | |
| { type: 'UPLOAD_IMAGE_ERROR', payload: { err: Error } } | |
| { type: 'LOAD_TAGS' } | |
| { type: 'LOAD_TAGS_SUCCESS', payload: { tags: Array<string> } } | |
| { type: 'LOAD_TAGS_ERROR', payload: { err: Error } } | |
| { type: 'LOAD_IMAGE' } | |
| { type: 'LOAD_IMAGE_SUCCESS', payload: { image: Image } } | |
| { type: 'LOAD_IMAGE_ERROR', payload: { err: Error } } | |
// action creators | |
function loadTotalCount(): ThunkAction {} | |
function loadTotalCountSuccess(count: number): Action {} | |
function loadTotalCountError(err: Error): Action {} | |
function loadImages(itemsPerPage: number, terms: ?string, nextCursor: ?string): ThunkAction {} | |
function loadImagesSuccess(images: Array<Image>): Action {} | |
function loadImagesError(err: Error): Action {} | |
function uploadImage(file: File): ThunkAction {} | |
function uploadImageProgress(progress: number): Action {} | |
function uploadImageSuccess(image: Image): Action {} | |
function uploadImageError(err: Error): Action {} | |
function loadTags(prefix: string): ThunkAction {} | |
function loadTagsSuccess(tags: Array<string>): Action {} | |
function loadTagsError(err: Error): Action {} | |
function loadImage(id: string, options: Object): ThunkAction {} | |
function loadImageSuccess(image: Image): Action {} | |
function loadImageError(err: Error): Action {} | |
type Filter = 'All' | 'Recent'; | |
// reducer | |
type State = { | |
images: Array<Image>, | |
totalCount: number, | |
suggestions: Array<string>, | |
searchTerm: string, | |
imageToChoose: ?Image, | |
activeFilter: Filter, | |
isLoading: boolean | |
}; | |
const initialState = { | |
images: [], | |
totalCount: 0, | |
suggestions: [], | |
searchTerm: '', | |
imageToChoose: null, | |
activeFilter: 'All', | |
isLoading: false | |
}; | |
function (state: State = initialState, action: Action): State { | |
} | |
// selectors | |
function getAllImages(state: State): Array<Image> { | |
return state.images; | |
} | |
function getRecentImages(state: State): Array<Image> { | |
// implement logic | |
} | |
function getFilteredImages(state: State): Array<Image> { | |
if (state.activeFilter === 'Recent') { | |
return getRecentImages(state); | |
} | |
return getAllImages(state); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment