
/* /mass_mailing/static/src/interactions/subscribe.js */
odoo.define('@mass_mailing/interactions/subscribe',['@web/public/interaction','@web/core/registry','@web/core/network/rpc','@web/core/utils/render','@web/core/l10n/translation'],function(require){'use strict';let __exports={};const{Interaction}=require("@web/public/interaction");const{registry}=require("@web/core/registry");const{rpc}=require("@web/core/network/rpc");const{renderToMarkup,renderToFragment}=require("@web/core/utils/render");const{appTranslateFn}=require("@web/core/l10n/translation");const _t=(str,...args)=>appTranslateFn(str,"mass_mailing",...args);const Subscribe=__exports.Subscribe=class Subscribe extends Interaction{static selector="#o_mailing_portal_subscription";dynamicContent={"#o_mailing_subscription_form #o_mailing_subscription_form_manage":{"t-att-class":()=>({"d-none":this.customerData.isBlocklisted,}),},"#o_mailing_subscription_form #o_mailing_subscription_form_manage input":{"t-att-disabled":()=>this.customerData.isBlocklisted,},"#o_mailing_subscription_form #button_form_send":{"t-on-click.prevent":this.onFormSend,"t-att-class":()=>({"d-none":this.customerData.isBlocklisted,}),"t-att-disabled":()=>this.customerData.isBlocklisted,},"#o_mailing_subscription_form #o_mailing_subscription_update_info":{"t-att-class":()=>({"d-none":this.changeSubscriptionStatus==="hidden","text-success":!this.changeSubscriptionStatus.endsWith("error"),"text-danger":this.changeSubscriptionStatus.endsWith("error"),}),"t-out":()=>this.changeSubscriptionResultMessage,},"#o_mailing_subscription_form #button_blocklist_add":{"t-on-click.prevent":this.onBlocklistAddClick,"t-att-class":()=>({"d-none":!this.customerData.blocklistEnabled||!this.customerData.blocklistPossible||this.customerData.isBlocklisted,}),},"#o_mailing_subscription_form #button_blocklist_remove":{"t-on-click.prevent":this.onBlocklistRemoveClick,"t-att-class":()=>({"d-none":!this.customerData.isBlocklisted,}),},"#o_mailing_subscription_form #o_mailing_subscription_form_blocklisted":{"t-att-class":()=>({"d-none":!this.customerData.isBlocklisted,}),},"#o_mailing_subscription_feedback":{"t-att-class":()=>({"d-none":!this.customerData.feedbackEnabled,}),},"#o_mailing_subscription_feedback p":{"t-out":()=>this.askingFeedbackFor==="blocklist_add"?_t("Please let us know why you want to be in our block list."):_t("Please let us know why you updated your subscription."),},"#o_mailing_subscription_feedback input":{"t-att-disabled":()=>this.unsubscribeFeedbackStatus==="feedback_sent",},"#o_mailing_subscription_feedback .o_mailing_subscription_opt_out_reason":{"t-on-click":this.onOptOutReasonClick,},"#o_mailing_subscription_feedback textarea":{"t-att-class":()=>({"d-none":!this.showFeedbackTextbox,}),"t-att-disabled":()=>this.unsubscribeFeedbackStatus==="feedback_sent",},"#o_mailing_subscription_feedback #button_feedback":{"t-on-click.prevent":this.onFeedbackClick,"t-att-disabled":()=>this.unsubscribeFeedbackStatus==="feedback_sent",},"#o_mailing_subscription_feedback #o_mailing_subscription_feedback_info":{"t-att-class":()=>({"d-none":this.unsubscribeFeedbackStatus==="hidden","text-success":this.unsubscribeFeedbackStatus!=="error","text-danger":this.unsubscribeFeedbackStatus==="error",}),"t-out":()=>this.unsubscribeFeedbackResultMessage,},};setup(){this.customerData={...document.getElementById("o_mailing_portal_subscription").dataset};this.customerData.documentId=parseInt(this.customerData.documentId||0);this.customerData.mailingId=parseInt(this.customerData.mailingId||0);this.lastAction=this.customerData.lastAction;this.listInfo=this.getListInfo();this.showFeedbackTextbox=false;this.changeSubscriptionStatus="hidden";this.unsubscribeFeedbackStatus="hidden";}
async onBlocklistAddClick(){const result=await this.waitFor(rpc("/mailing/blocklist/add",{document_id:this.customerData.documentId,email:this.customerData.email,hash_token:this.customerData.hashToken,mailing_id:this.customerData.mailingId,}));this.protectSyncAfterAsync((result)=>{if(result===true){this.customerData.isBlocklisted=true;this.customerData.feedbackEnabled=true;}
this.changeSubscriptionStatus=result===true?"blocklist_add":"blocklist_error";this.onActionDone(result===true?"blocklist_add":result);})(result);}
async onBlocklistRemoveClick(){const result=await this.waitFor(rpc("/mailing/blocklist/remove",{document_id:this.customerData.documentId,email:this.customerData.email,hash_token:this.customerData.hashToken,mailing_id:this.customerData.mailingId,}));this.protectSyncAfterAsync((result)=>{if(result===true){this.customerData.isBlocklisted=false;this.customerData.feedbackEnabled=false;}
this.changeSubscriptionStatus=result===true?"blocklist_remove":"blocklist_error";this.onActionDone(result===true?"blocklist_remove":result);})(result);}
async onFormSend(){const formData=new FormData(document.querySelector("div#o_mailing_subscription_form form"));const mailingListOptinIds=formData.getAll("mailing_list_ids").map((id_str)=>parseInt(id_str));const result=await this.waitFor(rpc("/mailing/list/update",{csrf_token:formData.get("csrf_token"),document_id:this.customerData.documentId,email:this.customerData.email,hash_token:this.customerData.hashToken,lists_optin_ids:mailingListOptinIds,mailing_id:this.customerData.mailingId,}));this.protectSyncAfterAsync((result)=>{const has_error=["error","unauthorized"].includes(result);let callKey;if(has_error){callKey="error";}else{callKey=parseInt(result)>0?"subscription_updated_optout":"subscription_updated";this.updateDisplayForm(mailingListOptinIds);}
this.changeSubscriptionStatus=has_error?"subscription_error":"subscription_updated";if(callKey==="subscription_updated_optout"){this.customerData.feedbackEnabled=true;}else if(callKey==="subscription_updated"){this.customerData.feedbackEnabled=false;}
this.onActionDone(callKey);})(result);}
async onFeedbackClick(){const formData=new FormData(document.querySelector("div#o_mailing_subscription_feedback form"));const optoutReasonId=parseInt(formData.get("opt_out_reason_id"));const result=await this.waitFor(rpc("/mailing/feedback",{csrf_token:formData.get("csrf_token"),document_id:this.customerData.documentId,email:this.customerData.email,feedback:formData.get("feedback"),hash_token:this.customerData.hashToken,last_action:this.lastAction,mailing_id:this.customerData.mailingId,opt_out_reason_id:optoutReasonId,}));this.protectSyncAfterAsync((result)=>{if(result===true){this.unsubscribeFeedbackStatus="feedback_sent";}else{this.unsubscribeFeedbackStatus="error";}
this.lastAction=result===true?"feedback_sent":result;})(result);}
onOptOutReasonClick(ev){this.showFeedbackTextbox=ev.target.dataset["isFeedback"];document.querySelector("div#o_mailing_subscription_feedback textarea").value="";}
getListInfo(){return[...document.querySelectorAll("#o_mailing_subscription_form_manage input")].map((node)=>{const listInfo={id:parseInt(node.getAttribute("value")),member:node.dataset.member==="1",name:node.getAttribute("title"),opt_out:node.getAttribute("checked")!=="checked",};return listInfo;});}
onActionDone(callKey){this.lastAction=callKey;if(["blocklist_add","subscription_updated_optout"].includes(callKey)){this.askingFeedbackFor=callKey;}
this.unsubscribeFeedbackStatus="hidden";document.querySelector("div#o_mailing_subscription_feedback textarea").value="";}
updateDisplayForm(listOptinIds){this.listInfo.forEach((listItem)=>{listItem.member=listItem.member||listOptinIds.includes(listItem.id);listItem.opt_out=!listOptinIds.includes(listItem.id);});const formContent=renderToFragment("mass_mailing.portal.list_form_content",{listsMember:this.listInfo.filter((item)=>item.member===true),listsProposal:this.listInfo.filter((item)=>item.member===false),});const manageForm=document.getElementById("o_mailing_subscription_form_manage");manageForm.replaceChildren(formContent);const formReadonlyContent=renderToFragment("mass_mailing.portal.list_form_content_readonly",{listsOptin:this.listInfo.filter((item)=>item.opt_out===false),});const readonlyForm=document.getElementById("o_mailing_subscription_form_blocklisted");readonlyForm.replaceChildren(formReadonlyContent);}
get changeSubscriptionResultMessage(){if(this.changeSubscriptionStatus=="hidden"){return null;}
return renderToMarkup(this.changeSubscriptionStatus.startsWith("subscription")?"mass_mailing.portal.list_form_update_info":"mass_mailing.portal.blocklist_update_info",{infoKey:this.changeSubscriptionStatus,});}
get unsubscribeFeedbackResultMessage(){if(this.unsubscribeFeedbackStatus=="hidden"){return null;}
return renderToMarkup("mass_mailing.portal.feedback_update_info",{infoKey:this.unsubscribeFeedbackStatus,});}}
registry.category("public.interactions").add("mass_mailing.subscribe",Subscribe);return __exports;});

                    /*******************************************
                    *  Templates                               *
                    *******************************************/

                    odoo.define("mass_mailing.mailing_assets.bundle.xml", ["@web/core/templates"], function(require) {
                        "use strict";
                        const { checkPrimaryTemplateParents, registerTemplate, registerTemplateExtension } = require("@web/core/templates");
                        /* mass_mailing.mailing_assets */
                        registerTemplate("mass_mailing.portal.blocklist_update_info", `/mass_mailing/static/src/xml/mailing_portal_subscription_blocklist.xml`, `<t t-name="mass_mailing.portal.blocklist_update_info" xml:space="preserve">
        <span>
            <t t-if="infoKey == 'blocklist_add'">
                <i class="fa fa-check me-1"/>
                <span>Email added to our blocklist</span>
            </t>
            <t t-elif="infoKey == 'blocklist_remove'">
                <i class="fa fa-check me-1"/>
                <span>Email removed from our blocklist</span>
            </t>
            <t t-else="">
                <i class="fa fa-exclamation-triangle me-1"/>
                <span>An error occurred. Please retry later.</span>
            </t>
        </span>
    </t>
`);
registerTemplate("mass_mailing.portal.feedback_update_info", `/mass_mailing/static/src/xml/mailing_portal_subscription_feedback.xml`, `<t t-name="mass_mailing.portal.feedback_update_info" xml:space="preserve">
        <span>
            <t t-if="infoKey == 'feedback_sent'">
                <i class="fa fa-check me-1"/>
                <span>Sent. Thanks you for your feedback!</span>
            </t>
            <t t-else="">
                <i class="fa fa-exclamation-triangle me-1"/>
                <span>An error occurred. Please retry later or contact us.</span>
            </t>
        </span>
    </t>
`);
registerTemplate("mass_mailing.portal.list_form_content", `/mass_mailing/static/src/xml/mailing_portal_subscription_form.xml`, `<t t-name="mass_mailing.portal.list_form_content" xml:space="preserve">
        <p t-if="listsMember.length">
            Choose your mailing subscriptions
        </p>
        <ul id="o_mailing_subscription_form_lists" t-if="listsMember.length" class="list-group">
            <li t-foreach="listsMember" t-as="mailing_list" t-key="mailing_list_key" class="list-group-item d-flex align-items-center">
                <input type="checkbox" name="mailing_list_ids" t-att-data-member="1" t-att-value="mailing_list['id']" t-att-checked="mailing_list['opt_out'] ? undefined : 'checked'" t-att-title="mailing_list['name']"/>
                <span class="ms-1" t-out="mailing_list['name']"/>
                <span class="o_mailing_list_unsubscribed ms-auto">
                    <t t-if="mailing_list['opt_out']">
                        Not subscribed
                    </t>
                    <t t-else="">
                        Subscribed
                    </t>
                </span>
            </li>
        </ul>
        <p t-if="listsProposal.length">
            You may also be interested in
        </p>
        <ul id="o_mailing_subscription_form_lists_additional" t-if="listsProposal.length" class="list-group">
            <li t-foreach="listsProposal" t-as="mailing_list" t-key="mailing_list_key" class="list-group-item">
                <input type="checkbox" name="mailing_list_ids" t-att-value="mailing_list['id']" t-att-title="mailing_list['name']"/>
                <span class="ms-1" t-out="mailing_list['name']"/>
            </li>
        </ul>
    </t>


    `);
registerTemplate("mass_mailing.portal.list_form_content_readonly", `/mass_mailing/static/src/xml/mailing_portal_subscription_form.xml`, `<t t-name="mass_mailing.portal.list_form_content_readonly" xml:space="preserve">
        <p>
            Your email is currently <strong>in our block list</strong>.
            <t t-if="listsOptin.length">
                You will not receive any news from those mailing lists you are a member of:
            </t>
            <t t-else="">
                You will not hear from us anymore.
            </t>
        </p>
        <ul class="list-group mb-4" t-if="listsOptin.length">
            <t t-foreach="listsOptin" t-as="mailing_list" t-key="mailing_list_key">
                <li class="list-group-item d-flex align-items-center">
                    <strong t-out="mailing_list['name']"/>
                </li>
            </t>
        </ul>
    </t>


    `);
registerTemplate("mass_mailing.portal.list_form_update_info", `/mass_mailing/static/src/xml/mailing_portal_subscription_form.xml`, `<t t-name="mass_mailing.portal.list_form_update_info" xml:space="preserve">
        <t t-if="infoKey == 'subscription_updated'">
            <i class="fa fa-check me-1"/>
            <span>Membership updated</span>
        </t>
        <t t-else="">
            <i class="fa fa-exclamation-triangle me-1"/>
            <span>An error occurred. Please retry later.</span>
        </t>
    </t>
`);
                    });
