Passed
Push — feature/application-skill-info... ( 9c1770...3584f9 )
by Chris
05:42 queued 15s
created

resources/assets/js/helpers/forms.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 45
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 23
dl 0
loc 45
rs 10
c 0
b 0
f 0
mnd 1
bc 1
fnc 0
bpm 0
cpm 0
noi 0
1
import { FormikProps, FormikValues } from "formik";
2
import isEmpty from "lodash/isEmpty";
3
import { MutableRefObject } from "react";
4
5
/**
6
 * Focuses on a element with given id.
7
 * @param id
8
 */
9
export const focusOnElement = (elementId: string): void => {
10
  const element = document.getElementById(elementId);
11
  if (element) {
12
    element.focus();
13
  }
14
};
15
16
/**
17
 * Runs validation on all forms, then returns true if they are all valid.
18
 * TODO: Figure out how to focus the first (or last) invalid input, if any.
19
 * @param refs
20
 */
21
export const validateAllForms = (
22
  refs: MutableRefObject<FormikProps<FormikValues>>[],
23
): Promise<boolean> => {
24
  return Promise.all(
25
    refs.map((ref: MutableRefObject<FormikProps<FormikValues>>) =>
26
      ref.current.validateForm(),
27
    ),
28
  ).then((errors) => errors.every(isEmpty));
29
};
30
31
/**
32
 * Submits all forms.
33
 * @param refs
34
 */
35
export const submitAllForms = (
36
  refs: React.MutableRefObject<FormikProps<FormikValues>>[],
37
): Promise<void[]> => {
38
  return Promise.all(
39
    refs.map((ref: MutableRefObject<FormikProps<FormikValues>>) =>
40
      // TODO: Might need to make one mass submission by combining all values into an array.
41
      ref.current.submitForm(),
42
    ),
43
  );
44
};
45