From 5e7e428fdb76e9f4057c61fc2b8396c8ada5cfc5 Mon Sep 17 00:00:00 2001 From: Salah Elhossiny Date: Sun, 24 Oct 2021 12:12:27 +0200 Subject: [PATCH 1/4] refactor --- client/src/actions/index.js | 12 +++++++----- client/src/api/fetchUsers.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 client/src/api/fetchUsers.js diff --git a/client/src/actions/index.js b/client/src/actions/index.js index a3c7b185..24795a44 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -1,16 +1,18 @@ -import axios from 'axios'; +//import axios from 'axios'; +import {fetchUsers, postToken} 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 => { diff --git a/client/src/api/fetchUsers.js b/client/src/api/fetchUsers.js new file mode 100644 index 00000000..46605e94 --- /dev/null +++ b/client/src/api/fetchUsers.js @@ -0,0 +1,16 @@ +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; +}; + + + From afabec8cf02632a549b20daf9aeefc6aa80515f9 Mon Sep 17 00:00:00 2001 From: Salah Elhossiny Date: Sun, 24 Oct 2021 12:17:15 +0200 Subject: [PATCH 2/4] refactor api --- client/src/actions/index.js | 18 +++++++++++------- client/src/api/fetchUsers.js | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/client/src/actions/index.js b/client/src/actions/index.js index 24795a44..2a6ef4c9 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -1,5 +1,8 @@ //import axios from 'axios'; -import {fetchUsers, postToken} from '../api/fetchUsers'; +import { + fetchUsers, postToken, + postBlog, getBlogs, getBlog, +} from '../api/fetchUsers'; import { FETCH_USER, FETCH_BLOGS, FETCH_BLOG } from './types'; @@ -16,20 +19,21 @@ export const handleToken = token => async dispatch => { }; 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 index 46605e94..83a1e6b8 100644 --- a/client/src/api/fetchUsers.js +++ b/client/src/api/fetchUsers.js @@ -13,4 +13,25 @@ export const postToken = async (token) => { }; +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`); + +} + + From d322bf23d6e6a66075cc82303fa0fabaa94872bb Mon Sep 17 00:00:00 2001 From: Salah Elhossiny Date: Sun, 24 Oct 2021 12:29:01 +0200 Subject: [PATCH 3/4] refactor server.js --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index f722fdad..e4b71809 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()); From 9df89526dd4e84209f4ea6482a4948b9d835ee7a Mon Sep 17 00:00:00 2001 From: Salah Elhossiny Date: Sun, 24 Oct 2021 12:29:15 +0200 Subject: [PATCH 4/4] server.js --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index e4b71809..d08a7e48 100644 --- a/index.js +++ b/index.js @@ -39,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); });