Replace typesafe-actions with unionize, add fetching user details action
[arvados-workbench2.git] / src / store / auth-action.ts
index 02fdcac20215cb0c57181db6fc29e07f0f2b39fb..719cb81be7469893c2bb907196fdaca78dd782f7 100644 (file)
@@ -2,14 +2,40 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { ActionType, createStandardAction } from "typesafe-actions";
+import { serverApi } from "../common/server-api";
+import { ofType, default as unionize, UnionOf } from "unionize";
+import { Dispatch } from "redux";
 
-const actions = {
-    saveApiToken: createStandardAction('@@auth/saveApiToken')<string>(),
-    getUserTokenDetails: createStandardAction('@@auth/userTokenDetails')(),
-    login: createStandardAction('@@auth/login')(),
-    logout: createStandardAction('@@auth/logout')()
-};
+export interface UserDetailsResponse {
+    email: string;
+    first_name: string;
+    last_name: string;
+    is_admin: boolean;
+}
+
+const actions = unionize({
+    SAVE_API_TOKEN: ofType<string>(),
+    LOGIN: {},
+    LOGOUT: {},
+    USER_DETAILS_REQUEST: {},
+    USER_DETAILS_SUCCESS: ofType<UserDetailsResponse>()
+}, {
+    tag: 'type',
+    value: 'payload'
+});
 
-export type AuthAction = ActionType<typeof actions>;
+export type AuthAction = UnionOf<typeof actions>;
 export default actions;
+
+export const getUserDetails = () => (dispatch: Dispatch) => {
+    dispatch(actions.USER_DETAILS_REQUEST());
+    serverApi
+        .get<UserDetailsResponse>('/users/current')
+        .then(resp => {
+            dispatch(actions.USER_DETAILS_SUCCESS(resp.data));
+        })
+        // .catch(err => {
+        // });
+};
+
+