2017-07-20 20:52:46 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
|
import omit from 'lodash/omit';
|
2018-03-23 19:13:08 +00:00
|
|
|
import encodeParams from 'client/libs/encodeParams';
|
2017-07-20 20:52:46 +00:00
|
|
|
|
|
|
|
|
export async function createChallenge (store, payload) {
|
2017-07-31 19:54:52 +00:00
|
|
|
let response = await axios.post('/api/v3/challenges', payload.challenge);
|
2017-08-07 20:26:17 +00:00
|
|
|
let newChallenge = response.data.data;
|
2017-07-20 20:52:46 +00:00
|
|
|
|
2017-08-07 20:26:17 +00:00
|
|
|
store.state.user.data.challenges.push(newChallenge._id);
|
|
|
|
|
|
|
|
|
|
return newChallenge;
|
2017-07-20 20:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-30 15:23:20 +00:00
|
|
|
export async function cloneChallenge (store, payload) {
|
|
|
|
|
const response = await axios.post(`/api/v3/challenges/${payload.cloningChallengeId}/clone`, payload.challenge);
|
|
|
|
|
const newChallenge = response.data.data.clonedChallenge;
|
|
|
|
|
store.state.user.data.challenges.push(newChallenge._id);
|
|
|
|
|
return newChallenge;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 20:52:46 +00:00
|
|
|
export async function joinChallenge (store, payload) {
|
|
|
|
|
let response = await axios.post(`/api/v3/challenges/${payload.challengeId}/join`);
|
|
|
|
|
|
|
|
|
|
return response.data.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function leaveChallenge (store, payload) {
|
2017-09-22 21:47:16 +00:00
|
|
|
let url = `/api/v3/challenges/${payload.challengeId}/leave`;
|
|
|
|
|
let response = await axios.post(url, {
|
|
|
|
|
keep: payload.keep,
|
2017-07-20 20:52:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return response.data.data;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-17 17:03:32 +00:00
|
|
|
export async function getUserChallenges (store, payload) {
|
|
|
|
|
let url = '/api/v3/challenges/user';
|
2018-03-23 19:13:08 +00:00
|
|
|
let {
|
|
|
|
|
member,
|
|
|
|
|
page,
|
|
|
|
|
search,
|
|
|
|
|
categories,
|
|
|
|
|
owned,
|
|
|
|
|
} = payload;
|
|
|
|
|
|
|
|
|
|
let query = {};
|
|
|
|
|
if (member) query.member = member;
|
2018-03-30 16:34:00 +00:00
|
|
|
if (page >= 0) query.page = page;
|
2018-03-23 19:13:08 +00:00
|
|
|
if (search) query.search = search;
|
|
|
|
|
if (categories) query.categories = categories;
|
|
|
|
|
if (owned) query.owned = owned;
|
|
|
|
|
|
|
|
|
|
const parms = encodeParams(query);
|
|
|
|
|
const response = await axios.get(`${url}?${parms}`);
|
|
|
|
|
|
2017-07-20 20:52:46 +00:00
|
|
|
return response.data.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getGroupChallenges (store, payload) {
|
|
|
|
|
let response = await axios.get(`/api/v3/challenges/groups/${payload.groupId}`);
|
|
|
|
|
|
|
|
|
|
return response.data.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getChallenge (store, payload) {
|
|
|
|
|
let response = await axios.get(`/api/v3/challenges/${payload.challengeId}`);
|
|
|
|
|
|
|
|
|
|
return response.data.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function exportChallengeCsv (store, payload) {
|
2017-08-07 20:26:17 +00:00
|
|
|
let response = await axios.get(`/api/v3/challenges/${payload.challengeId}/export/csv`);
|
2017-07-20 20:52:46 +00:00
|
|
|
|
|
|
|
|
return response.data.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function updateChallenge (store, payload) {
|
|
|
|
|
let challengeDataToSend = omit(payload.challenge, ['tasks', 'habits', 'todos', 'rewards', 'group']);
|
|
|
|
|
|
|
|
|
|
if (challengeDataToSend.leader && challengeDataToSend.leader._id) challengeDataToSend.leader = challengeDataToSend.leader._id;
|
|
|
|
|
|
2017-07-31 19:54:52 +00:00
|
|
|
let response = await axios.put(`/api/v3/challenges/${payload.challenge._id}`, challengeDataToSend);
|
2017-07-20 20:52:46 +00:00
|
|
|
|
|
|
|
|
return response.data.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteChallenge (store, payload) {
|
2017-08-07 20:26:17 +00:00
|
|
|
let response = await axios.delete(`/api/v3/challenges/${payload.challengeId}`);
|
2017-07-20 20:52:46 +00:00
|
|
|
|
|
|
|
|
return response.data.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function selectChallengeWinner (store, payload) {
|
2017-08-07 20:26:17 +00:00
|
|
|
let response = await axios.post(`/api/v3/challenges/${payload.challengeId}/selectWinner/${payload.winnerId}`);
|
2017-07-20 20:52:46 +00:00
|
|
|
|
|
|
|
|
return response.data.data;
|
|
|
|
|
}
|