hook SET_ROOMS into rooms Service
This commit is contained in:
parent
8ad488c3ce
commit
05b7f338ff
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
|
||||
}
|
|
@ -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