Added deletion to news-editor.jsx

Resovled #6
This commit is contained in:
2021-03-12 13:58:14 +11:00
parent 8a920c5316
commit e930fd2173
2 changed files with 33 additions and 2 deletions
+31
View File
@@ -94,6 +94,20 @@ const NewsEditor = props => {
</div>
<button type='submit'>Update</button>
<button type='button' onClick={async evt => {
//onDelete
const [err, result] = await handleDelete(index, authTokens.tokenFetch);
if (err) {
alert(err);
return;
}
if (result) {
titleRef.current.value = authorRef.current.value = bodyRef.current.value = '';
alert(`Article deleted`);
}
}}>Delete</button>
</form>
</div>
);
@@ -125,4 +139,21 @@ const handleSubmit = async (title, author, body, index, tokenFetch) => {
return [null];
};
const handleDelete = async (index, tokenFetch) => {
const conf = confirm('Are you sure you want to delete this article?');
if (conf) {
const result = await tokenFetch(`${process.env.NEWS_URI}/${index}`, {
method: 'DELETE'
});
if (!result.ok) {
const err = `${result.status}: ${await result.text()}`;
return [err, false];
}
}
return [null, conf];
};
export default NewsEditor;