Skip to content

PROD-2439 Save Draft -> PROD-2321 #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const contactSupportFormDef: FormDefinition = {
primaryGroup: [
{
buttonStyle: 'secondary',
isSave: true,
isSubmit: true,
label: 'Submit',
size: 'lg',
type: 'submit',
Expand Down
7 changes: 4 additions & 3 deletions src-ts/lib/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Button } from '../button'
import '../styles/index.scss'
import { IconOutline } from '../svgs'

import { FormButton, FormDefinition, FormInputModel } from '.'
import { FormAction, FormButton, FormDefinition, FormInputModel } from '.'
import {
formGetInputFields,
formInitializeValues,
Expand All @@ -29,6 +29,7 @@ import { FormGroups } from './form-groups'
import styles from './Form.module.scss'

interface FormProps<ValueType, RequestType> {
readonly action?: FormAction // only type submit will perform validation
readonly formDef: FormDefinition
readonly formValues?: ValueType
readonly onChange?: (inputs: ReadonlyArray<FormInputModel>) => void,
Expand Down Expand Up @@ -96,7 +97,7 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va

async function onSubmitAsync(event: FormEvent<HTMLFormElement>): Promise<void> {
const values: RequestType = props.requestGenerator(inputs)
formOnSubmitAsync<RequestType>(event, formDef, values, props.save, props.onSuccess)
formOnSubmitAsync<RequestType>(props.action || 'submit', event, formDef, values, props.save, props.onSuccess)
.then(() => {
setFormKey(Date.now())
formOnReset(inputs, props.formValues)
Expand Down Expand Up @@ -126,7 +127,7 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va
<Button
{...button}
key={button.label || `button-${index}`}
disable={isPrimaryGroup && isFormInvalid}
disable={button.isSubmit && isFormInvalid}
tabIndex={button.notTabble ? -1 : index + (inputs ? inputs.length : 0) + (formDef.tabIndexStart || 0)}
/>
)
Expand Down
2 changes: 1 addition & 1 deletion src-ts/lib/form/form-button.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface FormButton {
readonly buttonStyle?: ButtonStyle
readonly icon?: FC<SVGProps<SVGSVGElement>>
readonly isReset?: boolean
readonly isSave?: boolean
readonly isSubmit?: boolean
readonly label?: string
readonly notTabble?: boolean
onClick?: (event?: any) => void
Expand Down
2 changes: 2 additions & 0 deletions src-ts/lib/form/form-definition.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FormButton, FormGroup } from '.'

export type FormAction = 'save' | 'submit' | undefined

export interface FormButtons {
primaryGroup: ReadonlyArray<FormButton>
secondaryGroup?: ReadonlyArray<FormButton>
Expand Down
25 changes: 14 additions & 11 deletions src-ts/lib/form/form-functions/form.functions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChangeEvent, FormEvent } from 'react'
import { toast } from 'react-toastify'

import { FormDefinition } from '../form-definition.model'
import { FormAction, FormDefinition } from '../form-definition.model'
import { FormGroup } from '../form-group.model'
import { FormInputModel } from '../form-input.model'

Expand Down Expand Up @@ -31,11 +31,11 @@ export function getInputModel(inputs: ReadonlyArray<FormInputModel>, fieldName:

export function initializeValues<T>(inputs: Array<FormInputModel>, formValues?: T): void {
inputs
.filter(input => !input.dirty && !input.touched)
.filter(input => !input.dirty && !input.touched)
.forEach(input => {
input.value = !!(formValues as any)?.hasOwnProperty(input.name)
? (formValues as any)[input.name]
: undefined
? (formValues as any)[input.name]
: undefined
})
}

Expand All @@ -58,6 +58,7 @@ export function onReset(inputs: ReadonlyArray<FormInputModel>, formValue?: any):
}

export async function onSubmitAsync<T>(
action: FormAction,
event: FormEvent<HTMLFormElement>,
formDef: FormDefinition,
formValue: T,
Expand All @@ -78,9 +79,11 @@ export async function onSubmitAsync<T>(
// could have a form that's not dirty but has errors and you wouldn't
// want to have it look like the submit succeeded
const formValues: HTMLFormControlsCollection = (event.target as HTMLFormElement).elements
const isValid: boolean = validateForm(formValues, 'submit', inputs)
if (!isValid) {
return Promise.reject()
if (action === 'submit') {
const isValid: boolean = validateForm(formValues, action, inputs)
if (!isValid) {
return Promise.reject()
}
}

// set the properties for the updated T value
Expand Down Expand Up @@ -170,9 +173,9 @@ function validateField(formInputDef: FormInputModel, formElements: HTMLFormContr

export function validateForm(formElements: HTMLFormControlsCollection, event: 'blur' | 'change' | 'submit' | 'initial', inputs: ReadonlyArray<FormInputModel>): boolean {
const errors: ReadonlyArray<FormInputModel> = inputs?.filter(formInputDef => {
formInputDef.dirty = formInputDef.dirty || event === 'submit'
validateField(formInputDef, formElements, event)
return !!formInputDef.error
})
formInputDef.dirty = formInputDef.dirty || event === 'submit'
validateField(formInputDef, formElements, event)
return !!formInputDef.error
})
return !errors.length
}
12 changes: 8 additions & 4 deletions src-ts/lib/styles/_buttons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
border: solid $border;
white-space: nowrap;
cursor: pointer;
font-family: $font-roboto;
font-family: $font-roboto;
font-weight: $font-weight-bold;
font-size: 11px;
line-height: 24px;
Expand All @@ -32,7 +32,7 @@
&.primary,
&.secondary {

&:focus {
&:not(.disabled):focus {
outline: $border solid $turq-140;
}
}
Expand All @@ -47,7 +47,7 @@
padding: $border $pad-xl;
font-size: 13px;
}

&.button-md {
padding: 3*$border $pad-xl;
font-size: 13px;
Expand All @@ -57,7 +57,7 @@
padding: $pad-sm $pad-xxl;
font-size: 14px;
}

&.button-xl {
padding: $pad-md $pad-xxl;
font-size: 16px;
Expand Down Expand Up @@ -85,6 +85,7 @@
color: $black-60;
background-color: $black-5;
border-color: $black-5;
cursor: default;
}
}

Expand All @@ -105,6 +106,7 @@
color: $black-60;
background-color: $black-5;
border-color: $black-5;
cursor: default;
}
}

Expand All @@ -125,6 +127,7 @@
color: $black-60;
background-color: $tc-white;
border-color: $black-5;
cursor: default;
}
}

Expand Down Expand Up @@ -156,6 +159,7 @@
color: $black-60;
background-color: $tc-white;
border-color: $black-5;
cursor: default;
}

svg {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const workFeedbackFormDef: FormDefinition = {
primaryGroup: [
{
buttonStyle: 'primary',
isSave: true,
isSubmit: true,
label: 'Mark as done',
size: 'xl',
type: 'submit',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NavigateFunction, useNavigate, useParams } from 'react-router-dom'

import {
Form,
FormAction,
FormDefinition,
formGetInputModel,
FormInputModel,
Expand Down Expand Up @@ -45,9 +46,13 @@ const BugHuntIntakeForm: React.FC = () => {
const isMobile: boolean = useCheckIsMobile()
const { isLoggedIn }: ProfileContextData = useContext<ProfileContextData>(profileContext)

let action: string = ''
BugHuntFormConfig.buttons.primaryGroup[0].onClick = () => { action = 'save' }
BugHuntFormConfig.buttons.primaryGroup[1].onClick = () => { action = 'submit' }
const [action, setAction]: [FormAction, Dispatch<SetStateAction<FormAction>>] = useState()

BugHuntFormConfig.buttons.primaryGroup[0].onClick = () => { setAction('save') }
BugHuntFormConfig.buttons.primaryGroup[1].onClick = () => { setAction('submit') }
if (BugHuntFormConfig.buttons.secondaryGroup) {
BugHuntFormConfig.buttons.secondaryGroup[0].onClick = () => { navigate(-1) }
}

const [challenge, setChallenge]: [Challenge | undefined, Dispatch<SetStateAction<Challenge | undefined>>] = useState()
const [formDef, setFormDef]: [FormDefinition, Dispatch<SetStateAction<FormDefinition>>]
Expand Down Expand Up @@ -187,6 +192,7 @@ const BugHuntIntakeForm: React.FC = () => {
onSuccess={onSaveSuccess}
requestGenerator={requestGenerator}
save={onSave}
action={action}
/>
</div>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const BugHuntFormConfig: FormDefinition = {
},
{
buttonStyle: 'primary',
isSubmit: true,
label: 'Complete and pay',
onClick: () => { },
type: 'submit',
Expand Down Expand Up @@ -92,7 +93,7 @@ export const BugHuntFormConfig: FormDefinition = {
{
label: 'Features to test (optional)',
name: ChallengeMetadataName.featuresToTest,
placeholder: 'List the sepcific features',
placeholder: 'List the specific features',
type: 'textarea',
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const changePasswordFormDef: FormDefinition = {
primaryGroup: [
{
buttonStyle: 'secondary',
isSave: true,
isSubmit: true,
label: 'Change password',
size: 'xl',
type: 'submit',
Expand Down Expand Up @@ -80,7 +80,7 @@ export const changePasswordFormDef: FormDefinition = {
{
autocomplete: FormInputAutocompleteOption.off,
dependentFields: [
ChangePasswordFieldName.newPassword,
ChangePasswordFieldName.newPassword,
],
label: 'Confirm Password',
name: ChangePasswordFieldName.confirmPassword,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const editNameFormDef: FormDefinition = {
primaryGroup: [
{
buttonStyle: 'secondary',
isSave: true,
isSubmit: true,
label: 'Save',
size: 'lg',
type: 'submit',
Expand Down