hook SET_ROOMS into rooms Service
This commit is contained in:
parent
03648fcf3f
commit
eb86556f2a
3 changed files with 50 additions and 3 deletions
|
@ -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 (
|
||||
<div className="page">
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
29
play-node-go/src/services/api/roomsServices.js
Normal file
29
play-node-go/src/services/api/roomsServices.js
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue