15088: Updates state to account for both types of linking and adds UI
[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
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         SAVE_USER: (user: UserResource) => {
37             return { ...state, user};
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         INIT: ({ user, token }) => {
48             return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
49         },
50         LOGIN: () => {
51             return state;
52         },
53         LOGOUT: () => {
54             return { ...state, apiToken: undefined };
55         },
56         USER_DETAILS_SUCCESS: (user: User) => {
57             return { ...state, user };
58         },
59         SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
60             return { ...state, sshKeys };
61         },
62         ADD_SSH_KEY: (sshKey: SshKeyResource) => {
63             return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
64         },
65         REMOVE_SSH_KEY: (uuid: string) => {
66             return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
67         },
68         SET_HOME_CLUSTER: (homeCluster: string) => {
69             return { ...state, homeCluster };
70         },
71         SET_SESSIONS: (sessions: Session[]) => {
72             return { ...state, sessions };
73         },
74         ADD_SESSION: (session: Session) => {
75             return { ...state, sessions: state.sessions.concat(session) };
76         },
77         REMOVE_SESSION: (clusterId: string) => {
78             return {
79                 ...state,
80                 sessions: state.sessions.filter(
81                     session => session.clusterId !== clusterId
82                 )
83             };
84         },
85         UPDATE_SESSION: (session: Session) => {
86             return {
87                 ...state,
88                 sessions: state.sessions.map(
89                     s => s.clusterId === session.clusterId ? session : s
90                 )
91             };
92         },
93         default: () => state
94     });
95 };