Merge branch 'origin/master' into 14478-log-in-into-clusters
[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 }
17
18 const initialState: AuthState = {
19     user: undefined,
20     apiToken: undefined,
21     sshKeys: [],
22     sessions: []
23 };
24
25 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
26     return authActions.match(action, {
27         SAVE_API_TOKEN: (token: string) => {
28             return {...state, apiToken: token};
29         },
30         INIT: ({ user, token }) => {
31             return { ...state, user, apiToken: token };
32         },
33         LOGIN: () => {
34             return state;
35         },
36         LOGOUT: () => {
37             return {...state, apiToken: undefined};
38         },
39         USER_DETAILS_SUCCESS: (user: User) => {
40             return {...state, user};
41         },
42         SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
43             return {...state, sshKeys};
44         },
45         ADD_SSH_KEY: (sshKey: SshKeyResource) => {
46             return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
47         },
48         REMOVE_SSH_KEY: (uuid: string) => {
49             return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid )};
50         },
51         SET_SESSIONS: (sessions: Session[]) => {
52             return { ...state, sessions };
53         },
54         ADD_SESSION: (session: Session) => {
55             return { ...state, sessions: state.sessions.concat(session) };
56         },
57         REMOVE_SESSION: (clusterId: string) => {
58             return {
59                 ...state,
60                 sessions: state.sessions.filter(
61                     session => session.clusterId !== clusterId
62                 )};
63         },
64         UPDATE_SESSION: (session: Session) => {
65             return {
66                 ...state,
67                 sessions: state.sessions.map(
68                     s => s.clusterId === session.clusterId ? session : s
69                 )};
70         },
71         default: () => state
72     });
73 };