@@ -36,10 +38,20 @@ class App extends Component {
);
}
- _onClickLatest(selectedOptions) {
+ _onSelectLatest(selectedOptions) {
this.setState({options: Object.assign({}, selectedOptions)});
}
+ _onRemoveLatest(item) {
+ removeLatest(item)
+ .then(() => {
+ const latest = this.state.latest
+ .filter(curr => curr.id !== item.id);
+
+ this.setState({ latest });
+ });
+ }
+
_onClear() {
window.location.reload();
}
diff --git a/client/src/LatestList.js b/client/src/LatestList.js
index 7240ba7..22c5dbc 100644
--- a/client/src/LatestList.js
+++ b/client/src/LatestList.js
@@ -6,14 +6,16 @@ export default function(props) {
const created = new Date(item.created);
return (
- props.clickHandler(item)}>Brief vom {created.toLocaleString()}
+ props.onSelect(item)}>{created.toLocaleString()}
+
+ props.onRemove(item)}>(Remove)
);
});
return (
-
Vergangene:
+
Vergangene Briefe:
);
diff --git a/client/src/apiHelper.js b/client/src/apiHelper.js
index 1193911..45d3214 100644
--- a/client/src/apiHelper.js
+++ b/client/src/apiHelper.js
@@ -1,23 +1,32 @@
+function checkStatus(res) {
+ if (res.status < 200 || res.status >= 400) {
+ throw new Error(`Something went wrong (Status ${res.status}) - I do feel very sorry!`);
+ }
+
+ return res;
+}
+
export function generatePdf(state){
- return fetch('/api/pdf/generate/brief', {
+ const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(state)
- }).then(function(res) {
- if (res.status !== 200) {
- throw new Error(`Something went wrong (Status ${res.status}) - I do feel very sorry!`);
- }
- return res.json();
- });
+ };
+
+ return fetch('/api/pdf/generate/brief', options)
+ .then(checkStatus)
+ .then(res => res.json());
}
export function getLatest() {
- return fetch('/api/pdf/latest').then(function(res) {
- if (res.status !== 200) {
- throw new Error(`Something went wrong (Status ${res.status}) - Cannot get latest`);
- }
- return res.json();
- });
+ return fetch('/api/pdf/latest')
+ .then(checkStatus)
+ .then(res => res.json());
+}
+
+export function removeLatest(item) {
+ return fetch(`/api/pdf/latest/${item.id}`, {method: 'DELETE'})
+ .then(checkStatus);
}
diff --git a/server/index.js b/server/index.js
index 7206a54..75526bc 100644
--- a/server/index.js
+++ b/server/index.js
@@ -25,27 +25,50 @@ app.post('/api/pdf/generate/:template', (req, res) => {
.then(() => id);
})
.then(id => {
- res.send({id: id});
- res.end();
+ res
+ .status(200)
+ .json({id: id});
})
.catch((err) => {
console.error('Error:', err, 'for', req.url);
- res.sendStatus(500).end(err);
+ res
+ .status(500)
+ .json({error: err.toString()});
});
});
app.get('/api/pdf/latest', (req, res) => {
store
.list()
- .then(results => res.json(results))
- .catch(err => res.sendStatus(500).end(err));
+ .then(results => res
+ .status(200)
+ .json(results)
+ )
+ .catch(err => res
+ .status(500)
+ .json({error: err.toString()})
+ );
+});
+
+app.delete('/api/pdf/latest/:id', (req, res) => {
+ const { id } = req.params;
+ console.log(`Deleting ${id}...`);
+ store
+ .remove(id)
+ .then(() => res.sendStatus(202))
+ .catch(err => res
+ .status(500)
+ .json({error: err.toString()})
+ );
});
app.options('/api/pdf/:id');
app.get('/api/pdf/:id', (req, res) => {
const {id} = req.params;
- res.sendFile(getPdfPath(id));
+ res
+ .status(200)
+ .sendFile(getPdfPath(id));
});
app.use(express.static('../client/build'));
diff --git a/server/store.js b/server/store.js
index d3a46ce..e18c75c 100644
--- a/server/store.js
+++ b/server/store.js
@@ -9,12 +9,14 @@ const sortBy = require('lodash.sortby');
const list = promisify(store.list);
const load = promisify(store.load);
const add = promisify(store.add);
+const remove = promisify(store.remove);
module.exports = {
list: () => list()
.then(result => sortBy(result, 'created').reverse()),
load: id => load(id),
add: item => add(item),
+ remove: id => remove(id),
}