useEntity: accept an EntityRequest object
This commit is contained in:
parent
b4c3248791
commit
1b569b6c82
|
@ -5,8 +5,10 @@ import { useApi, useAppDispatch, useAppSelector } from 'soapbox/hooks';
|
||||||
|
|
||||||
import { importEntities } from '../actions';
|
import { importEntities } from '../actions';
|
||||||
|
|
||||||
|
import { toAxiosRequest } from './utils';
|
||||||
|
|
||||||
import type { Entity } from '../types';
|
import type { Entity } from '../types';
|
||||||
import type { EntitySchema, EntityPath } from './types';
|
import type { EntitySchema, EntityPath, EntityRequest } from './types';
|
||||||
|
|
||||||
/** Additional options for the hook. */
|
/** Additional options for the hook. */
|
||||||
interface UseEntityOpts<TEntity extends Entity> {
|
interface UseEntityOpts<TEntity extends Entity> {
|
||||||
|
@ -18,7 +20,7 @@ interface UseEntityOpts<TEntity extends Entity> {
|
||||||
|
|
||||||
function useEntity<TEntity extends Entity>(
|
function useEntity<TEntity extends Entity>(
|
||||||
path: EntityPath,
|
path: EntityPath,
|
||||||
endpoint: string,
|
request: EntityRequest,
|
||||||
opts: UseEntityOpts<TEntity> = {},
|
opts: UseEntityOpts<TEntity> = {},
|
||||||
) {
|
) {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
|
@ -34,15 +36,18 @@ function useEntity<TEntity extends Entity>(
|
||||||
const [isFetching, setIsFetching] = useState(false);
|
const [isFetching, setIsFetching] = useState(false);
|
||||||
const isLoading = isFetching && !entity;
|
const isLoading = isFetching && !entity;
|
||||||
|
|
||||||
const fetchEntity = () => {
|
const fetchEntity = async () => {
|
||||||
setIsFetching(true);
|
setIsFetching(true);
|
||||||
api.get(endpoint).then(({ data }) => {
|
|
||||||
const entity = schema.parse(data);
|
try {
|
||||||
|
const response = await api.request(toAxiosRequest(request));
|
||||||
|
const entity = schema.parse(response.data);
|
||||||
dispatch(importEntities([entity], entityType));
|
dispatch(importEntities([entity], entityType));
|
||||||
|
} catch (e) {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
setIsFetching(false);
|
setIsFetching(false);
|
||||||
}).catch(() => {
|
|
||||||
setIsFetching(false);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
Loading…
Reference in New Issue