0335752678c56421336751dfd79eaef11316db94
[arvados-workbench2.git] / src / store / auth / auth-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { authActions, AuthAction } from "./auth-action";
6 import { User } from "~/models/user";
7 import { ServiceRepository } from "~/services/services";
8 import { SshKeyResource } from '~/models/ssh-key';
9 import { Session } from "~/models/session";
10
11 export interface AuthState {
12     user?: User;
13     apiToken?: string;
14     sshKeys: SshKeyResource[];
15     sessions: Session[];
16     localCluster: string;
17     homeCluster: string;
18     remoteHosts: { [key: string]: string };
19 }
20
21 const initialState: AuthState = {
22     user: undefined,
23     apiToken: undefined,
24     sshKeys: [],
25     sessions: [],
26     localCluster: "",
27     homeCluster: "",
28     remoteHosts: {}
29 };
30
31 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
32     return authActions.match(action, {
33         SAVE_API_TOKEN: (token: string) => {
34             return { ...state, apiToken: token };
35         },
36         CONFIG: ({ config }) => {
37             return {
38                 ...state,
39                 localCluster: config.uuidPrefix,
40                 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
41                 homeCluster: config.uuidPrefix
42             };
43         },
44         INIT: ({ user, token }) => {
45             return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
46         },
47         LOGIN: () => {
48             return state;
49         },
50         LOGOUT: () => {
51             return { ...state, apiToken: undefined };
52         },
53         USER_DETAILS_SUCCESS: (user: User) => {
54             return { ...state, user };
55         },
56         SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
57             return { ...state, sshKeys };
58         },
59         ADD_SSH_KEY: (sshKey: SshKeyResource) => {
60             return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
61         },
62         REMOVE_SSH_KEY: (uuid: string) => {
63             return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
64         },
65         SET_HOME_CLUSTER: (homeCluster: string) => {
66             return { ...state, homeCluster };
67         },
68         SET_SESSIONS: (sessions: Session[]) => {
69             return { ...state, sessions };
70         },
71         ADD_SESSION: (session: Session) => {
72             return { ...state, sessions: state.sessions.concat(session) };
73         },
74         REMOVE_SESSION: (clusterId: string) => {
75             return {
76                 ...state,
77                 sessions: state.sessions.filter(
78                     session => session.clusterId !== clusterId
79                 )
80             };
81         },
82         UPDATE_SESSION: (session: Session) => {
83             return {
84                 ...state,
85                 sessions: state.sessions.map(
86                     s => s.clusterId === session.clusterId ? session : s
87                 )
88             };
89         },
90         default: () => state
91     });
92 };