Skip to content

Commit 83c39d4

Browse files
ning-yremo5000
authored andcommitted
Add fetching of username (#180)
* Add fetch username * Make name TitleCase
1 parent ea05594 commit 83c39d4

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/components/NavigationBar.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,15 @@ const NavigationBar: React.SFC<INavigationBarProps> = props => (
7272
className="NavigationBar__link pt-button pt-minimal"
7373
>
7474
<Icon icon={IconNames.USER} />
75-
<div className="navbar-button-text hidden-xs">{props.username}</div>
75+
<div className="navbar-button-text hidden-xs">{titleCase(props.username)}</div>
7676
</NavLink>
7777
</>
7878
)}
7979
</NavbarGroup>
8080
</Navbar>
8181
)
8282

83+
const titleCase = (str: string) =>
84+
str.replace(/\w\S*/g, wrd => wrd.charAt(0).toUpperCase() + wrd.substr(1).toLowerCase())
85+
8386
export default NavigationBar

src/sagas/backend.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,37 @@ function* backendSaga(): SagaIterator {
1111
const ivleToken = (action as actionTypes.IAction).payload
1212
const resp = yield call(request, 'auth', {
1313
method: 'POST',
14-
body: JSON.stringify({ login: { ivle_token: ivleToken } })
14+
body: JSON.stringify({ login: { ivle_token: ivleToken } }),
15+
headers: new Headers({
16+
Accept: 'application/json',
17+
'Content-Type': 'application/json'
18+
})
1519
})
1620
const tokens = {
17-
accessToken: resp.refresh_token,
18-
refreshToken: resp.access_token
21+
accessToken: resp.access_token,
22+
refreshToken: resp.refresh_token
1923
}
20-
const username = yield call(() => 'IVLE USER') // TODO: fetchUsername
24+
const username = yield getUsername(tokens.accessToken)
2125
yield put(actions.setTokens(tokens))
2226
yield put(actions.setUsername(username))
2327
yield delay(2000)
2428
yield history.push('/academy')
2529
})
2630
}
2731

32+
function* getUsername(accessToken: string) {
33+
const resp = yield call(request, 'user', {
34+
method: 'GET',
35+
headers: new Headers({
36+
Authorization: `Bearer ${accessToken}`,
37+
Accept: 'application/json'
38+
})
39+
})
40+
return resp.name
41+
}
42+
2843
function request(path: string, opts: {}) {
29-
const defaultOpts = {
30-
headers: {
31-
Accept: 'application/json',
32-
'Content-Type': 'application/json'
33-
}
34-
}
35-
const requestOpts = { ...defaultOpts, ...opts }
36-
return fetch(`${BACKEND_URL}/v1/${path}`, requestOpts)
44+
return fetch(`${BACKEND_URL}/v1/${path}`, opts)
3745
.then(data => data.json())
3846
.catch(err => err)
3947
}

0 commit comments

Comments
 (0)