mirror of
https://github.com/FranP-code/Pomodoro-Timer-with-Clockify-integration.git
synced 2025-10-12 23:52:30 +00:00
Upload to the API introduced to user document in Firestore done
This commit is contained in:
34
src/components/Account Childrens/Message.jsx
Normal file
34
src/components/Account Childrens/Message.jsx
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
const Message = (props) => {
|
||||||
|
|
||||||
|
const [message, setMessage] = React.useState('')
|
||||||
|
|
||||||
|
React.useEffect( () => {
|
||||||
|
switch (props.message) {
|
||||||
|
case 'API NOT VALID':
|
||||||
|
setMessage('The API is not valid')
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'API NOT UPLOADED':
|
||||||
|
setMessage(`There's been an error while we upload your API Key. Please try again`)
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'API UPLOADED':
|
||||||
|
setMessage(`API uploaded successfully`)
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
setMessage('')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>{message}</h1>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Message
|
||||||
@@ -1,37 +1,58 @@
|
|||||||
import React, {useState} from 'react'
|
import React, {useState} from 'react'
|
||||||
import {firebase} from './Firebase/firebase'
|
import {firebase} from './Firebase/firebase'
|
||||||
import {getAuth, onAuthStateChanged} from 'firebase/auth'
|
import {getAuth, onAuthStateChanged} from 'firebase/auth'
|
||||||
|
import { doc, updateDoc, getFirestore } from "firebase/firestore";
|
||||||
|
import Message from './Account Childrens/Message';
|
||||||
|
|
||||||
const Account = () => {
|
const Account = () => {
|
||||||
|
|
||||||
const [signIn, setSignIn] = useState('false')
|
const [signIn, setSignIn] = useState('false')
|
||||||
const [apiKey, setApiKey] = useState('')
|
const [apiKey, setApiKey] = useState('')
|
||||||
|
const [fristThreeApiKey, setFristThreeApiKey] = useState('')
|
||||||
|
|
||||||
|
const [actualState, setActualState] = useState('')
|
||||||
|
|
||||||
const auth = getAuth()
|
const auth = getAuth()
|
||||||
|
|
||||||
|
let userUID
|
||||||
|
|
||||||
onAuthStateChanged(auth, (user) => {
|
onAuthStateChanged(auth, (user) => {
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
setSignIn(true)
|
setSignIn(true)
|
||||||
|
userUID = user.uid
|
||||||
} else {
|
} else {
|
||||||
setSignIn(false)
|
setSignIn(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const uploadApiKey = async (e) => {
|
const submitApiKey = async (e) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
e.target.reset()
|
||||||
|
|
||||||
const data = await makeRequest()
|
const data = await makeRequest()
|
||||||
console.log(data)
|
console.log(data)
|
||||||
|
|
||||||
if (await validateRequest(data)) {
|
if (await validateRequest(data)) {
|
||||||
|
|
||||||
|
setActualState('API VALID')
|
||||||
|
|
||||||
|
if (await uploadApiKey()) {
|
||||||
|
|
||||||
|
setActualState('API UPLOADED')
|
||||||
|
setFristThreeApiKey(apiKey[0] + apiKey[1] + apiKey[2])
|
||||||
|
} else {
|
||||||
|
|
||||||
|
setActualState('API NOT UPLOADED')
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
setActualState('API NOT VALID')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setApiKey('')
|
||||||
}
|
}
|
||||||
|
|
||||||
const makeRequest = async () => {
|
const makeRequest = async () => {
|
||||||
@@ -39,7 +60,7 @@ const Account = () => {
|
|||||||
const request = {
|
const request = {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
'X-Api-Key': apiKey,
|
'X-Api-Key': apiKey.trim(),
|
||||||
"content-type": "application/json"
|
"content-type": "application/json"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -55,23 +76,49 @@ const Account = () => {
|
|||||||
|
|
||||||
const validateRequest = async (data) => {
|
const validateRequest = async (data) => {
|
||||||
if (data.code !== 1000) {
|
if (data.code !== 1000) {
|
||||||
console.log('TODO BIEN')
|
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
console.log('TODO MAL')
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const uploadApiKey = async () => {
|
||||||
|
|
||||||
|
try {
|
||||||
|
const db = await getFirestore(firebase)
|
||||||
|
const referenceDocument = await doc(db, 'users', userUID)
|
||||||
|
|
||||||
|
await updateDoc(referenceDocument, {
|
||||||
|
keyClockify: apiKey
|
||||||
|
})
|
||||||
|
|
||||||
|
return await true
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (error) {
|
||||||
|
console.log(error)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
{
|
||||||
|
actualState === 'API NOT VALID' || actualState === 'API NOT UPLOADED' || actualState === 'API UPLOADED' ? <Message message={actualState}/> : null
|
||||||
|
}
|
||||||
{
|
{
|
||||||
signIn ?
|
signIn ?
|
||||||
<div>
|
<div>
|
||||||
<h1>One more step...</h1>
|
<h1>One more step...</h1>
|
||||||
<h2>Insert your Clockify API here</h2>
|
<h2>Insert your Clockify API here</h2>
|
||||||
<form
|
<form
|
||||||
onSubmit={uploadApiKey}
|
onSubmit={submitApiKey}
|
||||||
|
className=
|
||||||
|
{
|
||||||
|
actualState === 'API UPLOADED' ? 'disabled' : null
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
@@ -85,6 +132,34 @@ const Account = () => {
|
|||||||
type="submit"
|
type="submit"
|
||||||
/>
|
/>
|
||||||
</form>
|
</form>
|
||||||
|
{
|
||||||
|
actualState === 'API UPLOADED' ?
|
||||||
|
|
||||||
|
<div>
|
||||||
|
|
||||||
|
<label
|
||||||
|
for="change-API-button">
|
||||||
|
<div>
|
||||||
|
<p>
|
||||||
|
Change the API Key
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
{fristThreeApiKey}*******************
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<button
|
||||||
|
id="change-API-button"
|
||||||
|
onClick={() => setActualState('')}
|
||||||
|
> Change
|
||||||
|
|
||||||
|
</button>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
: null
|
||||||
|
}
|
||||||
</div>:
|
</div>:
|
||||||
<h1>Sign in before access to this page...</h1>
|
<h1>Sign in before access to this page...</h1>
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user