Combine group hooks into one useGroups file

environments/review-group-enti-e2lbwq/deployments/2781
Alex Gleason 2023-03-09 11:23:14 -06:00
rodzic 4c6d13e4ef
commit 8923e7b5d0
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
3 zmienionych plików z 21 dodań i 28 usunięć

Wyświetl plik

@ -5,8 +5,7 @@ export { useAppSelector } from './useAppSelector';
export { useClickOutside } from './useClickOutside';
export { useCompose } from './useCompose';
export { useDebounce } from './useDebounce';
export { useGroup } from './useGroup';
export { useGroups } from './useGroups';
export { useGroup, useGroups } from './useGroups';
export { useGroupsPath } from './useGroupsPath';
export { useDimensions } from './useDimensions';
export { useFeatures } from './useFeatures';

Wyświetl plik

@ -1,24 +0,0 @@
import { useEffect } from 'react';
import { useEntity } from 'soapbox/entity-store/hooks';
import { normalizeGroup } from 'soapbox/normalizers';
import type { Group } from 'soapbox/types/entities';
function useGroup(groupId: string) {
const result = useEntity<Group>(['Group', groupId], `/api/v1/groups/${groupId}`);
const { entity, isLoading, fetchEntity } = result;
useEffect(() => {
if (!isLoading) {
fetchEntity();
}
}, []);
return {
...result,
group: entity ? normalizeGroup(entity) : undefined,
};
}
export { useGroup };

Wyświetl plik

@ -1,6 +1,6 @@
import { useEffect } from 'react';
import { useEntities } from 'soapbox/entity-store/hooks';
import { useEntities, useEntity } from 'soapbox/entity-store/hooks';
import { normalizeGroup } from 'soapbox/normalizers';
import type { Group } from 'soapbox/types/entities';
@ -9,6 +9,8 @@ function useGroups() {
const result = useEntities<Group>(['Group', ''], '/api/v1/groups');
const { entities, isLoading, fetchEntities } = result;
// Note: we have to fetch them in the hook right now because I haven't implemented
// max-age or cache expiry in the entity store yet. It's planned.
useEffect(() => {
if (!isLoading) {
fetchEntities();
@ -21,4 +23,20 @@ function useGroups() {
};
}
export { useGroups };
function useGroup(groupId: string) {
const result = useEntity<Group>(['Group', groupId], `/api/v1/groups/${groupId}`);
const { entity, isLoading, fetchEntity } = result;
useEffect(() => {
if (!isLoading) {
fetchEntity();
}
}, []);
return {
...result,
group: entity ? normalizeGroup(entity) : undefined,
};
}
export { useGroup, useGroups };