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

finishing #6

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
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
![cf](https://i.imgur.com/7v5ASc8.png) 35: Putting it All Together
![cf](https://i.imgur.com/7v5ASc8.png) 33: Photo Uploads
======

## Submission Instructions
* continue working on the fork you created from lab 33
* continue working on the fork you created from lab 32
* open a **new branch** for today's assignment
* upon completion, create a **new pull request** in github
* submit a link to your PR in canvas

## Learning Objectives
* students will be able to complete a full stack application (using the MERN stack)
* students will be able to upload images (and other assets) on the client side
* students will be able to use the `FileReader` API to preview files

## Requirements
#### Configuration
Expand Down Expand Up @@ -36,13 +37,11 @@
* `_content.scss`

#### Feature Tasks
* create a `DashboardContainer`
* create an `Avatar` component for housing a profile image
* create a `PhotoForm` component
* create a `PhotoItem` component - this should hold your `PhotoForm` and to display all uploaded photos
* style your application's components
* this should include **at least** "lo-fi" styling (grayscale)
* create a `SettingsContainer`
* give the user the ability to create and update their profile
* **optional:** create a `DashboardContainer`
* give the user the ability to create, read, update and delete photos as part of their photo gallery

#### Test
* test your `PhotoForm` component
* test your `PhotoItem` component
* test your redux reducers
* test your `util` methods
1 change: 1 addition & 0 deletions backend
Submodule backend added at 1fc6e9
4 changes: 4 additions & 0 deletions frontend/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": [ "es2015", "react"],
"plugins": ["transform-object-rest-spread"]
}
21 changes: 21 additions & 0 deletions frontend/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": [ "error", "single" ],
"semi": ["error", "always"],
"linebreak-style": [ "error", "unix" ]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
1 change: 1 addition & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
12 changes: 12 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "frontend",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
30 changes: 30 additions & 0 deletions frontend/src/__test__/auth-reducer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import authReducer from '../reducer/auth.js';

describe('Auth Reducer', () => {
let state = {auth: '1234567890987654321'}

test('initial state should be null', () => {
let result = authReducer(undefined, {type: null});
expect(result).toEqual(null)
})

test('no action provided should return default state', () => {
let result = authReducer(state, {type: null});
expect(result).toEqual(state);
})

test('TOKEN_SET should return a token', () => {
let action = {
type: 'TOKEN_SET',
payload: 'sample token'
}

let result = authReducer({}, action);
expect(result).toBe(action.payload);
})

test('TOKEN_DELETE should return null', () => {
let result = authReducer(state, {type: 'TOKEN_DELETE'});
expect(result).toBe(null);
})
})
36 changes: 36 additions & 0 deletions frontend/src/action/auth-action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import superagent from 'superagent';

export const tokenSet = (token) => ({
type: 'TOKEN_SET',
payload: token
})

export const tokenDelete = () => ({
type: 'TOKEN_DELETE'
})

export const signupRequest = (user) => (dispatch) => {
return superagent.post(`${__API_URL__}/signup`)
.withCredentials()
.send(user)
.then( res => {
dispatch(tokenSet(res.text));

try {
localStorage.token = res.token;
} catch (err) {
console.log(err);
}
return res;
})
}

export const loginRequest = (user) => (dispatch) => {
return superagent.get(`${__API_URL__}/login`)
.withCredentials()
.auth(user.username, user.password)
.then( res => {
dispatch(tokenSet(res.text))
return res;
})
}
22 changes: 22 additions & 0 deletions frontend/src/action/profile-action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import superagent from 'superagent';

export const profileCreate = (profile) => ({
type: 'PROFILE_CREATE',
payload: profile
})

export const profileUpdate = (profile) => ({
type: 'PROFILE_UPDATE',
payload: profile
})

export const profileCreateRequest = (profile) => (dispatch, getState) => {
let {auth} = getState();
return superagent.post(`${__API_URL__}/profile`)
.set('Authorization', `Bearer ${auth}`)
.field('bio', profile.bio)
.attach( res => {
dispatch(dispatchCreate(res.body));
return res;
})
}
49 changes: 49 additions & 0 deletions frontend/src/component/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import {connect} from 'react-redux';
import {BrowserRouter, Route, Link} from 'react-router-dom';
import LandingContainer from '../landing-container';
import SettingsContainer from '../settings-container';
import * as util from '../../lib/util.js';
import {tokenSet} from '../../action/auth-actions.js';

class App extends React.Component {
componentDidMount() {
let token = util.readCookie('X-Sluggram-Token');
if(token) {
this.props.tokenSet(token);
}
}

render() {
return (
<div className='sluggram'>
<BrowserRouter>
<section>
<header>
<h1>Sluggram</h1>
<nav>
<ul>
<li><Link to='/welcome/signup'>signup</Link></li>
<li><Link to='/welcome/login'>login</Link></li>
<li><Link to='/settings'>settings</Link></li>
</ul>
</nav>
</header>
<Route exact path='/welcome/:auth' component={LandingContainer} />
<Route exact path='/settings' component={SettingsContainer} />
</section>
</BrowserRouter>
</div>
)
}
}

let mapStateToProps = (state) => ({
profile: state.profile
})

let mapDispatchToProps =(dispatch) => ({
tokenSet: (token) => dispatch(tokenSet(token))
})

export default connect(mapStateToProps, mapDispatchToProps)(App);
95 changes: 95 additions & 0 deletions frontend/src/component/auth-form/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import React from 'react';
import * as util from '../../lib/util.js';

class AuthForm extends React.Component {
constructor(props) {
super(props);

this.state ={
username: '',
password: '',
email: '',
usernameError: null,
passwordError: null,
emailError: null,
error: null
}

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(e) {
let {name, value} = e.target;

this.setState({
[name]: value,
usernameError: name === 'username' && !value ? 'username required' : null,
emailError: name === 'email' && !value ? 'email required' : null,
passwordError: name === 'password' && !value ? 'password required' : null
})
}

handleSubmit(e) {
e.preventDefault();
this.props.onComplete(this.state)
.then(() => {
this.setState({ username: '', email: '', password: ''})
})
.catch(error => {
console.error(error);
this.setState({error})
})
}

render() {
return (
<form
onSubmit={this.handleSubmit}
className='auth-form'>

{util.renderIf(this.props.auth === 'signup',
<input
type='text'
name='email'
placeholder='email'
value={this.state.email}
onChange={this.handleChange}
/>
)}

{util.renderIf(this.state.usernameError,
<span className='tooltip'>
{this.state.usernameError}
</span>
)}

<input
type='text'
name='username'
placeholder='username'
value={this.state.username}
onChange={this.handleChange}
/>

{util.renderIf(this.state.passwordError,
<span className='tooltip'>
{this.state.passwordError}
</span>
)}

<input
type='password'
name='password'
placeholder='enter password'
value={this.state.password}
onChange={this.handleChange}
/>

<button type='submit'>{this.props.auth}</button>
</form>
)
}
}

export default AuthForm;
33 changes: 33 additions & 0 deletions frontend/src/component/landing-container/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import {connect} from 'react-redux';
import AuthForm from '../auth-form';
import * as util from '../../lib/util.js';
import {signupRequest, loginRequest} from '../../action/auth-actions.js';

class LandingContainer extends React.Component {
render() {
let {params} = this.props.match;

let handleComplete = params.auth === 'login'
? this.props.login
: this.props.signup

return (
<div>
<AuthForm
auth={params.auth}
onComplete={handleComplete}
/>
</div>
)
}
}

let mapDispatchToProps = (dispatch) => {
return {
signup: (user) => dispatch(signupRequest(user)),
login: (user) => dispatch(loginRequest(user))
}
}

export default connect(undefined, mapDispatchToProps)(LandingContainer);
Loading