Commit d6063666 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Rewrite url.js content envelope to use exceptions

Return empty object when response statuscode is 204

Change network handling in remaing pizza sagas

Small styling changes in pizzas

Remove log commands and revert apiRequest usage
parent 5ce68854
......@@ -54,8 +54,9 @@ const styles = StyleSheet.create({
paddingBottom: 6,
},
card: {
elevation: 2,
backgroundColor: colors.white,
borderRadius: 2,
elevation: 2,
},
orderStatus: {
fontFamily: 'sans-serif-medium',
......@@ -65,7 +66,6 @@ const styles = StyleSheet.create({
borderBottomWidth: 1,
borderTopRightRadius: 2,
borderTopLeftRadius: 2,
margin: 2,
},
paidStatus: {
backgroundColor: colors.lightGreen,
......@@ -80,9 +80,9 @@ const styles = StyleSheet.create({
justifyContent: 'space-between',
alignItems: 'center',
paddingLeft: 16,
paddingRight: 8,
paddingTop: 10,
paddingBottom: 10,
paddingRight: 16,
paddingTop: 8,
paddingBottom: 8,
borderBottomWidth: 1,
borderBottomColor: colors.dividerGrey,
},
......@@ -95,7 +95,7 @@ const styles = StyleSheet.create({
fontSize: 14,
},
pizzaDescription: {
fontFamily: 'sans-serif-medium',
fontFamily: 'sans-serif-regular',
color: colors.gray,
fontSize: 14,
marginTop: 2,
......@@ -109,7 +109,7 @@ const styles = StyleSheet.create({
button: {
backgroundColor: colors.magenta,
padding: 8,
margin: 8,
marginLeft: 16,
borderRadius: 2,
elevation: 2,
},
......
......@@ -20,23 +20,13 @@ const event = function* event(action) {
};
try {
let response = yield call(apiRequest, `events/${pk}`, data);
const eventData = response.content;
if (!response.success) {
throw Error();
}
const eventData = yield call(apiRequest, `events/${pk}`, data);
const params = {
status: 'registered',
};
response = yield call(apiRequest, `events/${pk}/registrations`, data, params);
const eventRegistrations = response.content;
if (!response.success) {
throw Error();
}
const eventRegistrations = yield call(apiRequest, `events/${pk}/registrations`, data, params);
yield put(eventActions.success(
eventData,
......
......@@ -29,10 +29,7 @@ const login = function* login(action) {
};
try {
let response = yield call(apiRequest, 'token-auth', data);
const { token } = response.content;
if (!response.success) {
throw Error();
}
const { token } = response;
data = {
method: 'GET',
headers: {
......@@ -42,11 +39,8 @@ const login = function* login(action) {
},
};
response = yield call(apiRequest, 'members/me', data);
if (!response.success) {
throw Error();
}
const displayName = response.content.display_name;
const avatar = response.content.photo === null ? defaultAvatar : response.content.photo;
const displayName = response.display_name;
const avatar = response.photo === null ? defaultAvatar : response.photo;
yield call(AsyncStorage.multiSet, [
[USERNAMEKEY, user],
[TOKENKEY, token],
......
import { call, takeEvery, put } from 'redux-saga/effects';
import { call, put, takeEvery } from 'redux-saga/effects';
import { apiRequest } from '../url';
import * as pizzaActions from '../actions/pizza';
......@@ -22,32 +22,24 @@ const retrievePizzaInfo = function* retrievePizzaInfo(action) {
};
try {
let response = yield call(apiRequest, 'pizzas/event', data);
const event = response.content;
if (response.status === NOT_FOUND) {
yield put(pizzaActions.success(null, null, []));
} else if (!response.success) {
throw Error();
} else {
response = yield call(apiRequest, 'pizzas', data);
const pizzaList = response.content;
if (!response.success) {
throw Error();
}
response = yield call(apiRequest, 'pizzas/orders/me', data);
const order = response.content;
if (response.status === NOT_FOUND) {
const event = yield call(apiRequest, 'pizzas/event', data);
const pizzaList = yield call(apiRequest, 'pizzas', data);
try {
const order = yield call(apiRequest, 'pizzas/orders/me', data);
yield put(pizzaActions.success(event, order, pizzaList));
} catch (error) {
if (error.response !== null && error.response.status === NOT_FOUND) {
yield put(pizzaActions.success(event, null, pizzaList));
} else if (!response.success) {
yield put(pizzaActions.failure());
} else {
yield put(pizzaActions.success(event, order, pizzaList));
yield put(pizzaActions.failure());
}
}
} catch (error) {
yield put(pizzaActions.failure());
if (error.response !== null && error.response.status === NOT_FOUND) {
yield put(pizzaActions.success(null, null, []));
} else {
yield put(pizzaActions.failure());
}
}
};
......@@ -63,12 +55,8 @@ const cancel = function* cancel(action) {
};
try {
const response = yield call(apiRequest, 'pizzas/orders/me', data);
if (response.success) {
yield put(pizzaActions.cancelSuccess());
} else {
yield put(pizzaActions.failure());
}
yield call(apiRequest, 'pizzas/orders/me', data);
yield put(pizzaActions.cancelSuccess());
} catch (error) {
yield put(pizzaActions.failure());
}
......@@ -90,12 +78,8 @@ const order = function* order(action) {
const route = hasOrder ? 'pizzas/orders/me' : 'pizzas/orders';
try {
const response = yield call(apiRequest, route, data);
if (response.success) {
yield put(pizzaActions.orderSuccess(response.content));
} else {
yield put(pizzaActions.failure());
}
const orderData = yield call(apiRequest, route, data);
yield put(pizzaActions.orderSuccess(orderData));
} catch (error) {
yield put(pizzaActions.failure());
}
......
......@@ -28,7 +28,11 @@ const register = function* register(action) {
}),
};
yield call(apiRequest, 'devices', data);
try {
yield call(apiRequest, 'devices', data);
} catch (err) {
// eat error, om nom nom
}
};
const invalidate = function* invalidate() {
......
......@@ -8,7 +8,13 @@ export const apiUrl = `${server}/api/v1`;
export const pizzaUrl = 'https://pizza.thalia.nu';
export const tokenSelector = state => state.session.token;
const NO_CONTENT = 204;
export class ServerError extends Error {
constructor(message, response) {
super(message);
this.name = this.constructor.name;
this.response = response;
}
}
export const apiRequest = (route, fetchOpts, params) => {
let query = '';
......@@ -17,18 +23,13 @@ export const apiRequest = (route, fetchOpts, params) => {
.map(k => `${encodeURIComponent(k)} = ${encodeURIComponent(params[k])}`)
.join('&')}`;
}
return fetch(`${apiUrl}/${route}/${query}`, fetchOpts).then((response) => {
if (response.status === NO_CONTENT) {
return {
success: response.ok,
content: null,
status: response.status,
};
}
return response.json().then(json => ({
success: response.ok,
content: json,
status: response.status,
}));
});
return fetch(`${apiUrl}/${route}/${query}`, fetchOpts)
.then((response) => {
if (response.status >= 400 && response.status <= 500) {
throw new ServerError(`Invalid status code: ${response.status}`, response);
} else if (response.status === 204) {
return {};
}
return response.json();
});
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment