Merge branch '17782-react-scripts-ts-migration' into main. Closes #17782
[arvados.git] / src / store / link-account-panel / link-account-panel-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { linkAccountPanelActions, LinkAccountPanelAction } from "store/link-account-panel/link-account-panel-actions";
6 import { UserResource } from "models/user";
7
8 export enum LinkAccountPanelStatus {
9     NONE,
10     INITIAL,
11     HAS_SESSION_DATA,
12     LINKING,
13     ERROR
14 }
15
16 export enum LinkAccountPanelError {
17     NONE,
18     INACTIVE,
19     NON_ADMIN,
20     SAME_USER
21 }
22
23 export enum OriginatingUser {
24     NONE,
25     TARGET_USER,
26     USER_TO_LINK
27 }
28
29 export interface LinkAccountPanelState {
30     selectedCluster: string | undefined;
31     originatingUser: OriginatingUser | undefined;
32     targetUser: UserResource | undefined;
33     targetUserToken: string | undefined;
34     userToLink: UserResource | undefined;
35     userToLinkToken: string | undefined;
36     status: LinkAccountPanelStatus;
37     error: LinkAccountPanelError;
38     isProcessing: boolean;
39 }
40
41 const initialState = {
42     selectedCluster: undefined,
43     originatingUser: OriginatingUser.NONE,
44     targetUser: undefined,
45     targetUserToken: undefined,
46     userToLink: undefined,
47     userToLinkToken: undefined,
48     isProcessing: false,
49     status: LinkAccountPanelStatus.NONE,
50     error: LinkAccountPanelError.NONE
51 };
52
53 export const linkAccountPanelReducer = (state: LinkAccountPanelState = initialState, action: LinkAccountPanelAction) =>
54     linkAccountPanelActions.match(action, {
55         default: () => state,
56         LINK_INIT: ({ targetUser }) => ({
57             ...state,
58             targetUser, targetUserToken: undefined,
59             userToLink: undefined, userToLinkToken: undefined,
60             status: LinkAccountPanelStatus.INITIAL, error: LinkAccountPanelError.NONE, originatingUser: OriginatingUser.NONE
61         }),
62         LINK_LOAD: ({ originatingUser, userToLink, targetUser, targetUserToken, userToLinkToken}) => ({
63             ...state,
64             originatingUser,
65             targetUser, targetUserToken,
66             userToLink, userToLinkToken,
67             status: LinkAccountPanelStatus.LINKING, error: LinkAccountPanelError.NONE
68         }),
69         LINK_INVALID: ({ originatingUser, targetUser, userToLink, error }) => ({
70             ...state,
71             originatingUser,
72             targetUser, targetUserToken: undefined,
73             userToLink, userToLinkToken: undefined,
74             error, status: LinkAccountPanelStatus.ERROR
75         }),
76         SET_SELECTED_CLUSTER: ({ selectedCluster }) => ({
77             ...state, selectedCluster
78         }),
79         SET_IS_PROCESSING: ({ isProcessing }) =>({
80             ...state,
81             isProcessing
82         }),
83         HAS_SESSION_DATA: () => ({
84             ...state, status: LinkAccountPanelStatus.HAS_SESSION_DATA
85         })
86     });