From cba61a78e9fcaddc0e5906d04280c7ed7b923771 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Sat, 18 Jan 2020 16:40:57 -0800 Subject: [PATCH] hook SET_ROOMS into rooms Service --- .../play-node-go/src/pages/Home/Home.js | 22 ++++++++++++-- .../src/reducers/rooms/stateReducer.rooms.js | 2 +- .../src/services/api/roomsServices.js | 29 +++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 packages/server/play-node-go/src/services/api/roomsServices.js diff --git a/packages/server/play-node-go/src/pages/Home/Home.js b/packages/server/play-node-go/src/pages/Home/Home.js index 139e770..655203d 100644 --- a/packages/server/play-node-go/src/pages/Home/Home.js +++ b/packages/server/play-node-go/src/pages/Home/Home.js @@ -1,7 +1,25 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import './Home.scss'; +import roomsServices from '../../services/api/roomsServices'; + +const Home = props => { + + const fetchRoomsAPI = async () => { + const response = await roomsServices.indexService(); + if (response) { + const action = { + type: 'ROOMS', + message: 'SET_ROOMS', + body: response + } + return props.dispatch(action) + } + } + + useEffect(() => { + fetchRoomsAPI(); + }, []) -const Home = () => { return (
diff --git a/packages/server/play-node-go/src/reducers/rooms/stateReducer.rooms.js b/packages/server/play-node-go/src/reducers/rooms/stateReducer.rooms.js index bfc07b3..0670b32 100644 --- a/packages/server/play-node-go/src/reducers/rooms/stateReducer.rooms.js +++ b/packages/server/play-node-go/src/reducers/rooms/stateReducer.rooms.js @@ -15,5 +15,5 @@ export const roomsReducer = (state: state, action: action):state => { function roomsParse(roomsData) { const rooms = JSON.parse(roomsData); - return rooms + return rooms.rooms } \ No newline at end of file diff --git a/packages/server/play-node-go/src/services/api/roomsServices.js b/packages/server/play-node-go/src/services/api/roomsServices.js new file mode 100644 index 0000000..3681a2b --- /dev/null +++ b/packages/server/play-node-go/src/services/api/roomsServices.js @@ -0,0 +1,29 @@ +import config from '../../config'; + +const apiAddress = config.apiAddress; +const roomsAddress = `${apiAddress}/rooms` + +var headers = new Headers(); +headers.append('Content-Type', 'application/json'); +headers.append('Accept', 'application/json'); +headers.append('Sec-Fetch-Site', 'cross-site') + +const indexService = async () => { + const response = await fetch(roomsAddress, + {method: 'GET', credentials: 'include', headers: headers} + ) + .then(res => { + return res.text(); + }) + // .then(text => { + // return JSON.parse(text) + // }) + .catch(err => { + return err; + }); + return response; +} + +export default { + indexService +} \ No newline at end of file