Skip to content

Commit

Permalink
adding email check to avoid duplicates (#26)
Browse files Browse the repository at this point in the history
Co-authored-by: Visakh Vijayan <visakh.vijayan@jalantechnologies.com>
  • Loading branch information
visakhvjn and vjnvisakh-jtc authored Mar 16, 2024
1 parent 8a2ccbf commit 9c82486
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/components/main/main.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getQuoteFromGemini } from "../../services/gemini";
import { QuoteResponse } from "../../types/common";
import { getQuoteFromNinja } from "../../services/ninja";
import { Input } from "baseui/input";
import { addSubscriber } from "../../services/firebase";
import { addSubscriber, isSubscribedAlready } from "../../services/firebase";

let firstLoad = true;

Expand Down Expand Up @@ -84,6 +84,14 @@ export const Main = () => {
setIsEmailValid(true);

setIsButtonLoading(true);

if (await isSubscribedAlready(email)) {
localStorage.setItem('isSubscribed', 'true');
setIsSubscribed(true);
setIsButtonLoading(false);
return;
}

await addSubscriber(email).then(() => {
setEmail('');
localStorage.setItem('isSubscribed', 'true');
Expand Down
10 changes: 9 additions & 1 deletion src/services/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { initializeApp } from "firebase/app";
import { getFirestore, collection, addDoc } from 'firebase/firestore'
import { getFirestore, collection, addDoc, getDocs, where, query } from 'firebase/firestore'

const firebaseConfig = {
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
Expand All @@ -19,4 +19,12 @@ export const addSubscriber = async (email: string) => {
email,
status: 'ACTIVE'
});
}

export const isSubscribedAlready = async (email: string) => {
const querySnapshot = await getDocs(
query(collection(db, 'subscribers'), where('email', '==', email))
);

return !querySnapshot.empty;
}

0 comments on commit 9c82486

Please sign in to comment.