import { gql, useQuery } from '@apollo/client';
import { Workgroup } from '../../types/workgroup';
import { useGraphQLData } from './helper';

export const QUERY_WORKGROUP = gql`
  query workgroups($filter: WorkgroupsFilter) {
    workgroups(filter: $filter) {
      id,
      name,
      createdAt,
      closedAt,
      members {
        id,
        email,
        name
      }
    }
  }
`;

export function useWorkgroupsQuery(options = {}) {
  return useQuery(QUERY_WORKGROUP, options);
}

export function useWorkgroups() {
  const { data, loading, error } = useGraphQLData<Workgroup[]>(
    QUERY_WORKGROUP, 'workgroups', []
  );
  return { workgroups: data, loading, error };
}