Newer
Older
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
import { useState, useEffect } from "react";
import Input from "../components/Input";
import Button from "../components/Button";
import { useNavigate } from "react-router-dom";
function LinkPageForm() {
const [random, setRandom] = useState("");
const [link, setLink] = useState("");
const navigate = useNavigate();
const generateRandom = () => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const linkLength = 10;
let randomLink = '';
for (let i = 0; i < linkLength; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
randomLink += characters[randomIndex];
}
setRandom(randomLink);
};
const copyToClipboard = async (link) => {
try {
const textToCopy = `localhost:3000/HomeParticipate/${random}`;
await navigator.clipboard.writeText(textToCopy);
alert('클립보드에 복사되었습니다');
} catch (err) {
alert('클립보드 복사에 실패하였습니다');
}
};
const handleSubmit = (event) => {
event.preventDefault();
setLink(`/HomeParticipate/${random}`)
navigate(link);
}
useEffect(() => {
generateRandom();
}, [])
return (
<form onSubmit={handleSubmit}>
<div>
<Input
value={`localhost:3000/HomeParticipate/${random}`}
/>
<Button
type="button"
onClick={copyToClipboard}
text="링크 복사"
/>
<Button
type="submit"
text="투표 페이지로 이동"
/>
</div>
</form>
);
}
export default LinkPageForm;