Skip to content

Commit

Permalink
fix: merge variables on subsequent calls
Browse files Browse the repository at this point in the history
  • Loading branch information
christianmat committed Oct 9, 2024
1 parent 3022788 commit 51b6444
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/ninety-rivers-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@frigade/js": patch
"@frigade/react": patch
---

Fixes an issue where variables are sometimes not applied when using useFlow() with the same Flow ID in a component
22 changes: 8 additions & 14 deletions packages/js-api/src/core/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export class Flow extends Fetchable {

if (this.getGlobalState().variables[this.id]) {
this.applyVariablesInternal(this.getGlobalState().variables[this.id] ?? {})
} else {
this.applyVariablesInternal({})
}
}

Expand All @@ -167,9 +169,6 @@ export class Flow extends Fetchable {
*/
private init() {
const statefulFlow = this.getStatefulFlow()

this.resyncState()

const newSteps = new Map<string, FlowStep>()

statefulFlow.data.steps.forEach((step, index) => {
Expand Down Expand Up @@ -339,16 +338,8 @@ export class Flow extends Fetchable {
newSteps.set(step.id, stepObj as FlowStep)
})
this.steps = newSteps
// Check if empty object
if (
this.getGlobalState().variables &&
this.getGlobalState().variables[this.id] &&
Object.keys(this.getGlobalState().variables[this.id]).length > 0
) {
this.applyVariablesInternal(this.getGlobalState().variables[this.id])
} else {
this.applyVariablesInternal({})
}

this.resyncState()
}

/**
Expand Down Expand Up @@ -595,7 +586,10 @@ export class Flow extends Fetchable {
})
}

this.getGlobalState().variables[this.id] = variables
this.getGlobalState().variables[this.id] = {
...this.getGlobalState().variables[this.id],
...variables,
}

if (resyncState) {
this.resyncState()
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/hooks/useFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function useFlow(
...config?.variables,
})
}, [config?.variables, flowId, variables])

return {
flow,
isLoading: frigade?.hasFailedToLoad() ? false : !flow,
Expand Down

0 comments on commit 51b6444

Please sign in to comment.