Return isLoading from useCreateEntity and useDeleteEntity
This commit is contained in:
parent
1b569b6c82
commit
ac9718e6ed
|
@ -1,3 +1,4 @@
|
||||||
|
import { useState } from 'react';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { useApi, useAppDispatch } from 'soapbox/hooks';
|
import { useApi, useAppDispatch } from 'soapbox/hooks';
|
||||||
|
@ -25,13 +26,13 @@ function useCreateEntity<TEntity extends Entity = Entity, Data = any>(
|
||||||
) {
|
) {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||||
|
|
||||||
const { entityType, listKey } = parseEntitiesPath(expandedPath);
|
const { entityType, listKey } = parseEntitiesPath(expandedPath);
|
||||||
|
|
||||||
return async function createEntity(
|
async function createEntity(data: Data, callbacks: CreateEntityCallbacks = {}): Promise<void> {
|
||||||
data: Data,
|
setIsLoading(true);
|
||||||
callbacks: CreateEntityCallbacks = {},
|
|
||||||
): Promise<void> {
|
|
||||||
try {
|
try {
|
||||||
const result = await api.request({
|
const result = await api.request({
|
||||||
...toAxiosRequest(request),
|
...toAxiosRequest(request),
|
||||||
|
@ -52,6 +53,13 @@ function useCreateEntity<TEntity extends Entity = Entity, Data = any>(
|
||||||
callbacks.onError(error);
|
callbacks.onError(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
createEntity,
|
||||||
|
isLoading,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
import { useApi, useAppDispatch, useGetState } from 'soapbox/hooks';
|
import { useApi, useAppDispatch, useGetState } from 'soapbox/hooks';
|
||||||
|
|
||||||
import { deleteEntities, importEntities } from '../actions';
|
import { deleteEntities, importEntities } from '../actions';
|
||||||
|
@ -23,8 +25,11 @@ function useDeleteEntity(
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
const getState = useGetState();
|
const getState = useGetState();
|
||||||
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||||
|
|
||||||
|
async function deleteEntity(entityId: string, callbacks: DeleteEntityCallbacks = {}): Promise<void> {
|
||||||
|
setIsLoading(true);
|
||||||
|
|
||||||
return async function deleteEntity(entityId: string, callbacks: DeleteEntityCallbacks = {}): Promise<void> {
|
|
||||||
// Get the entity before deleting, so we can reverse the action if the API request fails.
|
// Get the entity before deleting, so we can reverse the action if the API request fails.
|
||||||
const entity = getState().entities[entityType]?.store[entityId];
|
const entity = getState().entities[entityType]?.store[entityId];
|
||||||
|
|
||||||
|
@ -54,6 +59,13 @@ function useDeleteEntity(
|
||||||
callbacks.onError();
|
callbacks.onError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
deleteEntity,
|
||||||
|
isLoading,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import { useState } from 'react';
|
|
||||||
|
|
||||||
import { useCreateEntity } from './useCreateEntity';
|
import { useCreateEntity } from './useCreateEntity';
|
||||||
import { useDeleteEntity } from './useDeleteEntity';
|
import { useDeleteEntity } from './useDeleteEntity';
|
||||||
import { parseEntitiesPath } from './utils';
|
import { parseEntitiesPath } from './utils';
|
||||||
|
@ -22,27 +20,17 @@ function useEntityActions<TEntity extends Entity = Entity, Data = any>(
|
||||||
opts: UseEntityActionsOpts<TEntity> = {},
|
opts: UseEntityActionsOpts<TEntity> = {},
|
||||||
) {
|
) {
|
||||||
const { entityType, path } = parseEntitiesPath(expandedPath);
|
const { entityType, path } = parseEntitiesPath(expandedPath);
|
||||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
||||||
|
|
||||||
const _delete = useDeleteEntity(entityType, { method: 'delete', url: endpoints.delete });
|
const { deleteEntity, isLoading: deleteLoading } =
|
||||||
const create = useCreateEntity<TEntity, Data>(path, { method: 'post', url: endpoints.post }, opts);
|
useDeleteEntity(entityType, { method: 'delete', url: endpoints.delete });
|
||||||
|
|
||||||
const createEntity: typeof create = async (...args) => {
|
const { createEntity, isLoading: createLoading } =
|
||||||
setIsLoading(true);
|
useCreateEntity<TEntity, Data>(path, { method: 'post', url: endpoints.post }, opts);
|
||||||
await create(...args);
|
|
||||||
setIsLoading(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
const deleteEntity: typeof _delete = async (...args) => {
|
|
||||||
setIsLoading(true);
|
|
||||||
await _delete(...args);
|
|
||||||
setIsLoading(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
createEntity,
|
createEntity,
|
||||||
deleteEntity,
|
deleteEntity,
|
||||||
isLoading,
|
isLoading: createLoading || deleteLoading,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue