{"size": 1256, "ext": "js", "max_stars_repo_path": "packages/webiny-app-cms/src/admin/components/CategoriesAutoComplete.js", "max_stars_repo_name": "gatarelib/webiny-js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/webiny-app-cms/src/admin/components/CategoriesAutoComplete.js", "max_issues_repo_name": "gatarelib/webiny-js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/webiny-app-cms/src/admin/components/CategoriesAutoComplete.js", "max_forks_repo_name": "gatarelib/webiny-js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.6326530612, "max_line_length": 93, "alphanum_fraction": 0.527866242}
// @flow
import * as React from "react";
import { AutoComplete } from "webiny-ui/AutoComplete";
import { withAutoComplete } from "webiny-app/components";
import { compose } from "recompose";
import gql from "graphql-tag";
import { get } from "lodash";
import { Query } from "react-apollo";

const loadCategory = gql`
    query LoadCategory($id: ID!) {
        cms {
            getCategory(id: $id) {
                data {
                    id
                    name
                }
            }
        }
    }
`;

const listCategories = gql`
    query LoadCategories($search: SearchInput) {
        cms {
            categories: listCategories(search: $search) {
                data {
                    id
                    name
                }
            }
        }
    }
`;

const CategoriesAutoComplete = props => (
    <Query skip={!props.value} variables={{ id: props.value }} query={loadCategory}>
        {({ data }) => <AutoComplete {...props} value={get(data, "cms.getCategory.data")} />}
    </Query>
);

export default compose(
    withAutoComplete({
        response: data => get(data, "cms.categories"),
        search: query => ({ query, fields: ["name"] }),
        query: listCategories
    })
)(CategoriesAutoComplete);
