hook SET_ROOMS into rooms Service

This commit is contained in:
Sorrel Bri 2020-01-18 16:40:57 -08:00
parent 03648fcf3f
commit eb86556f2a
3 changed files with 50 additions and 3 deletions

View file

@ -1,7 +1,25 @@
import React from 'react'; import React, { useEffect } from 'react';
import './Home.scss'; 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 ( return (
<div className="page"> <div className="page">

View file

@ -15,5 +15,5 @@ export const roomsReducer = (state: state, action: action):state => {
function roomsParse(roomsData) { function roomsParse(roomsData) {
const rooms = JSON.parse(roomsData); const rooms = JSON.parse(roomsData);
return rooms return rooms.rooms
} }

View 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
}