Repository navigation
Expand file tree
/
Copy pathactions.js
More file actions
82 lines (74 loc) · 1.91 KB
/
Copy pathactions.js
File metadata and controls
82 lines (74 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import * as firebase from 'firebase';
import 'firebase/firestore';
export const SET_MUSIC = 'SET_MUSIC';
export const ADD_MUSIC = 'ADD_MUSIC';
const firebaseConfig = {
apiKey: 'AIzaSyCaHkSQGdyvtyTEK-Ys2AjpKuhDu0rxegw',
authDomain: 'mycollctor.firebaseapp.com',
databaseURL: 'https://mycollctor.firebaseio.com',
projectId: 'mycollctor',
storageBucket: 'mycollctor.appspot.com',
messagingSenderId: '198939433139'
};
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
db.settings({
timestampsInSnapshots: true
});
export function setMusic(music) {
return {
type: SET_MUSIC,
music
};
}
export function addMusic(response) {
return {
type: ADD_MUSIC,
response
};
}
export function saveMusic(data) {
return dispatch => {
db.collection('collector')
.doc()
.set(data)
.then(function() {
console.log('Document successfully written!');
dispatch(addMusic({ status: 'added' }));
})
.catch(function(error) {
console.error('Error writing document: ', error);
});
};
}
// export function saveMusic(data) {
// return dispatch => {
// return fetch('/api/music', {
// method: 'post',
// body: JSON.stringify(data),
// headers: { 'Content-Type': 'application/json' }
// });
// };
// }
// export function fetchMusic() {
// return dispatch => {
// fetch('/api/music')
// .then(res => res.json())
// .then(data => dispatch(setMusic(data.music)));
// };
// }
export function fetchMusic() {
var dataArr = [];
return dispatch => {
db.collection('collector')
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
// console.log(doc.id, ' => ', doc.data());
dataArr.push(doc.data());
});
})
.then(() => dispatch(setMusic(dataArr)));
};
}