gamebrary/src/store/actions.js

498 lines
13 KiB
JavaScript
Raw Normal View History

2018-10-19 05:15:28 +00:00
import axios from 'axios';
2019-10-01 22:27:34 +00:00
import firebase from 'firebase/app';
2020-09-09 20:38:52 +00:00
import 'firebase/storage';
2019-10-01 22:27:34 +00:00
import 'firebase/firestore';
2018-10-19 05:15:28 +00:00
2020-10-21 23:03:09 +00:00
const API_BASE = 'https://us-central1-gamebrary-8c736.cloudfunctions.net';
// const API_BASE = 'http://localhost:5001/gamebrary-8c736/us-central1';
2018-10-19 05:15:28 +00:00
export default {
2020-10-21 16:26:00 +00:00
LOAD_IGDB_PLATFORMS({ state, commit }) {
2020-08-18 18:56:10 +00:00
return new Promise((resolve, reject) => {
2020-10-21 16:26:00 +00:00
axios.get(`${API_BASE}/platforms?token=${state.twitchToken.access_token}`)
2020-08-18 18:56:10 +00:00
.then(({ data }) => {
2020-08-22 12:21:56 +00:00
commit('SET_PLATFORMS', data);
2020-08-18 18:56:10 +00:00
resolve(data);
}).catch(reject);
});
},
2021-02-12 23:08:02 +00:00
LOAD_WIKIPEDIA_ARTICLE(context, articleTitle) {
return new Promise((resolve, reject) => {
axios.get(`https://en.wikipedia.org/w/api.php?origin=*&action=query&prop=extracts&titles=${articleTitle}&format=json`)
2021-02-12 23:08:02 +00:00
.then(({ data: { query } }) => {
const pageIds = Object.keys(query.pages);
const article = pageIds.length
? query.pages[pageIds[0]]
: null;
resolve(article);
}).catch(reject);
});
},
2020-08-18 18:56:10 +00:00
LOAD_BOARDS({ state, commit }) {
return new Promise((resolve, reject) => {
const db = firebase.firestore();
2021-03-02 06:26:45 +00:00
db.collection('boards')
.where('owner', '==', state.user.uid)
2020-08-18 18:56:10 +00:00
.get()
.then(({ docs }) => {
const boards = docs.length
? docs.map((doc) => {
const board = doc.data();
return {
id: doc.id,
...board,
};
})
: null;
if (boards) {
commit('SET_BOARDS', boards);
}
resolve();
})
.catch(reject);
});
},
2020-08-18 22:01:16 +00:00
LOAD_BOARD({ state, commit }, id) {
2020-08-18 18:56:10 +00:00
return new Promise((resolve, reject) => {
const db = firebase.firestore();
db.collection('boards')
.doc(id)
.get()
2020-08-21 06:13:45 +00:00
.then((doc) => {
const board = doc.data();
2020-08-18 18:56:10 +00:00
2020-08-18 22:01:16 +00:00
if (state.user.uid !== board.owner) {
return reject();
}
2021-01-25 21:04:56 +00:00
commit('SET_ACTIVE_BOARD', {
2020-08-21 06:13:45 +00:00
...board,
id: doc.id,
});
2020-08-18 22:01:16 +00:00
return resolve();
2020-08-18 18:56:10 +00:00
})
.catch(reject);
});
},
2020-10-05 18:42:28 +00:00
LOAD_PUBIC_BOARD({ commit }, id) {
return new Promise((resolve, reject) => {
const db = firebase.firestore();
db.collection('boards')
.doc(id)
.get()
.then((doc) => {
const board = doc.data();
2021-01-25 21:04:56 +00:00
commit('SET_ACTIVE_BOARD', {
2020-10-05 18:42:28 +00:00
...board,
id: doc.id,
});
return resolve();
})
.catch(reject);
});
},
LOAD_PUBIC_BOARDS({ commit }) {
// TODO: paginate
return new Promise((resolve, reject) => {
const db = firebase.firestore();
db.collection('boards')
.where('isPublic', '==', true)
.get()
.then((querySnapshot) => {
const boards = querySnapshot.docs.map(doc => doc.data());
commit('SET_PUBLIC_BOARDS', boards);
return resolve(boards);
})
.catch(reject);
});
},
2020-08-18 18:56:10 +00:00
CREATE_BOARD({ state, commit }, board) {
return new Promise((resolve, reject) => {
const db = firebase.firestore();
const payload = {
...board,
owner: state.user.uid,
};
2021-03-02 06:26:45 +00:00
db.collection('boards')
.add(payload)
2020-08-18 18:56:10 +00:00
.then(({ id }) => {
2020-11-23 18:03:20 +00:00
const newBoard = {
2020-08-18 18:56:10 +00:00
...payload,
id,
2020-11-23 18:03:20 +00:00
};
2020-08-18 18:56:10 +00:00
2020-11-23 18:03:20 +00:00
commit('ADD_BOARD', newBoard);
resolve(newBoard);
2020-08-18 18:56:10 +00:00
})
.catch(reject);
});
},
2020-08-25 04:23:22 +00:00
DELETE_WALLPAPER({ commit }, { fullPath }) {
2020-08-25 04:23:22 +00:00
return new Promise((resolve, reject) => {
2021-03-02 06:26:45 +00:00
firebase.storage()
.ref(fullPath)
.delete()
2020-08-25 04:23:22 +00:00
.then(() => {
commit('REMOVE_WALLPAPER', fullPath);
2020-08-25 04:23:22 +00:00
resolve();
})
.catch(reject);
});
},
LOAD_WALLPAPER(context, path) {
2020-08-25 04:23:22 +00:00
return new Promise((resolve, reject) => {
2021-03-02 06:26:45 +00:00
firebase.storage()
.ref()
.child(path)
.getDownloadURL()
2020-08-25 04:23:22 +00:00
.then((url) => {
resolve(url);
})
.catch(reject);
});
},
LOAD_WALLPAPERS({ state, commit }) {
2020-08-25 04:23:22 +00:00
return new Promise((resolve, reject) => {
storage
2021-03-02 06:26:45 +00:00
firebase.storage()
.ref(`${state.user.uid}/wallpapers`)
2020-08-25 04:23:22 +00:00
.listAll()
.then(({ items }) => {
const wallpapers = items.map(({ fullPath, name }) => {
const wallpaper = {
fullPath,
name,
};
return wallpaper;
});
// TODO: refactor? there's gotta be a better way to do this
2020-11-01 21:46:17 +00:00
// TODO: for real, refactor this crap, use promise.all or something better
const fetchedUrls = [];
wallpapers.forEach(({ fullPath }, index) => {
firebase.storage()
.ref()
2021-03-02 06:26:45 +00:00
.child(fullPath)
.getDownloadURL()
.then((url) => {
fetchedUrls.push(url);
wallpapers[index].url = url;
if (fetchedUrls.length === wallpapers.length) {
2020-11-01 21:46:17 +00:00
const fetchedMetadatas = [];
wallpapers.forEach((wallpaper, i) => {
2021-03-02 06:26:45 +00:00
firebase.storage()
.ref(wallpaper.fullPath)
.getMetadata()
.then((metadata) => {
fetchedMetadatas.push(metadata);
wallpapers[i].metadata = metadata;
if (fetchedMetadatas.length === wallpapers.length) {
commit('SET_WALLPAPERS', wallpapers);
resolve();
}
2020-11-01 21:46:17 +00:00
});
});
}
})
.catch(reject);
});
})
.catch(reject);
});
},
UPLOAD_WALLPAPER({ state, commit }, file) {
return new Promise((resolve, reject) => {
firebase.storage()
.ref(`${state.user.uid}/wallpapers/${file.name}`)
.put(file)
.then((response) => {
if (response.state === 'success') {
const { metadata: { fullPath, name } } = response;
firebase.storage()
.ref()
2021-03-02 06:26:45 +00:00
.child(fullPath)
.getDownloadURL()
.then((url) => {
const wallpaper = {
fullPath,
name,
url,
};
commit('ADD_WALLPAPER', wallpaper);
resolve();
})
.catch(reject);
}
})
2020-08-25 04:23:22 +00:00
.catch(reject);
});
},
2020-08-21 07:43:30 +00:00
// set merge to true when deleting lists
2020-08-21 06:50:06 +00:00
SAVE_BOARD({ state }, merge = false) {
2020-08-21 06:13:45 +00:00
const db = firebase.firestore();
return new Promise((resolve, reject) => {
db.collection('boards')
.doc(state.board.id)
2020-08-21 06:50:06 +00:00
.set(state.board, { merge })
2020-08-21 06:13:45 +00:00
.then(() => {
resolve();
})
.catch(reject);
});
},
2020-08-18 18:56:10 +00:00
DELETE_BOARD({ commit }, id) {
return new Promise((resolve, reject) => {
const db = firebase.firestore();
2021-03-02 06:26:45 +00:00
db.collection('boards')
.doc(id)
.delete()
2020-08-18 18:56:10 +00:00
.then(() => {
commit('REMOVE_BOARD', id);
resolve();
})
.catch(reject);
});
},
2020-11-05 04:57:48 +00:00
LOAD_GAMES({ state, commit }, gameList) {
2020-08-21 06:13:45 +00:00
return new Promise((resolve, reject) => {
2020-10-21 16:26:00 +00:00
axios.get(`${API_BASE}/games?games=${gameList}&token=${state.twitchToken.access_token}`)
2020-08-21 06:13:45 +00:00
.then(({ data }) => {
commit('CACHE_GAME_DATA', data);
resolve();
}).catch(reject);
});
},
2020-11-05 04:57:48 +00:00
SEARCH_BOARD_GAMES({ commit, state }, searchText) {
2020-08-21 06:14:13 +00:00
const platforms = state.board.platforms.join(',');
2020-08-21 06:13:45 +00:00
return new Promise((resolve, reject) => {
2020-10-21 16:26:00 +00:00
axios.get(`${API_BASE}/search?search=${searchText}&platform=${platforms}&token=${state.twitchToken.access_token}`)
2020-11-05 04:57:48 +00:00
.then(({ data }) => {
commit('SET_SEARCH_RESULTS', data);
commit('CACHE_GAME_DATA', data);
resolve();
}).catch(reject);
});
},
CUSTOM_SEARCH({ commit, state }, { platforms, sortQuery }) {
2021-01-08 06:52:45 +00:00
return new Promise((resolve, reject) => {
axios.get(`${API_BASE}/customSearch?platforms=${platforms}&sortQuery=${sortQuery}&token=${state.twitchToken.access_token}`)
2021-01-08 06:52:45 +00:00
.then(({ data }) => {
commit('CACHE_GAME_DATA', data);
resolve(data);
}).catch(reject);
});
},
2020-11-18 05:55:54 +00:00
// SEARCH_GAMES({ commit, state }, { searchText, platforms, sortField, sortOrder }) {
2021-03-02 06:26:45 +00:00
// TODO: user CUSTOM_SEARCH
2020-11-18 05:55:54 +00:00
SEARCH_GAMES({ commit, state }, { searchText, platforms }) {
2020-11-05 04:57:48 +00:00
return new Promise((resolve, reject) => {
axios.get(`${API_BASE}/search?search=${searchText}&platform=${platforms}&token=${state.twitchToken.access_token}`)
2020-08-21 06:13:45 +00:00
.then(({ data }) => {
commit('SET_SEARCH_RESULTS', data);
commit('CACHE_GAME_DATA', data);
resolve();
}).catch(reject);
});
},
2020-09-09 20:38:52 +00:00
SAVE_TAGS({ state }, tags) {
2019-12-03 18:26:19 +00:00
const db = firebase.firestore();
return new Promise((resolve, reject) => {
2021-03-02 06:26:45 +00:00
db.collection('tags')
.doc(state.user.uid)
.set(tags, { merge: true })
2019-12-03 18:26:19 +00:00
.then(() => resolve())
.catch(reject);
});
},
2020-10-21 23:03:09 +00:00
GET_TWITCH_TOKEN({ commit }) {
2020-10-15 23:15:09 +00:00
const db = firebase.firestore();
return new Promise((resolve, reject) => {
db.collection('app')
.doc('twitch')
.get()
.then((doc) => {
commit('SET_TWITCH_TOKEN', doc.data());
2020-10-31 17:43:12 +00:00
resolve();
2020-10-15 23:15:09 +00:00
})
.catch(reject);
});
},
2020-09-06 14:53:04 +00:00
SAVE_TAGS_NO_MERGE({ state }, tags) {
2019-12-03 18:26:19 +00:00
const db = firebase.firestore();
return new Promise((resolve, reject) => {
2021-03-02 06:26:45 +00:00
db.collection('tags')
.doc(state.user.uid)
.set(tags, { merge: false })
2019-12-03 18:26:19 +00:00
.then(() => resolve())
.catch(reject);
});
},
2020-08-25 04:23:22 +00:00
SAVE_SETTINGS({ commit, state }, settings) {
const db = firebase.firestore();
return new Promise((resolve, reject) => {
2021-03-02 06:26:45 +00:00
db.collection('settings')
.doc(state.user.uid)
.set(settings, { merge: true })
2020-08-25 04:23:22 +00:00
.then(() => {
commit('SET_SETTINGS', settings);
resolve();
})
.catch(reject);
});
},
2020-08-22 12:22:32 +00:00
// TODO: combine into single action
SAVE_NOTES({ state }) {
return new Promise((resolve, reject) => {
const db = firebase.firestore();
2021-03-02 06:26:45 +00:00
db.collection('notes')
.doc(state.user.uid)
.set(state.notes, { merge: true })
2020-08-22 12:22:32 +00:00
.then(() => resolve())
.catch(reject);
});
},
SAVE_NOTES_NO_MERGE({ state }) {
return new Promise((resolve, reject) => {
const db = firebase.firestore();
2021-03-02 06:26:45 +00:00
db.collection('notes')
.doc(state.user.uid)
.set(state.notes, { merge: false })
2020-08-22 12:22:32 +00:00
.then(() => resolve())
.catch(reject);
});
},
// TODO: combine into single action
SAVE_PROGRESSES_NO_MERGE({ state }) {
return new Promise((resolve, reject) => {
const db = firebase.firestore();
2021-03-02 06:26:45 +00:00
db.collection('progresses')
.doc(state.user.uid)
.set(state.progresses, { merge: false })
2020-08-22 12:22:32 +00:00
.then(() => resolve())
.catch(reject);
});
},
SAVE_PROGRESSES({ state }) {
return new Promise((resolve, reject) => {
const db = firebase.firestore();
2021-03-02 06:26:45 +00:00
db.collection('progresses')
.doc(state.user.uid)
.set(state.progresses, { merge: true })
2020-08-22 12:22:32 +00:00
.then(() => resolve())
.catch(reject);
});
},
// TODO: use firebase email extension instead
SEND_WELCOME_EMAIL(context, additionalUserInfo) {
return new Promise((resolve, reject) => {
if (additionalUserInfo && additionalUserInfo.profile) {
axios.get(`${API_BASE}/email?address=${additionalUserInfo.profile.email}&template_id=welcome`)
.catch(reject);
} else {
reject();
}
});
},
2020-09-01 22:38:59 +00:00
LOAD_RELEASES({ commit }) {
2019-11-08 19:56:03 +00:00
return new Promise((resolve, reject) => {
axios.get('https://api.github.com/repos/romancm/gamebrary/releases')
.then(({ data }) => {
2020-09-01 22:38:59 +00:00
commit('SET_RELEASES', data);
resolve(data);
2020-08-15 00:02:34 +00:00
}).catch(reject);
});
},
LOAD_GITHUB_REPOSITORY() {
return new Promise((resolve, reject) => {
axios.get('https://api.github.com/repos/romancm/gamebrary')
.then(({ data }) => {
resolve(data);
}).catch(reject);
});
},
LOAD_GITHUB_README() {
2020-08-15 00:02:34 +00:00
return new Promise((resolve, reject) => {
axios.get('https://raw.githubusercontent.com/romancm/gamebrary/master/README.md')
.then(({ data }) => {
resolve(data);
2019-11-08 19:56:03 +00:00
}).catch(reject);
});
},
2019-02-08 05:46:29 +00:00
2020-10-21 16:26:00 +00:00
LOAD_GAME({ state }, gameId) {
2019-11-08 19:56:03 +00:00
return new Promise((resolve, reject) => {
2020-10-21 16:26:00 +00:00
axios.get(`${API_BASE}/game?gameId=${gameId}&token=${state.twitchToken.access_token}`)
2019-11-08 19:56:03 +00:00
.then(({ data }) => {
2020-10-21 23:03:09 +00:00
const [game] = data;
2020-08-15 00:02:34 +00:00
2020-10-21 23:03:09 +00:00
resolve(game);
}).catch(reject);
2019-11-08 19:56:03 +00:00
});
},
2018-10-19 05:15:28 +00:00
};