Skip to content

Commit

Permalink
breaking: change getData interface to more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
ilfa committed Apr 13, 2022
1 parent c3d5058 commit 8c70141
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Custom prefix for localStorage and sessionStorage cache keys. Will be ignored if
- `config: UseVisitorDataConfig`'s property `immediate` determines whether the `getData()` method will be called immediately after the hook mounts or not.

#### Returned object
- `getData: (ignoreCache: boolean) => Promise<VisitorData>` Performs identification request to server and returns visitors data.
- `getData: ({ignoreCache?: boolean}) => Promise<VisitorData>` Performs identification request to server and returns visitors data.
- `isLoading: boolean` Indicates `getData` request status.
- `data: VisitorData` Contains visitors data requested after `getData()` call.
- `error: Error` Error information in case the request failed.
Expand Down
2 changes: 1 addition & 1 deletion examples/spa/src/shared/pages/SignInPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function SignInPage() {
className='form'
onSubmit={(e) => {
e.preventDefault()
getData(ignoreCache).then((data) => {
getData({ ignoreCache }).then((data) => {
if (data) {
// do something with the visitor data
// for example, append visitor data to the form data to send to your server
Expand Down
6 changes: 5 additions & 1 deletion src/fpjs-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ export interface VisitorQueryResult<TExtended extends boolean> extends QueryResu
data?: VisitorData<TExtended>
}

export interface GetDataOptions {
ignoreCache?: boolean
}

export interface VisitorQueryContext<TExtended extends boolean> extends VisitorQueryResult<TExtended> {
getData: (ignoreCache?: boolean) => Promise<VisitorData<TExtended> | void>
getData: (getDataOptions?: GetDataOptions) => Promise<VisitorData<TExtended> | void>
}

const stub = (): never => {
Expand Down
4 changes: 2 additions & 2 deletions src/use-visitor-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export function useVisitorData<TExtended extends boolean>(
const initialState = { isLoading: false }
const [state, setState] = useState<QueryResult<VisitorData<TExtended>>>(initialState)

const getData = useCallback(
async (ignoreCache = false) => {
const getData = useCallback<VisitorQueryContext<TExtended>['getData']>(
async ({ ignoreCache = false } = {}) => {
try {
setState((state) => ({ ...state, isLoading: true }))

Expand Down

0 comments on commit 8c70141

Please sign in to comment.