diff --git a/delia/src/components/structures/anecdote/Anecdote.jsx b/delia/src/components/structures/anecdote/Anecdote.jsx index 28fc90e..86214f1 100644 --- a/delia/src/components/structures/anecdote/Anecdote.jsx +++ b/delia/src/components/structures/anecdote/Anecdote.jsx @@ -67,20 +67,24 @@ export default function Anecdote() { }, [user, room, allowed]); const saveAnecdote = (title, anecdote) => { - axios({ - method: 'post', - url: '/api/ancdt/save', - data: { - title: title, - body: anecdote, - token: user.token, - room: room, - iteration: iteration, - } - }).then(res => { - // todo: saved - }).catch((err) =>{ - // todo: err + return new Promise(function(resolve, reject) { + axios({ + method: 'post', + url: '/api/ancdt/save', + data: { + title: title, + body: anecdote, + token: user.token, + room: room, + iteration: iteration, + } + }).then(res => { + if(res.status === 200) { + resolve(); + } + }).catch((err) =>{ + reject(err); + }); }); } diff --git a/delia/src/components/views/anecdote/AnecdoteEditer.jsx b/delia/src/components/views/anecdote/AnecdoteEditer.jsx index 3df60a5..1608dc9 100644 --- a/delia/src/components/views/anecdote/AnecdoteEditer.jsx +++ b/delia/src/components/views/anecdote/AnecdoteEditer.jsx @@ -7,7 +7,8 @@ export default class AnecdoteEditer extends Component { super(props); this.title = createRef(); this.anecdote = createRef(); - + this.state = {button: true, buttonText: "Up to date !"} + this.state = { title: props?.anecdote?.title ?? "", body: props?.anecdote?.body ?? "", @@ -18,19 +19,29 @@ export default class AnecdoteEditer extends Component { } handleChange(e) { - this.setState({ ...this.state, [e.target.name]: e.target.value }); + this.setState({ ...this.state, [e.target.name]: e.target.value, button: false, buttonText: "Send" }); } - handleClick() { - this.props.saveAnecdote(this.title?.current?.value, this.anecdote?.current?.value) + handleClick(e) { + this.setState({ ...this.state, button: true }); + e.target.innerText = "Sending..."; + this.props.saveAnecdote(this.title?.current?.value, this.anecdote?.current?.value).then(() => { + e.target.innerText = "Up to date !"; + }).catch(err => { + if (err.response.status == 403) { + e.target.innerText = "You are after the deadline..."; + } else { + this.setState({ ...this.state, button: false }); + e.target.innerText = "Error, contact an administrator or try again"; + } + }) } setDefaultValues(ancdt) { - this.setState({...this.state, title: ancdt.title, body: ancdt.body}) + this.setState({...this.state, title: ancdt.title, body: ancdt.body, button: true, buttonText: "Up to date !"}) } render() { - return ( Your anecdote @@ -57,8 +68,8 @@ export default class AnecdoteEditer extends Component { -