This repository has been archived on 2021-07-29. You can view files and clone it, but cannot push or open issues or pull requests.
find_coach/src/store/modules/coaches/actions.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-07-28 16:36:52 +02:00
import getAPI from '../../../scripts/axios-api';
2021-06-29 21:55:19 +02:00
export default {
2021-07-28 16:36:52 +02:00
async registerCoach(context, data) {
const userId = context.rootGetters.userId;
2021-06-29 21:55:19 +02:00
const coachData = {
2021-06-29 22:05:56 +02:00
id: context.rootGetters.userId,
2021-06-29 21:55:19 +02:00
firstName: data.first,
lastName: data.last,
description: data.desc,
2021-06-29 21:58:27 +02:00
hourlyRate: data.rate,
2021-06-29 21:55:19 +02:00
areas: data.areas
};
2021-07-28 16:36:52 +02:00
try {
await getAPI.put(`coaches/${userId}.json`, coachData);
context.commit('registerCoach', {
...coachData,
id: userId
});
} catch (err) {
console.log(err.message);
}
},
2021-07-28 20:51:47 +02:00
async loadCoaches(context, payload) {
if (!payload.forceRefresh && !context.getters.shouldUpdate) {
return;
}
try {
const response = await getAPI.get('coaches.json');
const responseData = response.data;
const coaches = [];
for (const key in responseData) {
const coach = {
id: key,
firstName: responseData[key].firstName,
lastName: responseData[key].lastName,
description: responseData[key].description,
hourlyRate: responseData[key].hourlyRate,
areas: responseData[key].areas
};
coaches.push(coach);
}
context.commit('setCoaches', coaches);
2021-07-28 20:51:47 +02:00
context.commit('setFetchTimestamp');
2021-07-28 16:36:52 +02:00
} catch (err) {
const error = new Error(err.message || 'Failed to fetch!');
throw error;
2021-07-28 16:36:52 +02:00
}
2021-06-29 21:55:19 +02:00
}
};