Skip to content

Commit e335e3f

Browse files
authored
Merge pull request #16 from FAC-Sixteen/fetch-test
Fetch test
2 parents 52d2c26 + f07a4dd commit e335e3f

File tree

7 files changed

+127
-48
lines changed

7 files changed

+127
-48
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
npm-debug.log*
2323
yarn-debug.log*
2424
yarn-error.log*
25+

.netlify/functions/getData.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* eslint-disable */
2+
const fetch = require("node-fetch");
3+
const { config } = require("dotenv");
4+
config();
5+
6+
const key = process.env.AIRTABLE_KEY;
7+
exports.handler = async function(event, context) {
8+
try {
9+
const response = await fetch(
10+
`https://api.airtable.com/v0/appkLPDVgr4TwR3lM/Imported%20table?maxRecords=3&view=Grid%20view&api_key=${key}`,
11+
{
12+
headers: { Accept: "application/json" }
13+
}
14+
);
15+
if (!response.ok) {
16+
// NOT res.status >= 200 && res.status < 300
17+
return { statusCode: response.status, body: response.statusText };
18+
}
19+
const data = await response.json();
20+
21+
return {
22+
statusCode: 200,
23+
body: JSON.stringify(data)
24+
};
25+
} catch (err) {
26+
console.log(err); // output to netlify function log
27+
return {
28+
statusCode: 500,
29+
body: JSON.stringify(err.message) // Could be a custom message or object i.e. JSON.stringify(err)
30+
};
31+
}
32+
};

.netlify/state.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"siteId": "0a4736b3-c0be-4efe-9b3f-4810feb0b204"
3+
}

netlify.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Settings in the [build] context are global and are applied to all contexts
2+
# unless otherwise overridden by more specific contexts.
3+
[build]
4+
# Directory to change to before starting a build.
5+
# This is where we will look for package.json/.nvmrc/etc.
6+
base = "./"
7+
8+
# Directory (relative to root of your repo) that contains the deploy-ready
9+
# HTML files and assets generated by the build. If a base directory has
10+
# been specified, include it in the publish directory path.
11+
publish = "./build"
12+
13+
14+
# Directory with the serverless Lambda functions to deploy to AWS.
15+
functions = ".netlify/functions/"

package-lock.json

+15-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+35-44
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
11
{
2-
"name": "re4-w",
3-
"version": "0.1.0",
4-
"private": true,
5-
"dependencies": {
6-
"husky": "^2.4.0",
7-
"lint-staged": "^8.2.0",
8-
"prettier": "^1.18.1",
9-
"react": "^16.8.6",
10-
"react-dom": "^16.8.6",
11-
"react-scripts": "3.0.1"
12-
},
13-
"lint-staged": {
14-
"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
15-
"prettier --write",
16-
"git add"
17-
]
18-
},
19-
"scripts": {
20-
"start": "react-scripts start",
21-
"build": "react-scripts build",
22-
"format": "prettier --write '**/*.{js,css,md}'",
23-
"test": "react-scripts test",
24-
"eject": "react-scripts eject"
25-
},
26-
"eslintConfig": {
27-
"extends": "react-app"
28-
},
29-
"husky": {
30-
"hooks": {
31-
"pre-commit": "lint-staged"
32-
}
33-
},
34-
"browserslist": {
35-
"production": [
36-
">0.2%",
37-
"not dead",
38-
"not op_mini all"
39-
],
40-
"development": [
41-
"last 1 chrome version",
42-
"last 1 firefox version",
43-
"last 1 safari version"
44-
]
45-
}
2+
"name": "re4-w",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"husky": "^2.4.0",
7+
"lint-staged": "^8.2.0",
8+
"prettier": "^1.18.1",
9+
"dotenv": "^8.0.0",
10+
"node-fetch": "^2.6.0",
11+
"react": "^16.8.6",
12+
"react-dom": "^16.8.6",
13+
"react-scripts": "3.0.1"
14+
},
15+
"scripts": {
16+
"start": "react-scripts start",
17+
"build": "react-scripts build",
18+
"format": "prettier --write '**/*.{js,css,md}'",
19+
"test": "react-scripts test",
20+
"eject": "react-scripts eject"
21+
},
22+
"eslintConfig": {
23+
"extends": "react-app"
24+
},
25+
"browserslist": {
26+
"production": [
27+
">0.2%",
28+
"not dead",
29+
"not op_mini all"
30+
],
31+
"development": [
32+
"last 1 chrome version",
33+
"last 1 firefox version",
34+
"last 1 safari version"
35+
]
36+
}
4637
}

src/App.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
import React from "react"
2+
import logo from "./logo.svg"
23
import "./App.css"
34

45
function App() {
6+
const [data, setData] = React.useState(null)
7+
8+
const getData = () => {
9+
return fetch(`.netlify/functions/getData`)
10+
.then(result => result.json())
11+
.catch("error")
12+
}
13+
14+
const assignData = () => {
15+
getData().then(result => {
16+
setData(result.records[0].fields.Item)
17+
})
18+
}
19+
// eslint-disable-next-line
20+
React.useEffect(() => assignData(), [])
521
return (
622
<div className="App">
723
<header className="App-header">
8-
<h1>RE4.W</h1>
24+
<img src={logo} className="App-logo" alt="logo" />
25+
<p>{data}</p>
26+
<a
27+
className="App-link"
28+
href="https://reactjs.org"
29+
target="_blank"
30+
rel="noopener noreferrer"
31+
>
32+
Learn React
33+
</a>
934
</header>
1035
</div>
1136
)

0 commit comments

Comments
 (0)