forked from Transparency/kgroad-frontend2
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { apiInstance } from "@/shared/config/apiConfig";
|
|
import { IFetch } from "@/shared/types/fetch-type";
|
|
import { IList } from "@/shared/types/list-type";
|
|
import { IStatistics } from "@/shared/types/statistics-type";
|
|
import { IUserRatings } from "@/shared/types/user-rating-type";
|
|
import { AxiosError } from "axios";
|
|
import { create } from "zustand";
|
|
|
|
const filterCategories: Record<string, string> = {
|
|
report_count: "report_count",
|
|
likes_given_count: "likes_given_count",
|
|
likes_received_count: "likes_received_count",
|
|
average_rating: "average_rating",
|
|
};
|
|
|
|
interface IVolunteersStore extends IFetch {
|
|
data: IUserRatings[];
|
|
getVolunteers: (filter: {
|
|
option: string;
|
|
toggle: boolean;
|
|
}) => void;
|
|
}
|
|
|
|
export const useVolunteersStore = create<IVolunteersStore>((set) => ({
|
|
isLoading: false,
|
|
error: "",
|
|
data: [],
|
|
getVolunteers: async (filter: {
|
|
option: string;
|
|
toggle: boolean;
|
|
}) => {
|
|
try {
|
|
set({ isLoading: true });
|
|
const response = await apiInstance.get<IUserRatings[]>(
|
|
`/report/user_ratings/`
|
|
);
|
|
|
|
let data = response.data;
|
|
|
|
if (
|
|
filter.option === filterCategories[filter.option] &&
|
|
filter.toggle === false
|
|
) {
|
|
data = data.sort((a, b) => {
|
|
const optionKey = filter.option as keyof IUserRatings;
|
|
return (Number(a[optionKey]) -
|
|
Number(b[optionKey])) as number;
|
|
});
|
|
} else if (
|
|
filter.option === "rating" &&
|
|
filter.toggle === true
|
|
) {
|
|
data = data.sort((a, b) => {
|
|
const optionKey = filter.option as keyof IUserRatings;
|
|
return (Number(a[optionKey]) -
|
|
Number(b[optionKey])) as number;
|
|
});
|
|
}
|
|
|
|
set({ data: data });
|
|
} catch (error: unknown) {
|
|
if (error instanceof AxiosError) {
|
|
set({ error: error.message });
|
|
} else {
|
|
set({ error: "an error ocured" });
|
|
}
|
|
} finally {
|
|
set({ isLoading: false });
|
|
}
|
|
},
|
|
}));
|