Skip to content

Commit bf59292

Browse files
authored
Merge pull request #205 from topcoder-platform/PROD-2439_save-draft
PROD-2439 Save Draft -> PROD-2321
2 parents 9fa0448 + dc392ce commit bf59292

File tree

11 files changed

+45
-28
lines changed

11 files changed

+45
-28
lines changed

src-ts/lib/contact-support-form/contact-support-form.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const contactSupportFormDef: FormDefinition = {
1212
primaryGroup: [
1313
{
1414
buttonStyle: 'secondary',
15-
isSave: true,
15+
isSubmit: true,
1616
label: 'Submit',
1717
size: 'lg',
1818
type: 'submit',

src-ts/lib/form/Form.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Button } from '../button'
1515
import '../styles/index.scss'
1616
import { IconOutline } from '../svgs'
1717

18-
import { FormButton, FormDefinition, FormInputModel } from '.'
18+
import { FormAction, FormButton, FormDefinition, FormInputModel } from '.'
1919
import {
2020
formGetInputFields,
2121
formInitializeValues,
@@ -29,6 +29,7 @@ import { FormGroups } from './form-groups'
2929
import styles from './Form.module.scss'
3030

3131
interface FormProps<ValueType, RequestType> {
32+
readonly action?: FormAction // only type submit will perform validation
3233
readonly formDef: FormDefinition
3334
readonly formValues?: ValueType
3435
readonly onChange?: (inputs: ReadonlyArray<FormInputModel>) => void,
@@ -99,7 +100,7 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va
99100

100101
async function onSubmitAsync(event: FormEvent<HTMLFormElement>): Promise<void> {
101102
const values: RequestType = props.requestGenerator(inputs)
102-
formOnSubmitAsync<RequestType>(event, formDef, values, props.save, props.onSuccess)
103+
formOnSubmitAsync<RequestType>(props.action || 'submit', event, formDef, values, props.save, props.onSuccess)
103104
.then(() => {
104105
setFormKey(Date.now())
105106
formOnReset(inputs, props.formValues)
@@ -129,7 +130,7 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va
129130
<Button
130131
{...button}
131132
key={button.label || `button-${index}`}
132-
disable={isPrimaryGroup && isFormInvalid}
133+
disable={button.isSubmit && isFormInvalid}
133134
tabIndex={button.notTabble ? -1 : index + (inputs ? inputs.length : 0) + (formDef.tabIndexStart || 0)}
134135
/>
135136
)

src-ts/lib/form/form-button.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface FormButton {
66
readonly buttonStyle?: ButtonStyle
77
readonly icon?: FC<SVGProps<SVGSVGElement>>
88
readonly isReset?: boolean
9-
readonly isSave?: boolean
9+
readonly isSubmit?: boolean
1010
readonly label?: string
1111
readonly notTabble?: boolean
1212
onClick?: (event?: any) => void

src-ts/lib/form/form-definition.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { FormButton, FormGroup } from '.'
22

3+
export type FormAction = 'save' | 'submit' | undefined
4+
35
export interface FormButtons {
46
primaryGroup: ReadonlyArray<FormButton>
57
secondaryGroup?: ReadonlyArray<FormButton>

src-ts/lib/form/form-functions/form.functions.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ChangeEvent, FormEvent } from 'react'
22
import { toast } from 'react-toastify'
33

4-
import { FormDefinition } from '../form-definition.model'
4+
import { FormAction, FormDefinition } from '../form-definition.model'
55
import { FormGroup } from '../form-group.model'
66
import { FormInputModel } from '../form-input.model'
77

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

3232
export function initializeValues<T>(inputs: Array<FormInputModel>, formValues?: T): void {
3333
inputs
34-
.filter(input => !input.dirty && !input.touched)
34+
.filter(input => !input.dirty && !input.touched)
3535
.forEach(input => {
3636
input.value = !!(formValues as any)?.hasOwnProperty(input.name)
37-
? (formValues as any)[input.name]
38-
: undefined
37+
? (formValues as any)[input.name]
38+
: undefined
3939
})
4040
}
4141

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

6060
export async function onSubmitAsync<T>(
61+
action: FormAction,
6162
event: FormEvent<HTMLFormElement>,
6263
formDef: FormDefinition,
6364
formValue: T,
@@ -78,9 +79,11 @@ export async function onSubmitAsync<T>(
7879
// could have a form that's not dirty but has errors and you wouldn't
7980
// want to have it look like the submit succeeded
8081
const formValues: HTMLFormControlsCollection = (event.target as HTMLFormElement).elements
81-
const isValid: boolean = validateForm(formValues, 'submit', inputs)
82-
if (!isValid) {
83-
return Promise.reject()
82+
if (action === 'submit') {
83+
const isValid: boolean = validateForm(formValues, action, inputs)
84+
if (!isValid) {
85+
return Promise.reject()
86+
}
8487
}
8588

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

171174
export function validateForm(formElements: HTMLFormControlsCollection, event: 'blur' | 'change' | 'submit' | 'initial', inputs: ReadonlyArray<FormInputModel>): boolean {
172175
const errors: ReadonlyArray<FormInputModel> = inputs?.filter(formInputDef => {
173-
formInputDef.dirty = formInputDef.dirty || event === 'submit'
174-
validateField(formInputDef, formElements, event)
175-
return !!formInputDef.error
176-
})
176+
formInputDef.dirty = formInputDef.dirty || event === 'submit'
177+
validateField(formInputDef, formElements, event)
178+
return !!formInputDef.error
179+
})
177180
return !errors.length
178181
}

src-ts/lib/styles/_buttons.scss

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
border: solid $border;
2424
white-space: nowrap;
2525
cursor: pointer;
26-
font-family: $font-roboto;
26+
font-family: $font-roboto;
2727
font-weight: $font-weight-bold;
2828
font-size: 11px;
2929
line-height: 24px;
@@ -32,7 +32,7 @@
3232
&.primary,
3333
&.secondary {
3434

35-
&:focus {
35+
&:not(.disabled):focus {
3636
outline: $border solid $turq-140;
3737
}
3838
}
@@ -47,7 +47,7 @@
4747
padding: $border $pad-xl;
4848
font-size: 13px;
4949
}
50-
50+
5151
&.button-md {
5252
padding: 3*$border $pad-xl;
5353
font-size: 13px;
@@ -57,7 +57,7 @@
5757
padding: $pad-sm $pad-xxl;
5858
font-size: 14px;
5959
}
60-
60+
6161
&.button-xl {
6262
padding: $pad-md $pad-xxl;
6363
font-size: 16px;
@@ -85,6 +85,7 @@
8585
color: $black-60;
8686
background-color: $black-5;
8787
border-color: $black-5;
88+
cursor: default;
8889
}
8990
}
9091

@@ -105,6 +106,7 @@
105106
color: $black-60;
106107
background-color: $black-5;
107108
border-color: $black-5;
109+
cursor: default;
108110
}
109111
}
110112

@@ -125,6 +127,7 @@
125127
color: $black-60;
126128
background-color: $tc-white;
127129
border-color: $black-5;
130+
cursor: default;
128131
}
129132
}
130133

@@ -156,6 +159,7 @@
156159
color: $black-60;
157160
background-color: $tc-white;
158161
border-color: $black-5;
162+
cursor: default;
159163
}
160164

161165
svg {

src-ts/tools/work/work-detail-header/WorkFeedback/work-feedback-form.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const workFeedbackFormDef: FormDefinition = {
55
primaryGroup: [
66
{
77
buttonStyle: 'primary',
8-
isSave: true,
8+
isSubmit: true,
99
label: 'Mark as done',
1010
size: 'xl',
1111
type: 'submit',

src-ts/tools/work/work-self-service/intake-forms/bug-hunt/BugHuntIntakeForm.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { NavigateFunction, useNavigate, useParams } from 'react-router-dom'
33

44
import {
55
Form,
6+
FormAction,
67
FormDefinition,
78
formGetInputModel,
89
FormInputModel,
@@ -45,9 +46,13 @@ const BugHuntIntakeForm: React.FC = () => {
4546
const isMobile: boolean = useCheckIsMobile()
4647
const { isLoggedIn }: ProfileContextData = useContext<ProfileContextData>(profileContext)
4748

48-
let action: string = ''
49-
BugHuntFormConfig.buttons.primaryGroup[0].onClick = () => { action = 'save' }
50-
BugHuntFormConfig.buttons.primaryGroup[1].onClick = () => { action = 'submit' }
49+
const [action, setAction]: [FormAction, Dispatch<SetStateAction<FormAction>>] = useState()
50+
51+
BugHuntFormConfig.buttons.primaryGroup[0].onClick = () => { setAction('save') }
52+
BugHuntFormConfig.buttons.primaryGroup[1].onClick = () => { setAction('submit') }
53+
if (BugHuntFormConfig.buttons.secondaryGroup) {
54+
BugHuntFormConfig.buttons.secondaryGroup[0].onClick = () => { navigate(-1) }
55+
}
5156

5257
const [challenge, setChallenge]: [Challenge | undefined, Dispatch<SetStateAction<Challenge | undefined>>] = useState()
5358
const [formDef, setFormDef]: [FormDefinition, Dispatch<SetStateAction<FormDefinition>>]
@@ -180,6 +185,7 @@ const BugHuntIntakeForm: React.FC = () => {
180185
onSuccess={onSaveSuccess}
181186
requestGenerator={requestGenerator}
182187
save={onSave}
188+
action={action}
183189
/>
184190
</div>
185191
</>

src-ts/tools/work/work-self-service/intake-forms/bug-hunt/bug-hunt.form.config.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const BugHuntFormConfig: FormDefinition = {
1515
},
1616
{
1717
buttonStyle: 'primary',
18+
isSubmit: true,
1819
label: 'Complete and pay',
1920
onClick: () => { },
2021
type: 'submit',
@@ -92,7 +93,7 @@ export const BugHuntFormConfig: FormDefinition = {
9293
{
9394
label: 'Features to test (optional)',
9495
name: ChallengeMetadataName.featuresToTest,
95-
placeholder: 'List the sepcific features',
96+
placeholder: 'List the specific features',
9697
type: 'textarea',
9798
},
9899
],

src-ts/utils/settings/account/change-password/change-password-form.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const changePasswordFormDef: FormDefinition = {
2121
primaryGroup: [
2222
{
2323
buttonStyle: 'secondary',
24-
isSave: true,
24+
isSubmit: true,
2525
label: 'Change password',
2626
size: 'xl',
2727
type: 'submit',
@@ -80,7 +80,7 @@ export const changePasswordFormDef: FormDefinition = {
8080
{
8181
autocomplete: FormInputAutocompleteOption.off,
8282
dependentFields: [
83-
ChangePasswordFieldName.newPassword,
83+
ChangePasswordFieldName.newPassword,
8484
],
8585
label: 'Confirm Password',
8686
name: ChangePasswordFieldName.confirmPassword,

src-ts/utils/settings/account/edit-name/edit-name-form.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const editNameFormDef: FormDefinition = {
1212
primaryGroup: [
1313
{
1414
buttonStyle: 'secondary',
15-
isSave: true,
15+
isSubmit: true,
1616
label: 'Save',
1717
size: 'lg',
1818
type: 'submit',

0 commit comments

Comments
 (0)