From 29d341f088094912c0db85716d688a3855f174c4 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Thu, 3 Oct 2024 02:47:29 +0530 Subject: [PATCH] feat: generate api key --- src/components/organisms/ApiKey.tsx | 138 ++++++++++++++++++++++++++-- 1 file changed, 132 insertions(+), 6 deletions(-) diff --git a/src/components/organisms/ApiKey.tsx b/src/components/organisms/ApiKey.tsx index c2708ba6..5e33c67d 100644 --- a/src/components/organisms/ApiKey.tsx +++ b/src/components/organisms/ApiKey.tsx @@ -1,9 +1,135 @@ -import React from 'react' +import React, { useState, useEffect } from 'react'; +import { + Box, + Button, + TextField, + Typography, + IconButton, + InputAdornment, + Tooltip, + CircularProgress, +} from '@mui/material'; +import { Visibility, VisibilityOff, ContentCopy } from '@mui/icons-material'; +import styled from 'styled-components'; +import axios from 'axios'; +import { useGlobalInfoStore } from '../../context/globalInfo'; + +// Styled components +const Container = styled(Box)` + display: flex; + flex-direction: column; + align-items: center; + margin-top: 50px; +`; + +const ApiKeyField = styled(TextField)` + width: 400px; + margin: 20px 0; +`; + +const HiddenText = styled(Typography)` + color: #888; + font-style: italic; +`; const ApiKey = () => { - return ( -
ApiKey
- ) -} + const [apiKey, setApiKey] = useState(null); + const [loading, setLoading] = useState(true); + const [showKey, setShowKey] = useState(false); + const [copySuccess, setCopySuccess] = useState(false); -export default ApiKey \ No newline at end of file + const {notify} = useGlobalInfoStore(); + + // Fetch API Key on mount + useEffect(() => { + const fetchApiKey = async () => { + try { + const { data } = await axios.get('http://localhost:8080/auth/api-key'); + setApiKey(data.api_key); + notify('info', `Fetc API Key: ${data.api_key}`); + } catch (error) { + console.error('Error fetching API key', error); + } finally { + setLoading(false); + } + }; + + fetchApiKey(); + }, []); + + // Handle API Key generation + const generateApiKey = async () => { + setLoading(true); + try { + const { data } = await axios.post('http://localhost:8080/auth/generate-api-key'); + setApiKey(data.api_key); + notify('info', `Genrate API Key: ${data.api_key}`); + } catch (error) { + console.error('Error generating API key', error); + } finally { + setLoading(false); + } + }; + + // Copy to clipboard handler + const copyToClipboard = () => { + if (apiKey) { + navigator.clipboard.writeText(apiKey); + setCopySuccess(true); + setTimeout(() => setCopySuccess(false), 2000); + } + }; + + // Toggle key visibility + const toggleShowKey = () => { + setShowKey(!showKey); + }; + + if (loading) return ; + + return ( + + Manage Your API Key + + {apiKey ? ( + <> + + + {showKey ? : } + + + ), + }} + /> + + + + {copySuccess && ( + + + + )} + + ) : ( + <> + You haven't generated an API key yet. + + + )} + + ); +}; + +export default ApiKey;