{"size": 905, "ext": "js", "max_stars_repo_path": "packages/frontend/src/hooks/model/useGetModels.js", "max_stars_repo_name": "bbarber9/rpgtools", "max_stars_repo_stars_event_min_datetime": "2020-10-16T04:12:54.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-16T04:12:54.000Z", "max_issues_repo_path": "packages/frontend/src/hooks/model/useGetModels.js", "max_issues_repo_name": "bbarber9/rpgtools", "max_issues_repo_issues_event_min_datetime": "2019-12-03T00:06:27.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-18T06:42:08.000Z", "max_forks_repo_path": "packages/frontend/src/hooks/model/useGetModels.js", "max_forks_repo_name": "bbarber9/rpgtools", "max_forks_repo_forks_event_min_datetime": "2020-03-28T02:14:17.000Z", "max_forks_repo_forks_event_max_datetime": "2020-03-28T02:14:17.000Z"}
{"max_stars_count": 1, "max_issues_count": 47, "max_forks_count": 1, "avg_line_length": 25.1388888889, "max_line_length": 73, "alphanum_fraction": 0.661878453}
import useCurrentWorld from "../world/useCurrentWorld";
import { useLazyQuery } from "@apollo/client";
import React, { useEffect } from "react";
import gql from "graphql-tag";
import { MODEL_ATTRIBUTES } from "@rpgtools/common/src/gql-fragments";

export const GET_MODELS = gql`
	query getModels($worldId: ID!){
		models(worldId: $worldId){
			${MODEL_ATTRIBUTES}
		}
	}
`;
export const useGetModels = () => {
	const { currentWorld, loading } = useCurrentWorld();
	const [
		getModels,
		{ data, loading: modelLoading, error, refetch },
	] = useLazyQuery(GET_MODELS);

	useEffect(() => {
		(async () => {
			if (currentWorld) {
				await getModels({ variables: { worldId: currentWorld._id } });
			}
		})();
	}, [currentWorld]);

	return {
		loading: loading || modelLoading,
		models: data ? data.models : null,
		errors: error ? error.graphQLErrors.map((error) => error.message) : [],
		refetch,
	};
};
