This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from parodos-dev/FLPATH-423/m2k-input
show alert if a call to `status` returns an alertMessage field
- Loading branch information
Showing
13 changed files
with
400 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
plugins/parodos/src/components/workflow/workflowDetail/WorkflowContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { assert } from 'assert-ts'; | ||
import React, { ReactNode, useContext } from 'react'; | ||
import { createContext, useMemo } from 'react'; | ||
import { useImmerReducer } from 'use-immer'; | ||
import { WorkflowTask } from '../../../models/workflowTaskSchema'; | ||
|
||
type WorkFlowMode = 'RUNNING' | 'TASK_ALERT'; | ||
|
||
export interface WorkFlowContext { | ||
workflowMode: WorkFlowMode; | ||
showAlert(workflow: WorkflowTask): void; | ||
clearAlert(): void; | ||
} | ||
|
||
export type WorkflowActions = | ||
| { | ||
type: 'TASK_ALERT'; | ||
} | ||
| { | ||
type: 'CLEAR_ALERT'; | ||
}; | ||
|
||
interface State { | ||
workflowMode: WorkFlowMode; | ||
} | ||
|
||
const reducer = (draft: State, action: WorkflowActions) => { | ||
switch (action.type) { | ||
case 'TASK_ALERT': { | ||
draft.workflowMode = 'TASK_ALERT'; | ||
break; | ||
} | ||
case 'CLEAR_ALERT': { | ||
draft.workflowMode = 'RUNNING'; | ||
break; | ||
} | ||
default: | ||
throw new Error(`no action`); | ||
} | ||
}; | ||
|
||
export const WorkflowContext = createContext<WorkFlowContext | undefined>( | ||
undefined, | ||
); | ||
|
||
const initialState: State = { | ||
workflowMode: 'RUNNING', | ||
}; | ||
|
||
export function WorkflowProvider({ | ||
children, | ||
}: { | ||
children: ReactNode; | ||
}): JSX.Element { | ||
const [state, dispatch] = useImmerReducer(reducer, initialState); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
workflowMode: state.workflowMode, | ||
showAlert: () => dispatch({ type: 'TASK_ALERT' }), | ||
clearAlert: () => dispatch({ type: 'CLEAR_ALERT' }), | ||
}), | ||
[dispatch, state.workflowMode], | ||
); | ||
|
||
return ( | ||
<WorkflowContext.Provider value={value}> | ||
{children} | ||
</WorkflowContext.Provider> | ||
); | ||
} | ||
|
||
export function useWorkflowContext() { | ||
const context = useContext(WorkflowContext); | ||
|
||
assert(!!context, 'useWorkflowProvider must be within WorkflowProvider'); | ||
|
||
return context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.