Last active
November 3, 2018 13:37
-
-
Save gujc71/c7cad2112f4f189c51d1030050b52aee to your computer and use it in GitHub Desktop.
react_firestore
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
import { createAction, handleActions } from 'redux-actions'; | |
import firestore from './Firestore'; | |
~~ 생략 ~~ | |
export const firebase_board_list = () =>{ | |
return (dispatch) => { | |
return firestore.collection('boards').orderBy("brddate", "desc").get() | |
.then((snapshot) => { | |
var rows = []; | |
snapshot.forEach((doc) => { | |
var childData = doc.data(); | |
childData.brddate = dateFormat(childData.brddate, "yyyy-mm-dd"); | |
rows.push(childData); | |
}); | |
dispatch(board_list(rows)); | |
}); | |
} | |
} | |
export const firebase_board_remove = ( brdno = {}) => { | |
return (dispatch) => { | |
console.log(brdno); | |
return firestore.collection('boards').doc(brdno).delete().then(() => { | |
dispatch(board_remove(brdno)); | |
}) | |
} | |
}; | |
export const firebase_board_save = ( data = {}) => { | |
return (dispatch) => { | |
if (!data.brdno) { | |
var doc = firestore.collection('boards').doc(); | |
data.brdno = doc.id; | |
data.brddate = Date.now(); | |
return doc.set(data).then(() => { | |
data.brddate = dateFormat(data.brddate, "yyyy-mm-dd"); | |
dispatch(board_save(data)); | |
}) | |
} else { | |
return firestore.collection('boards').doc(data.brdno).update(data).then(() => { | |
dispatch(board_save(data)); | |
}) | |
} | |
} | |
}; | |
const initialState = { | |
boards: [], | |
selectedBoard: {} | |
}; | |
export default handleActions({ | |
[BOARD_LIST]: (state, { payload: data }) => { | |
return {boards: data, selectedBoard: {} }; | |
}, | |
[BOARD_SAVE]: (state, { payload: data }) => { | |
~~ 생략 ~~ | |
}, | |
[BOARD_REMOVE]: (state, { payload: brdno }) => { | |
~~ 생략 ~~ | |
}, | |
[BOARD_READ]: (state, { payload: brdno }) => { | |
~~ 생략 ~~ | |
} | |
}, initialState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment