oncall-engine/grafana-plugin/src/models/global_setting/global_setting.ts
Dominik Broj 6da36b3c0b
Use autogenerated types for alert_receive_channels (#3851)
# What this PR does

- autogenerate new types exposed by backend, remove custom types that
duplicate autogenerated ones
- use autogenerated types for alert receive channels
- in alert_receive_channel model:
  - use autogenerate http client (`onCallApi`) for http requests
- extract methods that don't update state into
alert_receive_channel.helpers.ts and make them pure (they accept
AlertReceiveChannelStore as param) to avoid inconsistency and issues
with `this` binding
  - use `makeAutoObservable`
  - remove unneeded decorators
- rename update* methods to fetch* whenever such methods retrieve data
from backend with GET requests
- in other models use `@action.bound` for actions and arrow functions
for store methods that are not actions (in subsequent PRs we will apply
the same changes as in alert_receive_channel, this is just for now until
we do it)
- refactor http-client so that it shows global notification on http
errors automatically and provide the possibility to opt-out from it when
making a call
- improve type-safety of `GSelect`
- fix bug related to attaching alert group
(https://raintank-corp.slack.com/archives/C04JCU51NF8/p1707476487580579)

## Which issue(s) this PR fixes

https://github.com/grafana/oncall/issues/3331

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)

---------

Co-authored-by: Vadim Stepanov <vadimkerr@gmail.com>
2024-02-20 12:09:22 +00:00

70 lines
1.7 KiB
TypeScript

import { action, observable, makeObservable, runInAction } from 'mobx';
import { BaseStore } from 'models/base_store';
import { RootStore } from 'state/rootStore';
import { GlobalSetting } from './global_setting.types';
export class GlobalSettingStore extends BaseStore {
@observable.shallow
items: { [id: string]: GlobalSetting } = {};
@observable.shallow
searchResult: { [key: string]: Array<GlobalSetting['id']> } = {};
constructor(rootStore: RootStore) {
super(rootStore);
makeObservable(this);
this.path = '/live_settings/';
}
@action.bound
async updateById(id: GlobalSetting['id']) {
const response = await this.getById(id);
runInAction(() => {
this.items = {
...this.items,
[id]: response,
};
});
}
@action.bound
async updateItems(query = '') {
const results = await this.getAll();
runInAction(() => {
this.items = {
...this.items,
...results.reduce(
(acc: { [key: number]: GlobalSetting }, item: GlobalSetting) => ({
...acc,
[item.id]: item,
}),
{}
),
};
this.searchResult = {
...this.searchResult,
[query]: results.map((item: GlobalSetting) => item.id),
};
});
}
getSearchResult = (query = '') => {
if (!this.searchResult[query]) {
return undefined;
}
return this.searchResult[query].map((globalSettingId: GlobalSetting['id']) => this.items[globalSettingId]);
};
async getGlobalSettingItemByName(name: string) {
const results = await this.getAll();
return results.find((element: { name: string }) => element.name === name);
}
}