-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add folder for student register issue: There was a router problem for student register resolve: always make page.js for route just separate them by making different folders. * database * - Created useContext for user (uid) - Added service folder with functions to read and write data from the database - Added authentication for student/homepage and student/information - Updated student/login page to fix email and password authentication issues * starting login page * login page design * signup page complete * adding colors * file change * forget password * add modal * add conditions * login page * add posst function for menu and restaurant details * meging with main * adding restaurant information page * fetching and adding data for restaurants * merging from origin * merge from orign * adding emails status in database * add and create function for adding new email with active status add and create function for getting email names where active status is true make changes in register page to check that email is in database for register * chengin import sttement * adding terms condition * adding logos facility * merge from master * update database sait-staff * adding search option * commit * connecting sait-staff with database * merging form master * changes on sait * mege from master to origin * changing restaurant info table * adding data to database * updating cards * adding logc for increase and decrease arrow * adding databse changes * updating datasbe function to getting student information * adding authentication for restaurants * changing Searching bar * adding search bar in sait-staff * updating restaurant login page * adding update,delete and change password features * adding fuctionality of deleting user's information from database including user data and restaurant menu, delete data from storage and from authentication * update edit functionallity in sait-staff and add delete functionality in restaurant * add add functionality for admins --------- Co-authored-by: Abdel Mouzahir <[email protected]>
- Loading branch information
1 parent
ec69477
commit 88f8882
Showing
3 changed files
with
234 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
"use client"; | ||
import React, { useState } from "react"; | ||
import { formatPhoneNumber } from "@/Constant/formated"; | ||
import Swal from "sweetalert2"; | ||
|
||
import { collection, addDoc } from "firebase/firestore"; | ||
import { db, auth } from "@/app/firebase/config"; | ||
import { createUserWithEmailAndPassword } from "firebase/auth"; | ||
import { existingRestaurantData } from "@/services/PostRequest/postRequest"; | ||
|
||
const Add = ({ admin, setAdmins, setIsAdding }) => { | ||
const [role, setRole] = useState(""); | ||
const [name, setName] = useState(""); | ||
const [email, setEmail] = useState(""); | ||
const [phoneNumber, setPhoneNumber] = useState(""); | ||
const [address, setAddress] = useState(""); | ||
|
||
const currentDate = new Date(); | ||
|
||
const day = currentDate.getDate(); | ||
const month = currentDate.getMonth(); // Remember, month is zero-indexed | ||
const year = currentDate.getFullYear(); | ||
|
||
const newDate = [day, month, year]; | ||
let firstWord = name.split(" "); | ||
|
||
//generic password will be first name of student or restaurant + last three digits of mobile number + "!" | ||
// like student name :- Moiz Khan mobilenumber :- 1234567890, so the password will be Moiz890! | ||
// by this the password will bw different for every one and they can change it on forget password | ||
const genericPassword = firstWord[0] + phoneNumber.slice(-3).concat("!"); | ||
|
||
const handleRoleChange = (event) => { | ||
setRole(event.target.value); | ||
}; | ||
|
||
const handleAdd = async (e) => { | ||
e.preventDefault(); | ||
|
||
if (!name || !email || !phoneNumber || !address || role.length <= 0) { | ||
return Swal.fire({ | ||
icon: "error", | ||
title: "Error!", | ||
text: "All fields are required.", | ||
showConfirmButton: true, | ||
}); | ||
} | ||
|
||
try { | ||
console.log("geneic password: ", genericPassword); | ||
// Create user in Firebase Authentication | ||
const userCredential = await createUserWithEmailAndPassword( | ||
auth, | ||
email, | ||
genericPassword | ||
); | ||
const user = userCredential.user; | ||
|
||
// Add restaurant to Firestore | ||
const newAdmin = { | ||
name, | ||
email, | ||
role, | ||
phoneNumber, | ||
address, | ||
uid: user.uid, | ||
acountCreated: newDate, // link with user ID | ||
active: true, | ||
}; | ||
|
||
await addDoc(collection(db, "saitStaff"), { | ||
...newAdmin, | ||
}); | ||
|
||
// Update local state | ||
admin.push(newAdmin); | ||
setAdmins(admin); | ||
setIsAdding(false); | ||
|
||
Swal.fire({ | ||
icon: "success", | ||
title: "Added!", | ||
text: `${name}'s data has been added.`, | ||
showConfirmButton: false, | ||
timer: 1500, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
if ( | ||
error == "FirebaseError: Firebase: Error (auth/email-already-in-use)." | ||
) { | ||
const newAdmin = { | ||
name, | ||
email, | ||
role, | ||
phoneNumber, | ||
}; | ||
//function to change active state from false to true | ||
await existingRestaurantData(email); | ||
Swal.fire({ | ||
icon: "success", | ||
title: "Added!", | ||
text: `We located your previous account, and you can resume using it.`, | ||
showConfirmButton: false, | ||
timer: 1500, | ||
}); | ||
// Update local state | ||
admin.push(newAdmin); | ||
setIsAdding(false); | ||
return; | ||
} | ||
Swal.fire({ | ||
icon: "error", | ||
title: "Error!", | ||
text: error.message, | ||
showConfirmButton: true, | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="container mx-auto mt-8 p-6 bg-gray-100 rounded-lg shadow-lg max-w-lg"> | ||
<form onSubmit={handleAdd} className="space-y-6"> | ||
<h1 className="text-2xl font-bold mb-4 text-center text-gray-700"> | ||
Add Admin | ||
</h1> | ||
<div> | ||
<label | ||
htmlFor="name" | ||
className="block text-sm font-medium text-gray-700" | ||
> | ||
Admin Name | ||
</label> | ||
<input | ||
id="name" | ||
type="text" | ||
name="name" | ||
value={name} | ||
onChange={(e) => setName(e.target.value)} | ||
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm" | ||
/> | ||
</div> | ||
<div> | ||
<label | ||
htmlFor="email" | ||
className="block text-sm font-medium text-gray-700" | ||
> | ||
</label> | ||
<input | ||
id="email" | ||
type="email" | ||
name="email" | ||
value={email} | ||
onChange={(e) => setEmail(e.target.value)} | ||
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm" | ||
/> | ||
</div> | ||
<div> | ||
<label | ||
htmlFor="Role" | ||
className="block text-sm font-medium text-gray-700" | ||
> | ||
Role | ||
</label> | ||
<select | ||
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm bg-white" | ||
onChange={handleRoleChange} | ||
value={role} | ||
> | ||
<option value="">Select a role</option> | ||
<option value="admin">Admin</option> | ||
<option value="editor">Editor</option> | ||
<option value="viewer">Viewer</option> | ||
</select> | ||
</div> | ||
<div> | ||
<label | ||
htmlFor="phone" | ||
className="block text-sm font-medium text-gray-700" | ||
> | ||
Phone Number | ||
</label> | ||
<input | ||
id="phone" | ||
type="text" | ||
maxLength={14} | ||
name="phone" | ||
value={phoneNumber} | ||
onChange={(e) => setPhoneNumber(formatPhoneNumber(e.target.value))} | ||
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm" | ||
/> | ||
</div> | ||
<div> | ||
<label | ||
htmlFor="address" | ||
className="block text-sm font-medium text-gray-700" | ||
> | ||
Address | ||
</label> | ||
<input | ||
id="address" | ||
type="text" | ||
name="address" | ||
value={address} | ||
onChange={(e) => setAddress(e.target.value)} | ||
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm" | ||
/> | ||
</div> | ||
<div className="flex justify-end space-x-3"> | ||
<button | ||
type="submit" | ||
className="bg-blue-600 text-white px-4 py-2 rounded shadow hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500" | ||
> | ||
Add | ||
</button> | ||
<button | ||
type="button" | ||
className="bg-gray-600 text-white px-4 py-2 rounded shadow hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500" | ||
onClick={() => setIsAdding(false)} | ||
> | ||
Cancel | ||
</button> | ||
</div> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Add; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters