(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[5184],{16820:(e,s,i)=>{(window.__NEXT_P=window.__NEXT_P||[]).push(["/adminpanel/add-access-user",function(){return i(18177)}])},18177:(e,s,i)=>{"use strict";i.r(s),i.d(s,{default:()=>g});var a=i(37876),r=i(14232),l=i(31777),n=i(92886),o=i(89099),t=i(64329),d=i(3945),u=i(80791),c=i(84495),m=i(1785),p=i(16190),h=i(59561);let f={control:e=>({...e,width:350,textAlign:"center",borderRadius:"20px",boxShadow:"#f8f8f8",padding:"0 10px",color:"#000",borderColor:"transparent",cursor:"pointer","&:hover":{borderColor:p.yW.colorPrimary}}),option:(e,s)=>({...e,fontWeight:"500",fontSize:"13px",width:"350px",backgroundColor:s.isSelected?p.yW.colorPrimary:"white",color:s.isSelected?"#fff":"#000","&:active":{background:p.yW.colorPrimary,color:"#f00"},"&:hover":{background:p.yW.colorPrimary,color:"#fff",cursor:"pointer"}}),singleValue:e=>({...e,fontWeight:"500"})},g=e=>{var s,i;let{accessData:g,mode:v}=e,b="edit"===v,{modules:x,user:_}=(0,l.d4)(e=>e.auth),{UserList:y}=(0,l.d4)(e=>e.assign),k=(0,l.wA)(),w=(0,o.useRouter)(),[C,j]=(0,r.useState)(null),[N,P]=(0,r.useState)(!1),[A,S]=(0,r.useState)([...x].filter(e=>"admin"===e.role).map(e=>({...e,permission:[]}))),E=(0,r.useMemo)(()=>Object.values(p.dn),[]),{checkRoleAccess:W}=(0,u.Z)();(0,r.useEffect)(()=>{W(["super_admin","admin"],[])&&k((0,c.Iu)())},[]),(0,r.useEffect)(()=>{b&&g&&(j(g.user_id),S(e=>{let s=[...e].map(e=>{let s=g.modules.find(s=>s.module_assign_id===e.module_id);return s&&("admin"===s.role&&(e.admin=!0),s.permission.forEach(s=>e[s]=!0),e.id=s.id,e.role=s.role,e.permission=s.permission),e});return s.every(e=>e.admin)&&P(!0),s}))},[b,E]);let R=(0,r.useCallback)(e=>{var s,i;let a=null==g||null==(i=g.modules)||null==(s=i.find(s=>s.module_assign_id===e.module_id))?void 0:s.permission,r=null==e?void 0:e.permission;if(!a||a.length!==r.length)return!0;let l=a.slice().sort((e,s)=>e.localeCompare(s)),n=r.slice().sort((e,s)=>e.localeCompare(s));for(let e=0;e<l.length;e++)if(l[e]!==n[e])return!0;return!1},[null==g?void 0:g.modules]),I=(0,r.useCallback)((e,s)=>{let{permission:i}=e;return -1===i.indexOf(s)?(e.permission.push(s),E.every(s=>e.permission.includes(s))&&(e.admin=!0)):(e.admin=!1,e.permission=e.filter(e=>e!==s)),b&&(e.updated=R(e)),e},[R,b,E]);(0,r.useCallback)((e,s)=>{S(i=>{let a=[...i].map(i=>(i.module_id!==e||("admin"===s?(i.admin=!i.admin,i.permission=i.admin?E:[],i.updated=R(i)):i=I(i,s)),i));return P(a.every(e=>e.admin)),a})},[R,I,E]),(0,r.useCallback)((e,s)=>{S(i=>{let a=i.map(i=>(i[s]=e,e&&!i.permission.includes(s)?(i.permission.push(s),E.every(e=>i.permission.includes(e))&&(i.admin=!0)):!e&&i.permission.includes(s)&&(i.permission=i.permission.filter(e=>e!==s),i.admin=!1),i.updated=R(i),i));return P(a.every(e=>e.admin)),a})},[R,E]),(0,r.useCallback)(e=>{let s=e.target.checked;P(s),S(e=>e.map(e=>(e.permission=s?E:[],e.admin=s,E.forEach(i=>e[i]=s),e.updated=R(e),e)))},[R,E]);let O=(0,r.useCallback)(async()=>{try{if(!C)return(0,m.Ax)("Please select user.");let e={user_id:C,permissions:A.filter(e=>b||e.permission.length>0).map(e=>{let{admin:s,permission:i,module_id:a,updated:r,id:l}=e;return{admin:!!s,permission:i,module_id:a,updated:r,...b&&{id:l}}})};b?await (0,m.R_)(k((0,c.FG)(e)),"Updating...","Permission has been updated successfully.","Failed to update, please try again"):await (0,m.R_)(k((0,c.rx)(e)),"Assigning...","Permission has been assigned successfully.","Failed to assign, please try again"),w.push("/adminpanel/user-access-listing")}catch(e){console.error(e)}},[C,k,w,A,b]);return(0,a.jsxs)("div",{className:"container-listing",children:[(0,a.jsx)("div",{className:"resource-header",children:(0,a.jsxs)("div",{className:"modulename d-flex align-items-center gap-2",children:[(0,a.jsx)(h.A,{to:"/adminpanel/user-access-listing"}),(0,a.jsx)("span",{className:"main-heading",children:"Access Panel"})]})}),(0,a.jsxs)("div",{className:"resource-body access_panel",children:[(0,a.jsx)("div",{className:"search-field",children:(0,a.jsx)(n.Ay,{className:"user-access-dropdown",components:{IndicatorSeparator:()=>null,DropdownIndicator:()=>(0,a.jsx)(t.WI,{width:20,height:14})},menuPortalTarget:document.body,menuPosition:"fixed",styles:f,onChange:e=>j(e.value),placeholder:"All Users",options:null==y?void 0:y.map(e=>({value:e.id,label:e.name})),defaultValue:b?{value:null==g||null==(s=g.user)?void 0:s.id,label:null==g||null==(i=g.user)?void 0:i.name}:void 0,isDisabled:b,isSearchable:!0})}),(0,a.jsx)("div",{className:"save_btn text-center mt-4",children:(0,a.jsxs)(d.jn,{background:"transparent",height:"40px",onClick:O,children:[b?"Update":"Save"," Access"]})})]})]})}},59561:(e,s,i)=>{"use strict";i.d(s,{A:()=>n});var a=i(37876);i(14232);var r=i(89099),l=i(64329);let n=e=>{let{to:s=-1,...i}=e,n=(0,r.useRouter)();return(0,a.jsx)("button",{type:"button","aria-label":"go-back",onClick:()=>-1===s?n.back():n.push(s),children:(0,a.jsx)(l.Z3,{...i})})}}},e=>{var s=s=>e(e.s=s);e.O(0,[636,6593,8792],()=>s(16820)),_N_E=e.O()}]);
//# sourceMappingURL=add-access-user-fc9b6d5bf0f94de9.js.map