Skip to content
Snippets Groups Projects
Unverified Commit a7bb5969 authored by Aleksandr Lesnenko's avatar Aleksandr Lesnenko Committed by GitHub
Browse files

convert user reducer to RTK (#36353)

parent 1f2447e0
No related branches found
No related tags found
No related merge requests found
// Reducers needed for admin section (only used in "main" app)
import app from "metabase/admin/app/reducers";
import { appReducer as app } from "metabase/admin/app/reducers";
import people from "metabase/admin/people/people";
import databases from "metabase/admin/databases/database";
import datamodel from "metabase/admin/datamodel/datamodel";
......
import { createAsyncThunk } from "@reduxjs/toolkit";
import Settings from "metabase/lib/settings";
import { createAction, createThunkAction } from "metabase/lib/redux";
import { updateSetting } from "metabase/admin/settings/settings";
export const DISABLE_NOTICE = "metabase/admin/app/DISABLE_NOTICE";
export const disableNotice = createThunkAction(
DISABLE_NOTICE,
() => async (dispatch: any) => {
export const disableNotice = createAsyncThunk(
"metabase/admin/app/DISABLE_NOTICE",
async (_, { dispatch }) => {
const setting = {
key: "deprecation-notice-version",
value: Settings.currentVersion(),
......@@ -13,11 +12,3 @@ export const disableNotice = createThunkAction(
await dispatch(updateSetting(setting));
},
);
export const DISABLE_ADMIN_PATH = "metabase/admin/app/DISABLE_ADMIN_PATH";
export const disableAdminPath = createAction(
DISABLE_ADMIN_PATH,
(pathKey: string) => {
return pathKey;
},
);
import { t } from "ttag";
import { combineReducers, handleActions } from "metabase/lib/redux";
import { createReducer } from "@reduxjs/toolkit";
import { combineReducers } from "metabase/lib/redux";
import Settings from "metabase/lib/settings";
import {
PLUGIN_ADMIN_ALLOWED_PATH_GETTERS,
PLUGIN_ADMIN_NAV_ITEMS,
PLUGIN_ADMIN_TOOLS,
} from "metabase/plugins";
import { REFRESH_CURRENT_USER } from "metabase/redux/user";
import { refreshCurrentUser } from "metabase/redux/user";
import type { AdminPath, AdminPathKey } from "metabase-types/store";
import { isNotNull } from "metabase/lib/types";
import { DISABLE_ADMIN_PATH, DISABLE_NOTICE } from "./actions";
import { disableNotice } from "./actions";
const getAdminPaths: () => AdminPath[] = () => {
const items: AdminPath[] = [
......@@ -59,46 +60,35 @@ const getAdminPaths: () => AdminPath[] = () => {
return items;
};
const paths = handleActions(
{
[DISABLE_ADMIN_PATH]: {
next: (state: AdminPath[], { payload: pathKey }: { payload: any }) => {
return state.filter(path => path.key !== pathKey);
},
},
[REFRESH_CURRENT_USER]: {
next: (state: AdminPath[], { payload: user }: { payload: any }) => {
if (user.is_superuser) {
return state;
}
const paths = createReducer(getAdminPaths(), builder => {
builder.addCase(refreshCurrentUser.fulfilled, (state, { payload: user }) => {
if (user?.is_superuser) {
return state;
}
const allowedPaths = PLUGIN_ADMIN_ALLOWED_PATH_GETTERS.map(getter => {
return getter(user);
})
.flat()
.reduce((acc, pathKey) => {
acc.add(pathKey);
return acc;
}, new Set<AdminPathKey>());
const allowedPaths = PLUGIN_ADMIN_ALLOWED_PATH_GETTERS.map(getter => {
return getter(user);
})
.flat()
.reduce((acc, pathKey) => {
acc.add(pathKey);
return acc;
}, new Set<AdminPathKey>());
return state
.filter(path => (allowedPaths.has(path.key) ? path : null))
.filter(isNotNull);
},
},
},
getAdminPaths(),
);
return state
.filter(path => (allowedPaths.has(path.key) ? path : null))
.filter(isNotNull);
});
});
const isNoticeEnabled = handleActions(
{
[DISABLE_NOTICE]: { next: () => false },
},
const isNoticeEnabled = createReducer(
Settings.deprecationNoticeEnabled(),
builder => {
builder.addCase(disableNotice.fulfilled, () => false);
},
);
// eslint-disable-next-line import/no-default-export -- deprecated usage
export default combineReducers({
export const appReducer = combineReducers({
isNoticeEnabled,
paths,
} as any);
});
......@@ -25,14 +25,3 @@ export const hasDeprecatedDatabase = (state: State, props: Props): boolean => {
export const getAdminPaths = (state: State) => {
return state.admin?.app?.paths ?? [];
};
export const canAccessAdmin = (state: State): boolean => {
return getAdminPaths(state).length > 0;
};
export const canAccessPath = (
state: State,
{ key }: { key: string },
): boolean => {
return state.admin.app.paths?.find(path => path.key === key) != null;
};
import {
createAction,
createThunkAction,
handleActions,
} from "metabase/lib/redux";
import { UserApi } from "metabase/services";
import { CLOSE_QB_NEWB_MODAL } from "metabase/query_builder/actions";
import Users from "metabase/entities/users";
export const REFRESH_CURRENT_USER = "metabase/user/REFRESH_CURRENT_USER";
export const refreshCurrentUser = createAction(REFRESH_CURRENT_USER, () => {
try {
return UserApi.current();
} catch (e) {
return null;
}
});
export const LOAD_CURRENT_USER = "metabase/user/LOAD_CURRENT_USER";
export const loadCurrentUser = createThunkAction(
LOAD_CURRENT_USER,
() => async (dispatch, getState) => {
if (!getState().currentUser) {
await dispatch(refreshCurrentUser());
}
},
);
export const CLEAR_CURRENT_USER = "metabase/user/CLEAR_CURRENT_USER";
export const clearCurrentUser = createAction(CLEAR_CURRENT_USER);
export const currentUser = handleActions(
{
[CLEAR_CURRENT_USER]: { next: (state, payload) => null },
[REFRESH_CURRENT_USER]: { next: (state, { payload }) => payload },
[CLOSE_QB_NEWB_MODAL]: {
next: (state, { payload }) => ({ ...state, is_qbnewb: false }),
},
[Users.actionTypes.UPDATE]: {
next: (state, { payload }) => {
const isCurrentUserUpdated = state.id === payload.user.id;
if (isCurrentUserUpdated) {
return {
...state,
...payload.user,
};
}
return state;
},
},
},
null,
);
import { createAction, createReducer } from "@reduxjs/toolkit";
import { UserApi } from "metabase/services";
import { CLOSE_QB_NEWB_MODAL } from "metabase/query_builder/actions";
import Users from "metabase/entities/users";
import { createAsyncThunk } from "metabase/lib/redux";
import type { User } from "metabase-types/api";
export const refreshCurrentUser = createAsyncThunk(
"metabase/user/REFRESH_CURRENT_USER",
async (_, { fulfillWithValue }) => {
try {
return UserApi.current();
} catch (e) {
return fulfillWithValue(null);
}
},
);
export const loadCurrentUser = createAsyncThunk(
"metabase/user/LOAD_CURRENT_USER",
async (_, { dispatch, getState }) => {
if (!getState().currentUser) {
await dispatch(refreshCurrentUser());
}
},
);
export const clearCurrentUser = createAction(
"metabase/user/CLEAR_CURRENT_USER",
);
export const currentUser = createReducer<User | null>(null, builder => {
builder
.addCase(clearCurrentUser, () => null)
.addCase(refreshCurrentUser.fulfilled, (state, action) => action.payload)
.addCase(CLOSE_QB_NEWB_MODAL, state => {
if (state) {
state.is_qbnewb = false;
return state;
}
return state;
})
.addCase(Users.actionTypes.UPDATE, (state, { payload }) => {
const isCurrentUserUpdated = state?.id === payload.user.id;
if (isCurrentUserUpdated) {
return {
...state,
...payload.user,
};
}
return state;
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment