"use strict";(globalThis.webpackChunk=globalThis.webpackChunk||[]).push([[766],{60505(e,t,a){var l=a(79124),s=a(13722),i=a(14762),o=a(7378),n=a(69139),r=a(14755),m=a(94779),d=a(84051),c=a(4686),u=a(25354),p=a(60782),_=a(77779),b=a(13911),h=a(37059),f=a(64929),g=a(69611),y=a(12146),x=a(2373),S=a(86106);function w({customField:e,dateSettings:t,onClose:a,onSuccess:l}){const[s,i]=(0,o.useState)(()=>e?(0,x.VR)(e,t):(0,x.uS)(t)),[r,m]=(0,o.useState)(!1),[d,c]=(0,o.useState)(null);return(0,S.jsx)(_.A,{title:e?(0,n.__)("Edit custom field","mailpoet"):(0,n.__)("Add new custom field","mailpoet"),onRequestClose:a,className:"mailpoet-custom-fields-form-modal",focusOnMount:!0,children:(0,S.jsxs)("form",{onSubmit:async function(t){t.preventDefault();const a=(0,x._J)(s);if(a)c(a);else{m(!0),c(null);try{const t=(0,x.WD)(s);l(e?await(0,y.Xt)(e.id,t):await(0,y.AA)(t))}catch(e){const t=e;c(t?.message||(0,n.__)("Something went wrong. Please try again.","mailpoet"))}finally{m(!1)}}},children:[(0,S.jsx)(x.bV,{data:s,dateSettings:t,disabled:r,onChange:i}),d&&(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(b.A,{marginTop:4}),(0,S.jsx)("div",{className:"mailpoet-custom-fields-form-error",role:"alert",children:d})]}),(0,S.jsx)(b.A,{marginTop:6}),(0,S.jsxs)(h.A,{justify:"flex-end",gap:3,children:[(0,S.jsx)(f.A,{children:(0,S.jsx)(g.Ay,{variant:"tertiary",onClick:a,disabled:r,__next40pxDefaultSize:!0,children:(0,n.__)("Cancel","mailpoet")})}),(0,S.jsx)(f.A,{children:(0,S.jsx)(g.Ay,{variant:"primary",type:"submit",isBusy:r,disabled:r,__next40pxDefaultSize:!0,children:e?(0,n.__)("Save custom field","mailpoet"):(0,n.__)("Create custom field","mailpoet")})})]})]})})}var j=a(83673);const v={text:(0,n.__)("Text","mailpoet"),textarea:(0,n.__)("Textarea","mailpoet"),radio:(0,n.__)("Radio buttons","mailpoet"),checkbox:(0,n.__)("Checkbox","mailpoet"),select:(0,n.__)("Select","mailpoet"),date:(0,n.__)("Date","mailpoet")},C=[{id:"name",label:(0,n.__)("Name","mailpoet"),type:"text",enableGlobalSearch:!0,enableSorting:!0},{id:"label",label:(0,n.__)("Label","mailpoet"),type:"text",enableGlobalSearch:!1,enableSorting:!1,render:({item:e})=>(0,o.createElement)("span",null,e.label||"—")},{id:"type",label:(0,n.__)("Type","mailpoet"),type:"text",enableGlobalSearch:!1,enableSorting:!0,render:({item:e})=>(0,o.createElement)("span",null,v[e.type]??e.type)},{id:"subscribers_count",label:(0,n.__)("Subscribers","mailpoet"),type:"integer",enableGlobalSearch:!1,enableSorting:!0,render:({item:e})=>(0,o.createElement)("span",null,e.subscribers_count.toLocaleString())},{id:"forms_count",label:(0,n.__)("Forms","mailpoet"),type:"integer",enableGlobalSearch:!1,enableSorting:!1,render:({item:e})=>(0,o.createElement)("span",null,e.forms_count.toLocaleString())},{id:"dynamic_segments_count",label:(0,n.__)("Segments","mailpoet"),type:"integer",enableGlobalSearch:!1,enableSorting:!1,render:({item:e})=>(0,o.createElement)("span",null,e.dynamic_segments_count.toLocaleString())},{id:"created_at",label:(0,n.__)("Created","mailpoet"),type:"datetime",enableGlobalSearch:!1,enableSorting:!0,render:({item:e})=>(0,o.createElement)("span",null,e.created_at?j.U.Date.short(e.created_at):"")}],k={type:"table",perPage:20,page:1,sort:{field:"name",direction:"asc"},fields:["label","type","subscribers_count","forms_count","dynamic_segments_count","created_at"],titleField:"name",showTitle:!0};function A(){const[e,t]=(0,o.useState)("all"),[a,l]=(0,o.useState)(!1),[s,i]=(0,o.useState)(null),[_,b]=(0,o.useState)([]),[h,f]=(0,o.useState)(null),[g,x]=(0,o.useState)(null),j=(0,o.useCallback)(async t=>{const a=await(0,y.Vg)({search:"string"==typeof t.search?t.search:"",orderby:t.orderby??"name",order:t.order??"asc",page:t.page,per_page:t.per_page,group:e});return{items:a.items,meta:a.meta,groups:a.groups}},[e]),{view:v,setView:A,items:E,meta:T,groups:F,isLoading:N,error:P,refresh:D,clearError:B}=(0,p.i)({initialView:k,load:j}),L=(0,o.useCallback)(async(e,t,a=t.length)=>{if(0!==a){if("delete"===e||"empty_trash"===e){const t="empty_trash"===e?(0,n.sprintf)((0,n._n)("Permanently delete %d custom field from the trash? It will be removed from subscribers, forms, and dynamic segments.","Permanently delete %d custom fields from the trash? They will be removed from subscribers, forms, and dynamic segments.",a,"mailpoet"),a):(0,n.sprintf)((0,n._n)("Permanently delete %d custom field? It will be removed from subscribers, forms, and dynamic segments.","Permanently delete %d custom fields? They will be removed from subscribers, forms, and dynamic segments.",a,"mailpoet"),a);if(!window.confirm(t))return}try{const a=await(0,y.XE)(e,t);b([]),x(function(e,t){return"trash"===e?1===t?(0,n.__)("1 custom field was moved to the trash.","mailpoet"):(0,n.sprintf)((0,n._n)("%d custom field was moved to the trash.","%d custom fields were moved to the trash.",t,"mailpoet"),t):"restore"===e?1===t?(0,n.__)("1 custom field has been restored from the trash.","mailpoet"):(0,n.sprintf)((0,n._n)("%d custom field has been restored from the trash.","%d custom fields have been restored from the trash.",t,"mailpoet"),t):1===t?(0,n.__)("1 custom field was permanently deleted.","mailpoet"):(0,n.sprintf)((0,n._n)("%d custom field was permanently deleted.","%d custom fields were permanently deleted.",t,"mailpoet"),t)}(e,a)),D()}catch(e){const t=e;f(t?.message||(0,n.__)("The bulk action could not be completed.","mailpoet"))}}},[D]),R=(0,o.useCallback)(async e=>{try{const a=await(0,y.Rl)(e.id);b([]),t("all"),x((0,n.sprintf)((0,n.__)('Custom field "%s" duplicated.',"mailpoet"),a.name)),D()}catch(e){const t=e;f(t?.message||(0,n.__)("The custom field could not be duplicated.","mailpoet"))}},[D]),G=(0,o.useMemo)(()=>[{id:"edit",label:(0,n.__)("Edit","mailpoet"),isPrimary:!0,icon:"edit",supportsBulk:!1,isEligible:e=>!e.deleted_at,callback:e=>{const[t]=e;t&&i(t)}},{id:"duplicate",label:(0,n.__)("Duplicate","mailpoet"),isEligible:e=>!e.deleted_at,callback:e=>{const[t]=e;t&&R(t)}},{id:"trash",label:(0,n.__)("Move to trash","mailpoet"),supportsBulk:!0,isEligible:e=>!e.deleted_at,callback:e=>{L("trash",e.map(e=>e.id))}},{id:"restore",label:(0,n.__)("Restore","mailpoet"),supportsBulk:!0,isEligible:e=>!!e.deleted_at,callback:e=>{L("restore",e.map(e=>e.id))}},{id:"delete",label:(0,n.__)("Delete permanently","mailpoet"),supportsBulk:!0,isDestructive:!0,isEligible:e=>!!e.deleted_at,callback:e=>{L("delete",e.map(e=>e.id))}}],[L,R]),I=(0,o.useMemo)(()=>({totalItems:T.count,totalPages:T.pages}),[T]),M=(0,o.useMemo)(()=>{const e={all:null,trash:null};return(F??[]).forEach(t=>{"all"!==t.name&&"trash"!==t.name||(e[t.name]=t.count)}),e},[F]),V=(0,o.useMemo)(()=>[{name:"all",label:(0,n.__)("All","mailpoet")},{name:"trash",label:(0,n.__)("Trash","mailpoet")}].map(e=>({name:e.name,title:null===M[e.name]?e.label:`${e.label} (${M[e.name]})`,className:`mailpoet-dataviews-group-${e.name}`})),[M]),$="trash"===e?(0,n.__)("Trash is empty.","mailpoet"):(0,n.__)("No custom fields yet.","mailpoet"),z=(0,o.useMemo)(()=>({dateTypes:window.mailpoet_custom_fields_date_types??[],dateFormats:window.mailpoet_custom_fields_date_formats??{}}),[]);return(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(u.x,{}),(0,S.jsx)(c.z,{heading:(0,n.__)("Custom Fields","mailpoet"),headingPrefix:(0,S.jsx)(c.o,{href:(0,y.$m)(),label:(0,n.__)("Subscribers","mailpoet"),"aria-label":(0,n.__)("Back to subscribers list","mailpoet")}),children:(0,S.jsx)("button",{type:"button",className:"page-title-action",onClick:()=>l(!0),"data-automation-id":"add-new-custom-field-button",children:(0,n.__)("Add new custom field","mailpoet")})}),P&&(0,S.jsx)(r.A,{status:"error",onRemove:B,children:"Failed to load data."===P?(0,n.__)("Failed to load custom fields.","mailpoet"):P}),h&&(0,S.jsx)(r.A,{status:"error",onRemove:()=>f(null),children:h}),g&&(0,S.jsx)(r.A,{status:"success",onRemove:()=>x(null),children:g}),(0,S.jsx)(m.A,{className:"mailpoet-custom-fields-dataviews__tabs",activeClass:"is-active",tabs:V,initialTabName:e,onSelect:a=>{"all"!==a&&"trash"!==a||a!==e&&(t(a),b([]),f(null),x(null),A(e=>({...e,page:1})))},children:()=>(0,S.jsx)("div",{className:"mailpoet-custom-fields-dataviews",children:(0,S.jsxs)(d.A,{data:E,fields:C,view:v,onChangeView:A,actions:G,paginationInfo:I,defaultLayouts:{table:{}},getItemId:e=>String(e.id),selection:_,onChangeSelection:b,isLoading:N,empty:(0,S.jsx)("div",{children:$}),children:[(0,S.jsx)("div",{className:"mailpoet-custom-fields-dataviews__toolbar",children:(0,S.jsx)(d.A.Search,{label:(0,n.__)("Search custom fields","mailpoet")})}),"trash"===e&&(M.trash??0)>0&&(0,S.jsx)("div",{className:"mailpoet-custom-fields-dataviews__toolbar",children:(0,S.jsx)("button",{type:"button",className:"button","data-automation-id":"empty_trash_custom_fields",onClick:()=>{L("empty_trash",[],M.trash??0)},children:(0,n.__)("Empty Trash","mailpoet")})}),(0,S.jsx)(d.A.Layout,{}),(0,S.jsx)(d.A.Footer,{})]})})}),a&&(0,S.jsx)(w,{dateSettings:z,onClose:()=>l(!1),onSuccess:e=>{l(!1),x((0,n.sprintf)((0,n.__)('Custom field "%s" created.',"mailpoet"),e.name)),t("all"),b([]),A(e=>({...e,page:1})),D()}}),s&&(0,S.jsx)(w,{customField:s,dateSettings:z,onClose:()=>i(null),onSuccess:e=>{i(null),x((0,n.sprintf)((0,n.__)('Custom field "%s" updated.',"mailpoet"),e.name)),D()}})]})}function E(){return(0,S.jsx)(s.F.Provider,{value:(0,s.f)(window),children:(0,S.jsx)(i.ErrorBoundary,{children:(0,S.jsx)(A,{})})})}const T=document.getElementById("mailpoet_custom_fields_container");T&&((0,i.registerTranslations)(),(0,l.H)(T).render((0,S.jsx)(E,{})))},41669(e){e.exports=jQuery}},e=>{e.O(0,[223],()=>e(e.s=60505)),e.O()}]);