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 const initialState: AuthState = {
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};
30 INIT: ({ user, token }) => {
31 return { ...state, user, apiToken: token };
37 return {...state, apiToken: undefined};
39 USER_DETAILS_SUCCESS: (user: User) => {
40 return {...state, user};
42 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
43 return {...state, sshKeys};
45 ADD_SSH_KEY: (sshKey: SshKeyResource) => {
46 return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
48 REMOVE_SSH_KEY: (uuid: string) => {
49 return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid )};
51 SET_SESSIONS: (sessions: Session[]) => {
52 return { ...state, sessions };
54 ADD_SESSION: (session: Session) => {
55 return { ...state, sessions: state.sessions.concat(session) };
57 REMOVE_SESSION: (clusterId: string) => {
60 sessions: state.sessions.filter(
61 session => session.clusterId !== clusterId
64 UPDATE_SESSION: (session: Session) => {
67 sessions: state.sessions.map(
68 s => s.clusterId === session.clusterId ? session : s