Skip to content

Instantly share code, notes, and snippets.

@velopert
Last active September 10, 2021 17:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velopert/36505bad9fc5e38d1be49f3c3263ff30 to your computer and use it in GitHub Desktop.
Save velopert/36505bad9fc5e38d1be49f3c3263ff30 to your computer and use it in GitHub Desktop.
// 리듀서에서 사용 할 수 있는 여러 유틸 함수들입니다.
export const reducerUtils = {
// 초기 상태. 초기 data 값은 기본적으로 null 이지만
// 바꿀 수도 있습니다.
initial: (initialData = null) => ({
loading: false,
data: initialData,
error: null
}),
// 로딩중 상태. prevState의 경우엔 기본값은 null 이지만
// 따로 값을 지정하면 null 로 바꾸지 않고 다른 값을 유지시킬 수 있습니다.
loading: (prevState = null) => ({
loading: true,
data: prevState,
error: null
}),
// 성공 상태
success: payload => ({
loading: false,
data: payload,
error: null
}),
// 실패 상태
error: error => ({
loading: false,
data: null,
error: error
})
};
// 비동기 관련 액션들을 처리하는 리듀서를 만들어줍니다.
// type 은 액션의 타입, key 는 상태의 key (예: posts, post) 입니다.
export const handleAsyncActions = (type, key, keepData = false) => {
const [SUCCESS, ERROR] = [`${type}_SUCCESS`, `${type}_ERROR`];
return (state, action) => {
switch (action.type) {
case type:
return {
...state,
[key]: reducerUtils.loading(keepData ? state[key].data : null)
};
case SUCCESS:
return {
...state,
[key]: reducerUtils.success(action.payload)
};
case ERROR:
return {
...state,
[key]: reducerUtils.error(action.payload)
};
default:
return state;
}
};
};
// id별로 처리하는 유틸함수
export const handleAsyncActionsById = (type, key, keepData = false) => {
const [SUCCESS, ERROR] = [`${type}_SUCCESS`, `${type}_ERROR`];
return (state, action) => {
const id = action.meta;
switch (action.type) {
case type:
return {
...state,
[key]: {
...state[key],
[id]: reducerUtils.loading(
// state[key][id]가 만들어져있지 않을 수도 있으니까 유효성을 먼저 검사 후 data 조회
keepData ? state[key][id] && state[key][id].data : null
)
}
};
case SUCCESS:
return {
...state,
[key]: {
...state[key],
[id]: reducerUtils.success(action.payload)
}
};
case ERROR:
return {
...state,
[key]: {
...state[key],
[id]: reducerUtils.error(action.payload)
}
};
default:
return state;
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment