Merge branch 'master' into 15088-merge-account
[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, UserResource } 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         SAVE_USER: (user: UserResource) => {
40             return { ...state, user};
41         },
42         CONFIG: ({ config }) => {
43             return {
44                 ...state,
45                 localCluster: config.uuidPrefix,
46                 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
47                 homeCluster: config.uuidPrefix
48             };
49         },
50         REMOTE_CLUSTER_CONFIG: ({ config }) => {
51             return {
52                 ...state,
53                 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
54             };
55         },
56         INIT: ({ user, token }) => {
57             return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
58         },
59         LOGIN: () => {
60             return state;
61         },
62         LOGOUT: () => {
63             return { ...state, apiToken: undefined };
64         },
65         USER_DETAILS_SUCCESS: (user: User) => {
66             return { ...state, user };
67         },
68         SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
69             return { ...state, sshKeys };
70         },
71         ADD_SSH_KEY: (sshKey: SshKeyResource) => {
72             return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
73         },
74         REMOVE_SSH_KEY: (uuid: string) => {
75             return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
76         },
77         SET_HOME_CLUSTER: (homeCluster: string) => {
78             return { ...state, homeCluster };
79         },
80         SET_SESSIONS: (sessions: Session[]) => {
81             return { ...state, sessions };
82         },
83         ADD_SESSION: (session: Session) => {
84             return { ...state, sessions: state.sessions.concat(session) };
85         },
86         REMOVE_SESSION: (clusterId: string) => {
87             return {
88                 ...state,
89                 sessions: state.sessions.filter(
90                     session => session.clusterId !== clusterId
91                 )
92             };
93         },
94         UPDATE_SESSION: (session: Session) => {
95             return {
96                 ...state,
97                 sessions: state.sessions.map(
98                     s => s.clusterId === session.clusterId ? session : s
99                 )
100             };
101         },
102         default: () => state
103     });
104 };