add localHost util

This commit is contained in:
DavidWells 2018-06-30 10:35:26 -07:00
parent a06ffff107
commit cede6634d1
3 changed files with 21 additions and 2 deletions

View file

@ -5,6 +5,7 @@ import SettingsMenu from './components/SettingsMenu'
import SettingsIcon from './components/SettingsIcon' import SettingsIcon from './components/SettingsIcon'
import api from './utils/api' import api from './utils/api'
import sortByDate from './utils/sortByDate' import sortByDate from './utils/sortByDate'
import isLocalHost from './utils/isLocalHost'
import './App.css' import './App.css'
export default class App extends Component { export default class App extends Component {
@ -15,6 +16,15 @@ export default class App extends Component {
componentDidMount() { componentDidMount() {
// Fetch all todos // Fetch all todos
api.readAll().then((todos) => { api.readAll().then((todos) => {
if (todos.message === 'unauthorized') {
if (isLocalHost()) {
alert('FaunaDB key is not unauthorized. Make sure you set it in terminal session where you ran `npm start`. Visit http://bit.ly/set-fauna-key for more info')
} else {
alert('FaunaDB key is not unauthorized. Verify the key `FAUNADB_SECRET` set in Netlify enviroment variables is correct')
}
return false
}
console.log('all todos', todos) console.log('all todos', todos)
this.setState({ this.setState({
todos: todos todos: todos

View file

@ -1,7 +1,6 @@
/* Api methods to call /functions */ /* Api methods to call /functions */
const create = (data) => { const create = (data) => {
console.log('run new create')
return fetch('/.netlify/functions/todos-create', { return fetch('/.netlify/functions/todos-create', {
body: JSON.stringify(data), body: JSON.stringify(data),
method: 'POST' method: 'POST'
@ -12,7 +11,6 @@ const create = (data) => {
const readAll = () => { const readAll = () => {
return fetch('/.netlify/functions/todos-read-all').then((response) => { return fetch('/.netlify/functions/todos-read-all').then((response) => {
console.log(response)
return response.json() return response.json()
}) })
} }

11
src/utils/isLocalHost.js Normal file
View file

@ -0,0 +1,11 @@
export default function isLocalHost() {
const isLocalhostName = window.location.hostname === 'localhost';
const isLocalhostIPv6 = window.location.hostname === '[::1]';
const isLocalhostIPv4 = window.location.hostname.match(
// 127.0.0.1/8
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
);
return isLocalhostName || isLocalhostIPv6 || isLocalhostIPv4;
}