Skip to content

Commit

Permalink
fix(flows): better apis for flows
Browse files Browse the repository at this point in the history
  • Loading branch information
christianmat committed Feb 3, 2023
1 parent 928f9fb commit 949d45f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/api/flow-responses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,5 @@ export function useFlowResponses() {
return flowResponse
}

return { addResponse, markFlowStarted, markFlowCompleted }
return { addResponse, markFlowStarted, markFlowCompleted, flowResponses: Array.from(successfulFlowResponses) }
}
46 changes: 39 additions & 7 deletions src/api/flows.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useContext } from 'react'
import { API_PREFIX, PaginatedResult, useConfig } from './common'
import { FrigadeContext } from '../FrigadeProvider'
import {useFlowResponses} from "./flow-responses";

export interface Flow {
id: number
Expand All @@ -12,14 +13,11 @@ export interface Flow {
slug: string
}

export function useFlows(): {
getFlows: () => Promise<PaginatedResult<Flow> | null>
getFlow(slug: string): Flow | null
getFlowData(slug: string): object | null
hasLoadedData: boolean
} {
export function useFlows() {
const { config } = useConfig()
const { flows, hasLoadedData, setHasLoadedData } = useContext(FrigadeContext)
const { userId } = useContext(FrigadeContext)
const { addResponse, flowResponses } = useFlowResponses()

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

function getFlowSteps(slug: string): any[] {
return JSON.parse(getFlow(slug).data).data
}

function markStepStarted(flowSlug: string, stepId: string, data?: any) {
addResponse({
foreignUserId: userId,
flowSlug,
stepId,
actionType: 'STARTED_STEP',
data: data ?? {},
createdAt: new Date(),
})
}

function markStepCompleted(flowSlug: string, stepId: string, data?: any) {
addResponse({
foreignUserId: userId,
flowSlug,
stepId,
actionType: 'COMPLETED_STEP',
data: data ?? {},
createdAt: new Date(),
})
}

function getStepStatus(flowSlug: string, stepId: string) {
// TODO: add server-side call to sync date.
// Iterate through all flow responses and find the latest one for this step
const responsesForStep = flowResponses.filter(
(r) => r.flowSlug === flowSlug && r.stepId === stepId
);
}

function getFlowData(slug: string): Flow {
return JSON.parse(flows.find((f) => f.slug === slug).data)
}

return { getFlows, getFlow, getFlowData, hasLoadedData }
return { getFlows, getFlow, getFlowData, hasLoadedData, getStepStatus, getFlowSteps }
}

0 comments on commit 949d45f

Please sign in to comment.