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

Shawn and Sarah news-site-V solution #2

Open
wants to merge 1 commit into
base: master
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 src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import AppNav from './components/AppNav/AppNav.js';
import HomePage from './pages/HomePage.js';
import ArticlePage from './pages/ArticlePage.js';
import SectionPage from './pages/SectionPage.js';
import AddArticlePage from './pages/AddArticlePage';
import LoginPage from './pages/LoginPage';

class App extends Component {
render() {
Expand All @@ -16,6 +18,8 @@ class App extends Component {
<Route exact path="/" component={HomePage} />
<Route exact path="/articles/:articleID" component={ArticlePage} />
<Route exact path="/sections/:sectionID" component={SectionPage} />
<Route exact path="/add-article" component={AddArticlePage} />
<Route exact path="/login" component={LoginPage} />
</div>
</Router>
</div>
Expand Down
26 changes: 26 additions & 0 deletions src/api/ArticlesAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,35 @@ const searchArticles = async (textToSearchFor) => {
return data;
}

const addArticle = async (articleObject) => {
try {
let response = await fetch(BASE_URL, {
headers: {
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify(articleObject)
})
let data = await response.json();

if (data.error) {
return {'message': "You done goofed. Try again."}
}
else {
return {"message": "You good, carry on."}
}
}

catch (e) {
console.error(e)
}

}

export {
fetchArticleByID,
fetchArticles,
fetchArticlesBySection,
searchArticles,
addArticle,
};
18 changes: 11 additions & 7 deletions src/components/AppNav/AppNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import navItems from '../../config/Sections.json';
class AppNav extends Component {
render() {
return (
<Navbar color="light">
{navItems.map((navItem) =>
<Link to={`/sections/${navItem.value}`} >
{ navItem.label }
</Link>
)}
</Navbar>
<div>
<Navbar color="light">
{navItems.map((navItem) =>
<Link to={`/sections/${navItem.value}`} >
{ navItem.label }
</Link>
)}
<Link to={`/add-article`} >Add an Article</Link>
<Link to={`/login`} >Login</Link>
</Navbar>
</div>
)
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/pages/AddArticlePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, {useState} from 'react';
import { Form, FormGroup, Label, Input, Button } from "reactstrap"
import { addArticle } from '../api/ArticlesAPI';

const AddArticlePage = () => {
const [articleMsg, addArticleMsg] = useState(undefined)

const handleFormSubmit = async (evt) => {
evt.preventDefault();
const articleObject = {
"title" : evt.target.title.value,
"byline" : evt.target.byline.value,
"abstract": evt.target.abstract.value,
}
addArticle(articleObject)
.then(response => {
addArticleMsg(response.message);
console.log(response);
})
}

return (
<div>
<h1>Add an Article</h1>
{ articleMsg != "You good, carry on." ?
<Form onSubmit={handleFormSubmit}>
<FormGroup>
<Label for="Title">Title</Label>
<Input type="text" name="title" id="article-title" placeholder="Enter Title" required/>
</FormGroup>
<FormGroup>
<Label for="byline">Byline</Label>
<Input type="text" name="byline" id="article-byline" placeholder="Enter byline" required />
</FormGroup>
<FormGroup>
<Label for="Abstract">Abstract</Label>
<Input type="textarea" name="abstract" id="article-abstract" required/>
</FormGroup>
<Button>Submit</Button>
</Form>
: <h1>Victory! You are now a published journalist!</h1>
}
</div>
);
};

export default AddArticlePage;
22 changes: 22 additions & 0 deletions src/pages/LoginPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import { Form, FormGroup, Label, Input, Button } from 'reactstrap';

const LoginPage = () => {
return (
<div>
<Form>
<FormGroup>
<Label for="Email">Email</Label>
<Input type="email" name="email" id="Email" placeholder="Enter your email" />
</FormGroup>
<FormGroup>
<Label for="Password">Password</Label>
<Input type="password" name="password" id="Password" placeholder="Enter your password" />
</FormGroup>
<Button>Submit</Button>
</Form>
</div>
);
};

export default LoginPage;