Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Set Custom Value" on Avalara Usage Type #2107

Merged
merged 12 commits into from
Apr 14, 2017
2 changes: 2 additions & 0 deletions client/modules/accounts/templates/members/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const getPermissionMap = (permissions) => {
*/
Template.member.events({
"click [data-event-action=showMemberSettings]": function () {
$(".customerUsageType input").val(""); // form reset
$(".customerUsageType").addClass("hide"); // form reset
Reaction.setActionViewDetail({
label: "Permissions",
i18nKeyLabel: "admin.settings.permissionsSettingsLabel",
Expand Down
48 changes: 34 additions & 14 deletions imports/plugins/included/taxes-avalara/client/accounts/exemption.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import { Packages, Accounts } from "/lib/collections";
import { Accounts as AccountsSchema } from "/lib/collections/schemas/accounts";
import { TaxEntityCodes } from "/client/collections";

let entityCodeList = [];
let currentAccount;

Template.taxSettingsPanel.helpers({
account() {
const sub = Meteor.subscribe("UserAccount", this.member.userId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticing this now, but should thus subscription be in the helper? Seems like it should be in an onCreated event?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I believe you mentioned this before.

The reason the subscription was done here is because when the subscription is done outside of the account() helper, Autoform doesn't pick the new member data change that happens when the "Manage" button is clicked for another member.

Hence the form data remains at the very first member info that was clicked when the taxsetting form was created.

But I'll confirm this again and update.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that was why we created that separate Publication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We created a separate publication so we can have a publication that takes in a userId and just returns the account with that id. Which is different from the way the existing Accounts publication works.

Copy link
Contributor Author

@impactmass impactmass Apr 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update:
I was wrong. The subscription can be moved into onCreated and it worked as expected (like you said).

Template.taxSettingsPanel.onCreated(function () {
  this.subscribe("UserAccount", Meteor.userId());
});

Template.taxSettingsPanel.helpers({
  account() {
    if (Template.instance().subscriptionsReady()) {
      const account = Accounts.findOne({ _id: this.member.userId });
      currentAccount = account;
      return account;
    }

    return null;
  },
........

I'll modify it.

if (sub.ready()) {
return Accounts.findOne({ _id: this.member.userId });
const account = Accounts.findOne({ _id: this.member.userId });
currentAccount = account;
return account;
}
return null;
},
Expand All @@ -32,18 +37,25 @@ Template.taxSettingsPanel.helpers({
value: entityCode.code
});
});

entityCodeList = (entityCodes || []).map((a) => a.code);
return (entityCodes || []).concat(customOption);
}
});

Template.taxSettingsPanel.events({
"change [data-event-action=customType]": function (event) {
event.stopPropagation();
const formId = $(event.currentTarget.closest("form")).attr("id");

if (isCustomValue()) {
if (isCustomValue(formId)) {
// show input field for custom; pre-fill with existing custom val
const currType = _.get(currentAccount, "taxSettings.customerUsageType", "");
if (entityCodeList.indexOf(currType) < 0) {
$(".customerUsageType input").val(currType);
}
return $(".customerUsageType").toggleClass("hide");
}

$(".customerUsageType").addClass("hide");
}
});
Expand All @@ -68,22 +80,30 @@ Template.taxSettingsPanel.onCreated(function () {
}
});

AutoForm.hooks({
"tax-settings-form": {
before: {
update: function (doc) {
if (isCustomValue()) {
const value = $(".customerUsageType input").val();
doc.$set["taxSettings.customerUsageType"] = value;
}
return doc;
AutoForm.addHooks(null, {
before: {
update: function (doc) {
const oldType = _.get(Template.instance(), "data.doc.taxSettings.customerUsageType");
if (isCustomValue()) {
const value = $(".customerUsageType input").val();
doc.$set["taxSettings.customerUsageType"] = value;
}
if (oldType && entityCodeList.indexOf(oldType) < 0) {
delete doc.$unset; // there's existing custom value.... this prevent autoform override
}

return doc;
}
}
});

function isCustomValue() {
const formData = AutoForm.getFormValues("tax-settings-form");
/**
* @summary Checks if customerUsageType is set to "custom"
* @param {String} formId - Id of the Autoform instance..
* @returns {boolean} - true if Custom Entity Type is set
*/
function isCustomValue(formId) {
const formData = AutoForm.getFormValues(formId);
const value = _.get(formData, "insertDoc.taxSettings.customerUsageType");
return value === "CUSTOM USER INPUT";
}