Skip to content

Commit fbbabac

Browse files
custom event
1 parent 6586ebb commit fbbabac

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2. Send File via Email Using EmailJS
2+
EmailJS allows you to send emails without a backend. To send an email with an attachment:
3+
4+
Steps to Set Up EmailJS:
5+
Sign up at EmailJS and create a service.
6+
Get the service_id, template_id, and user_id from EmailJS.
7+
Modify your code to send emails with the uploaded file.

src/components/feedback-form/feedback-form.tsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ export class FeedbackForm {
6767
@Prop({ mutable: true }) placeholder: string;
6868
@Prop() fontFamily: string = 'Calibri'; // Default font family doesnt work when initialvalue has font
6969
@Prop() fontSize: string; // Default font size prop
70+
@State() uploadedFile: File | null = null;
71+
7072
@Element() el: HTMLElement;
7173

7274
@Event() valueChange: EventEmitter<string>;
@@ -83,9 +85,13 @@ export class FeedbackForm {
8385
},
8486
};
8587
//https://fluid.libertymutual.com/fluid/fluid-file-upload.html
86-
fileListUpdated(event) {
87-
console.log(event.detail);
88+
fileListUpdated(event: CustomEvent) {
89+
console.log('uploaded file'+event.detail);
90+
const files = event.detail;
91+
if (files && files.length > 0) {
92+
this.uploadedFile = files[0]; // Store the first uploaded file
8893
}
94+
}
8995
//https://fluid-components.libertymutual.com/fluid/fluid-file-upload.html
9096
uploadClicked(event) {
9197
console.log(event.detail);
@@ -317,7 +323,7 @@ export class FeedbackForm {
317323
}
318324
//Before performing any operations- GET or SET- ensure that the this.editor instance is available
319325
//send email also without backend
320-
async getContentFromEditor() {
326+
async sendEmail() {
321327
if (this.editor) {
322328
// Access properties or methods of the TinyMCE editor instance
323329
this.editorContent = this.editor.getContent();
@@ -348,6 +354,16 @@ export class FeedbackForm {
348354
alert('Please fill out first 3 fields before submitting.');
349355
return;
350356
}
357+
if (!this.uploadedFile) {
358+
console.error("No file uploaded.");
359+
return;
360+
}
361+
362+
const formData = new FormData();
363+
formData.append('file', this.uploadedFile);
364+
formData.append('name', this.userName);
365+
formData.append('email', this.userEmail);
366+
formData.append('message', this.editorContent);
351367
// Call EmailJS to send the email (Browser based or client based)
352368
//Emailjs dashboard: https://dashboard.emailjs.com/sign-up
353369
//templateParams is this.editorContent
@@ -357,7 +373,8 @@ export class FeedbackForm {
357373
'template_szeawas', // Template ID from EmailJS dashboard
358374
{ user_name: this.userName,
359375
user_email: this.userEmail,
360-
content: this.editorContent }, // Template parameters (the content of the email)
376+
content: this.editorContent,
377+
file: this.uploadedFile }, // Template parameters (the content of the email)
361378
{
362379
publicKey: 'IRGsyXDXq7ZJHMbzF',
363380
}// Your user ID from EmailJS: YOUR_PUBLIC_KEY
@@ -447,12 +464,11 @@ export class FeedbackForm {
447464
aria-placeholder={this.placeholder}></div>
448465
</label>
449466
<fluid-file-upload
450-
uploadControlConfig="uploadControlConfig"
451-
fileListUpdated="fileListUpdated($event)"
467+
onFileListUpdated={(event) => this.fileListUpdated(event)}
452468
uploadClicked="uploadClicked($event)"
453469
showFileList="false">
454470
</fluid-file-upload>
455-
<button onClick={() => this.getContentFromEditor()}>Submit</button>
471+
<button onClick={() => this.sendEmail()}>Submit</button>
456472
<button onClick={() => this.setContentInEditor('')}>Clear Content</button>
457473
</div>
458474
);

0 commit comments

Comments
 (0)