All files / src/components/generate GenerateForm.tsx

0% Statements 0/114
0% Branches 0/1
0% Functions 0/1
0% Lines 0/114

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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181                                                                                                                                                                                                                                                                                                                                                                         
import { Controller, SubmitHandler, useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { Dispatch, FC, SetStateAction } from 'react';
import { ErrorMessage } from '@hookform/error-message';
import { generateSchema, GenerateSchemaType } from '../../validations/generate';
import { useFetchGenerate } from '../../hooks/useFetchGenerate';
import { ErrorResponse } from '../../models/errorResponse';
import { Pending } from '../../models/pending';
import { GenerateResponse } from '../../models/generateResponse';
import { Record } from '../../models/record';
import styled from 'styled-components';
import Select, { StylesConfig } from 'react-select';
 
type Props = {
  setResult: Dispatch<SetStateAction<GenerateResponse | ErrorResponse | Pending | null>>;
  setRecordType: Dispatch<SetStateAction<Record | null>>;
};
 
// react-select
type RecordOption = {
  label: string;
  value: Record;
};
 
const recordOptions: readonly RecordOption[] = [
  { label: 'QR', value: 'Qr' },
  { label: 'バーコード', value: 'Barcode' },
  { label: 'なし', value: 'Nothing' },
];
 
// Class for react-select
const styleSelect: StylesConfig<RecordOption> = {
  control: (styles, { isFocused }) => ({
    ...styles,
    backgroundColor: 'white',
    borderRadius: 0,
    height: 51,
    fontSize: '1.6rem',
    border: '1.5px solid #6f6f6f',
    margin: '20px 0 0 0',
    '&:hover': {
      border: '1.5px solid #6f6f6f',
    },
    ...(isFocused && {
      outline: '2.5px solid #c7d01c',
    }),
  }),
  option: (styles) => ({ ...styles, fontSize: '1.6rem' }),
  input: (styles) => ({
    ...styles,
    fontSize: '1.6rem',
  }),
  placeholder: (styles) => ({
    ...styles,
    fontSize: '1.6rem',
  }),
  singleValue: (styles) => ({
    ...styles,
    fontSize: '1.6rem',
  }),
  noOptionsMessage: (styles) => ({
    ...styles,
    fontSize: '1.6rem',
  }),
  indicatorSeparator: (styles) => ({
    ...styles,
    backgroundColor: '#6f6f6f',
  }),
  dropdownIndicator: (styles) => ({
    ...styles,
    color: '#6f6f6f',
  }),
};
 
// styled-components
const StyledBox = styled.div`
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
`;
 
const StyledLabel = styled.label`
  display: block;
  font-size: 1.6rem;
  margin: 20px 0 5px 0;
  padding: 0;
`;
 
const StyledInput = styled.input`
  width: 100%;
  max-width: 369px;
  font-size: 1.6rem;
  height: 48px;
  margin: 0;
  padding: 0 14px;
  border: 1.5px solid #6f6f6f;
  border-radius: 0;
  &:focus {
    outline: 2.5px solid #c7d01c;
  }
`;
 
const StyledSubmitWrapper = styled.div`
  display: flex;
  justify-content: center;
  width: 100%;
  margin: 50px 0 10px 0;
`;
 
const StyledSubmitInput = styled.input`
  padding: 5px 20px;
  background-color: #caad63;
  border: none;
  font-size: 1.6rem;
  cursor: pointer;
`;
 
const GenerateForm: FC<Props> = (props) => {
  // react hook form
  const {
    register,
    handleSubmit,
    formState: { errors },
    control,
  } = useForm<GenerateSchemaType>({
    resolver: zodResolver(generateSchema),
    defaultValues: {
      quantity: 49,
      record: recordOptions[0].value,
    },
  });
  // update url
  const onSubmit: SubmitHandler<GenerateSchemaType> = async (formData) => {
    props.setResult('pending');
    const result: GenerateResponse | ErrorResponse = await useFetchGenerate(formData.quantity, formData.record);
    props.setRecordType(formData.record);
    props.setResult(result);
  };
  return (
    <StyledBox>
      <form onSubmit={handleSubmit(onSubmit)}>
        <StyledLabel htmlFor="quantity">Quantity</StyledLabel>
        <StyledInput id="quantity" type="number" {...register('quantity')} />
        <br />
        <ErrorMessage errors={errors} name="quantity" message={errors.quantity?.message} />
        <br />
        <StyledLabel htmlFor="record">Record</StyledLabel>
        <Controller
          name={'record'}
          control={control}
          render={({ field }) => (
            <Select
              styles={styleSelect}
              options={recordOptions}
              isSearchable={true}
              noOptionsMessage={() => '存在しないラベルの種類です。'}
              value={recordOptions.find((element) => element.value === field.value)}
              onChange={(newValue) => field.onChange((newValue as RecordOption)?.value)}
            />
          )}
        />
        {/* <StyledSelect id="record" {...register('record')}>
          <option value="Qr">QR</option>
          <option value="Barcode">バーコード</option>
          <option value="Nothing">なし</option>
        </StyledSelect> */}
        <br />
        <ErrorMessage errors={errors} name="record" message={errors.record?.message} />
        <br />
        <StyledSubmitWrapper>
          <StyledSubmitInput type="submit" value="生成" />
        </StyledSubmitWrapper>
      </form>
    </StyledBox>
  );
};
 
export default GenerateForm;