@@ -94,6 +94,20 @@ const NewsEditor = props => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button type='submit'>Update</button>
|
<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>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -125,4 +139,21 @@ const handleSubmit = async (title, author, body, index, tokenFetch) => {
|
|||||||
return [null];
|
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;
|
export default NewsEditor;
|
||||||
@@ -69,9 +69,9 @@ const TokenProvider = props => {
|
|||||||
|
|
||||||
//finally, delegate to fetch
|
//finally, delegate to fetch
|
||||||
return fetch(url, {
|
return fetch(url, {
|
||||||
...options,
|
...(options || {}),
|
||||||
headers: {
|
headers: {
|
||||||
...options.headers,
|
...(options || { headers: {} }).headers,
|
||||||
'Authorization': `Bearer ${bearer}`
|
'Authorization': `Bearer ${bearer}`
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user