@machinemetrics/mm-react-tools 2.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -0
- package/dist/index.js +1755 -0
- package/dist/index.js.map +1 -0
- package/dist/index.modern.js +1739 -0
- package/dist/index.modern.js.map +1 -0
- package/package.json +68 -0
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# mm-react-tools
|
|
2
|
+
|
|
3
|
+
Components, hooks, and helper functions for writing React apps using MachineMetrics APIs.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/mm-react-tools) [](https://standardjs.com)
|
|
6
|
+
|
|
7
|
+
## Table of Contents
|
|
8
|
+
|
|
9
|
+
- [Install](#install)
|
|
10
|
+
- [Getting Started](#getting-started)
|
|
11
|
+
- [Client ID and Secret](#client-id-and-secret)
|
|
12
|
+
- [License](#license)
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
yarn add mm-react-tools
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Peer Dependencies
|
|
21
|
+
|
|
22
|
+
Due to how Apollo, React, and other libraries work, there are some additional dependencies that you'll need to include in your project:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
yarn add react react-router-dom styled-components @apollo/client subscriptions-transport-ws graphql
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Getting Started
|
|
29
|
+
|
|
30
|
+
Wrap your application in the `MMProvider` to wire up everything necessary to authenticate against MachineMetrics Cloud with OAuth.
|
|
31
|
+
|
|
32
|
+
```jsx
|
|
33
|
+
import React from 'react';
|
|
34
|
+
import ReactDOM from 'react-dom';
|
|
35
|
+
import { MMProvider } from 'mm-react-tools';
|
|
36
|
+
import App from './App';
|
|
37
|
+
|
|
38
|
+
ReactDOM.render(
|
|
39
|
+
<MMProvider
|
|
40
|
+
domain={process.env.REACT_APP_URL}
|
|
41
|
+
clientId={process.env.REACT_APP_CLIENT_ID}
|
|
42
|
+
clientSecret={process.env.REACT_APP_CLIENT_SECRET}
|
|
43
|
+
releaseStage={process.env.REACT_APP_RELEASE_STAGE}
|
|
44
|
+
scope={'reporting'}
|
|
45
|
+
>
|
|
46
|
+
<App />
|
|
47
|
+
</MMProvider>,
|
|
48
|
+
document.getElementById('root')
|
|
49
|
+
);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Protect a Route
|
|
53
|
+
|
|
54
|
+
Use the `ProtectedRoute` to kick off the login flow.
|
|
55
|
+
|
|
56
|
+
```jsx
|
|
57
|
+
// App.js
|
|
58
|
+
import React from 'react';
|
|
59
|
+
import { Route } from 'react-router-dom';
|
|
60
|
+
import { PrivateLayout } from 'mm-react-tools';
|
|
61
|
+
import PublicPage from './PublicPage';
|
|
62
|
+
import PrivatePage from './PrivatePage';
|
|
63
|
+
|
|
64
|
+
function App() {
|
|
65
|
+
return (
|
|
66
|
+
<Route exact path='/public' element={<PublicPage />} />
|
|
67
|
+
<Route element={<PrivateLayout />}>
|
|
68
|
+
<Route path="/private" element={<PrivatePage />}>
|
|
69
|
+
</Route>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export default App;
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### GraphQL Hooks
|
|
77
|
+
|
|
78
|
+
Apollo hooks for GraphQL are available via `useQuery` and `useSubscription`.
|
|
79
|
+
|
|
80
|
+
```jsx
|
|
81
|
+
// PrivatePage.js
|
|
82
|
+
import React, { useEffect, useState } from 'react';
|
|
83
|
+
import { gql, useQuery } from '@apollo/client';
|
|
84
|
+
|
|
85
|
+
const PrivatePage = () => {
|
|
86
|
+
const [company, setCompany] = useState();
|
|
87
|
+
|
|
88
|
+
const query = gql`
|
|
89
|
+
query {
|
|
90
|
+
companies {
|
|
91
|
+
name
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
`;
|
|
95
|
+
|
|
96
|
+
const { data, loading, error } = useQuery(query);
|
|
97
|
+
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
if (!data || !data.companies || !data.companies.length) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
setCompany(data.companies[0].name);
|
|
104
|
+
}, [data]);
|
|
105
|
+
|
|
106
|
+
return <div>{company}</div>;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export default PrivatePage;
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Client ID and Secret
|
|
113
|
+
|
|
114
|
+
Contact support@machinemetrics.com for a Client ID and Secret.
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
MIT © [MachineMetrics](https://github.com/machinemetrics)
|