27 lines
887 B
TypeScript
27 lines
887 B
TypeScript
import {Group, User} from "@/interfaces/user";
|
|
import axios from "axios";
|
|
import {useEffect, useState} from "react";
|
|
|
|
export default function useGroups(admin?: string) {
|
|
const [groups, setGroups] = useState<Group[]>([]);
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [isError, setIsError] = useState(false);
|
|
|
|
const getData = () => {
|
|
setIsLoading(true);
|
|
axios
|
|
.get<Group[]>("/api/groups")
|
|
.then((response) => {
|
|
const filter = (g: Group) => g.admin === admin || g.participants.includes(admin || "");
|
|
|
|
const filteredGroups = admin ? response.data.filter(filter) : response.data;
|
|
return setGroups(admin ? filteredGroups.map((g) => ({...g, disableEditing: g.disableEditing || g.admin !== admin})) : filteredGroups);
|
|
})
|
|
.finally(() => setIsLoading(false));
|
|
};
|
|
|
|
useEffect(getData, [admin]);
|
|
|
|
return {groups, isLoading, isError, reload: getData};
|
|
}
|