Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected form element and backend for the required field. #50

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/src/components/admin/AddBlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const AddBlog = ({ addBlog }) => {
placeholder="Enter Blog Tag"
type="text"
value={blogtag}
required={false}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -55,6 +56,7 @@ const AddBlog = ({ addBlog }) => {
placeholder="Enter Title"
type="text"
value={title}
required={true}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -63,6 +65,7 @@ const AddBlog = ({ addBlog }) => {
placeholder="Enter Content"
type="text"
value={content}
required={false}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -71,6 +74,7 @@ const AddBlog = ({ addBlog }) => {
placeholder="Enter Author"
type="text"
value={author}
required={false}
onChange={(e) => onChange(e)}
/>
<FileBase64
Expand Down
5 changes: 5 additions & 0 deletions client/src/components/admin/AddEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const AddEvent = ({ addEvent }) => {
placeholder="Enter Title"
type="text"
value={name}
required={true}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -57,6 +58,7 @@ const AddEvent = ({ addEvent }) => {
placeholder="Enter Venue"
type="text"
value={venue}
required={false}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -65,6 +67,7 @@ const AddEvent = ({ addEvent }) => {
placeholder="Enter Description"
type="text"
value={description}
required={false}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -73,6 +76,7 @@ const AddEvent = ({ addEvent }) => {
placeholder="Enter Eventdate"
type="date"
value={eventdate}
required={false}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -81,6 +85,7 @@ const AddEvent = ({ addEvent }) => {
placeholder="Enter Formurl"
type="text"
value={formurl}
required={false}
onChange={(e) => onChange(e)}
/>
<FileBase64
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/admin/AddImageslider.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const AddImageslider = ({ addImage }) => {
placeholder="Enter Title"
type="text"
value={title}
required={false}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -54,6 +55,7 @@ const AddImageslider = ({ addImage }) => {
placeholder="Enter Caption"
type="text"
value={caption}
required={false}
onChange={(e) => onChange(e)}
/>
<FileBase64
Expand Down
31 changes: 27 additions & 4 deletions client/src/components/admin/EditBlog.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import React, { useState, Fragment } from "react";
import React, { useEffect, useState, Fragment } from "react";
import FormElement from "../layout/FormElement";

import { connect } from "react-redux";
import PropTypes from "prop-types";
import { editBlog } from "../../actions/blog";
import { editBlog, getBlog } from "../../actions/blog";
import FileBase64 from 'react-file-base64';

const EditBlog = ({ match, editBlog }) => {
const EditBlog = ({ getBlog, editBlog, match, blog: { blog, loading } }) => {
useEffect(() => {
getBlog(match.params.id);
if (!loading && blog) {

setFormData({
blogtag: blog.blogtag,
title: blog.title,
content: blog.content,
author: blog.author,
picture: blog.picture
})
}
}, [getBlog, loading, blog && blog._id]);

const [formData, setFormData] = useState({
blogtag: "",
title: "",
content: "",
author: "",
});


const { blogtag, title, content, author, picture } = formData;

const onChange = (e) =>
Expand Down Expand Up @@ -46,6 +61,7 @@ const EditBlog = ({ match, editBlog }) => {
placeholder="Enter Blog Tag"
type="text"
value={blogtag}
required={false}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -54,6 +70,7 @@ const EditBlog = ({ match, editBlog }) => {
placeholder="Enter Title"
type="text"
value={title}
required={true}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -62,6 +79,7 @@ const EditBlog = ({ match, editBlog }) => {
placeholder="Enter Content"
type="text"
value={content}
required={false}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -70,6 +88,7 @@ const EditBlog = ({ match, editBlog }) => {
placeholder="Enter Author"
type="text"
value={author}
required={false}
onChange={(e) => onChange(e)}
/>
<FileBase64
Expand All @@ -90,6 +109,10 @@ const EditBlog = ({ match, editBlog }) => {
};
EditBlog.propTypes = {
editBlog: PropTypes.func.isRequired,
getBlog: PropTypes.func.isRequired,
};

export default connect(null, { editBlog })(EditBlog);
const mapStateToProps = state => ({
blog: state.blog,
});
export default connect(mapStateToProps, { editBlog, getBlog })(EditBlog);
5 changes: 5 additions & 0 deletions client/src/components/admin/EditEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const EditEvent = ({ getEvent, editEvent, match, event: { event, loading } }) =>
placeholder="Enter Title"
type="text"
value={name}
required={true}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -74,6 +75,7 @@ const EditEvent = ({ getEvent, editEvent, match, event: { event, loading } }) =>
placeholder="Enter Venue"
type="text"
value={venue}
required={false}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -82,6 +84,7 @@ const EditEvent = ({ getEvent, editEvent, match, event: { event, loading } }) =>
placeholder="Enter Description"
type="text"
value={description}
required={false}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -90,6 +93,7 @@ const EditEvent = ({ getEvent, editEvent, match, event: { event, loading } }) =>
placeholder="Enter Eventdate"
type="date"
value={eventdate}
required={false}
onChange={(e) => onChange(e)}
/>
<FormElement
Expand All @@ -98,6 +102,7 @@ const EditEvent = ({ getEvent, editEvent, match, event: { event, loading } }) =>
placeholder="Enter Formurl"
type="text"
value={formurl}
required={false}
onChange={(e) => onChange(e)}
/>
<FileBase64
Expand Down
22 changes: 11 additions & 11 deletions client/src/components/layout/FormElement.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react';

const FormElement = (props) => {
const {label,name,placeholder,type,value,onChange}=props
const { label, name, placeholder, type, value, required, onChange } = props
return (
<div className="form-group">
<label htmlFor={name}>{label}</label>
<input
type={type}
name={name}
placeholder={placeholder}
value={value}
onChange={onChange}
className="form-control form-control-lg"
required
/>
<label htmlFor={name}>{label}</label>
<input
type={type}
name={name}
placeholder={placeholder}
value={value}
onChange={onChange}
className="form-control form-control-lg"
required={required}
/>
</div>
)
}
Expand Down
4 changes: 4 additions & 0 deletions routes/api/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ router.post('/', [auth, [
if (req.body.blogtag != null) blogtag = req.body.blogtag;
author = "";
if (req.body.author != null) author = req.body.author;
picture = {};
if (req.body.picture != null) picture = req.body.picture;

const newBlog = new Blog({
blogtag: blogtag,
Expand Down Expand Up @@ -143,6 +145,8 @@ router.put(
if (req.body.blogtag != null) blogtag = req.body.blogtag;
author = "";
if (req.body.author != null) author = req.body.author;
picture = {};
if (req.body.picture != null) picture = req.body.picture;

blog.title = req.body.title;
blog.content = content;
Expand Down
4 changes: 4 additions & 0 deletions routes/api/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ router.post(
if (req.body.eventdate != null) eventdate = req.body.eventdate;
formurl = "";
if (req.body.formurl != null) formurl = req.body.formurl;
picture = {};
if (req.body.picture != null) picture = req.body.picture;

const newEvent = new Event({
name: req.body.name,
Expand Down Expand Up @@ -136,6 +138,8 @@ router.put(
if (req.body.eventdate != null) eventdate = req.body.eventdate;
formurl = "";
if (req.body.formurl != null) formurl = req.body.formurl;
picture = {};
if (req.body.picture != null) picture = req.body.picture;

event.name = req.body.name;
event.venue = venue;
Expand Down
6 changes: 2 additions & 4 deletions routes/api/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@ const Image = require('../../models/Image');

router.post(
"/",
[auth],
[auth, [check("picture", "picture is required").not().isEmpty()]],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}

try {
picture = {};
if (req.body.picture != null) picture = req.body.picture;
title = "";
if (req.body.title != null) title = req.body.title;
caption = "";
if (req.body.caption != null) caption = req.body.caption;

const newImage = new Image({
picture: picture,
picture: req.body.picture,
title: title,
caption: caption,
});
Expand Down