Code Review Friday: Let's Improve your code, ask Corvid master

May 1st, 2020
Join this week code review organized by Corvid master,
Comment your code below(as per instruction) we will analyze provide you suggestion or refactored code that will be easy to read, to modify, and plenty of good practice.

We also welcome experience corvid forum members to share the knowledge and expertise.

Instructions:

*1: FULL PAGE CODE is recommended
*2: Include database structure like Collection name & Field type (if you refer them in the code)
3: Is this a live site? If so, drop a link so, we can check the code.
4: Videos or Images for explanation of what the code does is useful!
Marked * are required

Don’ts

1: Do NOT include any sensitive code (like passwords, secrect key).

// try to rewrite it as xxx if it's used inside the code
const key = "XXX-XXX";

2: Asking any questions unrelated to code review or corvid
3: Screenshot the code and not pasting the code in text format
4: Code that was not working before and asking to fix (Please create a new post in the community, This Post is dedicated to improve your current working code)

Notes

  • We do code review every friday PST timezone!

  • This post will be locked after 24 hours

1 Like

@admin51915 It is fortunate that you are doing code review because I posted this post a few days ago and your code is part of the examples I used. Can you look at this? https://www.wix.com/corvid/forum/community-discussion/send-grid-and-data-hooks-variables-not-working

I am also struggling with sendgrid v3 so it would be nice to see this reviewed. ( had it working in V2 but I decided to use a dynamic template and V3 Sendgrid. 1) I have a bug somewhere - how to better instrument my code to get the most out of debugging would be nice as sendgrid doesn’t work in preview mode and getting site events to be informative would be helpful ( Try/Catch/logging).

Extra points for 2) appropriate method for ensuring “primary keys” are unique in collection and 3) Should I be leveraging BeforeInsert hook to enforce uniqueness for primary? field and cross field validation ( belt and suspenders both at BeforeInsert and on the page) ? Appropriate method messaging back to user on page?

I’ll be working through this over the weekend.

Page Code


$w.onReady(function () {
 
});

export function PartnershipTypeinpt_change(event) {
 
 let isChecked = false;
    $w('#PartnershipTypeinpt').value.forEach((ChkboxValue) => {
 if (ChkboxValue === "OEM"){
            isChecked = true;
        }
    })
 if (isChecked === true){
        $w('#OEMSddwn').show();
    } else {
        $w('#OEMSddwn').hide();
    }
}



SendGrid Dynamic Template

Collection Partner Request Schema

data.js

// data.js

import {sendEmail} from 'backend/email'; 
import wixData from 'wix-data';

function emailPartnerRequest(item) {
 const subject = `WIX Partner Request - ${item.firstName} ${item.lastName}  ${item.companyName}`;
 const recipient = item.email
 const body = `This is a test`
 
    sendEmail(subject, recipient, body)
      .then(response => console.log(response)); 
}

export function PartnerRequest_afterInsert(item, context) { 
    emailPartnerRequest(item);
}



sendemail.jsw

// sendemail.jsw 
// SendGrid V3
import {sendInstruction} from 'backend/sendgrid';

export function sendEmail(subject, body, recipient) {
 const key = "obfuscate.mQVO5w7gv0tm7snJBp7oMHdjibbeishSTw";
 const sender = "somvalidemail@here.ca";

 //return sendInstruction(key, sender, recipient, subject, body);     
}

sendgrid.js

//sendgrid.js
// SendGrid V3
import { fetch } from 'wix-fetch';

export function sendInstruction(APIKey, Sender, RecipientEmail, Subject, body) {
 const url = "https://api.sendgrid.com/v3/mail/send";

 const MyHeaders = {
 "Authorization": "Bearer " + APIKey,
 "Content-Type": "application/json"
    };
 const MyBody = {
 "personalizations": [{
 "to": [{
 "email": RecipientEmail
            }]
        }],
 "from": {
 "email": Sender
        },
 "subject": Subject,
 "content": [{
 "type": "text",
 "value": "this is a test"
        }],
 "template_id": "d-obfuscate5a4a6e2bff69493f2"
    };


 return fetch(url, {
 "method": "POST",
 "headers": MyHeaders,
 "body": JSON.stringify(MyBody)
        })
        .then(Response => console.log(Response.text));
}

Hey I am gonna lock this post feel free to create a new post and tag me on that and regarding @elizabethjhay I have answered on her post.