-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce484ff
commit 9a0f667
Showing
13 changed files
with
211 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,47 @@ | ||
import axios from 'axios'; | ||
import { FETCH_USER, | ||
FETCH_EXPLORE_QUOTES, | ||
SAVE_QUOTE_TO_PROFILE, | ||
REMOVE_QUOTE_FROM_PROFILE } from '../constants/constants'; | ||
import axios from "axios"; | ||
import { | ||
FETCH_USER, | ||
FETCH_EXPLORE_QUOTES, | ||
SAVE_QUOTE_TO_PROFILE, | ||
REMOVE_QUOTE_FROM_PROFILE, | ||
} from "../constants/constants"; | ||
|
||
export const fetchUser = (user) => { | ||
return dispatch => { | ||
axios.get('/api/user', {withCredentials: 'include'}) | ||
.then(res => res.data) | ||
.then(user => dispatch({ type: FETCH_USER, payload: user})) | ||
.catch(err => console.log(err)) | ||
} | ||
} | ||
return (dispatch) => { | ||
axios | ||
.get("/api/user", { withCredentials: "include" }) | ||
.then((res) => res.data) | ||
.then((user) => dispatch({ type: FETCH_USER, payload: user })) | ||
.catch((err) => console.log(err)); | ||
}; | ||
}; | ||
|
||
export const fetchExploreQuotes = () => { | ||
return dispatch => { | ||
axios.get('/api/quotes') | ||
.then(res => res.data) | ||
.then(data => dispatch({type: FETCH_EXPLORE_QUOTES, payload: data})) | ||
.catch(err => console.log(err)) | ||
} | ||
} | ||
return (dispatch) => { | ||
axios | ||
.get("/api/quotes") | ||
.then((res) => res.data) | ||
.then((data) => dispatch({ type: FETCH_EXPLORE_QUOTES, payload: data })) | ||
.catch((err) => console.log(err)); | ||
}; | ||
}; | ||
|
||
export const saveQuoteToProfile = (quote) => { | ||
return dispatch => { | ||
axios.post('/api/saved', quote) | ||
.then(data => dispatch({ type: SAVE_QUOTE_TO_PROFILE, payload: data })) | ||
.catch(err => console.log(err)) | ||
} | ||
} | ||
return (dispatch) => { | ||
axios | ||
.post("/api/quotes/save", quote) | ||
.then((data) => dispatch({ type: SAVE_QUOTE_TO_PROFILE, payload: data })) | ||
.catch((err) => console.log(err)); | ||
}; | ||
}; | ||
|
||
export const removeQuoteFromProfile = (id) => { | ||
return dispatch => { | ||
axios.delete(`/api/saved/${id}`) | ||
.then(data => dispatch({ type: REMOVE_QUOTE_FROM_PROFILE, payload: data })) | ||
.catch(err => console.log(err)) | ||
} | ||
} | ||
return (dispatch) => { | ||
axios | ||
.delete(`/api/saved/${id}`) | ||
.then((data) => | ||
dispatch({ type: REMOVE_QUOTE_FROM_PROFILE, payload: data }) | ||
) | ||
.catch((err) => console.log(err)); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React, { Component } from "react"; | ||
import { Redirect } from "react-router-dom"; | ||
import axios from "axios"; | ||
|
||
class LoginPage extends Component { | ||
constructor() { | ||
super(); | ||
|
||
this.state = { | ||
username: "", | ||
password: "", | ||
errors: {}, | ||
}; | ||
} | ||
|
||
handleChange = (e) => { | ||
this.setState({ | ||
[e.target.name]: e.target.value, | ||
}); | ||
}; | ||
|
||
handleSubmit = (e) => { | ||
const { username, password } = this.state; | ||
e.preventDefault(); | ||
|
||
axios | ||
.post("/auth/local", { username, password }) | ||
.then(() => <Redirect to="/dashboard" />) | ||
.catch((error) => { | ||
if (error.response) { | ||
console.log(this.state); | ||
this.setState({ ...this.state, errors: error.response.data }); | ||
} | ||
}); | ||
}; | ||
|
||
render() { | ||
const { username, password, errors } = this.state; | ||
return ( | ||
<div> | ||
<form onSubmit={this.handleSubmit}> | ||
<input | ||
type="text" | ||
value={username} | ||
name="username" | ||
placeholder="username" | ||
onChange={this.handleChange} | ||
/> | ||
<input | ||
type="password" | ||
value={password} | ||
name="password" | ||
placeholder="password" | ||
onChange={this.handleChange} | ||
/> | ||
{errors.length ? <p style={{ color: "red" }}>{errors}</p> : null} | ||
<input type="Submit" value="Submit" /> | ||
</form> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default LoginPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
const express = require('express'), | ||
User = require('../models/User'), | ||
fetch = require('node-fetch') | ||
app = express(); | ||
const express = require("express"), | ||
User = require("../models/User"), | ||
fetch = require("node-fetch"); | ||
app = express(); | ||
|
||
module.exports = (app) => { | ||
app.get("/api/quotes", async (req, res) => { | ||
const result = await fetch( | ||
"https://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json&lang=en" | ||
); | ||
const json = await result.json(); | ||
|
||
app.get('/api/quotes', async (req, res) => { | ||
const result = await fetch('https://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json&lang=en') | ||
const json = await result.json(); | ||
|
||
res.send(json) | ||
}) | ||
|
||
// Add Quote | ||
app.post('/api/saved', (req, res) => { | ||
const {author, quote} = req.body; | ||
|
||
const newQuote = User.findOne({ id: req.user.id }, (err, user) => { | ||
user.savedQuotes.push({ | ||
author, | ||
quote, | ||
isSaved: true | ||
}); | ||
user.save(); | ||
}); | ||
|
||
}) | ||
res.send(json); | ||
}); | ||
|
||
// Remove Quote | ||
app.delete('/api/saved/:id', (req, res, next) => { | ||
// Add Quote | ||
app.post("/api/quotes/save", (req, res) => { | ||
const { author, quote } = req.body; | ||
const { _id } = req.user; | ||
|
||
const user = User.findOne({ _id: req.user._id}, (err, user) => { | ||
user.savedQuotes.id(req.params.id).remove(); | ||
const newQuote = User.findOne({ _id }, (err, user) => { | ||
user.savedQuotes.push({ | ||
author, | ||
quote, | ||
isSaved: true, | ||
}); | ||
user.save(); | ||
}); | ||
}); | ||
|
||
user.save(); | ||
}); | ||
}) | ||
} | ||
// Remove Quote | ||
app.delete("/api/saved/:id", (req, res, next) => { | ||
const user = User.findOne({ _id: req.user._id }, (err, user) => { | ||
user.savedQuotes.id(req.params.id).remove(); | ||
|
||
user.save(); | ||
}); | ||
}); | ||
}; |
Oops, something went wrong.