hooks#useForm TypeScript Examples

The following examples show how to use hooks#useForm. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AuctionModal.tsx    From gear-js with GNU General Public License v3.0 5 votes vote down vote up
function AuctionModal({ close }: Props) {
  const { id } = useParams() as Params;

  const { values, handleChange } = useForm({ minPrice: '', duration: '', bidPeriod: '' });
  const { minPrice, duration, bidPeriod } = values;

  const sendMessage = useMarketplaceMessage();

  const getMilliseconds = (value: string) => Number(value) * MILLISECONDS_MULTIPLIER;

  const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
    e.preventDefault();

    if (minPrice && duration) {
      const payload = {
        CreateAuction: {
          nftContractId: NFT_CONTRACT_ADDRESS,
          ftContractId: null,
          tokenId: id,
          duration: getMilliseconds(duration),
          bidPeriod: getMilliseconds(bidPeriod),
          minPrice,
        },
      };

      sendMessage(payload).then(close);
    }
  };

  return (
    <Modal heading="Auction" close={close}>
      <form className={styles.form} onSubmit={handleSubmit}>
        <Input placeholder="min price" name="minPrice" value={minPrice} onChange={handleChange} />
        <Input placeholder="duration (min)" name="duration" value={duration} onChange={handleChange} />
        <Input placeholder="bid period (min)" name="bidPeriod" value={bidPeriod} onChange={handleChange} />
        <Button type="submit" text="Start auction" block />
      </form>
    </Modal>
  );
}
Example #2
Source File: Create.tsx    From gear-js with GNU General Public License v3.0 4 votes vote down vote up
function Create() {
  const ipfs = useIPFS();
  const sendMessage = useNftMessage();

  const { values, handleChange, handleFileChange } = useForm<Values>(initValues);
  const { name, description, json, image } = values;

  const trimmedName = useMemo(() => name.trim(), [name]);
  const trimmedDescription = useMemo(() => description.trim(), [description]);

  const [error, setError] = useState('');

  const getMintPayload = (jsonCid: CID, imgCid: CID) => {
    const tokenMetadata = {
      name: trimmedName,
      description: trimmedDescription,
      media: imgCid.toString(),
      reference: jsonCid.toString(),
    };

    return { Mint: { tokenMetadata } };
  };

  const resetError = () => {
    setError('');
  };

  const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
    e.preventDefault();

    if (trimmedName && trimmedDescription) {
      if (image && json) {
        const imageTypes = ['image/png', 'image/gif', 'image/jpeg'];
        const isImage = imageTypes.includes(image.type);
        const isImageSizeValid = image.size / 1024 ** 2 < 10;
        const isJson = json.type === 'application/json';

        if (isImage && isImageSizeValid) {
          if (isJson) {
            const attachments = [ipfs.add(json), ipfs.add(image)];

            Promise.all(attachments)
              .then(([jsonResult, imgResult]) => getMintPayload(jsonResult.cid, imgResult.cid))
              .then((payload) => sendMessage(payload));
          } else {
            setError('Please provide valid .json');
          }
        } else {
          setError("Image should be .jpg, .png or .gif and it's size should not exceed 10MB");
        }
      } else {
        setError('Please attach files');
      }
    } else {
      setError('Name and description are required');
    }
  };

  return (
    <>
      <h2 className={styles.heading}>Create NFT</h2>
      <div className={styles.main}>
        <form className={styles.form} onSubmit={handleSubmit} onChange={resetError}>
          <Input className={styles.input} label="Name" name="name" value={name} onChange={handleChange} />
          <Textarea
            className={styles.input}
            label="Description"
            name="description"
            value={description}
            onChange={handleChange}
          />
          <FileInput
            label="JSON"
            className={styles.input}
            name="json"
            value={json}
            onChange={handleFileChange}
            accept="application/json"
          />
          <FileInput
            label="Image"
            className={styles.input}
            name="image"
            value={image}
            onChange={handleFileChange}
            accept="image/png, image/gif, image/jpeg"
          />
          <Button type="submit" text="Create" disabled={!!error} block />
          <p className={styles.error}>{error}</p>
        </form>
      </div>
    </>
  );
}