2018-10-19 05:15:28 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
const API_URL = process.env.API_URL;
|
2018-10-19 06:04:27 +00:00
|
|
|
const FIREBASE_URL = 'https://us-central1-gamebrary-8c736.cloudfunctions.net';
|
2018-10-19 05:15:28 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
REGISTER({ commit }, payload) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios.post(`${API_URL}/auth/register`, payload)
|
|
|
|
.then(({ data }) => {
|
|
|
|
commit('SET_SESSION', data);
|
|
|
|
resolve(data);
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
LOGIN({ commit }, payload) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios.post(`${API_URL}/auth/login`, payload)
|
|
|
|
.then(({ data }) => {
|
|
|
|
commit('SET_SESSION', data);
|
|
|
|
commit('SET_UPDATED_TIMESTAMP');
|
|
|
|
resolve(data);
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
UPDATE_SETTINGS({ commit, state: { token } }, payload) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const options = { headers: { token } };
|
|
|
|
|
|
|
|
axios.put(`${API_URL}/settings`, payload, options)
|
|
|
|
.then(({ data: { settings } }) => {
|
|
|
|
commit('SET_SETTINGS', settings);
|
|
|
|
resolve();
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
UPDATE_LISTS({ commit, state: { user, token } }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const options = { headers: { token } };
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
lists: user.lists,
|
|
|
|
};
|
|
|
|
|
|
|
|
axios.put(`${API_URL}/lists`, payload, options)
|
|
|
|
.then(() => {
|
|
|
|
commit('SET_UPDATED_TIMESTAMP');
|
|
|
|
resolve();
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
LOAD_LISTS({ commit, state: { token } }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const options = { headers: { token } };
|
|
|
|
|
|
|
|
axios.get(`${API_URL}/lists`, options)
|
|
|
|
.then(({ data: { lists } }) => {
|
|
|
|
commit('UPDATE_LIST', lists);
|
|
|
|
commit('SET_UPDATED_TIMESTAMP');
|
|
|
|
resolve();
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
LOAD_USERS({ commit, state: { token } }) {
|
|
|
|
commit('CLEAR_ADMIN_DATA');
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const options = { headers: { token } };
|
|
|
|
|
|
|
|
axios.get(`${API_URL}/users`, options)
|
|
|
|
.then(({ data }) => {
|
|
|
|
commit('SET_ADMIN_DATA', data.reverse());
|
|
|
|
resolve();
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
LOAD_SHARE_LIST(context, listId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios.get(`${API_URL}/share/${listId}`)
|
|
|
|
.then(({ data: { lists } }) => {
|
|
|
|
resolve(lists);
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
LOAD_SHARE_GAMES(context, gameList) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios.get(`${API_URL}/games?games=${gameList.join(',')}`)
|
|
|
|
.then(({ data }) => {
|
|
|
|
resolve(data);
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-10-19 06:04:27 +00:00
|
|
|
LOAD_GAMES({ commit }, gameList) {
|
2018-10-19 05:15:28 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-10-19 06:04:27 +00:00
|
|
|
axios.get(`${FIREBASE_URL}/games?games=${gameList.join(',')}`)
|
2018-10-19 05:15:28 +00:00
|
|
|
.then(({ data }) => {
|
|
|
|
commit('CACHE_GAME_DATA', data);
|
|
|
|
resolve();
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-10-19 05:55:06 +00:00
|
|
|
SEARCH({ commit }, searchText) {
|
2018-10-19 05:15:28 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-10-19 06:04:27 +00:00
|
|
|
axios.get(`${FIREBASE_URL}/search?searchText=${searchText}&platformId=130`)
|
2018-10-19 05:15:28 +00:00
|
|
|
.then(({ data }) => {
|
|
|
|
commit('SET_SEARCH_RESULTS', data);
|
|
|
|
commit('CACHE_GAME_DATA', data);
|
|
|
|
resolve();
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
DELETE_USER({ state: { token } }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios.delete(`${API_URL}/user?token=${token}`)
|
|
|
|
.then(() => {
|
|
|
|
resolve();
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|