I am writing a ticketing system in react using Node.js as backend and Python for email server.
What I am trying to do is when somebody submits a form, code access all the form data and send it to plain old JavaScript class with constructor only and append this class object to queue class.
However, there are multiple problems:
When I add this form to queue class it adds but when I click submit button again it does not add. Array size remains 1
When I try to access queue class in home page it gives me blank array
My main logic is when someone submits form it should not submit immediately rather it should get added to the queue and processed (submitting form to server) in the background independent of main thread.
How can I achieve this logic?
This is my queue class:
class EmailQueue{
constructor(){
this.items = []
}
enqueu(element){
this.items.push(element)
}
dequeue(){
if(this.isEmpty()){
return "No Email Present"
}
return this.items[0]
}
// isEmpty function
isEmpty()
{
// return true if the queue is empty.
return this.items.length == 0;
}
// front function
front()
{
// returns the Front element of
// the queue without removing it.
if(this.isEmpty())
return "No Email in Queue";
return this.items[0];
}
// printQueue function
printQueue(){
console.log("in print")
console.log(this.items)
var str = "";
for(var i = 0; i < this.items.length; i++)
str += this.items[i] +" ";
return str;
}
}
export default EmailQueue
This is my email message class:
class emailMessageclass{
constructor(emailid,from,subject,message,messageID){
this.emailid = emailid
this.from = from
this.subject = subject
this.message = message
this.messageID = messageID
}
}
export default emailMessageclass
This is my formdata class:
const getformData = (e)=>{
e.preventDefault()
const Formdata = new FormData(e.currentTarget)
let formObeject= Object.fromEntries(Formdata.entries())
const getclass = new emailMessageclass()
getclass.emailid = formObeject["toEmail"]
getclass.from = formObeject['FromEmail']
getclass.subject = formObeject["subject"]
getclass.message = formObeject["toEmail"]
getclass.messageID = formObeject["messageid"]
const emailQueue = new EmailQueue()
emailQueue.enqueu(getclass)
// console.log(emailQueue.printQueue())
console.log(emailQueue)
This is how I am trying to access it in home page route:
setTimeout(()=>{
const x = new EmailQueue();
console.log(x.printQueue())
},2000)
How can I improve this?