-
Notifications
You must be signed in to change notification settings - Fork 4
Adding React GraphQL example #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| REACT_APP_API_URL=https://api.remote.it | ||
| REACT_APP_DEV_API_KEY=YOUR_API_DEV_KEY |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
|
||
| # dependencies | ||
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
| # production | ||
| /build | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
|
|
||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| .idea |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Remote.it React GraphQL | ||
|
|
||
| ## Start the project locally | ||
|
|
||
| First of all, create a `.env` file in root of the project. | ||
|
|
||
| ```shell | ||
| REACT_APP_API_URL=https://api.remote.it | ||
| REACT_APP_DEV_API_KEY=YOUR_DEV_API_KEY | ||
| ``` | ||
| And then | ||
| ``` | ||
| yarn install | ||
| yarn start | ||
| ``` | ||
|
|
||
| By default the project will be running on http://localhost:3000 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| { | ||
| "name": "remote-it-graphql", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "@testing-library/jest-dom": "^5.11.4", | ||
| "@testing-library/react": "^11.1.0", | ||
| "@testing-library/user-event": "^12.1.10", | ||
| "axios": "^0.23.0", | ||
| "axios-auth-refresh": "^3.2.1", | ||
| "bootstrap": "5", | ||
| "react": "^17.0.2", | ||
| "react-dom": "^17.0.2", | ||
| "react-router-dom": "^5.3.0", | ||
| "react-scripts": "4.0.3", | ||
| "swr": "^1.0.1", | ||
| "web-vitals": "^1.0.1" | ||
| }, | ||
| "scripts": { | ||
| "start": "react-scripts start", | ||
| "build": "react-scripts build", | ||
| "test": "react-scripts test", | ||
| "eject": "react-scripts eject" | ||
| }, | ||
| "eslintConfig": { | ||
| "extends": [ | ||
| "react-app", | ||
| "react-app/jest" | ||
| ] | ||
| }, | ||
| "browserslist": { | ||
| "production": [ | ||
| ">0.2%", | ||
| "not dead", | ||
| "not op_mini all" | ||
| ], | ||
| "development": [ | ||
| "last 1 chrome version", | ||
| "last 1 firefox version", | ||
| "last 1 safari version" | ||
| ] | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <meta name="theme-color" content="#000000" /> | ||
| <meta | ||
| name="description" | ||
| content="Web site created using create-react-app" | ||
| /> | ||
| <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> | ||
| <!-- | ||
| manifest.json provides metadata used when your web app is installed on a | ||
| user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ | ||
| --> | ||
| <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> | ||
| <!-- | ||
| Notice the use of %PUBLIC_URL% in the tags above. | ||
| It will be replaced with the URL of the `public` folder during the build. | ||
| Only files inside the `public` folder can be referenced from the HTML. | ||
|
|
||
| Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will | ||
| work correctly both with client-side routing and a non-root public URL. | ||
| Learn how to configure a non-root public URL by running `npm run build`. | ||
| --> | ||
| <title>React App</title> | ||
| </head> | ||
| <body> | ||
| <noscript>You need to enable JavaScript to run this app.</noscript> | ||
| <div id="root"></div> | ||
| <!-- | ||
| This HTML file is a template. | ||
| If you open it directly in the browser, you will see an empty page. | ||
|
|
||
| You can add webfonts, meta tags, or analytics to this file. | ||
| The build step will place the bundled scripts into the <body> tag. | ||
|
|
||
| To begin the development, run `npm start` or `yarn start`. | ||
| To create a production bundle, use `npm run build` or `yarn build`. | ||
| --> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "short_name": "React App", | ||
| "name": "Create React App Sample", | ||
| "icons": [ | ||
| { | ||
| "src": "favicon.ico", | ||
| "sizes": "64x64 32x32 24x24 16x16", | ||
| "type": "image/x-icon" | ||
| }, | ||
| { | ||
| "src": "logo192.png", | ||
| "type": "image/png", | ||
| "sizes": "192x192" | ||
| }, | ||
| { | ||
| "src": "logo512.png", | ||
| "type": "image/png", | ||
| "sizes": "512x512" | ||
| } | ||
| ], | ||
| "start_url": ".", | ||
| "display": "standalone", | ||
| "theme_color": "#000000", | ||
| "background_color": "#ffffff" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # https://www.robotstxt.org/robotstxt.html | ||
| User-agent: * | ||
| Disallow: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| .App { | ||
| height: 100vh; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { | ||
| BrowserRouter as Router, | ||
| Switch, | ||
| Route, | ||
| } from "react-router-dom"; | ||
| import Login from "./Login"; | ||
| import Home from "./Home"; | ||
| import "bootstrap/dist/css/bootstrap.min.css"; | ||
| import './App.css'; | ||
| function App() { | ||
| return ( | ||
| <div className="App"> | ||
| <Router> | ||
| <Switch> | ||
| <Route exact path="/" component={Login} /> | ||
| <Route exact path="/home" component={Home} /> | ||
| </Switch> | ||
| </Router> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default App; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import App from './App'; | ||
|
|
||
| test('renders learn react link', () => { | ||
| render(<App />); | ||
| const linkElement = screen.getByText(/learn react/i); | ||
| expect(linkElement).toBeInTheDocument(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import React from "react"; | ||
| import useSWR from 'swr' | ||
| import {fetcher} from "./axios"; | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both devices query and events query should have examples of issuing a new request if the hasMore is true and incrementing the from variable. |
||
| const devicesQuery = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should rename the home to devices.jsx so that it is easier to find. |
||
| query: `{ | ||
| login { | ||
| devices(size: 10, from: 0) { | ||
| total | ||
| hasMore | ||
| items { | ||
| id | ||
| name | ||
| hardwareId | ||
| created | ||
| state | ||
| endpoint{geo{latitude longitude}} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ` | ||
| } | ||
|
|
||
| const eventsQuery = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would also recommend that we move events information to its own events page. |
||
| query: `{ | ||
| login { | ||
| events(size: 10, from: 0) { | ||
| hasMore | ||
| total | ||
| items { | ||
| type | ||
| owner { | ||
| } | ||
| actor { | ||
| } | ||
| target { | ||
| created | ||
| id | ||
| name | ||
| } | ||
| users { | ||
| } | ||
| timestamp | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ` | ||
| } | ||
|
|
||
| const Home = () => { | ||
|
|
||
| const dataDevices = useSWR('devices', () => fetcher('/graphql/v1', devicesQuery)); | ||
|
|
||
| const dataEvents = useSWR('events', () => fetcher('/graphql/v1', eventsQuery)); | ||
|
|
||
|
|
||
| function formatDate(date){ | ||
| let newDate = new Date(date); | ||
|
|
||
| return newDate.toLocaleDateString() | ||
| } | ||
| function getStatusColor(status){ | ||
| if (status === 'active'){ | ||
| return 'text-success' | ||
| }else { | ||
| return 'text-danger' | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <div className="container"> | ||
| <div className="m-5"> | ||
| <h3 className="h3">Number of devices: {dataDevices.data?.data?.login?.devices?.total}</h3> | ||
| <table className="table table-hover"> | ||
| <thead> | ||
| <tr> | ||
| <th scope="col">Device id</th> | ||
| <th scope="col">Name</th> | ||
| <th scope="col">hardwareId</th> | ||
| <th scope="col">Created</th> | ||
| <th scope="col">State</th> | ||
| <th scope="col">Geo-localisation</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| { | ||
| dataDevices.data?.data?.login?.devices?.items.map((device, index) => | ||
| <tr> | ||
| <th scope="row">{device.id}</th> | ||
| <td>{device.name}</td> | ||
| <td>{device.hardwareId}</td> | ||
| <td>{formatDate(device.created)}</td> | ||
| <td className={getStatusColor(device.state)}>{device.state}</td> | ||
| <td><a href={`https://www.google.com/maps/place/${device.endpoint.geo.latitude},${device.endpoint.geo.longitude}`} target="_blank" | ||
| rel="noopener noreferrer">See localisation</a></td> | ||
| </tr> | ||
| ) | ||
| } | ||
| </tbody> | ||
| </table> | ||
| </div> | ||
|
|
||
| <hr /> | ||
|
|
||
| <div className="m-5"> | ||
| <h3 className="h3">Number of events: {dataEvents.data?.data?.login?.events?.total}</h3> | ||
| <table className="table table-hover"> | ||
| <thead> | ||
| <tr> | ||
| <th scope="col">Type</th> | ||
| <th scope="col">Owner</th> | ||
| <th scope="col">Actor</th> | ||
| <th scope="col">Target Name</th> | ||
| <th scope="col">Time</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| { | ||
| dataEvents.data?.data?.login?.events?.items?.map((event, index) => ( | ||
| <tr> | ||
| <th scope="row">{event.type}</th> | ||
| <td>{event.owner?.email}</td> | ||
| <td>{event.actor?.email}</td> | ||
| <td>{event.target?.map((target, index) => ( | ||
| <p>{target.name} |</p> | ||
| ))}</td> | ||
| <td>{event.timestamp}</td> | ||
| </tr> | ||
| )) | ||
| } | ||
| </tbody> | ||
| </table> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default Home; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add information about where they can see the graphQL query and results properly.