Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
k-nasa committed Aug 14, 2019
1 parent 662f06c commit 9886f04
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ const MainConponent = props => {
render={() => <AddArticle user={props.user} />}
/>

<Route path="/index_articles" component={IndexArticle} />
<Route path="/index_articles" render={ () => <IndexArticle user={props.user} />} />
<Route path="/article/:id" component={ShowArticle} />
<Route path="/update_article/:id" render={(p) => <UpdateArticle user={props.user} p={p} /> } />
</div>
</Router>
);
Expand Down
21 changes: 21 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ export const postArticle = (idToken, title, body) => {
});
};

export const DeleteArticle = (id, idToken) => {
console.log(idToken)
return fetch(`${API_ENDPOINT}/articles/${id}`, {
method: "delete",
headers: new Headers({
Authorization: `Bearer ${idToken}`
}),
mode: "cors",
headers: {
"Access-Control-Request-Method": "DELETE"
},
credentials: "same-origin"
}).then(res => {
if (res.ok) {
return res.json();
} else {
throw Error(`Request rejected with status ${res.status}`);
}
});
}

export const getPublicMessage = function() {
return fetch(`${API_ENDPOINT}/public`);
};
19 changes: 18 additions & 1 deletion src/pages/index_article.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { useState, useEffect } from "react";
import { getArticles } from "../api";
import { getArticles, DeleteArticle } from "../api";
import { Link } from "react-router-dom";

const IndexArticles = props => {
Expand All @@ -17,6 +17,21 @@ const IndexArticles = props => {
});
}, []);

const deleteArticle = id => {
props.user
.getIdToken()
.then(token => {
return DeleteArticle(id, token);
})
.then(resp => {
window.location.reload();
})
.catch(e => {
setErrorMessage(e.toString());
});
};


return (
<div>
<p>{errorMessage}</p>
Expand All @@ -26,6 +41,8 @@ const IndexArticles = props => {
<div key={i}>
<Link to={`/article/${article.id}`}><li>{article.title}</li></Link>
<p>{article.body} </p>
<a onClick={() => deleteArticle(article.id)}>削除</a>
<Link to={`/update_article/${article.id}`}>編集</Link>
</div>
);
})
Expand Down

0 comments on commit 9886f04

Please sign in to comment.