Skip to content

Commit

Permalink
feat: create helper React hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Mar 31, 2020
1 parent 34ea5e6 commit 4224bca
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/app-headless-cms/src/admin/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as useCms } from "./useCms";
export { default as useQuery } from "./useQuery";
export { default as useMutation } from "./useMutation";
13 changes: 13 additions & 0 deletions packages/app-headless-cms/src/admin/hooks/useCms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useContext } from "react";
import { CmsContext } from "@webiny/app-headless-cms/admin/contexts/Cms";

const useCms = () => {
const context = useContext<any>(CmsContext);
if (!context) {
throw new Error("useCms must be used within a CmsProvider");
}

return context;
};

export default useCms;
15 changes: 15 additions & 0 deletions packages/app-headless-cms/src/admin/hooks/useMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import useCms from "./useCms";
import { useMutation as apolloUseMutation } from "react-apollo";

const useMutation = function(mutation, options = {}) {
const {
environments: { apolloClient }
} = useCms();

return apolloUseMutation(mutation, {
client: apolloClient,
...options
});
};

export default useMutation;
16 changes: 16 additions & 0 deletions packages/app-headless-cms/src/admin/hooks/useQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import useCms from "./useCms";
import { useQuery as apolloUseQuery } from "react-apollo";

const useQuery = function(query, options = {}) {
const {
environments: { apolloClient }
} = useCms();

return apolloUseQuery(query, {
client: apolloClient,
skip: !apolloClient,
...options
});
};

export default useQuery;

0 comments on commit 4224bca

Please sign in to comment.