Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
리액트 심화 과정 - 3강 /write 페이지에서 파이어베이스로 이미지 업로드하고 이미지 미리보기 영역 만들기
- Loading branch information
Showing
5 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { createAction, handleActions } from "redux-actions"; | ||
import { produce } from "immer"; | ||
import { storage } from "../../shared/firebase"; | ||
|
||
// initial state | ||
const initialState = { | ||
image_url: "", | ||
uploading: false, | ||
preview: null, | ||
}; | ||
|
||
// actions | ||
const UPLOADING = "UPLOADING"; | ||
const UPLOAD_IMAGE = "UPLOAD_IMAGE"; | ||
const SET_PREVIEW = "SET_PREVIEW"; | ||
|
||
// action creators | ||
const uploading = createAction(UPLOADING, (uploading) => ({ uploading })); | ||
const uploadImage = createAction(UPLOAD_IMAGE, (image_url) => ({ image_url })); | ||
const setPreview = createAction(SET_PREVIEW, (preview) => ({ preview })); | ||
|
||
// middleware actions | ||
const uploadImageFB = (image) => { | ||
return function (dispatch, getState, { history }) { | ||
dispatch(uploading(true)); | ||
const _upload = storage.ref(`images/${image.name}`).put(image); | ||
|
||
_upload.then((snapshot) => { | ||
snapshot.ref.getDownloadURL().then((image_url) => { | ||
dispatch(uploadImage(image_url)); | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
// reducer using handle actions, immer | ||
export default handleActions( | ||
{ | ||
[UPLOADING]: (state, action) => | ||
produce(state, (draft) => { | ||
draft.uploading = action.payload.uploading; | ||
}), | ||
[UPLOAD_IMAGE]: (state, action) => | ||
produce(state, (draft) => { | ||
draft.image_url = action.payload.image_url; | ||
draft.uploading = false; | ||
}), | ||
[SET_PREVIEW]: (state, action) => | ||
produce(state, (draft) => { | ||
draft.preview = action.payload.preview; | ||
}), | ||
}, | ||
initialState | ||
); | ||
|
||
const actionCreators = { | ||
uploadImageFB, | ||
setPreview, | ||
}; | ||
export { actionCreators }; |
This file contains 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
This file contains 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