File size: 3,836 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import * as actionTypes from "./actionTypes";
import firebase from "../firebase";

// store user in store
export const login = user => {
	return {
		type: actionTypes.LOGIN,
		payload: { user }
	};
};

// servers list that user ahs joined
// export const addJoinedServers = server => {
//   return {
//     type: actionTypes.ADD_JOINED_SERVER,
//     payload: server
//   };
// };
export const setServerLoading = isLoading => {
	return {
		type: actionTypes.SET_SERVER_LOADING,
		payload: isLoading
	};
};

//total server list on platform,includes joined servers and nonjoined severs
const addTotalServers = servers => {
	return {
		type: actionTypes.ADD_TOTAL_SERVER,
		payload: servers
	};
};

// // loading total server list or not
// const setLoadingTotalServer = isLoading => {
//   return {
//     type: actionTypes.SET_LOADING_TOTAL_SERVERS,
//     payload: isLoading
//   };
// };

export const selectServer = server => {
	return {
		type: actionTypes.SELECT_SERVER,
		payload: server
	};
};

// async func to load total servers from db
export const loadTotalServers = () => {
	return dispatch => {
		firebase
			.database()
			.ref("servers")
			.once("value", snap => {
				const servers = convertToArray(snap.val());
				dispatch(addTotalServers(servers));
			});
	};
};

export const updateServer = (id, server) => {
	return {
		type: actionTypes.UPDATE_SERVER,
		payload: {
			id,
			server
		}
	};
};
export const addDm = (id, dms) => {
	return {
		type: actionTypes.ADD_DM,
		payload: {
			id,
			dms
		}
	};
};

export const removeServer = id => {
	return {
		type: actionTypes.REMOVE_SERVER,
		payload: id
	};
};
export const addMessages = (serverId, channels) => {
	return {
		type: actionTypes.ADD_MESSAGES,
		payload: {
			serverId,
			channels
		}
	};
};

export const setCall = call => {
	return {
		type: actionTypes.SET_CALL,
		payload: call
	};
};

// //loading single server
// export const loadServer = id => {
//   const db = firebase.database();
//   console.log("loaded single server");

//   return dispatch => {
//     db.ref("servers/" + id).once("value", snap => {
//       dispatch(addJoinedServers(snap.val()));
//     });
//   };
// };

// // async func to load all joined servers from db
// // first we are fetch all the servers id's from user data after that
// //we are fetching individual server data using id's from servers ref
// export const loadJoinedServers = uid => {
//   console.clear();
//   console.log("loaded all servers");

//   return dispatch => {
//     const db = firebase.database();
//     db.ref("users/" + uid + "/servers").once("value", snap => {
//       const joinedServers = convertToArray(snap.val());
//       const servers = [];
//       joinedServers.forEach(server => {
//         db.ref("servers/" + server.id).once("value", snap => {
//           servers.push(snap.val());

//           if (servers.length === joinedServers.length) {
//             const filtered = filterServers(servers, joinedServers, uid);
//             dispatch(addJoinedServers(filtered));
//           }
//         });
//       });
//     });
//   };
// };

//convert db object to array before storing
const convertToArray = servers => {
	if (servers === null) return [];
	const keys = Object.keys(servers);
	return keys.map(key => servers[key]);
};

// //checking if the server is deleted, if yes then removing from users db
// const filterServers = (filterables, servers, uid) => {
//   if (Array.isArray(filterables)) {
//     return filterables.filter((server, index) =>
//       !server ? removeDeletedServer(uid, servers[index].id) : server
//     );
//   } else return [];
// };

// //remove server from user db
// export const removeDeletedServer = (uid, id) => {
//   firebase
//     .database()
//     .ref("users/" + uid + "/servers/")
//     .child(id)
//     .remove();
// };