1
Fork 0

Externalised reverser into helper function

This commit is contained in:
Jake Howard 2016-01-18 18:35:50 +00:00
parent 37f5e1cfef
commit 36ff669a00
2 changed files with 23 additions and 21 deletions

View file

@ -1,4 +1,5 @@
import React from 'react';
import Reverser from '../../helpers/reverser';
var ReactBootstrap = require('react-bootstrap');
var Col = ReactBootstrap.Col;
@ -13,28 +14,11 @@ export default class ProjectImage extends React.Component {
if (this.props.data.link.startsWith('#')) {
return;
}
fetch('/reverse/', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
ident: this.props.data.link,
})
}).then(function (response) {
if (response.status === 302) {
return response;
} else {
var error = new Error(response.statusText);
error.response = response;
throw error;
}
}).then(
(response) => response.json()
).then(function (url) {
Reverser(this.props.data.link)
.then(function (url) {
this.setState({ url });
}.bind(this)).catch(function (error) {
}.bind(this))
.catch(function (error) {
console.log('Got this error:', error);
});
}

View file

@ -0,0 +1,18 @@
export default function Reverser(ident) {
return fetch('/reverse/', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ident})
}).then(function (response) {
if (response.status === 302) {
return response;
} else {
var error = new Error(response.statusText);
error.response = response;
throw error;
}
}).then((response) => response.json());
}