bongzilla

Untitled

May 15th, 2021 (edited)
282
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. const COMMENTS_ENDPOINT = "http://alcovegan.myjino.ru/webflow/index.php";
  3.  
  4. const COMMENT_FORM = $("#wf-form-comment-form");
  5. const COMMENT_NAME_FIELD = $("#name");
  6. const COMMENT_EMAIL_FIELD = $("#email");
  7. // TODO: rename id
  8. const COMMENT_CONTENT_FIELD = $("#field");
  9.  
  10. const COMMENT_SUBMIT_BUTTON = $("#wf-form-comment-form input[type='submit']");
  11. const SENDING_TEXT = COMMENT_SUBMIT_BUTTON.data("wait");
  12. const SUCCESS_SENDING = $(".w-form-done");
  13. const FAILED_SENDING = $(".w-form-fail");
  14.  
  15. const getFormContent = () => {
  16.     const postSlug = location.pathname.replace("/post/", "");
  17.     const commentName = COMMENT_NAME_FIELD.val();
  18.     const commentEmail = COMMENT_EMAIL_FIELD.val();
  19.     const commentContent = COMMENT_CONTENT_FIELD.val();
  20.  
  21.     return {
  22.         "slug": postSlug,
  23.         "name": commentName,
  24.         "email": commentEmail,
  25.         "text": commentContent
  26.     }
  27. }
  28.  
  29. const generateSlug = (name) => {
  30.     return name.split("").map(letter => letter.toLowerCase()).map(letter => letter.replace(" ", "-")).join("")
  31. }
  32.  
  33. const generateRequestData = (formData) => {
  34.     return {
  35.         "slug": formData.slug,
  36.         "fields": {
  37.             "name": formData.name,
  38.             "slug": generateSlug(formData.name),
  39.             "slug-post": "",
  40.             "comment-texts": formData.text,
  41.             "mail": formData.email,
  42.             "date-comment": new Date(),
  43.             "_archived": false,
  44.             "_draft": false
  45.         }
  46.     }
  47. }
  48.  
  49. COMMENT_SUBMIT_BUTTON.on("click", function(e) {
  50.  
  51.     e.preventDefault();
  52.     COMMENT_SUBMIT_BUTTON.val(SENDING_TEXT);
  53.  
  54.     const formData = getFormContent();
  55.     const sendData = generateRequestData(formData);
  56.     console.log("sendData", JSON.stringify(sendData));
  57.  
  58.     fetch(COMMENTS_ENDPOINT, {
  59.         "method": "POST",
  60.         "body": JSON.stringify(sendData),
  61.         "headers": {
  62.             "Content-Type": "application/json"
  63.         }
  64.     })
  65.     .then(response => response.json())
  66.     .then(result => {
  67.         console.log("result is", result);
  68.  
  69.         COMMENT_FORM.hide();
  70.         SUCCESS_SENDING.show();
  71.     })
  72.     .catch(err => {
  73.         console.log("error", err);
  74.         FAILED_SENDING.show();
  75.     })
  76. });
  77. </script>
RAW Paste Data