1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
11 export interface AuthState {
14 sshKeys: SshKeyResource[];
18 remoteHosts: { [key: string]: string };
21 const initialState: AuthState = {
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 };
36 CONFIG: ({ uuidPrefix, remoteHosts }) => {
38 ...state, localCluster: uuidPrefix, remoteHosts, homeCluster: uuidPrefix
41 INIT: ({ user, token }) => {
42 return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
48 return { ...state, apiToken: undefined };
50 USER_DETAILS_SUCCESS: (user: User) => {
51 return { ...state, user };
53 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
54 return { ...state, sshKeys };
56 ADD_SSH_KEY: (sshKey: SshKeyResource) => {
57 return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
59 REMOVE_SSH_KEY: (uuid: string) => {
60 return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
62 SET_HOME_CLUSTER: (homeCluster: string) => {
63 return { ...state, homeCluster };
65 SET_SESSIONS: (sessions: Session[]) => {
66 return { ...state, sessions };
68 ADD_SESSION: (session: Session) => {
69 return { ...state, sessions: state.sessions.concat(session) };
71 REMOVE_SESSION: (clusterId: string) => {
74 sessions: state.sessions.filter(
75 session => session.clusterId !== clusterId
79 UPDATE_SESSION: (session: Session) => {
82 sessions: state.sessions.map(
83 s => s.clusterId === session.clusterId ? session : s