1
- import React , { useReducer , useState } from 'react' ;
1
+ import React , { useReducer } from 'react' ;
2
+ import MiniApp from 'js-miniapp-sdk' ;
3
+ import { displayDate } from '../js_sdk' ;
2
4
3
5
import {
4
6
Button ,
5
- Switch ,
6
7
CircularProgress ,
7
8
FormGroup ,
8
- Grid ,
9
9
Typography ,
10
10
CardContent ,
11
11
} from '@material-ui/core' ;
12
12
import { red , green } from '@material-ui/core/colors' ;
13
13
import { makeStyles } from '@material-ui/core/styles' ;
14
- import axios from 'axios' ;
15
14
import clsx from 'clsx' ;
16
15
17
16
import GreyCard from '../components/GreyCard' ;
@@ -73,13 +72,14 @@ const dataFetchReducer = (state, action) => {
73
72
...state ,
74
73
isLoading : false ,
75
74
isError : false ,
76
- response : action . payload ,
75
+ response : action . tokenData ,
77
76
} ;
78
77
case 'FETCH_FAILURE' :
79
78
return {
80
79
...state ,
81
80
isLoading : false ,
82
81
isError : true ,
82
+ errorMessage : action . errorMessage ,
83
83
} ;
84
84
default :
85
85
throw new Error ( ) ;
@@ -95,20 +95,15 @@ function AuthToken() {
95
95
[ classes . buttonSuccess ] : state . response ,
96
96
} ) ;
97
97
98
- const [ switchState , setSwitchState ] = useState ( true ) ;
99
-
100
98
function requestToken ( ) {
101
- // Hardcoded API values to test
102
- const API = switchState
103
- ? 'http://www.mocky.io/v2/5e9406873100006c005e2d00'
104
- : 'http://www.mocky.io/v2/5e9806e43500006a00c47d6f' ;
105
- axios
106
- . get ( API )
99
+ MiniApp . user
100
+ . getAccessToken ( )
107
101
. then ( ( response ) => {
108
- dispatch ( { type : 'FETCH_SUCCESS' , payload : response . data } ) ;
102
+ dispatch ( { type : 'FETCH_SUCCESS' , tokenData : response } ) ;
109
103
} )
110
104
. catch ( ( error ) => {
111
- dispatch ( { type : 'FETCH_FAILURE' } ) ;
105
+ console . error ( error ) ;
106
+ dispatch ( { type : 'FETCH_FAILURE' , errorMessage : error } ) ;
112
107
} ) ;
113
108
}
114
109
@@ -120,26 +115,6 @@ function AuthToken() {
120
115
}
121
116
}
122
117
123
- function SwitchToggle ( ) {
124
- return (
125
- < Typography component = "div" >
126
- < Grid component = "label" container alignItems = "center" spacing = { 1 } >
127
- < Grid item > Success</ Grid >
128
- < Grid item >
129
- < Switch
130
- color = "primary"
131
- checked = { switchState }
132
- onChange = { ( ) => setSwitchState ( ! switchState ) }
133
- name = "switchState"
134
- data-testid = "authSwitch"
135
- />
136
- </ Grid >
137
- < Grid item > Failure</ Grid >
138
- </ Grid >
139
- </ Typography >
140
- ) ;
141
- }
142
-
143
118
function ButtonWrapper ( ) {
144
119
return (
145
120
< div className = { classes . wrapper } >
@@ -164,28 +139,20 @@ function AuthToken() {
164
139
< GreyCard height = "auto" >
165
140
< CardContent >
166
141
< FormGroup column = "true" classes = { { root : classes . rootFormGroup } } >
167
- < Typography variant = "body2" align = "center" >
168
- Please note that we use a < strong > mocked API</ strong > in this
169
- example (
170
- < a href = "http://www.mocky.io/v2/5e9806e43500006a00c47d6f" >
171
- Success
172
- </ a > { ' ' }
173
- & { ' ' }
174
- < a href = "http://www.mocky.io/v2/5e9406873100006c005e2d00" >
175
- Failure
176
- </ a >
177
- )
178
- </ Typography >
179
- { SwitchToggle ( ) }
180
142
{ ButtonWrapper ( ) }
181
143
{ state . isError && (
182
144
< Typography variant = "body1" className = { classes . error } >
183
- Error fetching the Token
145
+ { state . errorMessage }
146
+ </ Typography >
147
+ ) }
148
+ { state . response && (
149
+ < Typography variant = "body1" className = { classes . success } >
150
+ Token: { state . response . token }
184
151
</ Typography >
185
152
) }
186
153
{ state . response && (
187
154
< Typography variant = "body1" className = { classes . success } >
188
- { JSON . stringify ( state . response . data ) }
155
+ Valid until: { displayDate ( state . response . validUntil ) }
189
156
</ Typography >
190
157
) }
191
158
</ FormGroup >
0 commit comments