Migrate to external library for interacting with API
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import type { Entity } from '../types';
|
||||
import type { PlfeResponse } from 'soapbox/api';
|
||||
import type { PlApiResponse } from 'pl-api';
|
||||
import type z from 'zod';
|
||||
|
||||
type EntitySchema<TEntity extends Entity = Entity> = z.ZodType<TEntity, z.ZodTypeDef, any>;
|
||||
@ -35,7 +35,7 @@ interface EntityCallbacks<Value, Error = unknown> {
|
||||
* Passed into hooks to make requests.
|
||||
* Must return a response.
|
||||
*/
|
||||
type EntityFn<T> = (value: T) => Promise<PlfeResponse>
|
||||
type EntityFn<T> = (value: T) => Promise<PlApiResponse>
|
||||
|
||||
export type {
|
||||
EntitySchema,
|
||||
|
||||
@ -2,7 +2,7 @@ import { useEffect } from 'react';
|
||||
import z from 'zod';
|
||||
|
||||
import { getNextLink, getPrevLink } from 'soapbox/api';
|
||||
import { useApi } from 'soapbox/hooks/useApi';
|
||||
import { useClient } from 'soapbox/hooks';
|
||||
import { useAppDispatch } from 'soapbox/hooks/useAppDispatch';
|
||||
import { useAppSelector } from 'soapbox/hooks/useAppSelector';
|
||||
import { useGetState } from 'soapbox/hooks/useGetState';
|
||||
@ -39,7 +39,7 @@ const useEntities = <TEntity extends Entity>(
|
||||
/** Additional options for the hook. */
|
||||
opts: UseEntitiesOpts<TEntity> = {},
|
||||
) => {
|
||||
const api = useApi();
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
const getState = useGetState();
|
||||
|
||||
@ -91,13 +91,13 @@ const useEntities = <TEntity extends Entity>(
|
||||
|
||||
const fetchNextPage = async(): Promise<void> => {
|
||||
if (next) {
|
||||
await fetchPage(() => api(next), 'end');
|
||||
await fetchPage(() => client.request(next), 'end');
|
||||
}
|
||||
};
|
||||
|
||||
const fetchPreviousPage = async(): Promise<void> => {
|
||||
if (prev) {
|
||||
await fetchPage(() => api(prev), 'start');
|
||||
await fetchPage(() => client.request(prev), 'start');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { useApi } from 'soapbox/hooks/useApi';
|
||||
import { useClient } from 'soapbox/hooks';
|
||||
|
||||
import { useCreateEntity } from './useCreateEntity';
|
||||
import { useDeleteEntity } from './useDeleteEntity';
|
||||
@ -22,22 +22,20 @@ const useEntityActions = <TEntity extends Entity = Entity, Data = any>(
|
||||
endpoints: EntityActionEndpoints,
|
||||
opts: UseEntityActionsOpts<TEntity> = {},
|
||||
) => {
|
||||
const api = useApi();
|
||||
const client = useClient();
|
||||
const { entityType, path } = parseEntitiesPath(expandedPath);
|
||||
|
||||
const { deleteEntity, isSubmitting: deleteSubmitting } =
|
||||
useDeleteEntity(entityType, (entityId) => api(endpoints.delete!.replace(/:id/g, entityId), { method: 'DELETE' }));
|
||||
useDeleteEntity(entityType, (entityId) => client.request(endpoints.delete!.replace(/:id/g, entityId), { method: 'DELETE' }));
|
||||
|
||||
const { createEntity, isSubmitting: createSubmitting } =
|
||||
useCreateEntity<TEntity, Data>(path, (data) => api(endpoints.post!, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
useCreateEntity<TEntity, Data>(path, (data) => client.request(endpoints.post!, {
|
||||
method: 'POST', body: data,
|
||||
}), opts);
|
||||
|
||||
const { createEntity: updateEntity, isSubmitting: updateSubmitting } =
|
||||
useCreateEntity<TEntity, Data>(path, (data) => api(endpoints.patch!, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(data),
|
||||
useCreateEntity<TEntity, Data>(path, (data) => client.request(endpoints.patch!, {
|
||||
method: 'PATCH', body: data,
|
||||
}), opts);
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user