diff --git a/client/src/actions/index.js b/client/src/actions/index.js index a3c7b185..2a6ef4c9 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -1,33 +1,39 @@ -import axios from 'axios'; +//import axios from 'axios'; +import { + fetchUsers, postToken, + postBlog, getBlogs, getBlog, +} from '../api/fetchUsers'; + import { FETCH_USER, FETCH_BLOGS, FETCH_BLOG } from './types'; export const fetchUser = () => async dispatch => { - const res = await axios.get('/api/current_user'); + const { data } = fetchUsers(); - dispatch({ type: FETCH_USER, payload: res.data }); + dispatch({ type: FETCH_USER, payload: data }); }; export const handleToken = token => async dispatch => { - const res = await axios.post('/api/stripe', token); + const {data} = await postToken(token); - dispatch({ type: FETCH_USER, payload: res.data }); + dispatch({ type: FETCH_USER, payload: data }); }; export const submitBlog = (values, history) => async dispatch => { - const res = await axios.post('/api/blogs', values); + const {data} = await postBlog(values); history.push('/blogs'); - dispatch({ type: FETCH_BLOG, payload: res.data }); + dispatch({ type: FETCH_BLOG, payload: data }); }; export const fetchBlogs = () => async dispatch => { - const res = await axios.get('/api/blogs'); + const {data} = await getBlogs(); - dispatch({ type: FETCH_BLOGS, payload: res.data }); + dispatch({ type: FETCH_BLOGS, payload: data }); }; export const fetchBlog = id => async dispatch => { - const res = await axios.get(`/api/blogs/${id}`); + //const res = await axios.get(`/api/blogs/${id}`); + const {data} = await getBlog(id); - dispatch({ type: FETCH_BLOG, payload: res.data }); + dispatch({ type: FETCH_BLOG, payload: data }); }; diff --git a/client/src/api/fetchUsers.js b/client/src/api/fetchUsers.js new file mode 100644 index 00000000..83a1e6b8 --- /dev/null +++ b/client/src/api/fetchUsers.js @@ -0,0 +1,37 @@ +import axios from 'axios'; + +export const fetchUsers = async () =>{ + + return await axios.get('/api/current_user'); + +}; + + +export const postToken = async (token) => { + const res = await axios.post('/api/stripe', token); + return res; +}; + + +export const postBlog = async(values) =>{ + + return await axios.post('/api/blogs', values); +}; + + +export const getBlog = async (id) => { + + return await axios.get(`/api/blog/${id}`); + +} + + + +export const getBlogs = async (id) => { + + return await axios.get(`/api/blogs`); + +} + + + diff --git a/index.js b/index.js index f722fdad..d08a7e48 100644 --- a/index.js +++ b/index.js @@ -15,12 +15,14 @@ mongoose.connect(keys.mongoURI, { useMongoClient: true }); const app = express(); app.use(bodyParser.json()); + app.use( cookieSession({ maxAge: 30 * 24 * 60 * 60 * 1000, keys: [keys.cookieKey] }) ); + app.use(passport.initialize()); app.use(passport.session()); @@ -37,6 +39,7 @@ if (['production'].includes(process.env.NODE_ENV)) { } const PORT = process.env.PORT || 5000; + app.listen(PORT, () => { console.log(`Listening on port`, PORT); });