Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

users.ts 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { uniq } from 'lodash';
  21. import { combineReducers, Dispatch } from 'redux';
  22. import * as api from '../api/users';
  23. import { isLoggedIn } from '../helpers/users';
  24. import { ActionType } from './utils/actions';
  25. const enum Actions {
  26. ReceiveCurrentUser = 'RECEIVE_CURRENT_USER',
  27. SetCurrentUserSetting = 'SET_CURRENT_USER_SETTING',
  28. SkipOnboardingAction = 'SKIP_ONBOARDING',
  29. SetHomePageAction = 'SET_HOMEPAGE'
  30. }
  31. type Action =
  32. | ActionType<typeof receiveCurrentUser, Actions.ReceiveCurrentUser>
  33. | ActionType<typeof setCurrentUserSettingAction, Actions.SetCurrentUserSetting>
  34. | ActionType<typeof setHomePageAction, Actions.SetHomePageAction>
  35. | ActionType<typeof skipOnboardingAction, Actions.SkipOnboardingAction>;
  36. export interface State {
  37. usersByLogin: T.Dict<any>;
  38. userLogins: string[];
  39. currentUser: T.CurrentUser;
  40. }
  41. export function receiveCurrentUser(user: T.CurrentUser) {
  42. return { type: Actions.ReceiveCurrentUser, user };
  43. }
  44. export function skipOnboardingAction() {
  45. return { type: Actions.SkipOnboardingAction };
  46. }
  47. export function skipOnboarding() {
  48. return (dispatch: Dispatch) =>
  49. api.skipOnboarding().then(
  50. () => dispatch(skipOnboardingAction()),
  51. () => dispatch(skipOnboardingAction())
  52. );
  53. }
  54. export function setHomePageAction(homepage: T.HomePage) {
  55. return { type: Actions.SetHomePageAction, homepage };
  56. }
  57. export function setCurrentUserSettingAction(setting: T.CurrentUserSetting) {
  58. return { type: Actions.SetCurrentUserSetting, setting };
  59. }
  60. export function setHomePage(homepage: T.HomePage) {
  61. return (dispatch: Dispatch) => {
  62. api.setHomePage(homepage).then(
  63. () => {
  64. dispatch(setHomePageAction(homepage));
  65. },
  66. () => {}
  67. );
  68. };
  69. }
  70. export function setCurrentUserSetting(setting: T.CurrentUserSetting) {
  71. return (dispatch: Dispatch, getState: () => { users: State }) => {
  72. const oldSetting = getCurrentUserSetting(getState().users, setting.key);
  73. dispatch(setCurrentUserSettingAction(setting));
  74. api.setUserSetting(setting).then(
  75. () => {},
  76. () => {
  77. dispatch(setCurrentUserSettingAction({ ...setting, value: oldSetting || '' }));
  78. }
  79. );
  80. };
  81. }
  82. function usersByLogin(state: State['usersByLogin'] = {}, action: Action): State['usersByLogin'] {
  83. if (action.type === Actions.ReceiveCurrentUser && isLoggedIn(action.user)) {
  84. return { ...state, [action.user.login]: action.user };
  85. } else {
  86. return state;
  87. }
  88. }
  89. function userLogins(state: State['userLogins'] = [], action: Action): State['userLogins'] {
  90. if (action.type === Actions.ReceiveCurrentUser && isLoggedIn(action.user)) {
  91. return uniq([...state, action.user.login]);
  92. } else {
  93. return state;
  94. }
  95. }
  96. function currentUser(
  97. state: State['currentUser'] = { isLoggedIn: false },
  98. action: Action
  99. ): State['currentUser'] {
  100. if (action.type === Actions.ReceiveCurrentUser) {
  101. return action.user;
  102. }
  103. if (action.type === Actions.SkipOnboardingAction && isLoggedIn(state)) {
  104. return { ...state, showOnboardingTutorial: false } as T.LoggedInUser;
  105. }
  106. if (action.type === Actions.SetHomePageAction && isLoggedIn(state)) {
  107. return { ...state, homepage: action.homepage } as T.LoggedInUser;
  108. }
  109. if (action.type === Actions.SetCurrentUserSetting && isLoggedIn(state)) {
  110. let settings: T.CurrentUserSetting[];
  111. if (state.settings) {
  112. settings = [...state.settings];
  113. const index = settings.findIndex(setting => setting.key === action.setting.key);
  114. if (index === -1) {
  115. settings.push(action.setting);
  116. } else {
  117. settings[index] = action.setting;
  118. }
  119. } else {
  120. settings = [action.setting];
  121. }
  122. return { ...state, settings } as T.LoggedInUser;
  123. }
  124. return state;
  125. }
  126. export default combineReducers({ usersByLogin, userLogins, currentUser });
  127. export function getCurrentUser(state: State) {
  128. return state.currentUser;
  129. }
  130. export function getCurrentUserSetting(state: State, key: T.CurrentUserSettingNames) {
  131. let setting;
  132. if (isLoggedIn(state.currentUser) && state.currentUser.settings) {
  133. setting = state.currentUser.settings.find(setting => setting.key === key);
  134. }
  135. return setting && setting.value;
  136. }
  137. export function getUserByLogin(state: State, login: string) {
  138. return state.usersByLogin[login];
  139. }
  140. export function getUsersByLogins(state: State, logins: string[]) {
  141. return logins.map(login => getUserByLogin(state, login));
  142. }