Skip to content

Commit

Permalink
add information when anecdote correctly saved
Browse files Browse the repository at this point in the history
  • Loading branch information
grimhilt committed Feb 3, 2023
1 parent b8f3103 commit 3a8255b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
32 changes: 18 additions & 14 deletions delia/src/components/structures/anecdote/Anecdote.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}

Expand Down
27 changes: 19 additions & 8 deletions delia/src/components/views/anecdote/AnecdoteEditer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? "",
Expand All @@ -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 (
<Card className="text-center">
<Card.Header>Your anecdote</Card.Header>
Expand All @@ -57,8 +68,8 @@ export default class AnecdoteEditer extends Component {
</Card.Body>

<Card.Footer className="d-grid gap-3">
<Button variant="primary" onClick={this.handleClick}>
Send
<Button ref={this.button} disabled={this.state.button} variant="primary" onClick={this.handleClick}>
{this.state.buttonText}
</Button>
</Card.Footer>
</Card>
Expand Down

0 comments on commit 3a8255b

Please sign in to comment.