2019-12-23 12:16:33 +00:00
|
|
|
import _ from "lodash";
|
|
|
|
|
import { createReducer } from "utils/AppsmithUtils";
|
|
|
|
|
import {
|
|
|
|
|
ReduxAction,
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
ReduxActionErrorTypes,
|
|
|
|
|
} from "constants/ReduxActionConstants";
|
|
|
|
|
|
2020-10-19 13:34:55 +00:00
|
|
|
import { DefaultCurrentUserDetails, User } from "constants/userConstants";
|
2019-12-23 12:16:33 +00:00
|
|
|
|
|
|
|
|
const initialState: UsersReduxState = {
|
|
|
|
|
loadingStates: {
|
|
|
|
|
fetchingUsers: false,
|
|
|
|
|
fetchingUser: false,
|
|
|
|
|
},
|
|
|
|
|
list: [],
|
2020-06-17 10:19:56 +00:00
|
|
|
users: [],
|
2020-08-03 14:18:48 +00:00
|
|
|
error: "",
|
|
|
|
|
current: undefined,
|
|
|
|
|
currentUser: undefined,
|
2021-08-05 06:10:19 +00:00
|
|
|
featureFlagFetched: false,
|
2019-12-23 12:16:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const usersReducer = createReducer(initialState, {
|
|
|
|
|
[ReduxActionTypes.FETCH_USER_INIT]: (state: UsersReduxState) => ({
|
|
|
|
|
...state,
|
|
|
|
|
loadingStates: {
|
|
|
|
|
...state.loadingStates,
|
|
|
|
|
fetchingUser: true,
|
|
|
|
|
},
|
|
|
|
|
}),
|
2021-03-29 15:47:22 +00:00
|
|
|
[ReduxActionTypes.PROP_PANE_MOVED]: (
|
|
|
|
|
state: UsersReduxState,
|
|
|
|
|
action: ReduxAction<PropertyPanePositionConfig>,
|
|
|
|
|
) => ({
|
|
|
|
|
...state,
|
|
|
|
|
propPanePreferences: {
|
|
|
|
|
isMoved: true,
|
|
|
|
|
position: {
|
|
|
|
|
...action.payload.position,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
2020-06-17 10:19:56 +00:00
|
|
|
[ReduxActionTypes.FETCH_USER_DETAILS_SUCCESS]: (
|
|
|
|
|
state: UsersReduxState,
|
|
|
|
|
action: ReduxAction<User>,
|
|
|
|
|
) => {
|
|
|
|
|
const users = [...state.users];
|
2020-09-09 05:18:36 +00:00
|
|
|
const userIndex = _.findIndex(users, { username: action.payload.username });
|
2020-06-17 10:19:56 +00:00
|
|
|
if (userIndex > -1) {
|
|
|
|
|
users[userIndex] = action.payload;
|
|
|
|
|
} else {
|
|
|
|
|
users.push(action.payload);
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
loadingStates: {
|
|
|
|
|
...state.loadingStates,
|
|
|
|
|
fetchingUser: false,
|
|
|
|
|
},
|
|
|
|
|
users,
|
|
|
|
|
currentUser: action.payload,
|
|
|
|
|
};
|
|
|
|
|
},
|
2021-03-04 09:37:02 +00:00
|
|
|
[ReduxActionTypes.UPDATE_USER_DETAILS_SUCCESS]: (
|
|
|
|
|
state: UsersReduxState,
|
|
|
|
|
action: ReduxAction<User>,
|
|
|
|
|
) => {
|
|
|
|
|
const users = [...state.users];
|
|
|
|
|
const userIndex = _.findIndex(users, { username: action.payload.username });
|
|
|
|
|
if (userIndex > -1) {
|
|
|
|
|
users[userIndex] = action.payload;
|
|
|
|
|
} else {
|
|
|
|
|
users.push(action.payload);
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
loadingStates: {
|
|
|
|
|
...state.loadingStates,
|
|
|
|
|
fetchingUser: false,
|
|
|
|
|
},
|
|
|
|
|
users,
|
|
|
|
|
currentUser: action.payload,
|
|
|
|
|
};
|
|
|
|
|
},
|
2019-12-23 12:16:33 +00:00
|
|
|
[ReduxActionTypes.FETCH_USER_SUCCESS]: (
|
|
|
|
|
state: UsersReduxState,
|
|
|
|
|
action: ReduxAction<User>,
|
|
|
|
|
) => {
|
|
|
|
|
const users = [...state.list];
|
2020-09-09 05:18:36 +00:00
|
|
|
const userIndex = _.findIndex(users, { username: action.payload.username });
|
2019-12-23 12:16:33 +00:00
|
|
|
if (userIndex > -1) {
|
|
|
|
|
users[userIndex] = action.payload;
|
|
|
|
|
} else {
|
|
|
|
|
users.push(action.payload);
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
loadingStates: {
|
|
|
|
|
...state.loadingStates,
|
|
|
|
|
fetchingUser: false,
|
|
|
|
|
},
|
|
|
|
|
list: users,
|
|
|
|
|
};
|
|
|
|
|
},
|
2020-08-03 14:18:48 +00:00
|
|
|
[ReduxActionErrorTypes.FETCH_USER_DETAILS_ERROR]: (
|
|
|
|
|
state: UsersReduxState,
|
|
|
|
|
action: ReduxAction<{ error: string }>,
|
|
|
|
|
) => ({
|
|
|
|
|
...initialState,
|
|
|
|
|
error: action.payload.error,
|
|
|
|
|
}),
|
2019-12-23 12:16:33 +00:00
|
|
|
[ReduxActionErrorTypes.FETCH_USER_ERROR]: (state: UsersReduxState) => ({
|
|
|
|
|
...state,
|
|
|
|
|
loadingStates: { ...state.loadingStates, fetchingUser: false },
|
|
|
|
|
}),
|
|
|
|
|
[ReduxActionTypes.SET_CURRENT_USER_SUCCESS]: (
|
|
|
|
|
state: UsersReduxState,
|
|
|
|
|
action: ReduxAction<User>,
|
|
|
|
|
) => ({
|
|
|
|
|
...state,
|
|
|
|
|
current: action.payload,
|
|
|
|
|
}),
|
2021-09-12 16:36:43 +00:00
|
|
|
[ReduxActionTypes.LOGOUT_USER_SUCCESS]: (
|
|
|
|
|
state: UsersReduxState,
|
|
|
|
|
action: ReduxAction<boolean>,
|
|
|
|
|
) => ({
|
2020-08-03 14:18:48 +00:00
|
|
|
...state,
|
|
|
|
|
current: undefined,
|
2021-09-12 16:36:43 +00:00
|
|
|
currentUser: {
|
|
|
|
|
...DefaultCurrentUserDetails,
|
|
|
|
|
emptyInstance: action.payload,
|
|
|
|
|
},
|
|
|
|
|
users: [
|
|
|
|
|
{
|
|
|
|
|
...DefaultCurrentUserDetails,
|
|
|
|
|
emptyInstance: action.payload,
|
|
|
|
|
},
|
|
|
|
|
],
|
2020-08-03 14:18:48 +00:00
|
|
|
}),
|
2021-08-05 06:10:19 +00:00
|
|
|
[ReduxActionTypes.FETCH_FEATURE_FLAGS_SUCCESS]: (state: UsersReduxState) => ({
|
|
|
|
|
...state,
|
|
|
|
|
featureFlagFetched: true,
|
|
|
|
|
}),
|
|
|
|
|
[ReduxActionErrorTypes.FETCH_FEATURE_FLAGS_ERROR]: (
|
|
|
|
|
state: UsersReduxState,
|
|
|
|
|
) => ({
|
|
|
|
|
...state,
|
|
|
|
|
featureFlagFetched: true,
|
|
|
|
|
}),
|
2019-12-23 12:16:33 +00:00
|
|
|
});
|
|
|
|
|
|
2021-03-29 15:47:22 +00:00
|
|
|
export interface PropertyPanePositionConfig {
|
|
|
|
|
isMoved: boolean;
|
|
|
|
|
position: {
|
|
|
|
|
left: number;
|
|
|
|
|
top: number;
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-12-23 12:16:33 +00:00
|
|
|
export interface UsersReduxState {
|
|
|
|
|
current?: User;
|
|
|
|
|
list: User[];
|
|
|
|
|
loadingStates: {
|
|
|
|
|
fetchingUser: boolean;
|
|
|
|
|
fetchingUsers: boolean;
|
|
|
|
|
};
|
2020-06-17 10:19:56 +00:00
|
|
|
users: User[];
|
|
|
|
|
currentUser?: User;
|
2020-08-03 14:18:48 +00:00
|
|
|
error: string;
|
2021-03-29 15:47:22 +00:00
|
|
|
propPanePreferences?: PropertyPanePositionConfig;
|
2021-08-05 06:10:19 +00:00
|
|
|
featureFlagFetched: boolean;
|
2019-12-23 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default usersReducer;
|