File size: 2,199 Bytes
a85c9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from "react";
import { useRouter } from "next/router";

export default function CreateBot() {
  const [botName, setBotName] = useState("");
  const [status, setStatus] = useState("");
  const router = useRouter();

  const handleCreateBot = async (e) => {
    e.preventDefault();
    const data = {
      name: botName,
    };

    const response = await fetch("/api/create_bot", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
    });

    if (response.ok) {
      const botSlug = botName.toLowerCase().replace(/\s+/g, "_");
      router.push(`/${botSlug}/app`);
    } else {
      setBotName("");
      setStatus("fail");
      setTimeout(() => {
        setStatus("");
      }, 3000);
    }
  };

  return (
    <>
      <div className="w-full">
        {/* Create Bot */}
        <h2 className="text-xl font-bold text-gray-800">CREATE BOT</h2>
        <form className="py-2" onSubmit={handleCreateBot}>
          <label
            htmlFor="bot_name"
            className="block mb-2 text-sm font-medium text-gray-900"
          >
            Name of Bot
          </label>
          <div className="flex flex-col sm:flex-row gap-x-4 gap-y-4">
            <input
              type="text"
              id="bot_name"
              className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5"
              placeholder="Eg. Naval Ravikant"
              required
              value={botName}
              onChange={(e) => setBotName(e.target.value)}
            />
            <button
              type="submit"
              className="h-fit text-white bg-black hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center"
            >
              Submit
            </button>
          </div>
          {status === "fail" && (
            <div className="text-red-600 text-sm font-bold py-1">
              An error occurred while creating your bot!
            </div>
          )}
        </form>
      </div>
    </>
  );
}