init
This commit is contained in:
38
src/entities/dispatcher/model/onDutyCache.ts
Normal file
38
src/entities/dispatcher/model/onDutyCache.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { appEvents, storage } from '@/shared/lib';
|
||||
import type { OnDutyDispatcher } from './types';
|
||||
|
||||
const ON_DUTY_KEY = 'dispatchers_on_duty_v1';
|
||||
const STAFF_KEY = 'staff_list_v1';
|
||||
|
||||
interface StaffMember {
|
||||
id?: string | number;
|
||||
name?: string;
|
||||
fullName?: string;
|
||||
role?: string;
|
||||
onDuty?: boolean;
|
||||
}
|
||||
|
||||
export function cachedOnDuty(): OnDutyDispatcher[] {
|
||||
const direct = storage.getJson<OnDutyDispatcher[]>(ON_DUTY_KEY);
|
||||
if (Array.isArray(direct) && direct.length > 0) return direct;
|
||||
|
||||
const staff = storage.getJson<StaffMember[]>(STAFF_KEY);
|
||||
if (!Array.isArray(staff)) return [];
|
||||
return staff
|
||||
.filter((person) => (person.role === 'dispatcher' || person.role === 'senior_dispatcher') && person.onDuty !== false)
|
||||
.map((person, index) => ({
|
||||
id: String(person.id ?? `staff-${index}`),
|
||||
name: person.fullName || person.name || 'Без имени',
|
||||
role: (person.role as OnDutyDispatcher['role']) ?? 'dispatcher',
|
||||
}));
|
||||
}
|
||||
|
||||
export function cacheOnDuty(list: OnDutyDispatcher[]): void {
|
||||
storage.setJson(ON_DUTY_KEY, list);
|
||||
appEvents.emit('dispatchers-on-duty-changed');
|
||||
}
|
||||
|
||||
export function cacheStaffList(list: unknown[]): void {
|
||||
storage.setJson(STAFF_KEY, list);
|
||||
appEvents.emit('staff-list-changed');
|
||||
}
|
||||
45
src/entities/dispatcher/model/onDutyStore.ts
Normal file
45
src/entities/dispatcher/model/onDutyStore.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { makeAutoObservable, runInAction } from 'mobx';
|
||||
import { appEvents } from '@/shared/lib';
|
||||
import { fetchOnDutyDispatchers } from '../api/dispatcherApi';
|
||||
import { cachedOnDuty } from './onDutyCache';
|
||||
import type { OnDutyDispatcher } from './types';
|
||||
|
||||
const REFRESH_INTERVAL = 60_000;
|
||||
|
||||
class OnDutyStore {
|
||||
dispatchers: OnDutyDispatcher[] = cachedOnDuty();
|
||||
phrase = '';
|
||||
|
||||
private timer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
constructor() {
|
||||
makeAutoObservable(this, {}, { autoBind: true });
|
||||
}
|
||||
|
||||
start(): void {
|
||||
if (this.timer) return;
|
||||
void this.refresh();
|
||||
this.timer = setInterval(() => void this.refresh(), REFRESH_INTERVAL);
|
||||
appEvents.on('dispatchers-on-duty-changed', this.applyCache);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
if (this.timer) clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
appEvents.off('dispatchers-on-duty-changed', this.applyCache);
|
||||
}
|
||||
|
||||
async refresh(): Promise<void> {
|
||||
const { dispatchers, phrase } = await fetchOnDutyDispatchers();
|
||||
runInAction(() => {
|
||||
if (dispatchers.length > 0) this.dispatchers = dispatchers;
|
||||
if (phrase) this.phrase = phrase;
|
||||
});
|
||||
}
|
||||
|
||||
private applyCache(): void {
|
||||
this.dispatchers = cachedOnDuty();
|
||||
}
|
||||
}
|
||||
|
||||
export const onDutyStore = new OnDutyStore();
|
||||
10
src/entities/dispatcher/model/types.ts
Normal file
10
src/entities/dispatcher/model/types.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export interface OnDutyDispatcher {
|
||||
id: string;
|
||||
name: string;
|
||||
role?: 'dispatcher' | 'senior_dispatcher';
|
||||
}
|
||||
|
||||
export interface OnDutyResult {
|
||||
dispatchers: OnDutyDispatcher[];
|
||||
phrase: string;
|
||||
}
|
||||
Reference in New Issue
Block a user