import fetchResource from './FetchResource' export function unpack_api_result(promise, onError) { promise.then(result => { if(result.result !== 'Ok') { onError({ type: 'Error', message: result.message }); } }) .catch(error => { onError({ type: 'Error', message: `${error.status} ${error.message}` }); }); return promise; } export function get_gamenights(token) { return fetchResource('api/gamenights', { method: 'GET', headers: { 'Authorization': `Bearer ${token}` }, }); } export function get_gamenight(gamenight_id, token) { return fetchResource(`api/gamenights/${gamenight_id}`, { method: 'GET', headers: { 'Authorization': `Bearer ${token}` }, }); } export function post_gamenight(input, token) { return fetchResource('api/gamenights', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify(input) }); } export function patch_gamenight(gamenight_id, input, token) { return fetchResource(`api/gamenights/${gamenight_id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify(input) }) } export function delete_gamenight(input, token) { return fetchResource('api/gamenights', { method: 'DELETE', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify(input) }); } export function get_games(token) { return fetchResource('api/games', { method: 'GET', headers: { 'Authorization': `Bearer ${token}` } }); } export function login(body) { return fetchResource('api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }); } export function get_registration_tokens(token) { return fetchResource('api/admin/registration_tokens', { method: 'GET', headers: { 'Authorization': `Bearer ${token}` } }); } export function add_registration_token(token, registration_token) { return fetchResource('api/admin/registration_tokens', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify(registration_token) }); } export function delete_registration_token(token, registration_token_id) { return fetchResource(`api/admin/registration_tokens/${registration_token_id}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}`, } }); }