import { getDatabase, ref, remove, push } from "firebase/database";
import emailjs from "emailjs-com";
import {
REACT_APP_EMAILJS_SERVICE_ID,
REACT_APP_EMAILJS_TEMPLATE2_ID,
REACT_APP_EMAILJS_USER_ID,
} from "../env";
/**
* The function `acceptPOI` accepts a point of interest (POI) suggestion of a specific type, adds it to
* the database, removes the suggestion entry, sends an email notification, and displays an alert
* message.
* @param poiType - poiType is a string that specifies the type of point of interest (POI) being
* accepted. It can have values of "toiletpoint", "foodpoint", or "waterpoint".
* @param data - The `data` parameter in the `acceptPOI` function contains the following properties: Name, X, Y, and email.
*/
export async function acceptPOI(poiType, data) {
const database = getDatabase();
let poiRef;
if (poiType === "toiletpoint") poiRef = ref(database, `toilets`);
else if (poiType === "foodpoint") poiRef = ref(database, `foodpoints`);
else if (poiType === "waterpoint") poiRef = ref(database, `waterpoints`);
const poiData = {
Name: data.Name,
X: data.X,
Y: data.Y,
};
try {
await push(poiRef, poiData);
// Remove the POI entry from the database
await remove(ref(database, `newsuggestions/${poiType}/${data.id}`));
const templateParams = {
to_email: data.email,
};
emailjs
.send(
// process.env.REACT_APP_EMAILJS_SERVICE_ID, // Replace with your EmailJS template ID
// process.env.REACT_APP_EMAILJS_TEMPLATE_ID, // Replace with your EmailJS user ID
// templateParams,
// process.env.REACT_APP_EMAILJS_USER_ID, // Replace with your EmailJS service ID
REACT_APP_EMAILJS_SERVICE_ID,
REACT_APP_EMAILJS_TEMPLATE2_ID,
templateParams,
REACT_APP_EMAILJS_USER_ID
)
.then(
(response) => {
console.log("Email sent successfully:", response);
},
(error) => {
console.error("Email failed to send:", error);
}
);
alert(`The ${poiType} suggestion has been approved successfully!`);
} catch (error) {
alert(`Error approved the ${poiType} suggestion: ${error}`);
throw error;
}
}