Merge branch 'master' into #15165-running-a-process-from-workflow-section-doesnt...
[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 import { Config } from '~/common/config';
11
12 export interface AuthState {
13     user?: User;
14     apiToken?: string;
15     sshKeys: SshKeyResource[];
16     sessions: Session[];
17     localCluster: string;
18     homeCluster: string;
19     remoteHosts: { [key: string]: string };
20     remoteHostsConfig: { [key: string]: Config };
21 }
22
23 const initialState: AuthState = {
24     user: undefined,
25     apiToken: undefined,
26     sshKeys: [],
27     sessions: [],
28     localCluster: "",
29     homeCluster: "",
30     remoteHosts: {},
31     remoteHostsConfig: {}
32 };
33
34 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
35     return authActions.match(action, {
36         SAVE_API_TOKEN: (token: string) => {
37             return { ...state, apiToken: token };
38         },
39         CONFIG: ({ config }) => {
40             return {
41                 ...state,
42                 localCluster: config.uuidPrefix,
43                 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
44                 homeCluster: config.uuidPrefix
45             };
46         },
47         REMOTE_CLUSTER_CONFIG: ({ config }) => {
48             return {
49                 ...state,
50                 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
51             };
52         },
53         INIT: ({ user, token }) => {
54             return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
55         },
56         LOGIN: () => {
57             return state;
58         },
59         LOGOUT: () => {
60             return { ...state, apiToken: undefined };
61         },
62         USER_DETAILS_SUCCESS: (user: User) => {
63             return { ...state, user };
64         },
65         SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
66             return { ...state, sshKeys };
67         },
68         ADD_SSH_KEY: (sshKey: SshKeyResource) => {
69             return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
70         },
71         REMOVE_SSH_KEY: (uuid: string) => {
72             return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
73         },
74         SET_HOME_CLUSTER: (homeCluster: string) => {
75             return { ...state, homeCluster };
76         },
77         SET_SESSIONS: (sessions: Session[]) => {
78             return { ...state, sessions };
79         },
80         ADD_SESSION: (session: Session) => {
81             return { ...state, sessions: state.sessions.concat(session) };
82         },
83         REMOVE_SESSION: (clusterId: string) => {
84             return {
85                 ...state,
86                 sessions: state.sessions.filter(
87                     session => session.clusterId !== clusterId
88                 )
89             };
90         },
91         UPDATE_SESSION: (session: Session) => {
92             return {
93                 ...state,
94                 sessions: state.sessions.map(
95                     s => s.clusterId === session.clusterId ? session : s
96                 )
97             };
98         },
99         default: () => state
100     });
101 };