This commit refactor the role management, removing the dependencies on account ids throughout our system. Changes include removing account id related attributes in the roles table schema and functions, modifying seed data and simplifying the role data provider code. This makes role structure more generic and simplifies the role handling process.
43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import { useQuery } from '@tanstack/react-query';
|
|
|
|
import { useSupabase } from '@kit/supabase/hooks/use-supabase';
|
|
import { LoadingOverlay } from '@kit/ui/loading-overlay';
|
|
|
|
export function RolesDataProvider(props: {
|
|
maxRoleHierarchy: number;
|
|
children: (roles: string[]) => React.ReactNode;
|
|
}) {
|
|
const rolesQuery = useFetchRoles(props);
|
|
|
|
if (rolesQuery.isLoading) {
|
|
return <LoadingOverlay fullPage={false} />;
|
|
}
|
|
|
|
if (rolesQuery.isError) {
|
|
return null;
|
|
}
|
|
|
|
return <>{props.children(rolesQuery.data ?? [])}</>;
|
|
}
|
|
|
|
function useFetchRoles(props: { maxRoleHierarchy: number }) {
|
|
const supabase = useSupabase();
|
|
|
|
return useQuery({
|
|
queryKey: ['roles', props.maxRoleHierarchy],
|
|
queryFn: async () => {
|
|
const { error, data } = await supabase
|
|
.from('roles')
|
|
.select('name')
|
|
.gte('hierarchy_level', props.maxRoleHierarchy)
|
|
.order('hierarchy_level', { ascending: true });
|
|
|
|
if (error) {
|
|
throw error;
|
|
}
|
|
|
|
return data.map((item) => item.name);
|
|
},
|
|
});
|
|
}
|