Skip to content

Commit

Permalink
Do not allow addition if the user in question does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ln-dev7 committed Nov 28, 2023
1 parent 8847b84 commit 9c11fc6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
51 changes: 40 additions & 11 deletions components/form/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "@/constants/languages"
import { useAuthContext } from "@/context/AuthContext"
import { useGitHubLogout } from "@/firebase/auth/githubLogout"
import { checkIdAvailability } from "@/firebase/firestore/checkIfIDExistOnCollection"
import { useCreateDocument } from "@/firebase/firestore/createDocument"
import { useDeleteDocument } from "@/firebase/firestore/deleteDocument"
import { useDocument } from "@/firebase/firestore/getDocument"
Expand Down Expand Up @@ -205,14 +206,33 @@ export default function SettingsForms({ dataForm }: { dataForm: any }) {
}

const [usernamePeopleToAdd, setUsernamePeopleToAdd] = useState("")
const [checkIfUsernameExist, setCheckIfUsernameExist] = useState(false)
const addOrRemoveCollaborator = async (pseudo: string) => {
let updatedFormData = {
collaborators: dataForm.collaborators.includes(pseudo)
? dataForm.collaborators.filter((item) => item !== pseudo)
: [...dataForm.collaborators, pseudo],
}
setCheckIfUsernameExist(true)
checkIdAvailability("users", pseudo)
.then((isAvailable) => {
if (isAvailable) {
let updatedFormData = {
collaborators: dataForm.collaborators.includes(pseudo)
? dataForm.collaborators.filter((item) => item !== pseudo)
: [...dataForm.collaborators, pseudo],
}

updateFormDocument({ id, updatedFormData })
updateFormDocument({ id, updatedFormData })
} else {
toast({
variant: "destructive",
title: "This user not exist on Sharuco",
description: "Make sure you have entered the correct user name.",
action: <ToastAction altText="Okay">Okay</ToastAction>,
})
return
}
})
.catch((error) => console.error("Error : ", error))
.finally(() => {
setCheckIfUsernameExist(false)
})
}

const onSubmit = async (data) => {
Expand Down Expand Up @@ -375,13 +395,20 @@ export default function SettingsForms({ dataForm }: { dataForm: any }) {
value={usernamePeopleToAdd}
/>
<Button
disabled={usernamePeopleToAdd === "" || isLoadingUpdateForm}
disabled={
usernamePeopleToAdd === "" ||
checkIfUsernameExist ||
isLoadingUpdateForm
}
className="shrink-0"
onClick={() => {
setUsernamePeopleToAdd("")
addOrRemoveCollaborator(usernamePeopleToAdd)
}}
>
{checkIfUsernameExist && (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
)}
Add people
</Button>
</div>
Expand All @@ -395,15 +422,17 @@ export default function SettingsForms({ dataForm }: { dataForm: any }) {
<div className="w-full flex">
<a
href={`/user/${collaborator}`}
className="text-sm font-semibold"
className="text-sm font-semibold underline underline-offset-4 cursor-pointer"
>
{collaborator}
</a>
</div>
<span
className="shrink-0 block text-sm text-red-600 font-medium underline underline-offset-4 cursor-pointer"
onClick={(e) => {
addOrRemoveCollaborator(collaborator)
onClick={() => {
isLoadingUpdateForm
? undefined
: addOrRemoveCollaborator(collaborator)
}}
>
Remove
Expand Down Expand Up @@ -497,7 +526,7 @@ export default function SettingsForms({ dataForm }: { dataForm: any }) {
<div className="w-full flex items-center justify-between">
<Button
variant="default"
disabled={isLoadingUpdateForm}
disabled={isLoadingUpdateForm || checkIfUsernameExist}
onClick={isLoadingUpdateForm ? undefined : handleSubmit(onSubmit)}
>
{isLoadingUpdateForm ? (
Expand Down
16 changes: 16 additions & 0 deletions firebase/firestore/checkIfIDExistOnCollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getFirestore, collection, doc, getDoc } from 'firebase/firestore';

import firebase_app from "../config"

const db = getFirestore(firebase_app)

export async function checkIdAvailability(collectionName, id) {
try {
const docRef = doc(collection(db, collectionName), id);
const docSnap = await getDoc(docRef);
return docSnap.exists();
} catch (error) {
console.error('Error : ', error);
return false;
}
}

0 comments on commit 9c11fc6

Please sign in to comment.