35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import * as React from 'react';
|
|
import { Employee } from '../models';
|
|
import { ApiClientError } from '../services/httpClient';
|
|
import { PortalDictionaryClient } from '../services/portalDictionary';
|
|
|
|
export interface PortalEmployees {
|
|
items: Employee[];
|
|
isLoading: boolean;
|
|
error?: string;
|
|
}
|
|
|
|
/**
|
|
* Справочник сотрудников api.tnsu.kz — грузим один раз на экран, чтобы искать
|
|
* исполнителя/инициатора по ФИО или email на клиенте, без запроса на каждый символ.
|
|
*/
|
|
export function usePortalEmployees(dictionary?: PortalDictionaryClient): PortalEmployees {
|
|
const [items, setItems] = React.useState<Employee[]>([]);
|
|
const [isLoading, setIsLoading] = React.useState(true);
|
|
const [error, setError] = React.useState<string | undefined>();
|
|
|
|
React.useEffect(() => {
|
|
// Клиент появляется асинхронно: SPFx сначала выдаёт AAD-токен для api.tnsu.kz.
|
|
if (!dictionary) return;
|
|
|
|
setIsLoading(true);
|
|
setError(undefined);
|
|
dictionary.employees()
|
|
.then(setItems)
|
|
.catch((e: ApiClientError) => setError(e.message))
|
|
.finally(() => setIsLoading(false));
|
|
}, [dictionary]);
|
|
|
|
return { items, isLoading, error };
|
|
}
|