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

instructor solution #1

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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.6.0",
"bootstrap": "^5.0.2",
"compression": "^1.7.4",
"concurrently": "^6.0.1",
"cors": "^2.8.5",
Expand All @@ -17,9 +17,10 @@
"loopback-component-explorer": "^6.5.1",
"loopback-connector-postgresql": "^5.3.0",
"loopback-remote-routing": "^1.5.2",
"moment": "^2.29.1",
"node-fetch": "^2.6.1",
"react": "^17.0.2",
"react-bootstrap": "^1.5.2",
"react-bootstrap": "^1.6.1",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-test-renderer": "^17.0.2",
Expand Down
114 changes: 80 additions & 34 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,89 @@
import React, { Component } from 'react';
import React, { Component, useState } from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import './App.css';

import AppNav from './components/AppNav/AppNav.js';
import HomePage from './pages/HomePage.js';
import ArticlePage from './pages/ArticlePage.js';
import HomePage from './pages/HomePage';
import ArticlePage from './pages/ArticlePage';
import SectionPage from './pages/SectionPage.js';
import AddArticlePage from './pages/AddArticlePage.js';
import LoginPage from './pages/LoginPage.js';

class App extends Component {
render() {
return (
<div>
<Router>
<div>
<AppNav />
<Route exact path="/" component={HomePage} />
<Route exact path="/articles/:articleID" component={ArticlePage} />
<Route exact path="/sections/:sectionID" component={SectionPage} />
</div>
</Router>
</div>
);
}
}

export default App;
// --- Class Based Component
// class App extends Component {

// state = {
// query: null
// }

// handleSearch = (evt) => {
// evt.preventDefault()
// let searchQuery = evt.target.search.value;
// this.setState({
// query: searchQuery
// })
// }

// Functional solution:
// function App() {
// return (
// <div>
// <AppNav />

// render() {

// const renderHomePage = () => {
// return (
// <HomePage query={this.state.query} />
// )
// }

// return (
// <div>
// <Router>
// <div>
// <Route exact path="/" component={HomePage} />
// <Route exact path="/articles/:articleID" component={ArticlePage} />
// <Route exact path="/sections/:sectionID" component={SectionPage} />
// </div>
// </Router>
// </div>
// );
// <AppNav handleSearch={this.handleSearch}/>
// <div>
// <Route exact path="/" render={renderHomePage} />
// <Route exact path="/articles/:articleID" component={ArticlePage} />
// <Route exact path="/sections/:sectionID" component={SectionPage} />
// </div>
// </Router>
// </div>
// );
// }
// }

// --- Functional Component
const App = () => {
const [query, setQuery] = useState(null);

const handleSearch = (evt) => {
evt.preventDefault()
let searchQuery = evt.target.search.value;
setQuery(searchQuery);
}

const renderHomePage = () => {
return (
<HomePage query={query} />
)
}

const renderSectionPage = () => {
return (
<SectionPage query={query} />
)
}

return (
<div>
<Router>
<AppNav handleSearch={handleSearch} />
<div>
<Route exact path="/" render={renderHomePage} />
<Route exact path="/articles/:articleID" component={ArticlePage} />
<Route exact path="/sections/:sectionID" render={renderSectionPage} />
<Route exact path="/add-article" component={AddArticlePage} />
<Route exact path="/login" component={LoginPage} />
</div>
</Router>
</div>
);
}

export default App;
81 changes: 62 additions & 19 deletions src/api/ArticlesAPI.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,76 @@
const BASE_URL = 'http://localhost:3001/api/articles';
const URL = 'http://localhost:3001/api/articles'

const fetchArticleByID = async (articleID) => {
const response = await fetch(`${BASE_URL}/${articleID}`);
const data = await response.json();
return data;
};
try {
let response = await fetch(`${URL}/${articleID}`)
let data = await response.json()
return data
}
catch(err) {
console.log(err)
}
}

const fetchArticlesBySection = async (section) => {
const response = await fetch(`${BASE_URL}?filter={"where":{"section":"${section}"}}`);
const data = await response.json();
return data;
try {
const response = await fetch(`${URL}?filter={"where":{"section":"${section}"}}`);
const data = await response.json();
return data;
}
catch(err) {
console.error('There was an error.')
}
};

const fetchArticles = async (filters = null) => {
const url = filters ? `${BASE_URL}?filter={"where":${filters}}` : BASE_URL;
const response = await fetch(url);
const data = await response.json();
return data;
};
const fetchArticles = (filters = null) => {
console.log('Fetch Articles')
try {
return fetch(`${URL}${filters !== null ? `?filter={"where":{"title":{"ilike":"${filters}"}}}` : ''}`)
.then(res => res.json())
.then(data => data)
.catch(err => {
console.log(err)
})
}
catch(err) {
console.error(err)
}
}

const searchArticles = async (textToSearchFor) => {
const response = await fetch(`${BASE_URL}?filter={"where":{"title":{"ilike":"${textToSearchFor}"}}}`)
const data = await response.json();
return data;
const addArticle = async (articleObject) => {
try {
let response = await fetch(URL, {
headers: {
'Content-type': 'application/json'
},
method: 'POST',
body: JSON.stringify(articleObject)
})
let data = await response.json();
if (data.error) {
return {
'message': 'There was an error saving your message. Please make sure all fields are filled.',
'isError': true,
'statusCode': 200
}
}
else {
return {
'message': 'Article Successfully Created',
'isError': false,
'statusCode': 200
}
}
}
catch (err) {
console.error(err)
}
}


export {
fetchArticleByID,
fetchArticles,
fetchArticlesBySection,
searchArticles,
addArticle
};
84 changes: 57 additions & 27 deletions src/components/AppNav/AppNav.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,64 @@
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import { Navbar } from 'reactstrap';
import { Navbar, Container, Nav, Form, FormControl, Button } from 'react-bootstrap';
import navItems from '../../config/Sections.json';
import { withRouter } from "react-router";

class AppNav extends Component {
render() {
return (
<Navbar color="light">
{navItems.map((navItem) =>
<Link to={`/sections/${navItem.value}`} >
{ navItem.label }
</Link>
)}
</Navbar>
)
}
}
// --- Class Based Component
// class AppNav extends Component {

export default AppNav;
// render() {
// console.log(this.props)

// const createNavItems = () => {
// return navItems.map((navItem, idx) => {
// return <Nav.Link key={idx} onClick={() => this.props.history.push(`/sections/${navItem.value}`)}>{ navItem.label }</Nav.Link>
// })
// }

// Functional solution:
// function AppNav() {
// return (
// <Navbar color="light">
// {navItems.map((navItem) =>
// <Link to={`/sections/${navItem.value}`} >
// {navItem.label}
// </Link>
// )}
// </Navbar>
// );
// return (
// <Navbar bg="dark" variant="dark">
// <Navbar.Brand href="/" className='m-2'>News Site</Navbar.Brand>
// <Nav className="me-auto">
// {
// createNavItems()
// }
// </Nav>
// <Form className='d-flex' onSubmit={ this.props.handleSearch }>
// <FormControl type="search" placeholder="Search" className="mr-2" name='search' />
// <Button type='submit' variant="outline-success">Search</Button>
// </Form>
// </Navbar>
// )
// }
// }

// --- Functional Component
const AppNav = (props) => {
const { handleSearch } = props;


const createNavItems = () => {
return navItems.map((navItem, idx) => {
return <Nav.Link key={idx} onClick={() => props.history.push(`/sections/${navItem.value}`)}>{navItem.label}</Nav.Link>
})
}

return (
<Navbar bg="dark" variant="dark">
<Navbar.Brand href="/" className='m-2'>News Site</Navbar.Brand>
<Nav className="me-auto">
{
createNavItems()
}
<Button href='/login' variant='outline-primary' className='mx-2'>Login</Button>
<Button href='/add-article' variant='outline-secondary' className='mx-2'>Add Article</Button>
</Nav>
<Form className='d-flex' onSubmit={handleSearch}>
<FormControl type="search" placeholder="Search" className="mr-2" name='search' />
<Button type='submit' variant="outline-success">Search</Button>
</Form>
</Navbar>
)
}

export default withRouter(AppNav);
45 changes: 13 additions & 32 deletions src/components/Article/Article.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
import React, { Component } from 'react';
import { Media } from 'reactstrap';
import "./article.css";

import { Card } from 'react-bootstrap';
import moment from 'moment';

class Article extends Component {
render() {
const { title, created_date: createdDate, abstract, byline, image } = this.props;
console.log(this.props)
return (
<Media>
<Media left>
{ image && <img className="image" src={ image }/> }
</Media>
<Media body className="body">
<Media heading>{ title }</Media>
<p>{ createdDate }</p>
{ byline && <p>{ byline }</p> }
<p>{ abstract }</p>
</Media>
</Media>
<Card style={{ width: '18rem' }}>
{this.props.image && <Card.Img variant="top" src={this.props.image} />}
<Card.Body>
<Card.Title>{this.props.title}</Card.Title>
<p>{this.props.byline && this.props.byline}</p>
<Card.Text>{this.props.abstract}</Card.Text>
<p>{moment(this.props.created_date).format("MM-DD-YYYY")}</p>
</Card.Body>
</Card>
)
}
}

export default Article;


// Functional solution:
// function Article({ title, created_date: createdDate, abstract, byline, image }) {
// return (
// <Media>
// <Media left>
// {image && <img src={image} />}
// </Media>
// <Media body>
// <Media heading>{title}</Media>
// <p>{createdDate}</p>
// {byline && <h2>{byline}</h2>}
// <p>{abstract}</p>
// </Media>
// </Media >
// );
// }
Loading