UNPKG

@green-id/meexr-badge

Version:

Ce package permet d'intégrer un badge de notation environnementale dans vos projets React.

2 lines (1 loc) 3.82 kB
import e,{useState as t,useEffect as n}from"react";function r(e,t,n,r){return new(n||(n=Promise))((function(o,i){function a(e){try{c(r.next(e))}catch(e){i(e)}}function l(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,l)}c((r=r.apply(e,t||[])).next())}))}function o(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function l(l){return function(c){return function(l){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,l[0]&&(a=0)),a;)try{if(n=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=t.call(e,a)}catch(e){l=[6,e],r=0}finally{n=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,c])}}}"function"==typeof SuppressedError&&SuppressedError;var i=function(e){return e<25?"#E5726B":e<50?"#F6A673":e<75?"#A19FE2":"#68A17E"},a=function(a){var l,c=a.siteUrl,s=void 0===c?window.location.href:c,u=t(null),f=u[0],p=u[1],d=t(!0),m=d[0],g=d[1];return n((function(){r(void 0,void 0,void 0,(function(){var e,t,n;return o(this,(function(r){switch(r.label){case 0:g(!0),r.label=1;case 1:return r.trys.push([1,4,5,6]),e="https://api.meexr.fr/audits/badge-audit?brand=societech&nbVisits=1&website=".concat(encodeURIComponent(s)),[4,fetch(e)];case 2:return[4,r.sent().json()];case 3:return t=r.sent(),p(t),[3,6];case 4:return n=r.sent(),console.error("Request failed:",n),[3,6];case 5:return g(!1),[7];case 6:return[2]}}))}))}),[s]),m?e.createElement("div",{style:{backgroundColor:"#F4F4F4",width:"fit-content",border:"2px solid black",padding:"1.5rem"}},"Loading..."):f?e.createElement("div",{style:{backgroundColor:"#F4F4F4",width:"fit-content",border:"2px solid black"}},e.createElement("div",{style:{display:"flex",flexDirection:"column",padding:"1.5rem",alignItems:"center"}},e.createElement("h4",{style:{fontSize:"14px",margin:0,marginBottom:"10px",fontWeight:"normal"}},"Cette page émet"),e.createElement("h4",{style:{fontSize:"18px",fontWeight:"bold",margin:0}},f.co2.toFixed(2)," g de CO2e par visite")),e.createElement("div",{style:{display:"flex",flexDirection:"column",padding:"1.5rem",borderTop:"2px solid black",alignItems:"center"}},e.createElement("h4",{style:{fontSize:"0.875rem",margin:0,fontWeight:"normal"}},"Le score Meexr du site est"),e.createElement("div",{style:{display:"flex",flexDirection:"row",marginTop:"8px"}},e.createElement("img",{src:(l=f.score,l<25?"/assets/score_d.svg":l<50?"/assets/score_c.svg":l<75?"/assets/score_b.svg":"/assets/score_a.svg"),alt:"",style:{margin:0,height:"30px",width:"30px"}}),e.createElement("h2",{style:{fontSize:"30px",margin:0,marginTop:"auto",marginRight:"8px",fontWeight:"bold",paddingLeft:"8px",color:i(f.score)}},function(e){return e<25?"D":e<50?"C":e<75?"B":"A"}(f.score)),e.createElement("h1",{style:{fontWeight:"bold",margin:0,marginTop:"auto",fontSize:"22px",marginBottom:"2px",color:i(f.score)}},f.score),e.createElement("h1",{style:{fontSize:"13px",margin:0,marginTop:"auto",fontWeight:"normal",marginBottom:"6px",color:i(f.score)}},"/100pts")))):e.createElement("div",null,"No data available")};export{a as MeexrBadge};