Skip to content

Commit

Permalink
fix: guest id issue
Browse files Browse the repository at this point in the history
  • Loading branch information
christianmat committed Feb 8, 2023
1 parent 733dd48 commit 9b0cd3b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
7 changes: 2 additions & 5 deletions src/DataFetcher/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const DataFetcher: FC<DataFetcherProps> = ({}) => {

function generateGuestUserId() {
// If userId is null, generate a guest user id using uuid
if (userId === null) {
if (!userId) {
// Call local storage to see if we already have a guest user id
const guestUserId = localStorage.getItem(guestUserIdField)
if (guestUserId) {
Expand All @@ -75,12 +75,9 @@ export const DataFetcher: FC<DataFetcherProps> = ({}) => {
}
}

useEffect(() => {
generateGuestUserId()
}, [])

useEffect(() => {
if (userId !== null) {
generateGuestUserId()
syncFlows()
}
}, [userId, flows])
Expand Down
14 changes: 9 additions & 5 deletions src/FrigadeProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { createContext, FC, useState } from 'react'
import React, { createContext, FC, useEffect, useState } from 'react'
import { DataFetcher } from '../DataFetcher'
import { Flow } from '../api/flows'
import { FlowResponse } from '../api/flow-responses'

export interface IFrigadeContext {
publicApiKey: string
userId?: string
userId?: string | null
setUserId: React.Dispatch<React.SetStateAction<string | null>>
flows: Flow[]
setFlows: React.Dispatch<React.SetStateAction<Flow[]>>
Expand Down Expand Up @@ -38,14 +38,18 @@ export const FrigadeContext = createContext<IFrigadeContext>({
})

export const FrigadeProvider: FC<FrigadeProviderProps> = ({ publicApiKey, userId, children }) => {
const [userIdValue, setUserIdValue] = useState<string | null>(
userId === undefined ? null : userId
)
const [userIdValue, setUserIdValue] = useState<string | null>(!userId ? null : userId)
const [flows, setFlows] = useState<Flow[]>([])
const [failedFlowResponses, setFailedFlowResponses] = useState<FlowResponse[]>([])
const [flowResponses, setFlowResponses] = useState<FlowResponse[]>([])
const [isLoading, setIsLoading] = useState(true)

useEffect(() => {
if (userId !== userIdValue) {
setUserIdValue(userId)
}
}, [userId])

return (
<FrigadeContext.Provider
value={{
Expand Down

0 comments on commit 9b0cd3b

Please sign in to comment.