Skip to content

Commit 65a2c41

Browse files
committed
chore: show approval tag, and fix some frontend stuff
1 parent 0a77185 commit 65a2c41

File tree

6 files changed

+66
-25
lines changed

6 files changed

+66
-25
lines changed

packages/backend/src/apps/formsg/common/get-data-out-metadata.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,13 @@ async function getDataOutMetadata(
335335

336336
let questionIdsToShowForMrf: Set<string> | undefined
337337

338+
let approvalField: string | null = null
338339
if (parameters.mrf) {
339340
questionIdsToShowForMrf = new Set(
340341
(parameters.mrf as ParsedMrfWorkflowStep).fields,
341342
)
343+
approvalField =
344+
(parameters.mrf as ParsedMrfWorkflowStep).approvalField ?? null
342345
}
343346

344347
const fieldMetadata: IDataOutMetadata = Object.create(null)
@@ -391,6 +394,11 @@ async function getDataOutMetadata(
391394
isVisible: { isHidden: true },
392395
isHeader: { isHidden: true },
393396
}
397+
398+
if (approvalField && fieldId === approvalField) {
399+
fieldMetadata[fieldId].answer.type = 'approval'
400+
}
401+
394402
if (isAnswerArrayValid(fieldData)) {
395403
// table field will have a stringified table object in the answer field
396404
// so that it can be used in for-each

packages/backend/src/graphql/mutations/create-flow.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ const createFlow: MutationResolvers['createFlow'] = async (
1414
name: trimmedFlowName,
1515
})
1616

17+
/**
18+
* We do not need to create an empty action since the AddStepButton already looks like an empty step
19+
*/
1720
await flow.$relatedQuery('steps').insert([
1821
{
1922
type: 'trigger',
2023
position: 1,
2124
},
22-
{
23-
type: 'action',
24-
position: 2,
25-
},
2625
])
2726

2827
return flow

packages/frontend/src/components/Editor/components/AddStepButton.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ export function AddStepButton(props: AddStepButtonProps): JSX.Element {
3232
const { isOpen, onOpen, onClose } = useDisclosure()
3333

3434
const { dividerColor, iconBgColor } = useMemo(() => {
35-
if (approvalBranch === undefined) {
36-
return {
37-
dividerColor: 'base.divider.strong',
38-
iconBgColor: undefined,
39-
}
40-
}
4135
if (approvalBranch === 'approve') {
4236
return {
4337
dividerColor: 'green.500',
4438
iconBgColor: 'green.50',
4539
}
4640
}
41+
if (approvalBranch === 'reject') {
42+
return {
43+
dividerColor: 'red.500',
44+
iconBgColor: 'red.50',
45+
}
46+
}
4747
return {
48-
dividerColor: 'red.500',
49-
iconBgColor: 'red.50',
48+
dividerColor: 'base.divider.strong',
49+
iconBgColor: undefined,
5050
}
5151
}, [approvalBranch])
5252

@@ -93,7 +93,7 @@ export function AddStepButton(props: AddStepButtonProps): JSX.Element {
9393
</>
9494
)}
9595
{/* Top vertical line */}
96-
<Box h={6}>
96+
<Box h={4}>
9797
<Divider orientation="vertical" borderColor={dividerColor} />
9898
</Box>
9999
<TouchableTooltip
@@ -125,7 +125,7 @@ export function AddStepButton(props: AddStepButtonProps): JSX.Element {
125125
</TouchableTooltip>
126126
{/* Bottom vertical line */}
127127
{!isLastStep && (
128-
<Box h={6}>
128+
<Box h={4}>
129129
<Divider orientation="vertical" borderColor={dividerColor} />
130130
</Box>
131131
)}

packages/frontend/src/components/VariablesList/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ function VariableTag({
5050
label: 'Tile Row ID',
5151
tooltip: `This variable can be used in Tile's Update Single Row action`,
5252
}
53+
case 'approval':
54+
return {
55+
label: 'Approval',
56+
tooltip: 'This variable determines the approval status of this step',
57+
}
5358
default:
5459
return {
5560
label: null,

packages/frontend/src/contexts/MrfContext.tsx

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { IStep, IStepApprovalBranch } from '@plumber/types'
22

3-
import { createContext, useContext, useState } from 'react'
3+
import { createContext, useContext, useEffect, useMemo, useState } from 'react'
4+
import { isEqual } from 'lodash'
45
import get from 'lodash/get'
56

67
import { FORMSG_APP_KEY, MRF_ACTION_KEY } from '@/helpers/formsg'
@@ -29,28 +30,55 @@ interface MrfContextProviderProps {
2930
children: React.ReactNode
3031
}
3132

33+
function generateDefaultApprovalBranches(
34+
mrfApprovalSteps: IStep[],
35+
): Record<string, IStepApprovalBranch> {
36+
return mrfApprovalSteps.reduce((acc, step) => {
37+
acc[step.id] = 'approve'
38+
return acc
39+
}, {} as Record<string, IStepApprovalBranch>)
40+
}
41+
3242
export const MrfContextProvider = ({ children }: MrfContextProviderProps) => {
3343
const { flow } = useContext(EditorContext)
3444

3545
const [disabledMrfStepToDisplay, setDisabledMrfStepToDisplay] =
3646
useState<IStep | null>(null)
3747

38-
const mrfSteps = flow.steps.filter(
39-
(step) => step.appKey === FORMSG_APP_KEY && step.key === MRF_ACTION_KEY,
48+
const mrfSteps = useMemo(
49+
() =>
50+
flow.steps.filter(
51+
(step) => step.appKey === FORMSG_APP_KEY && step.key === MRF_ACTION_KEY,
52+
),
53+
[flow.steps],
4054
)
4155

42-
const mrfApprovalSteps = mrfSteps.filter(
43-
(step) => !!get(step.parameters, 'mrf.approvalField', false),
56+
const mrfApprovalSteps = useMemo(
57+
() =>
58+
mrfSteps.filter(
59+
(step) => !!get(step.parameters, 'mrf.approvalField', false),
60+
),
61+
[mrfSteps],
4462
)
4563

4664
const [approvalBranches, setApprovalBranches] = useState<
4765
Record<string, IStepApprovalBranch>
48-
>({
49-
...mrfApprovalSteps.reduce((acc, step) => {
50-
acc[step.id] = 'approve'
51-
return acc
52-
}, {} as Record<string, IStepApprovalBranch>),
53-
})
66+
>(generateDefaultApprovalBranches(mrfApprovalSteps))
67+
68+
/**
69+
* We need to reset the state when the mrf approval steps change
70+
*/
71+
useEffect(() => {
72+
if (
73+
isEqual(
74+
mrfApprovalSteps.map((step) => step.id),
75+
Object.keys(approvalBranches),
76+
)
77+
) {
78+
return
79+
}
80+
setApprovalBranches(generateDefaultApprovalBranches(mrfApprovalSteps))
81+
}, [approvalBranches, mrfApprovalSteps])
5482

5583
const setApprovalBranch = (stepId: string, branch: IStepApprovalBranch) => {
5684
setApprovalBranches((prev) => ({

packages/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export type TDataOutMetadatumType =
6060
| 'array'
6161
| 'tile_row_id'
6262
| 'table'
63+
| 'approval'
6364

6465
/**
6566
* This should only be defined on _leaf_ nodes (i.e. **primitive array

0 commit comments

Comments
 (0)