@formique/semantq 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/formique-semantq.js +113 -39
  2. package/package.json +1 -1
@@ -99,9 +99,8 @@ class Formique extends FormBuilder {
99
99
  ];
100
100
  this.formiqueEndpoint = "https://formiqueapi.onrender.com/api/send-email";
101
101
 
102
- // DISABLE DOM LISTENER
103
-
104
- // document.addEventListener('DOMContentLoaded', () => {
102
+ // DISABLE DOM LISTENER
103
+ //document.addEventListener('DOMContentLoaded', () => {
105
104
  // 1. Build the form's HTML in memory
106
105
  this.formMarkUp += this.renderFormElement(); // Adds opening <form> tag and any hidden inputs
107
106
 
@@ -155,7 +154,7 @@ class Formique extends FormBuilder {
155
154
  const formElement = document.getElementById(`${this.formId}`);
156
155
  if (formElement) { // Add a check here just in case, although it should now exist
157
156
  formElement.addEventListener('submit', function(event) {
158
- if (this.formSettings.submitMode === 'email') {
157
+ if (this.formSettings.submitMode === 'email' || this.formSettings.submitMode === 'rsvp') {
159
158
  event.preventDefault();
160
159
  document.getElementById("formiqueSpinner").style.display = "block";
161
160
  this.handleEmailSubmission(this.formId);
@@ -187,7 +186,7 @@ class Formique extends FormBuilder {
187
186
  this.applyTheme('dark', this.formContainerId); // Default to 'dark'
188
187
  }
189
188
 
190
- // DISABLE DOM LISTENER
189
+ // DISABLE DOM LISTENER
191
190
  //}); // DOM LISTENER WRAPPER
192
191
 
193
192
  // CONSTRUCTOR WRAPPER FOR FORMIQUE CLASS
@@ -799,14 +798,14 @@ hasFileInputs(form) {
799
798
 
800
799
  async handleEmailSubmission(formId) {
801
800
  console.log(`Starting email submission for form ID: ${formId}`);
802
-
801
+
803
802
  const form = document.getElementById(formId);
804
803
  if (!form) {
805
804
  console.error(`Form with ID ${formId} not found`);
806
805
  throw new Error(`Form with ID ${formId} not found`);
807
806
  }
808
807
 
809
- // Validate required settings - now checks if sendTo is array with at least one item
808
+ // Validate required settings for 'sendTo'
810
809
  if (!Array.isArray(this.formSettings?.sendTo) || this.formSettings.sendTo.length === 0) {
811
810
  console.error('formSettings.sendTo must be an array with at least one recipient email');
812
811
  throw new Error('formSettings.sendTo must be an array with at least one recipient email');
@@ -816,46 +815,53 @@ async handleEmailSubmission(formId) {
816
815
  const payload = {
817
816
  formData: {},
818
817
  metadata: {
819
- recipients: this.formSettings.sendTo, // Now sending array
820
- timestamp: new Date().toISOString()
821
- }
818
+ recipients: this.formSettings.sendTo,
819
+ timestamp: new Date().toISOString(),
820
+ },
822
821
  };
823
822
 
824
823
  let senderName = '';
825
824
  let senderEmail = '';
826
825
  let formSubject = '';
826
+ let registrantEmail = ''; // Variable to store the registrant's email
827
827
 
828
828
  console.log('Initial payload structure:', JSON.parse(JSON.stringify(payload)));
829
829
 
830
- // Process form fields (unchanged)
831
- new FormData(form).forEach((value, key) => {
830
+ // Process form fields and find registrant's email
831
+ const formData = new FormData(form);
832
+ formData.forEach((value, key) => {
832
833
  console.log(`Processing form field - Key: ${key}, Value: ${value}`);
833
834
  payload.formData[key] = value;
834
-
835
+
835
836
  const lowerKey = key.toLowerCase();
836
- if ((lowerKey === 'email' || lowerKey.includes('email'))) {
837
+ if (lowerKey.includes('email')) {
837
838
  senderEmail = value;
838
839
  }
839
- if ((lowerKey === 'name' || lowerKey.includes('name'))) {
840
+ if (lowerKey.includes('name')) {
840
841
  senderName = value;
841
842
  }
842
- if ((lowerKey === 'subject' || lowerKey.includes('subject'))) {
843
+ if (lowerKey.includes('subject')) {
843
844
  formSubject = value;
844
845
  }
846
+
847
+ // NEW: Check if the current field is the registrant's email
848
+ if (this.formSettings.emailField && key === this.formSettings.emailField) {
849
+ registrantEmail = value;
850
+ }
845
851
  });
846
852
 
847
853
  // Determine the email subject with fallback logic
848
- payload.metadata.subject = formSubject ||
849
- this.formSettings.subject ||
850
- 'Message From Contact Form';
851
-
854
+ payload.metadata.subject = formSubject ||
855
+ this.formSettings.subject ||
856
+ 'Message From Contact Form';
857
+
852
858
  console.log('Determined email subject:', payload.metadata.subject);
853
859
 
854
860
  // Add sender information to metadata
855
861
  if (senderEmail) {
856
862
  payload.metadata.sender = senderEmail;
857
- payload.metadata.replyTo = senderName
858
- ? `${senderName} <${senderEmail}>`
863
+ payload.metadata.replyTo = senderName
864
+ ? `${senderName} <${senderEmail}>`
859
865
  : senderEmail;
860
866
  }
861
867
 
@@ -864,54 +870,113 @@ async handleEmailSubmission(formId) {
864
870
  try {
865
871
  const endpoint = this.formiqueEndpoint || this.formAction;
866
872
  const method = this.method || 'POST';
867
-
868
- console.log(`Preparing to send request to: ${endpoint}`);
873
+
874
+ console.log(`Preparing to send primary request to: ${endpoint}`);
869
875
  console.log(`Request method: ${method}`);
870
- console.log('Final payload being sent:', payload);
876
+ console.log('Final payload being sent to recipients:', payload);
871
877
 
878
+ // Send the first email to the 'sendTo' recipients
872
879
  const response = await fetch(endpoint, {
873
880
  method: method,
874
- headers: {
881
+ headers: {
875
882
  'Content-Type': 'application/json',
876
- 'X-Formique-Version': '1.0'
883
+ 'X-Formique-Version': '1.0',
877
884
  },
878
- body: JSON.stringify(payload)
885
+ body: JSON.stringify(payload),
879
886
  });
880
887
 
881
- console.log(`Received response with status: ${response.status}`);
888
+ console.log(`Received response for primary email with status: ${response.status}`);
882
889
 
883
890
  if (!response.ok) {
884
891
  const errorData = await response.json().catch(() => ({}));
885
892
  console.error('API Error Response:', errorData);
886
893
  throw new Error(errorData.error || `HTTP error! status: ${response.status}`);
887
- document.getElementById("formiqueSpinner").style.display = "none";
888
-
889
894
  }
890
895
 
891
896
  const data = await response.json();
892
- console.log('API Success Response:', data);
893
-
894
- const successMessage = this.formSettings.successMessage ||
895
- data.message ||
896
- 'Your message has been sent successfully!';
897
+ console.log('Primary API Success Response:', data);
898
+
899
+ // ------------------- NEW RSVP LOGIC -------------------
900
+ if (this.formSettings.submitMode === 'rsvp' && registrantEmail && this.formSettings.registrantMessage) {
901
+ console.log('RSVP mode detected. Sending confirmation email to registrant.');
902
+
903
+ // Create a new payload for the registrant
904
+ const rsvpPayload = {
905
+ formData: payload.formData,
906
+ metadata: {
907
+ recipients: [registrantEmail], // Send only to the registrant
908
+ timestamp: new Date().toISOString(),
909
+ subject: this.formSettings.registrantSubject || 'RSVP Confirmation',
910
+ body: this.processDynamicMessage(this.formSettings.registrantMessage, payload.formData),
911
+ sender: this.formSettings.sendFrom || 'noreply@yourdomain.com',
912
+ replyTo: this.formSettings.sendFrom || 'noreply@yourdomain.com',
913
+ },
914
+ };
915
+
916
+ try {
917
+ console.log('Preparing to send RSVP email. Final payload:', rsvpPayload);
918
+ const rsvpResponse = await fetch(endpoint, {
919
+ method: method,
920
+ headers: {
921
+ 'Content-Type': 'application/json',
922
+ 'X-Formique-Version': '1.0',
923
+ },
924
+ body: JSON.stringify(rsvpPayload),
925
+ });
926
+
927
+ if (!rsvpResponse.ok) {
928
+ const rsvpErrorData = await rsvpResponse.json().catch(() => ({}));
929
+ console.error('RSVP API Error Response:', rsvpErrorData);
930
+ // Log the error but don't fail the entire submission since the primary email was sent
931
+ console.warn('Failed to send RSVP email to registrant, but primary submission was successful.');
932
+ } else {
933
+ console.log('RSVP email sent successfully to registrant.');
934
+ }
935
+ } catch (rsvpError) {
936
+ console.error('RSVP email submission failed:', rsvpError);
937
+ console.warn('Failed to send RSVP email to registrant, but primary submission was successful.');
938
+ }
939
+ }
940
+ // ------------------- END NEW RSVP LOGIC -------------------
941
+
942
+ const successMessage = this.formSettings.successMessage ||
943
+ data.message ||
944
+ 'Your message has been sent successfully!';
897
945
  console.log(`Showing success message: ${successMessage}`);
898
946
 
899
947
  this.showSuccessMessage(successMessage);
900
948
 
901
949
  } catch (error) {
902
950
  console.error('Email submission failed:', error);
903
- const errorMessage = this.formSettings.errorMessage ||
904
- error.message ||
905
- 'Failed to send message. Please try again later.';
951
+ const errorMessage = this.formSettings.errorMessage ||
952
+ error.message ||
953
+ 'Failed to send message. Please try again later.';
906
954
  console.log(`Showing error message: ${errorMessage}`);
907
955
  this.showErrorMessage(errorMessage);
956
+ } finally {
908
957
  document.getElementById("formiqueSpinner").style.display = "none";
958
+ }
959
+ }
909
960
 
961
+
962
+ // Add this method to your Formique class
963
+ processDynamicMessage(message, formData) {
964
+ let processedMessage = message;
965
+ // Iterate over each key-value pair in the form data
966
+ for (const key in formData) {
967
+ if (Object.prototype.hasOwnProperty.call(formData, key)) {
968
+ const placeholder = `{${key}}`;
969
+ // Replace all occurrences of the placeholder with the corresponding form data value
970
+ processedMessage = processedMessage.split(placeholder).join(formData[key]);
971
+ }
910
972
  }
973
+ return processedMessage;
911
974
  }
912
975
 
913
976
 
914
977
 
978
+
979
+
915
980
  // Email validation helper
916
981
  validateEmail(email) {
917
982
  const isValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
@@ -4129,3 +4194,12 @@ const spinner = `<div id="formiqueSpinner" style="display: flex; align-items: ce
4129
4194
  export default Formique;
4130
4195
 
4131
4196
 
4197
+
4198
+
4199
+
4200
+
4201
+
4202
+
4203
+
4204
+
4205
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formique/semantq",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Formique is a native form builder for the Semantq JS Framework",
5
5
  "main": "formique-semantq.js",
6
6
  "type": "module",