Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schedule Filters tweaks #1406

Merged
merged 19 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed importing of global grafana styles ([672](https://github.com/grafana/oncall/issues/672))
- Schedule filters improvements ([941](https://github.com/grafana/oncall/issues/941))

## v1.1.29 (2023-02-23)

Expand Down
40 changes: 22 additions & 18 deletions grafana-plugin/src/models/schedule/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,29 @@ export class ScheduleStore extends BaseStore {

@action
async updateItems(f: any = { searchTerm: '', type: undefined }) {
// async updateItems(query = '') {
const filters = typeof f === 'string' ? { searchTerm: f } : f;
const { searchTerm: search, type } = filters;
const result = await makeRequest(this.path, { method: 'GET', params: { search: search, type } });
return new Promise<void>(async (resolve) => {
const filters = typeof f === 'string' ? { searchTerm: f } : f;
const { searchTerm: search, type } = filters;
const result = await makeRequest(this.path, { method: 'GET', params: { search: search, type } });

this.items = {
...this.items,
...result.reduce(
(acc: { [key: number]: Schedule }, item: Schedule) => ({
...acc,
[item.id]: item,
}),
{}
),
};
this.searchResult = {
...this.searchResult,
results: result.map((item: Schedule) => item.id),
};
this.items = {
...this.items,
...result.reduce(
(acc: { [key: number]: Schedule }, item: Schedule) => ({
...acc,
[item.id]: item,
}),
{}
),
};

this.searchResult = {
...this.searchResult,
results: result.map((item: Schedule) => item.id),
};

resolve();
});
}

async updateItem(id: Schedule['id'], fromOrganization = false) {
Expand Down
50 changes: 27 additions & 23 deletions grafana-plugin/src/models/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,34 @@ export class UserStore extends BaseStore {

@action
async updateItems(f: any = { searchTerm: '' }, page = 1) {
const filters = typeof f === 'string' ? { searchTerm: f } : f; // for GSelect compatibility
const { searchTerm: search } = filters;
const { count, results } = await makeRequest(this.path, {
params: { search, page },
});

this.items = {
...this.items,
...results.reduce(
(acc: { [key: number]: User }, item: User) => ({
...acc,
[item.pk]: {
...item,
timezone: getTimezone(item),
},
}),
{}
),
};
return new Promise<void>(async (resolve) => {
const filters = typeof f === 'string' ? { searchTerm: f } : f; // for GSelect compatibility
const { searchTerm: search } = filters;
const { count, results } = await makeRequest(this.path, {
params: { search, page },
});

this.searchResult = {
count,
results: results.map((item: User) => item.pk),
};
this.items = {
...this.items,
...results.reduce(
(acc: { [key: number]: User }, item: User) => ({
...acc,
[item.pk]: {
...item,
timezone: getTimezone(item),
},
}),
{}
),
};

this.searchResult = {
count,
results: results.map((item: User) => item.pk),
};

resolve();
});
}

getSearchResult() {
Expand Down
97 changes: 63 additions & 34 deletions grafana-plugin/src/pages/schedules/Schedules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ import { PLUGIN_ROOT, TABLE_COLUMN_MAX_WIDTH } from 'utils/consts';
import styles from './Schedules.module.css';

const cx = cn.bind(styles);
const FILTERS_DEBOUNCE_MS = 500;

interface SchedulesPageProps extends WithStoreProps, RouteComponentProps {}

interface SchedulesPageState {
schedules: Schedule[];
startMoment: dayjs.Dayjs;
filters: SchedulesFiltersType;
showNewScheduleSelector: boolean;
expandedRowKeys: Array<Schedule['id']>;
scheduleIdToEdit?: Schedule['id'];
isLoading?: boolean;
}

@observer
Expand All @@ -52,29 +55,39 @@ class SchedulesPage extends React.Component<SchedulesPageProps, SchedulesPageSta
super(props);

const { store } = this.props;

this.state = {
schedules: undefined,
startMoment: getStartOfWeek(store.currentTimezone),
filters: { searchTerm: '', status: 'all', type: undefined },
showNewScheduleSelector: false,
expandedRowKeys: [],
scheduleIdToEdit: undefined,
isLoading: true,
};
}

async componentDidMount() {
const { store } = this.props;
const { filters } = this.state;

store.userStore.updateItems();
store.scheduleStore.updateItems();

await store.scheduleStore.updateItems().finally(() => {
if (filters === this.state.filters) {
// check for any change in filters in the meanwhile
this.setState({
schedules: this.getFilteredSchedules(),
isLoading: false,
});
}
});
}

render() {
const { store } = this.props;
const { filters, showNewScheduleSelector, expandedRowKeys, scheduleIdToEdit } = this.state;
const { schedules, filters, showNewScheduleSelector, expandedRowKeys, scheduleIdToEdit, isLoading } = this.state;

const { scheduleStore } = store;

const schedules = scheduleStore.getSearchResult();
const columns = [
{
width: '10%',
Expand Down Expand Up @@ -125,15 +138,6 @@ class SchedulesPage extends React.Component<SchedulesPageProps, SchedulesPageSta

const users = store.userStore.getSearchResult().results;

const data = schedules
? schedules.filter(
(schedule) =>
filters.status === 'all' ||
(filters.status === 'used' && schedule.number_of_escalation_chains) ||
(filters.status === 'unused' && !schedule.number_of_escalation_chains)
)
: undefined;

return (
<>
<div className={cx('root')}>
Expand All @@ -157,7 +161,8 @@ class SchedulesPage extends React.Component<SchedulesPageProps, SchedulesPageSta
</div>
<Table
columns={columns}
data={data}
data={schedules}
loading={isLoading}
pagination={{ page: 1, total: 1, onChange: this.handlePageChange }}
rowKey="id"
expandable={{
Expand All @@ -166,11 +171,7 @@ class SchedulesPage extends React.Component<SchedulesPageProps, SchedulesPageSta
expandedRowRender: this.renderSchedule,
expandRowByClick: true,
}}
emptyText={
<div className={cx('loader')}>
{data ? <Text type="secondary">Not found</Text> : <Text type="secondary">Loading schedules...</Text>}
</div>
}
emptyText={this.renderNotFound()}
/>
</VerticalGroup>
</div>
Expand All @@ -196,6 +197,22 @@ class SchedulesPage extends React.Component<SchedulesPageProps, SchedulesPageSta
);
}

renderLoading() {
teodosii marked this conversation as resolved.
Show resolved Hide resolved
return (
<div className={cx('loader')}>
<LoadingPlaceholder text="Loading schedules..."></LoadingPlaceholder>
</div>
);
}

renderNotFound() {
return (
<div className={cx('loader')}>
<Text type="secondary">Not found</Text>
</div>
);
}

handleTimezoneChange = (value: Timezone) => {
const { store } = this.props;

Expand Down Expand Up @@ -313,13 +330,6 @@ class SchedulesPage extends React.Component<SchedulesPageProps, SchedulesPageSta
onHover={this.getUpdateRelatedEscalationChainsHandler(item.id)}
/>
)}

{/* <ScheduleCounter
type="warning"
count={warningsCount}
tooltipTitle="Warnings"
tooltipContent="Schedule has unassigned time periods during next 7 days"
/>*/}
</HorizontalGroup>
);
};
Expand Down Expand Up @@ -394,18 +404,37 @@ class SchedulesPage extends React.Component<SchedulesPageProps, SchedulesPageSta
};
};

getFilteredSchedules = () => {
const { scheduleStore } = this.props.store;
const { filters } = this.state;

return scheduleStore
.getSearchResult()
.filter(
(schedule) =>
filters.status === 'all' ||
(filters.status === 'used' && schedule.number_of_escalation_chains) ||
(filters.status === 'unused' && !schedule.number_of_escalation_chains)
);
};

handleSchedulesFiltersChange = (filters: SchedulesFiltersType) => {
this.setState({ filters }, this.debouncedUpdateSchedules);
this.setState({ filters }, () => this.debouncedUpdateSchedules(filters));
};

applyFilters = () => {
const { filters } = this.state;
const { store } = this.props;
const { scheduleStore } = store;
scheduleStore.updateItems(filters);
applyFilters = (filters: SchedulesFiltersType) => {
const { scheduleStore } = this.props.store;

scheduleStore.updateItems(filters).finally(() => {
teodosii marked this conversation as resolved.
Show resolved Hide resolved
if (this.state.filters === filters) {
this.setState({
schedules: this.getFilteredSchedules(),
});
}
});
};

debouncedUpdateSchedules = debounce(this.applyFilters, 1000);
debouncedUpdateSchedules = debounce(this.applyFilters, FILTERS_DEBOUNCE_MS);

handlePageChange = (_page: number) => {};

Expand Down