Skip to content

Commit 22a831c

Browse files
authored
Merge pull request #1 from gauravchl/develop
Build out front-end UI to make the calls to the backend
2 parents 1a4c467 + 1194a78 commit 22a831c

File tree

12 files changed

+361
-100
lines changed

12 files changed

+361
-100
lines changed

package-lock.json

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

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"@apollo/react-hooks": "^3.1.2",
7+
"apollo-boost": "^0.4.4",
68
"apollo-server-lambda": "2.9.3",
79
"encoding": "0.1.12",
8-
"graphql": "14.5.4",
10+
"graphql": "^14.5.4",
911
"graphql-tag-pluck": "0.8.4",
1012
"merge-graphql-schemas": "1.7.0",
1113
"netlify-lambda": "1.6.3",
1214
"react": "^16.9.0",
1315
"react-dom": "^16.9.0",
16+
"react-router-dom": "^5.1.2",
1417
"react-scripts": "3.1.1"
1518
},
1619
"scripts": {

src/App.js

-26
This file was deleted.

src/App.css renamed to src/components/home.css

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
.App {
1+
.home {
22
text-align: center;
33
}
44

5-
.App-logo {
6-
animation: App-logo-spin infinite 20s linear;
7-
height: 40vmin;
8-
pointer-events: none;
5+
.home-logo {
6+
width: 320px;
7+
margin-top: 32px;
8+
margin-bottom: 28px
99
}
1010

11-
.App-header {
11+
.home-header {
1212
background-color: #282c34;
1313
min-height: 100vh;
1414
display: flex;
1515
flex-direction: column;
1616
align-items: center;
17-
justify-content: center;
17+
justify-content: flex-start;
1818
font-size: calc(10px + 2vmin);
1919
color: white;
2020
}
2121

22-
.App-link {
22+
.home-link {
2323
color: #61dafb;
2424
}
2525

26-
@keyframes App-logo-spin {
26+
@keyframes home-logo-spin {
2727
from {
2828
transform: rotate(0deg);
2929
}

src/components/home.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from "react";
2+
import "./home.css";
3+
4+
function Home(props) {
5+
const { loading, error, data } = props;
6+
return (
7+
<div className="home">
8+
<header className="home-header">
9+
<img
10+
src="https://codebuddies.org/images/logo.svg"
11+
className="home-logo"
12+
alt="logo"
13+
/>
14+
15+
{loading ? <div>Loading Data from backend</div> : null}
16+
{error ? <div>Error loading data</div> : null}
17+
18+
{data && data.resources && data.resources.map((r, i) => (
19+
<div key={i}>
20+
{r.title} : {r.description}
21+
</div>
22+
))}
23+
</header>
24+
</div>
25+
);
26+
}
27+
28+
export default Home;

src/config.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// env: local || development || production
2+
3+
const local = {
4+
graphUrl: 'http://localhost:8888/.netlify/functions/graphql',
5+
env: 'local',
6+
};
7+
8+
const config = {
9+
graphUrl: process.env.REACT_APP_GRAPH_URL || local.graphUrl,
10+
env: process.env.REACT_APP_ENV || local.env,
11+
};
12+
13+
export default config;

src/containers/home.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "react";
2+
import Home from "../components/home.js";
3+
import { useQuery } from "@apollo/react-hooks";
4+
import { RESOURCES } from "../queries/resources";
5+
6+
function HomeContainer() {
7+
const { loading, error, data } = useQuery(RESOURCES);
8+
9+
return <Home loading={loading} error={error} data={data} />;
10+
}
11+
12+
export default HomeContainer;

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
4-
import App from './App';
4+
import Routes from './router';
55
import * as serviceWorker from './serviceWorker';
66

7-
ReactDOM.render(<App />, document.getElementById('root'));
7+
ReactDOM.render(<Routes />, document.getElementById('root'));
88

99
// If you want your app to work offline and load faster, you can change
1010
// unregister() to register() below. Note this comes with some pitfalls.

src/lambda/graphql.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ const setup = require('./initializers/setup');
44
exports.handler = async (request, context) => {
55
await setup.run();
66
const response = await handleRequest(request, context);
7-
87
return response;
98
};

src/lambda/lib/apollo.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ exports.initialize = () => {
1010
};
1111

1212
const server = new ApolloServer(apolloServerConfig);
13-
exports.handler = server.createHandler();
13+
exports.handler = server.createHandler({
14+
cors: {
15+
origin: '*',
16+
credentials: true
17+
}
18+
});
1419
};
1520

1621
exports.handleRequest = (request, context) => {

src/queries/resources.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { gql } from 'apollo-boost';
2+
3+
4+
export const RESOURCES = gql`
5+
{
6+
resources {
7+
title
8+
description
9+
}
10+
}
11+
`;

src/router.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from "react";
2+
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
3+
import HomeContainer from "./containers/home";
4+
import ApolloClient from "apollo-boost";
5+
import { ApolloProvider } from "@apollo/react-hooks";
6+
import config from './config'
7+
8+
const client = new ApolloClient({
9+
uri: config.graphUrl
10+
});
11+
12+
const Routes = () => {
13+
return (
14+
<div>
15+
<ApolloProvider client={client}>
16+
<Router>
17+
<Switch>
18+
<Route path="/">
19+
<HomeContainer />
20+
</Route>
21+
</Switch>
22+
</Router>
23+
</ApolloProvider>
24+
</div>
25+
);
26+
};
27+
28+
export default Routes;

0 commit comments

Comments
 (0)