All files / src/components/views/Credentials/IssueVc IssueVcFormInput.tsx

0% Statements 0/47
0% Branches 0/50
0% Functions 0/9
0% Lines 0/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                                                                                                                                                                                                                                                                                                                               
import React from "react";
import { Box, Checkbox, Input, Field, Text } from "rimble-ui";
import { JsonSchemaNode } from "vc-schema-tools";
import { DidSearchWithMessagingInfo } from "../../../elements/DidSearchWithMessagingInfo";
import { DidSearch } from "../../../elements/DidSearch";
import { Identifier } from "../../../../types";
import { isoToDatetimeLocal } from "../../../../utils/helpers";
 
export interface IssueVcFormInputProps {
  name: string;
  value: any;
  node: JsonSchemaNode;
  identifiers: Identifier[];
  required?: boolean;
  defaultSubjectDid?: string;
  subjectSupportsMessaging?: boolean;
  isNested?: boolean;
  onChange(value: any): void;
  setSubjectSupportsMessaging(supported: boolean): void;
}
 
export const IssueVcFormInput: React.FunctionComponent<IssueVcFormInputProps> = (props) => {
  const {
    name,
    node,
    value,
    onChange,
    required,
    identifiers,
    defaultSubjectDid,
    subjectSupportsMessaging,
    setSubjectSupportsMessaging,
    isNested,
  } = props;
 
  const isDid = node.type === "string" && node.format === "uri" && node.$linkedData?.["@id"] === "@id";
  const isSubjectDid = isDid && name === "id" && !isNested;
 
  React.useEffect(() => {
    // Most required fields will be handled by form validation, but requiring a boolean or a nested object is not.
    // @TODO/tobek In some cases this isn't handling nested required booleans but that's a heck of an edge case and ideally we should check final VC against JSON Schema anyway which would catch this.
    if (required && typeof value === "undefined") {
      if (node.type === "boolean") {
        onChange(false);
      } else if (node.type === "object") {
        onChange({});
      }
    }
  }, [node.type, required, value, onChange]);
 
  const renderInput = () => {
    if (node.properties && node.type === "object") {
      return (
        <Box pt={2} pl="24px" required={node.required?.length}>
          {Object.entries(node.properties).map(([nestedKey, nestedNode]) => (
            <IssueVcFormInput
              key={nestedKey}
              name={nestedKey}
              node={nestedNode}
              value={value?.[nestedKey]}
              required={node.required?.includes(nestedKey)}
              identifiers={identifiers}
              onChange={(updatedNestedValue) =>
                onChange({
                  ...value,
                  [nestedKey]: updatedNestedValue,
                })
              }
              subjectSupportsMessaging={subjectSupportsMessaging}
              setSubjectSupportsMessaging={setSubjectSupportsMessaging}
              isNested
            />
          ))}
        </Box>
      );
    }
 
    if (isSubjectDid) {
      return (
        <Box mb={3}>
          <DidSearchWithMessagingInfo
            key={name}
            onChange={(val) => onChange(val.did)}
            required={required}
            identifiers={identifiers}
            defaultSelectedDid={defaultSubjectDid}
            supportsMessaging={subjectSupportsMessaging}
            setSupportsMessaging={setSubjectSupportsMessaging}
          />
        </Box>
      );
    } else if (isDid) {
      return (
        <Box mb={3}>
          <DidSearch
            key={name}
            onChange={(val) => onChange(val.did)}
            required={required}
            identifiers={identifiers}
            defaultSelectedDid={defaultSubjectDid}
          />
        </Box>
      );
    }
 
    if (node.type === "boolean") {
      return <Checkbox checked={!!value} onChange={() => onChange(!value)} />;
    }
 
    let type = "text";
    let placeholder = "";
    let width: string | undefined = "100%";
    let inputValue = value || "";
    if (node.type === "number" || node.type === "integer") {
      type = "number";
      width = undefined;
    } else if (node.format === "date") {
      type = "date";
    } else if (node.format === "date-time") {
      type = "datetime-local";
      inputValue = inputValue && isoToDatetimeLocal(inputValue);
    } else if (node.format === "uri") {
      type = "url";
      placeholder = "URL";
    }
 
    function changeHandler(event: any) {
      const newValue = event.target.value;
      if (type === "number") {
        onChange(parseInt(newValue, 10));
      } else if (type === "datetime-local") {
        onChange(new Date(newValue).toISOString());
      } else {
        onChange(newValue);
      }
    }
 
    return (
      <Input
        type={type}
        disabled={false}
        value={inputValue}
        onChange={changeHandler}
        required={required}
        width={width}
        placeholder={placeholder}
      />
    );
  };
 
  return (
    <Field label={node.title || name} width="100%" mb={isDid ? 0 : 3}>
      <Box required={required || node.required?.length} width="100%">
        {node.description ? <Text fontSize={1}>{node.description}</Text> : <></>}
        {renderInput()}
      </Box>
    </Field>
  );
};