Skip to content

Commit

Permalink
Merge pull request #445 from TheNileDev/jrea/customQuery
Browse files Browse the repository at this point in the history
feat(react): allow a custom react-query queryclient
  • Loading branch information
jrea committed Aug 17, 2022
2 parents d73a16d + 3e3da5a commit 40fd078
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
18 changes: 17 additions & 1 deletion packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@ function App() {
}
```

Once added, there is a hook and components available for use.
Once added, there is a hook and components available for use. It is recommended to place the `<NileProvider />` as high up in your render tree as possible, since it contains both stying and request wrappers.

### Included libraries

Out of the box, [react-query](https://react-query.tanstack.com/) and [mui joy](https://mui.com/joy-ui/getting-started/overview/) is available for use. No set up is required, simply add the dependencies to your code, then use components and functions provided by those libraries.

They can be customize as follows:

```typescript
function App() {
return (
<NileProvider basePath={API_URL} queryClient={myQueryClient} theme={theme}>
<div>Welcome to my great app</div>
</NileProvider>
);
}
```

### useNile

Expand Down
22 changes: 5 additions & 17 deletions packages/react/src/context/index.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import React, { useMemo, createContext, useContext } from 'react';
import Nile, { NileApi } from '@theniledev/js';
import { CssVarsProvider } from '@mui/joy/styles';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

import theme from './themeJoiner';
import defaultTheme from './themeJoiner';
import { NileContext, NileProviderProps } from './types';

const queryClient = new QueryClient();
const defaultQueryClient = new QueryClient();

const defaultContext: NileContext = {
instance: Nile({ basePath: '', workspace: 'none', credentials: 'include' }),
workspace: '',
};

const context = createContext<NileContext>(defaultContext);
const { Provider } = context;

export const NileProvider = (props: NileProviderProps) => {
const { children } = props;
const { children, theme, queryClient } = props;

const values = useMemo<NileContext>(() => {
return {
Expand All @@ -31,19 +29,9 @@ export const NileProvider = (props: NileProviderProps) => {
};
}, [props.basePath, props.workspace]);

if (props.theme) {
return (
<QueryClientProvider client={queryClient}>
<CssVarsProvider theme={props.theme}>
<Provider value={values}>{children}</Provider>
</CssVarsProvider>
</QueryClientProvider>
);
}

return (
<QueryClientProvider client={queryClient}>
<CssVarsProvider theme={theme}>
<QueryClientProvider client={queryClient ?? defaultQueryClient}>
<CssVarsProvider theme={theme ?? defaultTheme}>
<Provider value={values}>{children}</Provider>
</CssVarsProvider>
</QueryClientProvider>
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/context/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NileApi } from '@theniledev/js';
import React from 'react';
import { Theme } from '@mui/joy/styles';
import { QueryClient } from '@tanstack/react-query';

export interface NileContext {
instance: NileApi;
Expand All @@ -12,4 +13,5 @@ export interface NileProviderProps {
basePath: string;
workspace?: string;
theme?: Theme;
queryClient?: QueryClient;
}

2 comments on commit 40fd078

@vercel
Copy link

@vercel vercel bot commented on 40fd078 Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nile-js – ./

nile-js-theniledev.vercel.app
nile-js-git-master-theniledev.vercel.app
nile-js.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 40fd078 Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

react-storybook – ./packages/react

react-storybook-git-master-theniledev.vercel.app
react-storybook-theniledev.vercel.app
react-storybook-ten.vercel.app

Please sign in to comment.