Skip to content

Commit 949d45f

Browse files
committed
fix(flows): better apis for flows
1 parent 928f9fb commit 949d45f

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

src/api/flow-responses.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,5 @@ export function useFlowResponses() {
119119
return flowResponse
120120
}
121121

122-
return { addResponse, markFlowStarted, markFlowCompleted }
122+
return { addResponse, markFlowStarted, markFlowCompleted, flowResponses: Array.from(successfulFlowResponses) }
123123
}

src/api/flows.tsx

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useContext } from 'react'
22
import { API_PREFIX, PaginatedResult, useConfig } from './common'
33
import { FrigadeContext } from '../FrigadeProvider'
4+
import {useFlowResponses} from "./flow-responses";
45

56
export interface Flow {
67
id: number
@@ -12,14 +13,11 @@ export interface Flow {
1213
slug: string
1314
}
1415

15-
export function useFlows(): {
16-
getFlows: () => Promise<PaginatedResult<Flow> | null>
17-
getFlow(slug: string): Flow | null
18-
getFlowData(slug: string): object | null
19-
hasLoadedData: boolean
20-
} {
16+
export function useFlows() {
2117
const { config } = useConfig()
2218
const { flows, hasLoadedData, setHasLoadedData } = useContext(FrigadeContext)
19+
const { userId } = useContext(FrigadeContext)
20+
const { addResponse, flowResponses } = useFlowResponses()
2321

2422
function getFlows() {
2523
return fetch(`${API_PREFIX}flows`, config).then((r) => r.json())
@@ -29,9 +27,43 @@ export function useFlows(): {
2927
return flows.find((f) => f.slug === slug)
3028
}
3129

30+
function getFlowSteps(slug: string): any[] {
31+
return JSON.parse(getFlow(slug).data).data
32+
}
33+
34+
function markStepStarted(flowSlug: string, stepId: string, data?: any) {
35+
addResponse({
36+
foreignUserId: userId,
37+
flowSlug,
38+
stepId,
39+
actionType: 'STARTED_STEP',
40+
data: data ?? {},
41+
createdAt: new Date(),
42+
})
43+
}
44+
45+
function markStepCompleted(flowSlug: string, stepId: string, data?: any) {
46+
addResponse({
47+
foreignUserId: userId,
48+
flowSlug,
49+
stepId,
50+
actionType: 'COMPLETED_STEP',
51+
data: data ?? {},
52+
createdAt: new Date(),
53+
})
54+
}
55+
56+
function getStepStatus(flowSlug: string, stepId: string) {
57+
// TODO: add server-side call to sync date.
58+
// Iterate through all flow responses and find the latest one for this step
59+
const responsesForStep = flowResponses.filter(
60+
(r) => r.flowSlug === flowSlug && r.stepId === stepId
61+
);
62+
}
63+
3264
function getFlowData(slug: string): Flow {
3365
return JSON.parse(flows.find((f) => f.slug === slug).data)
3466
}
3567

36-
return { getFlows, getFlow, getFlowData, hasLoadedData }
68+
return { getFlows, getFlow, getFlowData, hasLoadedData, getStepStatus, getFlowSteps }
3769
}

0 commit comments

Comments
 (0)