2019-06-03 10:55:59 -05:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2020-08-07 02:00:44 -05:00
|
|
|
import { updateConfigurationSubtitle } from 'app/core/actions';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { ThunkResult } from 'app/types';
|
|
|
|
|
|
|
|
import { organizationLoaded, userOrganizationsLoaded } from './reducers';
|
2018-10-25 00:45:22 -05:00
|
|
|
|
2020-08-07 02:00:44 -05:00
|
|
|
type OrganizationDependencies = { getBackendSrv: typeof getBackendSrv };
|
|
|
|
|
|
|
|
export function loadOrganization(
|
|
|
|
dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv }
|
|
|
|
): ThunkResult<any> {
|
2021-01-20 00:59:48 -06:00
|
|
|
return async (dispatch) => {
|
2020-08-07 02:00:44 -05:00
|
|
|
const organizationResponse = await dependencies.getBackendSrv().get('/api/org');
|
2018-12-05 06:08:00 -06:00
|
|
|
dispatch(organizationLoaded(organizationResponse));
|
2018-10-25 09:56:49 -05:00
|
|
|
|
2018-12-05 06:08:00 -06:00
|
|
|
return organizationResponse;
|
2018-10-25 09:56:49 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:00:44 -05:00
|
|
|
export function updateOrganization(
|
|
|
|
dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv }
|
|
|
|
): ThunkResult<any> {
|
2018-10-26 07:51:33 -05:00
|
|
|
return async (dispatch, getStore) => {
|
|
|
|
const organization = getStore().organization.organization;
|
|
|
|
|
2020-08-07 02:00:44 -05:00
|
|
|
await dependencies.getBackendSrv().put('/api/org', { name: organization.name });
|
2018-10-26 07:51:33 -05:00
|
|
|
|
2020-08-07 02:00:44 -05:00
|
|
|
dispatch(updateConfigurationSubtitle(organization.name));
|
|
|
|
dispatch(loadOrganization(dependencies));
|
2018-10-26 07:51:33 -05:00
|
|
|
};
|
2018-10-26 07:15:37 -05:00
|
|
|
}
|
2021-12-15 02:42:52 -06:00
|
|
|
|
|
|
|
export function setUserOrganization(
|
|
|
|
orgId: number,
|
|
|
|
dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv }
|
|
|
|
): ThunkResult<any> {
|
|
|
|
return async (dispatch) => {
|
|
|
|
const organizationResponse = await dependencies.getBackendSrv().post('/api/user/using/' + orgId);
|
|
|
|
|
|
|
|
dispatch(updateConfigurationSubtitle(organizationResponse.name));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createOrganization(
|
|
|
|
newOrg: { name: string },
|
|
|
|
dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv }
|
|
|
|
): ThunkResult<any> {
|
|
|
|
return async (dispatch) => {
|
|
|
|
const result = await dependencies.getBackendSrv().post('/api/orgs/', newOrg);
|
|
|
|
|
|
|
|
dispatch(setUserOrganization(result.orgId));
|
|
|
|
};
|
|
|
|
}
|
2021-12-16 02:57:18 -06:00
|
|
|
|
|
|
|
export function getUserOrganizations(
|
|
|
|
dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv }
|
|
|
|
): ThunkResult<any> {
|
|
|
|
return async (dispatch) => {
|
|
|
|
const result = await dependencies.getBackendSrv().get('/api/user/orgs');
|
|
|
|
dispatch(userOrganizationsLoaded(result));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|