refactored usage of wrong team display

This commit is contained in:
Rares Mardare 2022-09-09 16:03:11 +03:00
parent 1a778f95c2
commit ced13786df
13 changed files with 27 additions and 46 deletions

View file

@ -17,14 +17,17 @@ export interface WrongTeamStubProps {
className?: string;
objectName: string;
pageName: string;
currentTeam?: string;
switchToTeam?: { name: string; id: string };
wrongTeamNoPermissions?: boolean;
}
const WrongTeamStub: FC<WrongTeamStubProps> = (props) => {
const store = useStore();
const { objectName, pageName, currentTeam, switchToTeam, className, wrongTeamNoPermissions } = props;
const currentTeamId = store.userStore.currentUser?.current_team;
const currentTeam = store.grafanaTeamStore.items[currentTeamId]?.name;
const { objectName, pageName, switchToTeam, wrongTeamNoPermissions } = props;
const onTeamChange = async (teamId: GrafanaTeam['id']) => {
await store.userStore.updateCurrentUser({ current_team: teamId });

View file

@ -71,7 +71,7 @@ export class AlertReceiveChannelStore extends BaseStore {
}
@action
async loadItem(id: AlertReceiveChannel['id'], skipErrorHandling: boolean = false): Promise<AlertReceiveChannel> {
async loadItem(id: AlertReceiveChannel['id'], skipErrorHandling = false): Promise<AlertReceiveChannel> {
const alertReceiveChannel = await this.getById(id, skipErrorHandling);
this.items = {

View file

@ -13,8 +13,8 @@ export default class BaseStore {
this.rootStore = rootStore;
}
onApiError(error: any, skipErrorHandling: boolean = false) {
if (skipErrorHandling) throw error
onApiError(error: any, skipErrorHandling = false) {
if (skipErrorHandling) {throw error}
if (error.response.status >= 400 && error.response.status < 500) {
const payload = error.response.data;
@ -39,7 +39,7 @@ export default class BaseStore {
}
@action
async getById(id: string, skipErrorHandling: boolean = false) {
async getById(id: string, skipErrorHandling = false) {
return await makeRequest(`${this.path}${id}/`, {
method: 'GET',
}).catch((error) => this.onApiError(error, skipErrorHandling));

View file

@ -23,7 +23,7 @@ export class EscalationChainStore extends BaseStore {
}
@action
async loadItem(id: EscalationChain['id'], skipErrorHandling: boolean = false): Promise<EscalationChain> {
async loadItem(id: EscalationChain['id'], skipErrorHandling = false): Promise<EscalationChain> {
const escalationChain = await this.getById(id, skipErrorHandling);
this.items = {

View file

@ -20,7 +20,7 @@ export class OutgoingWebhookStore extends BaseStore {
}
@action
async loadItem(id: OutgoingWebhook['id'], skipErrorHandling: boolean = false): Promise<OutgoingWebhook> {
async loadItem(id: OutgoingWebhook['id'], skipErrorHandling = false): Promise<OutgoingWebhook> {
const outgoingWebhook = await this.getById(id, skipErrorHandling);
this.items = {

View file

@ -26,7 +26,7 @@ export class ScheduleStore extends BaseStore {
}
@action
async loadItem(id: Schedule['id'], skipErrorHandling: boolean = false): Promise<Schedule> {
async loadItem(id: Schedule['id'], skipErrorHandling = false): Promise<Schedule> {
const schedule = await this.getById(id, skipErrorHandling);
this.items = {

View file

@ -60,7 +60,7 @@ export class UserStore extends BaseStore {
}
@action
async loadUser(userPk: User['pk'], skipErrorHandling: boolean = false) {
async loadUser(userPk: User['pk'], skipErrorHandling = false) {
const user = await this.getById(userPk, skipErrorHandling);
this.items = {

View file

@ -12,6 +12,7 @@ import EscalationsFilters from 'components/EscalationsFilters/EscalationsFilters
import Block from 'components/GBlock/Block';
import GList from 'components/GList/GList';
import { getWrongTeamResponseInfo } from 'components/NotFoundInTeam/WrongTeam.helpers';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
import PluginLink from 'components/PluginLink/PluginLink';
import Text from 'components/Text/Text';
import Tutorial from 'components/Tutorial/Tutorial';
@ -28,7 +29,6 @@ import { withMobXProviderContext } from 'state/withStore';
import { openWarningNotification } from 'utils';
import styles from './EscalationChains.module.css';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
const cx = cn.bind(styles);
@ -80,7 +80,7 @@ class EscalationChainsPage extends React.Component<EscalationChainsPageProps, Es
let escalationChain = await escalationChainStore
.loadItem(query.id, true)
.catch((error) => this.setState({ ...getWrongTeamResponseInfo(error) }));
if (!escalationChain) return;
if (!escalationChain) {return;}
escalationChain = escalationChainStore.items[query.id];
if (escalationChain) {
@ -131,14 +131,10 @@ class EscalationChainsPage extends React.Component<EscalationChainsPageProps, Es
} = this.state;
if (wrongTeamError) {
const currentTeamId = store.userStore.currentUser?.current_team;
const currentTeamName = store.grafanaTeamStore.items[currentTeamId]?.name;
return (
<WrongTeamStub
objectName="escalation"
pageName="escalations"
currentTeam={currentTeamName}
switchToTeam={teamToSwitch}
wrongTeamNoPermissions={wrongTeamNoPermissions}
/>

View file

@ -26,8 +26,8 @@ import reactStringReplace from 'react-string-replace';
import Collapse from 'components/Collapse/Collapse';
import Block from 'components/GBlock/Block';
import IntegrationLogo from 'components/IntegrationLogo/IntegrationLogo';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
import { getWrongTeamResponseInfo } from 'components/NotFoundInTeam/WrongTeam.helpers';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
import PluginLink from 'components/PluginLink/PluginLink';
import SourceCode from 'components/SourceCode/SourceCode';
import Text from 'components/Text/Text';
@ -122,8 +122,7 @@ class IncidentPage extends React.Component<IncidentPageProps, IncidentPageState>
const { alerts } = store.alertGroupStore;
const incident = alerts.get(id);
const currentTeamId = store.userStore.currentUser?.current_team;
const currentTeamName = store.grafanaTeamStore.items[currentTeamId]?.name;
if (notFound) {
return (
<div className={cx('root')}>
@ -147,7 +146,6 @@ class IncidentPage extends React.Component<IncidentPageProps, IncidentPageState>
<WrongTeamStub
objectName="alert group"
pageName="incidents"
currentTeam={currentTeamName}
switchToTeam={teamToSwitch}
wrongTeamNoPermissions={wrongTeamNoPermissions}
/>

View file

@ -7,9 +7,10 @@ import cn from 'classnames/bind';
import { debounce } from 'lodash-es';
import { observer } from 'mobx-react';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
import GList from 'components/GList/GList';
import IntegrationsFilters, { Filters } from 'components/IntegrationsFilters/IntegrationsFilters';
import { getWrongTeamResponseInfo } from 'components/NotFoundInTeam/WrongTeam.helpers';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
import Text from 'components/Text/Text';
import Tutorial from 'components/Tutorial/Tutorial';
import { TutorialStep } from 'components/Tutorial/Tutorial.types';
@ -25,7 +26,6 @@ import { WithStoreProps } from 'state/types';
import { UserAction } from 'state/userAction';
import { withMobXProviderContext } from 'state/withStore';
import { openWarningNotification } from 'utils';
import { getWrongTeamResponseInfo } from 'components/NotFoundInTeam/WrongTeam.helpers';
import styles from './Integrations.module.css';
@ -80,7 +80,7 @@ class Integrations extends React.Component<IntegrationsProps, IntegrationsState>
let alertReceiveChannel = await alertReceiveChannelStore
.loadItem(query.id, true)
.catch((error) => this.setState({ ...getWrongTeamResponseInfo(error) }));
if (!alertReceiveChannel) return;
if (!alertReceiveChannel) {return;}
if (alertReceiveChannel.id) {
selectedAlertReceiveChannel = alertReceiveChannel.id;
@ -132,14 +132,10 @@ class Integrations extends React.Component<IntegrationsProps, IntegrationsState>
} = this.state;
if (wrongTeamError) {
const currentTeamId = store.userStore.currentUser?.current_team;
const currentTeamName = store.grafanaTeamStore.items[currentTeamId]?.name;
return (
<WrongTeamStub
objectName="escalation"
pageName="escalations"
currentTeam={currentTeamName}
switchToTeam={teamToSwitch}
wrongTeamNoPermissions={wrongTeamNoPermissions}
/>

View file

@ -7,6 +7,8 @@ import cn from 'classnames/bind';
import { observer } from 'mobx-react';
import GTable from 'components/GTable/GTable';
import { getWrongTeamResponseInfo } from 'components/NotFoundInTeam/WrongTeam.helpers';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
import PluginLink from 'components/PluginLink/PluginLink';
import Text from 'components/Text/Text';
import WithConfirm from 'components/WithConfirm/WithConfirm';
@ -21,8 +23,6 @@ import { UserAction } from 'state/userAction';
import { withMobXProviderContext } from 'state/withStore';
import styles from './OutgoingWebhooks.module.css';
import { getWrongTeamResponseInfo } from 'components/NotFoundInTeam/WrongTeam.helpers';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
const cx = cn.bind(styles);
@ -79,14 +79,10 @@ class OutgoingWebhooks extends React.Component<OutgoingWebhooksProps, OutgoingWe
const { outgoingWebhookIdToEdit, wrongTeamError, teamToSwitch, wrongTeamNoPermissions } = this.state;
if (wrongTeamError) {
const currentTeamId = store.userStore.currentUser?.current_team;
const currentTeamName = store.grafanaTeamStore.items[currentTeamId]?.name;
return (
<WrongTeamStub
objectName="outgoing webhook"
pageName="outgoing_webhooks"
currentTeam={currentTeamName}
switchToTeam={teamToSwitch}
wrongTeamNoPermissions={wrongTeamNoPermissions}
/>

View file

@ -22,6 +22,8 @@ import moment, { Moment } from 'moment-timezone';
import instructionsImage from 'assets/img/events_instructions.png';
import Avatar from 'components/Avatar/Avatar';
import GTable from 'components/GTable/GTable';
import { getWrongTeamResponseInfo } from 'components/NotFoundInTeam/WrongTeam.helpers';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
import PluginLink from 'components/PluginLink/PluginLink';
import SchedulesFilters from 'components/SchedulesFilters/SchedulesFilters';
import { SchedulesFiltersType } from 'components/SchedulesFilters/SchedulesFilters.types';
@ -43,8 +45,6 @@ import { openErrorNotification } from 'utils';
import { getDatesString } from './Schedules.helpers';
import styles from './Schedules.module.css';
import { getWrongTeamResponseInfo } from 'components/NotFoundInTeam/WrongTeam.helpers';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
const cx = cn.bind(styles);
@ -93,7 +93,7 @@ class SchedulesPage extends React.Component<SchedulesPageProps, SchedulesPageSta
const schedule = store.scheduleStore
.loadItem(id, true)
.catch((error) => this.setState({ ...getWrongTeamResponseInfo(error) }));
if (!schedule) return;
if (!schedule) {return;}
const schedules = store.scheduleStore.getSearchResult();
const scheduleId = schedules && schedules.find((res) => res.id === id)?.id;
@ -119,14 +119,10 @@ class SchedulesPage extends React.Component<SchedulesPageProps, SchedulesPageSta
const { scheduleStore } = store;
if (wrongTeamError) {
const currentTeamId = store.userStore.currentUser?.current_team;
const currentTeamName = store.grafanaTeamStore.items[currentTeamId]?.name;
return (
<WrongTeamStub
objectName="escalation"
pageName="escalations"
currentTeam={currentTeamName}
switchToTeam={teamToSwitch}
wrongTeamNoPermissions={wrongTeamNoPermissions}
/>

View file

@ -9,6 +9,8 @@ import { observer } from 'mobx-react';
import Avatar from 'components/Avatar/Avatar';
import GTable from 'components/GTable/GTable';
import { getWrongTeamResponseInfo } from 'components/NotFoundInTeam/WrongTeam.helpers';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
import PluginLink from 'components/PluginLink/PluginLink';
import Text from 'components/Text/Text';
import UsersFilters from 'components/UsersFilters/UsersFilters';
@ -23,8 +25,6 @@ import { withMobXProviderContext } from 'state/withStore';
import { getRealFilters, getUserRowClassNameFn } from './Users.helpers';
import styles from './Users.module.css';
import { getWrongTeamResponseInfo } from 'components/NotFoundInTeam/WrongTeam.helpers';
import WrongTeamStub from 'components/NotFoundInTeam/WrongTeamStub';
const cx = cn.bind(styles);
@ -172,14 +172,10 @@ class Users extends React.Component<UsersProps, UsersState> {
const { count, results } = userStore.getSearchResult();
if (wrongTeamError) {
const currentTeamId = store.userStore.currentUser?.current_team;
const currentTeamName = store.grafanaTeamStore.items[currentTeamId]?.name;
return (
<WrongTeamStub
objectName="user"
pageName="users"
currentTeam={currentTeamName}
switchToTeam={teamToSwitch}
wrongTeamNoPermissions={wrongTeamNoPermissions}
/>