Skip to content

Commit

Permalink
#14 Add function to create post, alets, redirect to post
Browse files Browse the repository at this point in the history
  • Loading branch information
SandraBergstrom committed Jun 23, 2023
1 parent 8817103 commit 5060bed
Showing 1 changed file with 58 additions and 3 deletions.
61 changes: 58 additions & 3 deletions src/pages/posts/PostCreateForm.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import React, { useState } from "react";
import React, { useRef, useState } from "react";

import Form from "react-bootstrap/Form";
import Button from "react-bootstrap/Button";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
import Container from "react-bootstrap/Container";
import Alert from "react-bootstrap/Alert";

import Upload from "../../assets/upload.png";
import Asset from "../../components/Asset";

import styles from "../../styles/PostCreateEditForm.module.css";
import appStyles from "../../App.module.css";
import btnStyles from "../../styles/Button.module.css";
import alertStyles from "../../styles/AlertMessages.module.css";

import { Image } from "react-bootstrap";
import { useHistory } from "react-router-dom";
import { axiosReq } from "../../api/axiosDefaults";

function PostCreateForm() {
const [errors, setErrors] = useState({});
Expand All @@ -24,6 +29,9 @@ function PostCreateForm() {
});
const { title, content, image } = postData;

const imageInput = useRef(null);
const history = useHistory();

const handleChange = (event) => {
setPostData({
...postData,
Expand All @@ -41,6 +49,25 @@ function PostCreateForm() {
}
};

const handleSubmit = async (event) => {
event.preventDefault();
const formData = new FormData();

formData.append("title", title);
formData.append("content", content);
formData.append("image", imageInput.current.files[0]);

try {
const { data } = await axiosReq.post("/posts/", formData);
history.push(`/posts/${data.id}`);
} catch (err) {
console.log(err);
if (err.response?.status !== 401) {
setErrors(err.response?.data);
}
}
};

const textFields = (
<div className="text-center">
<Form.Group>
Expand All @@ -52,6 +79,15 @@ function PostCreateForm() {
onChange={handleChange}
/>
</Form.Group>
{errors.title?.map((message, idx) => (
<Alert
variant="warning"
key={idx}
className={alertStyles["alert-warning-custom"]}
>
{message}
</Alert>
))}
<Form.Group>
<Form.Label>A few words about this moment...</Form.Label>
<Form.Control
Expand All @@ -62,10 +98,19 @@ function PostCreateForm() {
onChange={handleChange}
/>
</Form.Group>
{errors.content?.map((message, idx) => (
<Alert
variant="warning"
key={idx}
className={alertStyles["alert-warning-custom"]}
>
{message}
</Alert>
))}

<Button
className={`${btnStyles.Button} ${btnStyles.Blue}`}
onClick={() => {}}
onClick={() => history.goBack()}
>
cancel
</Button>
Expand All @@ -76,7 +121,7 @@ function PostCreateForm() {
);

return (
<Form>
<Form onSubmit={handleSubmit}>
<Row className={styles.Row}>
<Col className="py-5 p-0 p-md-2" md={7} lg={8}>
<Container
Expand Down Expand Up @@ -114,8 +159,18 @@ function PostCreateForm() {
id="image-upload"
accept="image/*"
onChange={handleChangeImage}
ref={imageInput}
/>
</Form.Group>
{errors.image?.map((message, idx) => (
<Alert
variant="warning"
key={idx}
className={alertStyles["alert-warning-custom"]}
>
{message}
</Alert>
))}
<div className="d-md-none">{textFields}</div>
</Container>
</Col>
Expand Down

0 comments on commit 5060bed

Please sign in to comment.