Skip to content

Commit

Permalink
#23 Add boilerplate to CommentEditForm
Browse files Browse the repository at this point in the history
  • Loading branch information
SandraBergstrom committed Jun 28, 2023
1 parent 0f93346 commit ce21e39
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/pages/comments/CommentEditForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React, { useState } from "react";

import Form from "react-bootstrap/Form";
import { axiosRes } from "../../api/axiosDefaults";

import styles from "../../styles/CommentCreateEditForm.module.css";

function CommentEditForm(props) {
const { id, content, setShowEditForm, setComments } = props;

const [formContent, setFormContent] = useState(content);

const handleChange = (event) => {
setFormContent(event.target.value);
};

const handleSubmit = async (event) => {
event.preventDefault();
try {
await axiosRes.put(`/comments/${id}/`, {
content: formContent.trim(),
});
setComments((prevComments) => ({
...prevComments,
results: prevComments.results.map((comment) => {
return comment.id === id
? {
...comment,
content: formContent.trim(),
updated_at: "now",
}
: comment;
}),
}));
setShowEditForm(false);
} catch (err) {
console.log(err);
}
};

return (
<Form onSubmit={handleSubmit}>
<Form.Group className="pr-1">
<Form.Control
className={styles.Form}
as="textarea"
value={formContent}
onChange={handleChange}
rows={2}
/>
</Form.Group>
<div className="text-right">
<button
className={styles.Button}
onClick={() => setShowEditForm(false)}
type="button"
>
cancel
</button>
<button
className={styles.Button}
disabled={!content.trim()}
type="submit"
>
save
</button>
</div>
</Form>
);
}

export default CommentEditForm;

0 comments on commit ce21e39

Please sign in to comment.