-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessCodeModal.jsx
More file actions
146 lines (130 loc) · 6.32 KB
/
Copy pathAccessCodeModal.jsx
File metadata and controls
146 lines (130 loc) · 6.32 KB
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
import React, { useState, useContext, useEffect } from 'react';
import myContext from '../../context/data/myContext';
import { verifyAccessCode, isAccessCodeVerified } from '../../utils/accessCodeUtils';
import { FaKey, FaEye, FaEyeSlash, FaTimes } from 'react-icons/fa';
import toast from 'react-hot-toast';
const AccessCodeModal = ({ isOpen, onClose, onSuccess }) => {
const context = useContext(myContext);
const { mode } = context;
const [accessCode, setAccessCode] = useState('');
const [showPassword, setShowPassword] = useState(false);
const [isVerifying, setIsVerifying] = useState(false);
// Handle already verified access code
useEffect(() => {
if (isOpen && isAccessCodeVerified()) {
onSuccess();
onClose();
}
}, [isOpen, onSuccess, onClose]);
const handleClose = () => {
setAccessCode('');
setShowPassword(false);
setIsVerifying(false);
onClose();
};
const handleSubmit = async (e) => {
e.preventDefault();
if (!accessCode.trim()) {
toast.error('Please enter an access code');
return;
}
setIsVerifying(true);
try {
const result = await verifyAccessCode(accessCode.trim());
if (result.success) {
toast.success(result.message);
onSuccess();
onClose();
} else {
toast.error(result.message);
}
} catch (error) {
toast.error('An error occurred while verifying the access code');
} finally {
setIsVerifying(false);
}
};
if (!isOpen) return null;
return (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4">
<div className={`relative w-full max-w-md ${mode === 'dark' ? 'bg-gray-800' : 'bg-white'} rounded-xl shadow-2xl p-6`}>
{/* Close Button */}
<button
onClick={handleClose}
className={`absolute top-4 right-4 p-2 rounded-full hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors duration-200`}
>
<FaTimes className={`w-5 h-5 ${mode === 'dark' ? 'text-gray-300' : 'text-gray-600'}`} />
</button>
{/* Header */}
<div className="text-center mb-6">
<div className="mx-auto w-16 h-16 bg-teal-100 dark:bg-teal-900 rounded-full flex items-center justify-center mb-4">
<FaKey className="w-8 h-8 text-teal-600 dark:text-teal-400" />
</div>
<h2 className={`text-2xl font-bold mb-2 ${mode === 'dark' ? 'text-white' : 'text-gray-800'}`}>
Personal Blog Access
</h2>
<p className={`text-sm ${mode === 'dark' ? 'text-gray-300' : 'text-gray-600'}`}>
Enter your access code to view personal thoughts and reflections
</p>
</div>
{/* Form */}
<form onSubmit={handleSubmit} className="space-y-4">
<div className="relative">
<label className={`block text-sm font-medium mb-2 ${mode === 'dark' ? 'text-gray-300' : 'text-gray-700'}`}>
Access Code
</label>
<div className="relative">
<input
type={showPassword ? 'text' : 'password'}
value={accessCode}
onChange={(e) => setAccessCode(e.target.value)}
placeholder="Enter your access code"
className={`w-full px-4 py-3 pr-12 rounded-lg border-2 focus:ring-2 focus:ring-teal-500 transition-all duration-200 ${
mode === 'dark'
? 'bg-gray-700 border-gray-600 text-white placeholder-gray-400'
: 'bg-white border-gray-300 text-gray-900 placeholder-gray-500'
}`}
autoFocus
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className={`absolute right-3 top-1/2 transform -translate-y-1/2 p-1 rounded ${
mode === 'dark' ? 'text-gray-400 hover:text-gray-300' : 'text-gray-500 hover:text-gray-700'
}`}
>
{showPassword ? <FaEyeSlash className="w-4 h-4" /> : <FaEye className="w-4 h-4" />}
</button>
</div>
</div>
{/* Submit Button */}
<button
type="submit"
disabled={isVerifying || !accessCode.trim()}
className={`w-full py-3 px-4 rounded-lg font-medium transition-all duration-200 ${
isVerifying || !accessCode.trim()
? 'bg-gray-300 text-gray-500 cursor-not-allowed'
: 'bg-teal-500 text-white hover:bg-teal-600 active:bg-teal-700'
}`}
>
{isVerifying ? (
<div className="flex items-center justify-center">
<div className="animate-spin rounded-full h-5 w-5 border-b-2 border-white mr-2"></div>
Verifying...
</div>
) : (
'Access Personal Blog'
)}
</button>
</form>
{/* Footer */}
<div className="mt-6 text-center">
<p className={`text-xs ${mode === 'dark' ? 'text-gray-400' : 'text-gray-500'}`}>
This area contains personal thoughts and reflections
</p>
</div>
</div>
</div>
);
};
export default AccessCodeModal;