-
Notifications
You must be signed in to change notification settings - Fork 32
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 #957 from openedx/mkeating/ENT-6592/0
feat: New LMS Config Workflow Skeleton + Typescript
- Loading branch information
Showing
31 changed files
with
3,351 additions
and
6,097 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React, { | ||
createContext, | ||
useContext, | ||
Context, | ||
ReactNode, | ||
Dispatch, | ||
} from "react"; | ||
import type { FormActionArguments } from "./data/actions"; | ||
import type { FormWorkflowStep } from "./FormWorkflow"; | ||
|
||
export type FormFields = { [name: string]: any }; | ||
export type FormValidatorResult = boolean | string; | ||
export type FormValidator = (formFields: FormFields) => FormValidatorResult; | ||
export type FormFieldValidation = { | ||
formFieldId: string; | ||
validator: FormValidator; | ||
}; | ||
|
||
export type FormContext = { | ||
dispatch?: Dispatch<FormActionArguments>; | ||
formFields?: FormFields; | ||
isEdited?: boolean; | ||
hasErrors?: boolean; | ||
errorMap?: { [name: string]: string[] }; | ||
stateMap?: { [name: string]: any }; | ||
currentStep?: FormWorkflowStep<any>; | ||
}; | ||
|
||
export const FormContextObject: Context<FormContext> = createContext({}); | ||
|
||
export function useFormContext(): FormContext { | ||
return useContext(FormContextObject); | ||
} | ||
|
||
type FormContextProps = { | ||
children: ReactNode; | ||
formContext: FormContext; | ||
dispatch?: Dispatch<FormActionArguments>; | ||
}; | ||
|
||
// Context wrapper for all form components | ||
const FormContextProvider = ({ | ||
children, | ||
dispatch, | ||
formContext, | ||
}: FormContextProps) => { | ||
return ( | ||
<FormContextObject.Provider value={{ ...formContext, dispatch }}> | ||
{children} | ||
</FormContextObject.Provider> | ||
); | ||
}; | ||
|
||
export default FormContextProvider; |
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,45 @@ | ||
import React, { useReducer } from "react"; | ||
// @ts-ignore | ||
import FormContextProvider from "./FormContext.tsx"; | ||
import type { FormFields } from "./FormContext"; | ||
// @ts-ignore | ||
import FormWorkflow from "./FormWorkflow.tsx"; | ||
import type { FormWorkflowProps } from "./FormWorkflow"; | ||
// @ts-ignore | ||
import {FormReducer, initializeForm } from "./data/reducer.ts"; | ||
import type { FormActionArguments } from "./data/actions"; | ||
|
||
// Context wrapper for multi-step form container | ||
function FormContextWrapper<FormData>({ | ||
formWorkflowConfig, | ||
onClickOut, | ||
onSubmit, | ||
formData, | ||
}: FormWorkflowProps<FormData>) { | ||
const [formFieldsState, dispatch] = useReducer< | ||
FormReducer, | ||
FormActionArguments | ||
>( | ||
FormReducer, | ||
initializeForm( | ||
{}, | ||
{ | ||
formFields: formData as FormFields, | ||
currentStep: formWorkflowConfig.getCurrentStep(), | ||
} | ||
), | ||
initializeForm | ||
); | ||
return ( | ||
<FormContextProvider | ||
dispatch={dispatch} | ||
formContext={formFieldsState || {}} | ||
> | ||
<FormWorkflow | ||
{...{ formWorkflowConfig, onClickOut, onSubmit, dispatch }} | ||
/> | ||
</FormContextProvider> | ||
); | ||
} | ||
|
||
export default FormContextWrapper; |
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,41 @@ | ||
import React from "react"; | ||
import { AlertModal, Spinner } from "@edx/paragon"; | ||
// @ts-ignore | ||
import { useFormContext } from "./FormContext.tsx"; | ||
import type { FormContext } from "./FormContext"; | ||
|
||
type FormWaitModal = { | ||
// FormContext state that when truthy, shows the modal | ||
triggerState: string; | ||
onClose: () => void; | ||
header: string; | ||
text: string; | ||
}; | ||
|
||
// Modal shown when waiting for a background operation to complete in forms | ||
const FormWaitModal = ({ | ||
triggerState, | ||
onClose, | ||
header, | ||
text, | ||
}: FormWaitModal) => { | ||
const { stateMap }: FormContext = useFormContext(); | ||
|
||
const isOpen = stateMap && stateMap[triggerState]; | ||
|
||
return ( | ||
<AlertModal title={header} isOpen={isOpen} onClose={onClose} hasCloseButton> | ||
<div className="d-flex justify-content-center"> | ||
<Spinner | ||
screenReaderText={text} | ||
animation="border" | ||
className="mr-2" | ||
variant="primary" | ||
/> | ||
</div> | ||
<p>{text}</p> | ||
</AlertModal> | ||
); | ||
}; | ||
|
||
export default FormWaitModal; |
Oops, something went wrong.