react-use-mailchimp-signup
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -8,3 +8,3 @@ 'use strict'; | ||
var react = require('react'); | ||
var fetch = _interopDefault(require('isomorphic-fetch')); | ||
var fetch = _interopDefault(require('jsonp-promise')); | ||
@@ -819,3 +819,3 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
var regex = /^([\w_\.\-\+])+\@([\w\-]+\.)+([\w]{2,10})+$/; | ||
var regex = /^([\w_.\-+])+@([\w-]+\.)+([\w]{2,10})+$/; | ||
var useMailChimp = function useMailChimp(_ref) { | ||
@@ -825,3 +825,3 @@ var action = _ref.action, | ||
var _useState = react.useState(undefined), | ||
var _useState = react.useState(false), | ||
error = _useState[0], | ||
@@ -838,2 +838,6 @@ setError = _useState[1]; | ||
var _useState4 = react.useState(undefined), | ||
message = _useState4[0], | ||
setMessage = _useState4[1]; | ||
var handleError = function handleError(error) { | ||
@@ -846,4 +850,5 @@ setError(error); | ||
var handleReset = function handleReset() { | ||
setError(undefined); | ||
setStatus('Sending'); | ||
setError(false); | ||
setMessage(undefined); | ||
setStatus('sending'); | ||
setLoading(true); | ||
@@ -855,3 +860,3 @@ }; | ||
setLoading(false); | ||
setError(undefined); | ||
setError(false); | ||
}; | ||
@@ -861,3 +866,3 @@ | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(passedFields) { | ||
var fields, EMAIL, values, path, url, response, data; | ||
var fields, EMAIL, values, path, url, data; | ||
return runtime_1.wrap(function _callee$(_context) { | ||
@@ -878,10 +883,11 @@ while (1) { | ||
if (!(!EMAIL || !regex.test("" + EMAIL))) { | ||
_context.next = 7; | ||
_context.next = 8; | ||
break; | ||
} | ||
handleError('invalid email'); | ||
handleError(true); | ||
setMessage('Invalid email'); | ||
return _context.abrupt("return"); | ||
case 7: | ||
case 8: | ||
// if used lowercase befeore then we should restore it... | ||
@@ -902,41 +908,33 @@ if (fields.email && fields.EMAIL) { | ||
url = path.replace('/post?', '/post-json?'); // Reset everything for try | ||
url = path.replace('/post?', '/post-json?'); // Reset everything for try | ||
handleReset(); | ||
_context.next = 14; | ||
_context.next = 15; | ||
return fetch(url, { | ||
method: 'POST' | ||
}); | ||
param: 'c' | ||
}).promise; | ||
case 14: | ||
response = _context.sent; | ||
case 15: | ||
data = _context.sent; | ||
// Always set a the message that comes back from MC | ||
setMessage(data.msg); // Is the user already in the MC list | ||
if (!(response.status >= 400)) { | ||
_context.next = 18; | ||
break; | ||
} | ||
handleError('Failed to subscribe'); | ||
return _context.abrupt("return"); | ||
case 18: | ||
data = response.json(); | ||
if (data.msg.includes("already subscribed")) { | ||
handleStatus('duplicate'); | ||
if (data.msg.includes('already subscribed')) { | ||
handleStatus('duplicate'); // Check we are not getting any other errors back | ||
} else if (data.result !== 'success') { | ||
handleError('Failed to subscribe'); | ||
handleError(true); | ||
} else { | ||
handleStatus('success'); | ||
} | ||
handleStatus(response.json()); | ||
_context.next = 27; | ||
_context.next = 24; | ||
break; | ||
case 24: | ||
_context.prev = 24; | ||
case 20: | ||
_context.prev = 20; | ||
_context.t0 = _context["catch"](2); | ||
handleError('Failed to subscribe'); | ||
handleError(true); | ||
setMessage(_context.t0); | ||
case 27: | ||
case 24: | ||
case "end": | ||
@@ -946,3 +944,3 @@ return _context.stop(); | ||
} | ||
}, _callee, null, [[2, 24]]); | ||
}, _callee, null, [[2, 20]]); | ||
})); | ||
@@ -959,2 +957,3 @@ | ||
status: status, | ||
message: message, | ||
subscribe: subscribe | ||
@@ -961,0 +960,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("react"),r=(t=require("isomorphic-fetch"))&&"object"==typeof t&&"default"in t?t.default:t;function n(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function o(){return(o=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}var i,a=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function c(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function u(t,e,r,n){var o=Object.create((e&&e.prototype instanceof f?e:f).prototype),i=new E(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var c=b(a,r);if(c){if(c===l)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=s(t,e,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(t,r,i),o}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var l={};function f(){}function h(){}function p(){}var v={};v[o]=function(){return this};var d=Object.getPrototypeOf,y=d&&d(d(j([])));y&&y!==e&&r.call(y,o)&&(v=y);var g=p.prototype=f.prototype=Object.create(v);function m(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,c){var u=s(t[o],t,i);if("throw"!==u.type){var l=u.arg,f=l.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):e.resolve(f).then((function(t){l.value=t,a(l)}),(function(t){return n("throw",t,a,c)}))}c(u.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function b(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,b(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=s(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,l;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function x(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function L(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function E(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(x,this),this.reset(!0)}function j(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return i.next=i}}return{next:O}}function O(){return{value:void 0,done:!0}}return h.prototype=g.constructor=p,p.constructor=h,h.displayName=c(p,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,c(t,a,"GeneratorFunction")),t.prototype=Object.create(g),t},t.awrap=function(t){return{__await:t}},m(w.prototype),w.prototype[i]=function(){return this},t.AsyncIterator=w,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new w(u(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},m(g),c(g,a,"Generator"),g[o]=function(){return this},g.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=j,E.prototype={constructor:E,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(L),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var c=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),l},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),L(r),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;L(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:j(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}(i={exports:{}}),i.exports),c=/^([\w_\.\-\+])+\@([\w\-]+\.)+([\w]{2,10})+$/;exports.useMailChimp=function(t){var i=t.action,u=t.options,s=e.useState(void 0),l=s[0],f=s[1],h=e.useState(!1),p=h[0],v=h[1],d=e.useState(void 0),y=d[1],g=function(t){f(t),y(void 0),v(!1)},m=function(t){y(t),v(!1),f(void 0)};return{error:l,loading:p,status:d[0],subscribe:function(){var t,e=(t=a.mark((function t(e){var n,s,l,h,p,d;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if((n=o({},e)).email&&(n.EMAIL=n.email),t.prev=2,(s=n.EMAIL)&&c.test(""+s)){t.next=7;break}return g("invalid email"),t.abrupt("return");case 7:return n.email&&n.EMAIL&&delete n.EMAIL,l=Object.entries(n).map((function(t){var e=t[0],r=t[1];return(null!=u&&u.dontUpperCaseKeys?e:e.toUpperCase())+"="+encodeURIComponent(r||"")})).join("&"),h=(i+"&"+l).replace("/post?","/post-json?"),f(void 0),y("Sending"),v(!0),t.next=14,r(h,{method:"POST"});case 14:if(!((p=t.sent).status>=400)){t.next=18;break}return g("Failed to subscribe"),t.abrupt("return");case 18:(d=p.json()).msg.includes("already subscribed")?m("duplicate"):"success"!==d.result?g("Failed to subscribe"):m("success"),m(p.json()),t.next=27;break;case 24:t.prev=24,t.t0=t.catch(2),g("Failed to subscribe");case 27:case"end":return t.stop()}}),t,null,[[2,24]])})),function(){var e=this,r=arguments;return new Promise((function(o,i){var a=t.apply(e,r);function c(t){n(a,o,i,c,u,"next",t)}function u(t){n(a,o,i,c,u,"throw",t)}c(void 0)}))});return function(t){return e.apply(this,arguments)}}()}}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("react"),r=(t=require("jsonp-promise"))&&"object"==typeof t&&"default"in t?t.default:t;function n(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function o(){return(o=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}var i,a=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function c(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function u(t,e,r,n){var o=Object.create((e&&e.prototype instanceof f?e:f).prototype),i=new E(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var c=x(a,r);if(c){if(c===l)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=s(t,e,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(t,r,i),o}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var l={};function f(){}function h(){}function p(){}var v={};v[o]=function(){return this};var d=Object.getPrototypeOf,y=d&&d(d(j([])));y&&y!==e&&r.call(y,o)&&(v=y);var g=p.prototype=f.prototype=Object.create(v);function m(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,c){var u=s(t[o],t,i);if("throw"!==u.type){var l=u.arg,f=l.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):e.resolve(f).then((function(t){l.value=t,a(l)}),(function(t){return n("throw",t,a,c)}))}c(u.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function x(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=s(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,l;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function b(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function L(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function E(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function j(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return i.next=i}}return{next:_}}function _(){return{value:void 0,done:!0}}return h.prototype=g.constructor=p,p.constructor=h,h.displayName=c(p,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,c(t,a,"GeneratorFunction")),t.prototype=Object.create(g),t},t.awrap=function(t){return{__await:t}},m(w.prototype),w.prototype[i]=function(){return this},t.AsyncIterator=w,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new w(u(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},m(g),c(g,a,"Generator"),g[o]=function(){return this},g.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=j,E.prototype={constructor:E,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(L),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var c=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),l},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),L(r),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;L(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:j(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}(i={exports:{}}),i.exports),c=/^([\w_.\-+])+@([\w-]+\.)+([\w]{2,10})+$/;exports.useMailChimp=function(t){var i=t.action,u=t.options,s=e.useState(!1),l=s[0],f=s[1],h=e.useState(!1),p=h[0],v=h[1],d=e.useState(void 0),y=d[0],g=d[1],m=e.useState(void 0),w=m[1],x=function(t){f(t),g(void 0),v(!1)},b=function(t){g(t),v(!1),f(!1)};return{error:l,loading:p,status:y,message:m[0],subscribe:function(){var t,e=(t=a.mark((function t(e){var n,s,l,h,p;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if((n=o({},e)).email&&(n.EMAIL=n.email),t.prev=2,(s=n.EMAIL)&&c.test(""+s)){t.next=8;break}return x(!0),w("Invalid email"),t.abrupt("return");case 8:return n.email&&n.EMAIL&&delete n.EMAIL,l=Object.entries(n).map((function(t){var e=t[0],r=t[1];return(null!=u&&u.dontUpperCaseKeys?e:e.toUpperCase())+"="+encodeURIComponent(r||"")})).join("&"),h=(i+"&"+l).replace("/post?","/post-json?"),f(!1),w(void 0),g("sending"),v(!0),t.next=15,r(h,{param:"c"}).promise;case 15:w((p=t.sent).msg),p.msg.includes("already subscribed")?b("duplicate"):"success"!==p.result?x(!0):b("success"),t.next=24;break;case 20:t.prev=20,t.t0=t.catch(2),x(!0),w(t.t0);case 24:case"end":return t.stop()}}),t,null,[[2,20]])})),function(){var e=this,r=arguments;return new Promise((function(o,i){var a=t.apply(e,r);function c(t){n(a,o,i,c,u,"next",t)}function u(t){n(a,o,i,c,u,"throw",t)}c(void 0)}))});return function(t){return e.apply(this,arguments)}}()}}; | ||
//# sourceMappingURL=react-use-mailchimp-signup.cjs.production.min.js.map |
import { useState } from 'react'; | ||
import fetch from 'isomorphic-fetch'; | ||
import fetch from 'jsonp-promise'; | ||
@@ -812,3 +812,3 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
var regex = /^([\w_\.\-\+])+\@([\w\-]+\.)+([\w]{2,10})+$/; | ||
var regex = /^([\w_.\-+])+@([\w-]+\.)+([\w]{2,10})+$/; | ||
var useMailChimp = function useMailChimp(_ref) { | ||
@@ -818,3 +818,3 @@ var action = _ref.action, | ||
var _useState = useState(undefined), | ||
var _useState = useState(false), | ||
error = _useState[0], | ||
@@ -831,2 +831,6 @@ setError = _useState[1]; | ||
var _useState4 = useState(undefined), | ||
message = _useState4[0], | ||
setMessage = _useState4[1]; | ||
var handleError = function handleError(error) { | ||
@@ -839,4 +843,5 @@ setError(error); | ||
var handleReset = function handleReset() { | ||
setError(undefined); | ||
setStatus('Sending'); | ||
setError(false); | ||
setMessage(undefined); | ||
setStatus('sending'); | ||
setLoading(true); | ||
@@ -848,3 +853,3 @@ }; | ||
setLoading(false); | ||
setError(undefined); | ||
setError(false); | ||
}; | ||
@@ -854,3 +859,3 @@ | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(passedFields) { | ||
var fields, EMAIL, values, path, url, response, data; | ||
var fields, EMAIL, values, path, url, data; | ||
return runtime_1.wrap(function _callee$(_context) { | ||
@@ -871,10 +876,11 @@ while (1) { | ||
if (!(!EMAIL || !regex.test("" + EMAIL))) { | ||
_context.next = 7; | ||
_context.next = 8; | ||
break; | ||
} | ||
handleError('invalid email'); | ||
handleError(true); | ||
setMessage('Invalid email'); | ||
return _context.abrupt("return"); | ||
case 7: | ||
case 8: | ||
// if used lowercase befeore then we should restore it... | ||
@@ -895,41 +901,33 @@ if (fields.email && fields.EMAIL) { | ||
url = path.replace('/post?', '/post-json?'); // Reset everything for try | ||
url = path.replace('/post?', '/post-json?'); // Reset everything for try | ||
handleReset(); | ||
_context.next = 14; | ||
_context.next = 15; | ||
return fetch(url, { | ||
method: 'POST' | ||
}); | ||
param: 'c' | ||
}).promise; | ||
case 14: | ||
response = _context.sent; | ||
case 15: | ||
data = _context.sent; | ||
// Always set a the message that comes back from MC | ||
setMessage(data.msg); // Is the user already in the MC list | ||
if (!(response.status >= 400)) { | ||
_context.next = 18; | ||
break; | ||
} | ||
handleError('Failed to subscribe'); | ||
return _context.abrupt("return"); | ||
case 18: | ||
data = response.json(); | ||
if (data.msg.includes("already subscribed")) { | ||
handleStatus('duplicate'); | ||
if (data.msg.includes('already subscribed')) { | ||
handleStatus('duplicate'); // Check we are not getting any other errors back | ||
} else if (data.result !== 'success') { | ||
handleError('Failed to subscribe'); | ||
handleError(true); | ||
} else { | ||
handleStatus('success'); | ||
} | ||
handleStatus(response.json()); | ||
_context.next = 27; | ||
_context.next = 24; | ||
break; | ||
case 24: | ||
_context.prev = 24; | ||
case 20: | ||
_context.prev = 20; | ||
_context.t0 = _context["catch"](2); | ||
handleError('Failed to subscribe'); | ||
handleError(true); | ||
setMessage(_context.t0); | ||
case 27: | ||
case 24: | ||
case "end": | ||
@@ -939,3 +937,3 @@ return _context.stop(); | ||
} | ||
}, _callee, null, [[2, 24]]); | ||
}, _callee, null, [[2, 20]]); | ||
})); | ||
@@ -952,2 +950,3 @@ | ||
status: status, | ||
message: message, | ||
subscribe: subscribe | ||
@@ -954,0 +953,0 @@ }; |
@@ -1,17 +0,19 @@ | ||
interface UseMailChimpOptionsProps { | ||
export interface UseMailChimpOptionsProps { | ||
dontUpperCaseKeys?: boolean; | ||
} | ||
interface UseMailChimpProps { | ||
export interface UseMailChimpProps { | ||
action: string; | ||
options: UseMailChimpOptionsProps; | ||
options?: UseMailChimpOptionsProps; | ||
} | ||
export interface MailchimpFieldProps { | ||
export interface UseMailchimpFieldProps { | ||
[x: string]: string | number; | ||
} | ||
export declare const useMailChimp: ({ action, options }: UseMailChimpProps) => { | ||
error: string | undefined; | ||
export interface UseMailChimpResponse { | ||
error: boolean; | ||
loading: boolean; | ||
status: string | undefined; | ||
subscribe: (passedFields: Partial<MailchimpFieldProps>) => Promise<void>; | ||
}; | ||
export {}; | ||
status: UseMailChimpStatus; | ||
message: string | undefined; | ||
subscribe: (passedFields: Partial<UseMailchimpFieldProps>) => Promise<void>; | ||
} | ||
export declare type UseMailChimpStatus = 'sending' | 'duplicate' | 'success' | 'failed' | undefined; | ||
export declare const useMailChimp: ({ action, options }: UseMailChimpProps) => UseMailChimpResponse; |
{ | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
@@ -22,2 +22,3 @@ "main": "dist/index.js", | ||
"lint": "tsdx lint", | ||
"fix": "tsdx lint --fix", | ||
"prepare": "tsdx build", | ||
@@ -28,3 +29,3 @@ "size": "size-limit", | ||
"peerDependencies": { | ||
"isomorphic-fetch": ">=3", | ||
"jsonp-promise": ">=0.1.2", | ||
"react": ">=16" | ||
@@ -38,3 +39,3 @@ }, | ||
"prettier": { | ||
"printWidth": 80, | ||
"printWidth": 120, | ||
"semi": true, | ||
@@ -59,14 +60,13 @@ "singleQuote": true, | ||
"@size-limit/preset-small-lib": "^4.9.0", | ||
"@types/isomorphic-fetch": "^0.0.35", | ||
"@types/react": "^17.0.0", | ||
"@types/react-dom": "^17.0.0", | ||
"husky": "^4.3.0", | ||
"isomorphic-fetch": "^3.0.0", | ||
"jsonp-promise": "^0.1.2", | ||
"react": "^17.0.1", | ||
"react-dom": "^17.0.1", | ||
"size-limit": "^4.9.0", | ||
"size-limit": "^4.10.2", | ||
"tsdx": "^0.14.1", | ||
"tslib": "^2.0.3", | ||
"typescript": "^4.1.2" | ||
"tslib": "^2.2.0", | ||
"typescript": "^4.2.4" | ||
} | ||
} |
# Use React MailChimp | ||
It provides an easy-to use React Hook to subscribe users to your MailChimp lists | ||
It provides an easy-to use React Hook to subscribe users to your MailChimp lists. | ||
## Find this useful? | ||
<a href="https://www.buymeacoffee.com/hafowuvo" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 40px !important;width: auto !important;" ></a> | ||
# Install | ||
@@ -15,2 +19,7 @@ | ||
## Version 2 | ||
Note version 2 fixes possible CORs issues and includes a breaking change to return `error` as a Boolean. | ||
You can now get more information about the error (and success) from the new `message` property | ||
# Usage | ||
@@ -24,3 +33,3 @@ | ||
export const MyComponent = () => { | ||
const { error, loading, status, subscribe } = useMailChimp({ | ||
const { error, loading, status, subscribe, message } = useMailChimp({ | ||
action: `https://<YOUR-USER>.us18.list-manage.com/subscribe/post?u=XXXXXX&id=XXXXXX`, | ||
@@ -50,14 +59,10 @@ }); | ||
<> | ||
{error && <p>{error}</p>} | ||
{error && <p>ERROR</p>} | ||
{loading && <p>...Loading</p>} | ||
{status && <p>{status}</p>} | ||
{message && <p>{message}</p>} | ||
<form onSubmit={handleSubmit}> | ||
<label htmlFor="mchimpEmail">Email</label> | ||
<input | ||
type="email" | ||
name="email" | ||
id="mchimpEmail" | ||
onChange={handleInputChange} | ||
/> | ||
<input type="email" name="email" id="mchimpEmail" onChange={handleInputChange} /> | ||
<button type="submit">Sign me up!</button> | ||
@@ -69,1 +74,11 @@ </form> | ||
``` | ||
## Response from `subscribe` method | ||
``` | ||
error: boolean; | ||
loading: boolean; | ||
status: UseMailChimpStatus; | ||
message: string | undefined; | ||
subscribe: (passedFields: Partial<UseMailchimpFieldProps>) => Promise<void>; | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
220305
11
15
1753
81