aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/store/users.ts
blob: 8e1054686cb26a175eb376d7d400f8693b1a610d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * SonarQube
 * Copyright (C) 2009-2018 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { uniq } from 'lodash';
import { Dispatch, combineReducers } from 'redux';
import { ActionType } from './utils/actions';
import * as api from '../api/users';
import { isLoggedIn } from '../helpers/users';

export function receiveCurrentUser(user: T.CurrentUser) {
  return { type: 'RECEIVE_CURRENT_USER', user };
}

function skipOnboardingAction() {
  return { type: 'SKIP_ONBOARDING' };
}

export function skipOnboarding() {
  return (dispatch: Dispatch) =>
    api
      .skipOnboarding()
      .then(() => dispatch(skipOnboardingAction()), () => dispatch(skipOnboardingAction()));
}

function setHomePageAction(homepage: T.HomePage) {
  return { type: 'SET_HOMEPAGE', homepage };
}

export function setHomePage(homepage: T.HomePage) {
  return (dispatch: Dispatch) => {
    api.setHomePage(homepage).then(
      () => {
        dispatch(setHomePageAction(homepage));
      },
      () => {}
    );
  };
}

type Action =
  | ActionType<typeof receiveCurrentUser, 'RECEIVE_CURRENT_USER'>
  | ActionType<typeof skipOnboardingAction, 'SKIP_ONBOARDING'>
  | ActionType<typeof setHomePageAction, 'SET_HOMEPAGE'>;

export interface State {
  usersByLogin: { [login: string]: any };
  userLogins: string[];
  currentUser: T.CurrentUser;
}

function usersByLogin(state: State['usersByLogin'] = {}, action: Action): State['usersByLogin'] {
  if (action.type === 'RECEIVE_CURRENT_USER' && isLoggedIn(action.user)) {
    return { ...state, [action.user.login]: action.user };
  } else {
    return state;
  }
}

function userLogins(state: State['userLogins'] = [], action: Action): State['userLogins'] {
  if (action.type === 'RECEIVE_CURRENT_USER' && isLoggedIn(action.user)) {
    return uniq([...state, action.user.login]);
  } else {
    return state;
  }
}

function currentUser(
  state: State['currentUser'] = { isLoggedIn: false },
  action: Action
): State['currentUser'] {
  if (action.type === 'RECEIVE_CURRENT_USER') {
    return action.user;
  }
  if (action.type === 'SKIP_ONBOARDING' && isLoggedIn(state)) {
    return { ...state, showOnboardingTutorial: false } as T.LoggedInUser;
  }
  if (action.type === 'SET_HOMEPAGE' && isLoggedIn(state)) {
    return { ...state, homepage: action.homepage } as T.LoggedInUser;
  }
  return state;
}

export default combineReducers({ usersByLogin, userLogins, currentUser });

export function getCurrentUser(state: State) {
  return state.currentUser;
}

export function getUserByLogin(state: State, login: string) {
  return state.usersByLogin[login];
}

export function getUsersByLogins(state: State, logins: string[]) {
  return logins.map(login => getUserByLogin(state, login));
}