All files / src/components/Playbooks/steps ExternalLinks.tsx

0% Statements 0/44
0% Branches 0/12
0% Functions 0/14
0% Lines 0/42

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                                                                                                                                                                                                                                                                                                                                     
import { useEffect, useState } from "react";
import styles from "./index.module.css";
 
function ExternalLinks({ links, setLinks }) {
  const [formData, setFormData] = useState({
    name: "",
    url: "",
  });
  const [currentIndex, setCurrentIndex] = useState(null);
 
  const validate = () => {
    Iif (formData.url.trim()) {
      return true;
    }
 
    return false;
  };
 
  const resetForm = () => {
    setFormData({
      url: "",
      name: "",
    });
    setCurrentIndex(null);
  };
 
  const handleEdit = () => {
    const temp = structuredClone(links);
    Iif (!currentIndex) return;
    temp[currentIndex] = {
      url: formData.url.trim(),
      name: formData.name.trim(),
    };
    setLinks(temp);
  };
 
  const handleAdd = () => {
    setLinks([
      ...(links ?? []),
      {
        url: formData.url.trim(),
        name: formData.name.trim(),
      },
    ]);
  };
 
  const handleSubmit = (e) => {
    e.preventDefault();
    Iif (!validate()) return;
 
    if (currentIndex !== null) {
      handleEdit();
    } else {
      handleAdd();
    }
    resetForm();
  };
 
  const handleChange = (e) => {
    setFormData((prev) => {
      return {
        ...prev,
        [e.target.name]: e.target.value,
      };
    });
  };
 
  const handleDelete = (index) => {
    const temp = structuredClone(links);
    temp.splice(index, 1);
    setLinks(temp);
  };
 
  const selectCurrentLink = (index) => {
    setCurrentIndex(index);
  };
 
  useEffect(() => {
    Iif (currentIndex != null) {
      setFormData({
        url: links[currentIndex].url,
        name: links[currentIndex].name,
      });
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [currentIndex]);
 
  return (
    <div>
      <form className="flex justify-start gap-2" onSubmit={handleSubmit}>
        <div className="flex flex-col">
          <label htmlFor="name" className="text-xs">
            <b>Name</b>
          </label>
          <input
            value={formData.name}
            onChange={handleChange}
            className={styles.input}
            name="name"
            id="name"
            type="text"
            placeholder="Enter the name here"
          />
        </div>
        <div className="flex flex-col">
          <label htmlFor="url" className="text-xs">
            <b>URL</b>
          </label>
          <input
            className={styles.input}
            value={formData.url}
            onChange={handleChange}
            id="url"
            name="url"
            type="url"
            placeholder="Enter the URL here"
          />
        </div>
        <button className={styles.btn}>
          {currentIndex !== null ? "Edit" : "Add"}
        </button>
      </form>
 
      {links?.length > 0 && (
        <table className={styles.table}>
          <thead>
            <tr>
              <th>Name</th>
              <th>URL</th>
              <th colSpan={2}>Actions</th>
            </tr>
          </thead>
 
          <tbody>
            {links?.map((e, i) => (
              <tr key={i}>
                <td>{e.name}</td>
                <td>{e.url}</td>
                <td>
                  <button
                    onClick={() => selectCurrentLink(i)}
                    className={styles.btn}>
                    Edit
                  </button>
                </td>
                <td>
                  <button
                    onClick={() => handleDelete(i)}
                    className={styles.btn}>
                    Delete
                  </button>
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      )}
    </div>
  );
}
 
export default ExternalLinks;