Created
November 3, 2018 13:39
-
-
Save gujc71/7d22cd7e001167f246032df2c9bb4aac 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
export const firebase_board_list = () =>{ | |
return (dispatch) => { | |
return firestore.collection("boards").orderBy("brddate", "desc") | |
.onSnapshot(function(snapshot) { | |
snapshot.docChanges().forEach(function(change) { | |
var childData = change.doc.data(); | |
if (change.type === "added") { | |
childData.brddate = dateFormat(childData.brddate, "yyyy-mm-dd"); | |
dispatch(board_save(childData)); | |
} else | |
if (change.type === "modified") { | |
dispatch(board_save(childData)); | |
} else | |
if (change.type === "removed") { | |
dispatch(board_remove(childData.brdno)); | |
} | |
}); | |
}); | |
} | |
} | |
export const firebase_board_remove = ( brdno = {}) => { | |
return (dispatch) => { | |
return firestore.collection('boards').doc(brdno).delete(); | |
} | |
}; | |
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); | |
} else { | |
return firestore.collection('boards').doc(data.brdno).update(data); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment