Replace typesafe-actions with unionize, add fetching user details action
[arvados.git] / src / store / auth-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { serverApi } from "../common/server-api";
6 import { ofType, default as unionize, UnionOf } from "unionize";
7 import { Dispatch } from "redux";
8
9 export interface UserDetailsResponse {
10     email: string;
11     first_name: string;
12     last_name: string;
13     is_admin: boolean;
14 }
15
16 const actions = unionize({
17     SAVE_API_TOKEN: ofType<string>(),
18     LOGIN: {},
19     LOGOUT: {},
20     USER_DETAILS_REQUEST: {},
21     USER_DETAILS_SUCCESS: ofType<UserDetailsResponse>()
22 }, {
23     tag: 'type',
24     value: 'payload'
25 });
26
27 export type AuthAction = UnionOf<typeof actions>;
28 export default actions;
29
30 export const getUserDetails = () => (dispatch: Dispatch) => {
31     dispatch(actions.USER_DETAILS_REQUEST());
32     serverApi
33         .get<UserDetailsResponse>('/users/current')
34         .then(resp => {
35             dispatch(actions.USER_DETAILS_SUCCESS(resp.data));
36         })
37         // .catch(err => {
38         // });
39 };
40
41