jam-cloud/jam-ui/src/helpers/rest.js

314 lines
8.7 KiB
JavaScript

import { error } from 'is_js';
import apiFetch from './apiFetch';
export const getMusicians = page => {
return new Promise((resolve, reject) => {
apiFetch(`/search/musicians?results=true`)
.then(response => resolve(response))
.catch(error => reject(error));
});
};
// export const getPeople = (page) => {
// return new Promise((resolve, reject) => {
// apiFetch(`/filter?page=${page}`)
// .then(response => resolve(response))
// .catch(error => reject(error))
// })
// }
export const getPersonById = id => {
return new Promise((resolve, reject) =>
apiFetch(`/users/${id}/profile`)
.then(response => resolve(response))
.catch(error => reject(error))
);
};
export const getUserDetails = options => {
const { id, ...rest } = options;
return new Promise((resolve, reject) =>
apiFetch(`/users/${id}?${new URLSearchParams(rest)}`)
.then(response => resolve(response))
.catch(error => reject(error))
);
}
export const getPeople = ({ data, offset, limit } = {}) => {
return new Promise((resolve, reject) => {
apiFetch(`/filter?offset=${offset}&limit=${limit}`, {
method: 'POST',
body: JSON.stringify(data)
})
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getPeopleByIds = ({ userId, ids }) => {
return new Promise((resolve, reject) => {
apiFetch(`/users/${userId}/filter_by_ids`, {
method: 'POST',
body: JSON.stringify({ user_ids: ids })
})
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getPeopleIndex = () => {
return new Promise((resolve, reject) => {
apiFetch(`/users`)
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getLobbyUsers = () => {
return new Promise((resolve, reject) => {
apiFetch(`/users/lobby`)
.then(response => resolve(response))
.catch(error => reject(error))
})
}
export const updateUser = (id, data) => {
return new Promise((resolve, reject) => {
apiFetch(`/users/${id}`, {
method: 'POST',
body: JSON.stringify(data)
})
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getGenres = () => {
return new Promise((resolve, reject) => {
apiFetch('/genres')
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getInstruments = () => {
return new Promise((resolve, reject) => {
apiFetch('/instruments')
.then(response => resolve(response))
.catch(error => reject(error));
});
};
// export const getCurrentUser = () => {
// return new Promise((resolve, reject) => {
// apiFetch('/me')
// .then(response => resolve(response))
// .catch(error => reject(error))
// })
// }
export const getFriends = userId => {
return new Promise((resolve, reject) => {
apiFetch(`/users/${userId}/friends`)
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const addFriend = (userId, friendId) => {
return new Promise((resolve, reject) => {
apiFetch(`/users/${userId}/friend_requests`, {
method: 'POST',
body: JSON.stringify({ friend_id: friendId })
})
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const removeFriend = (userId, friendId) => {
return new Promise((resolve, reject) => {
apiFetch(`/users/${userId}/friends/${friendId}`, {
method: 'DELETE'
})
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getTextMessages = (options = {}) => {
return new Promise((resolve, reject) => {
apiFetch(`/text_messages?${new URLSearchParams(options)}`)
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const createTextMessage = options => {
return new Promise((resolve, reject) => {
apiFetch(`/text_messages`, {
method: 'POST',
body: JSON.stringify(options)
})
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const createLobbyChatMessage = options => {
return new Promise((resolve, reject) => {
apiFetch(`/chat`, {
method: 'POST',
body: JSON.stringify(options)
})
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getNotifications = (userId, options = {}) => {
return new Promise((resolve, reject) => {
apiFetch(`/users/${userId}/notifications?${new URLSearchParams(options)}`)
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const acceptFriendRequest = (userId, options = {}) => {
return new Promise((resolve, reject) => {
const { status, friend_request_id } = options;
apiFetch(`/users/${userId}/friend_requests/${friend_request_id}`, {
method: 'POST',
body: JSON.stringify({ status })
})
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const deleteNotification = (userId, notificationId) => {
return new Promise((resolve, reject) => {
apiFetch(`/users/${userId}/notifications/${notificationId}`, {
method: 'DELETE'
})
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getSessions = () => {
return new Promise((resolve, reject) => {
apiFetch(`/sessions`)
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getLatencyToUsers = (currentUserId, participantIds) => {
return new Promise((resolve, reject) => {
const query = participantIds.map(id => `user_ids[]=${id}`).join('&');
apiFetch(`/users/${currentUserId}/latencies?${query}`)
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getLobbyChatMessages = (options = {}) => {
return new Promise((resolve, reject) => {
console.log('getLobbyChatMessages', options);
apiFetch(`/chat?${new URLSearchParams(options)}`)
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getCountries = () => {
return new Promise((resolve, reject) => {
apiFetch(`/countries`)
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getRegions = countryId => {
return new Promise((resolve, reject) => {
apiFetch(`/regions?country=${countryId}`)
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const getCities = (countryId, regionId) => {
return new Promise((resolve, reject) => {
apiFetch(`/cities?country=${countryId}&region=${regionId}`)
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const postUpdateAccountEmail = (userId, options) => {
const { email, current_password } = options;
return new Promise((resolve, reject) => {
apiFetch(`/users/${userId}/update_email`, {
method: 'POST',
body: JSON.stringify({ update_email: email, current_password })
})
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const postUpdateAccountPassword = (userId, options) => {
const { new_password, current_password } = options;
return new Promise((resolve, reject) => {
apiFetch(`/users/${userId}/set_password`, {
method: 'POST',
body: JSON.stringify({ old_password: current_password, new_password, new_password_confirm: new_password })
})
.then(response => resolve(response))
.catch(error => reject(error));
});
};
export const requestPasswordReset = (userId) => {
return new Promise((resolve, reject) => {
apiFetch(`/users/${userId}/request_reset_password`, {
method: 'POST',
})
.then(response => resolve(response))
.catch(error => reject(error));
});
}
export const postUserAppInteraction = (userId, options) => {
return new Promise((resolve, reject) => {
apiFetch(`/users/${userId}/app_interactions`, {
method: 'POST',
body: JSON.stringify(options)
})
.then(response => resolve(response))
.catch(error => reject(error));
});
}
export const getSubscription = () => {
return new Promise((resolve, reject) => {
apiFetch('/recurly/get_subscription')
.then(response => resolve(response))
.catch(error => reject(error))
});
}
export const changeSubscription = (plan_code) => {
const options = {plan_code}
return new Promise((resolve, reject) => {
apiFetch('/recurly/change_subscription', {
method: 'POST',
body: JSON.stringify(options)
})
.then(response => resolve(response))
.catch(error => reject(error));
})
}