Introduce service repository
[arvados.git] / src / store / auth / auth-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ofType, default as unionize, UnionOf } from "unionize";
6 import { Dispatch } from "redux";
7 import { User } from "../../models/user";
8 import { RootState } from "../store";
9 import { ServiceRepository } from "../../services/services";
10
11 export const authActions = unionize({
12     SAVE_API_TOKEN: ofType<string>(),
13     LOGIN: {},
14     LOGOUT: {},
15     INIT: {},
16     USER_DETAILS_REQUEST: {},
17     USER_DETAILS_SUCCESS: ofType<User>()
18 }, {
19     tag: 'type',
20     value: 'payload'
21 });
22
23 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
24     dispatch(authActions.USER_DETAILS_REQUEST());
25     return services.authService.getUserDetails().then(details => {
26         dispatch(authActions.USER_DETAILS_SUCCESS(details));
27         return details;
28     });
29 };
30
31 export type AuthAction = UnionOf<typeof authActions>;