1 line
12 KiB
JavaScript
1 line
12 KiB
JavaScript
"use strict";exports.id=2996,exports.ids=[2996],exports.modules={55312:(e,a,t)=>{t.d(a,{A:()=>o});var s=t(8732),l=t(82015),i=t.n(l),n=t(32415),r=t(2471);t(27462),t(55158);var d=t(56987),c=t(12042);function o(e){let{user:a,dbImage:t,setDbImage:l,query:o}=i().useContext(r.AddDatabaseContext);if(!a)return(0,s.jsx)(i().Fragment,{});let[u,m]=i().useState(null),[g,p]=i().useState(!1);return(0,s.jsx)("section",{className:"card",children:(0,s.jsxs)("div",{className:"w-full grid grid-cols-1 xl:grid-cols-2 gap-6 items-stretch",children:[g&&(0,s.jsx)(n.A,{}),(0,s.jsxs)("div",{className:"flex-col items-start",children:[(0,s.jsx)("h2",{className:"text-xl m-0 text-left w-full",children:"New Database Information"}),a&&t&&l&&(0,s.jsx)(d.A,{user:a,dbImage:t,setDbImage:l,setLoading:p,query:o})]}),a&&t&&l&&(0,s.jsx)(c.A,{user:a,dbImage:t,setDbImage:l})]})})}},56987:(e,a,t)=>{let s;t.d(a,{A:()=>p});var l=t(8732),i=t(82015),n=t.n(i),r=t(27462),d=t(64535);t(22326);var c=t(63715),o=t(86152);async function u({e,setLoading:a,dbImage:t,database:s,user:l}){a(!0);let i={db_description:e.target.database_description?.value?.match(/./)?e.target.database_description.value:null,db_name:e.target.database_name?.value?.match(/./)?e.target.database_name.value:null,db_slug:e.target.database_slug?.value?.match(/./)?e.target.database_slug.value:null,db_full_name:s.db_full_name,id:s.id,active_clone:s.active_clone,active_clone_parent_db:s.active_clone_parent_db};if(t){if("string"==typeof t)i.db_image=t;else if("object"==typeof t){let e=await (0,c.A)("/api/addUserMedia",{method:"post",body:{media:[t],folder:i.db_slug.replace(/_/g,"-"),type:"image"}},!0);i.db_image=e?.mediaData[0]?.media_thumbnail_url?e.mediaData[0].media_thumbnail_url:"/images/database-placeholder.png"}}else i.db_image="/images/database-placeholder.png";(0,c.A)("/api/updateUserDatabase",{method:"post",body:i},!0).then(e=>{e.success?(0,o.A)().then(()=>{window.location.pathname=`/admin/${l?.id}/databases`,setTimeout(()=>{a(!1)},1e3)}):(alert("Error! Please Try Again"),setTimeout(()=>{a(!1)},1e3))})}async function m({e,setLoading:a,dbImage:t,keepUpdated:s,keepDataUpdated:l,query:i,user:n}){a(!0);let r={db_name:e.target.database_name.value,db_slug:e.target.database_slug.value,db_description:e.target.database_description?.value?.match(/./)?e.target.database_description.value:null};if(t){if("string"==typeof t)r.db_image=t;else if("object"==typeof t){let e=await (0,c.A)("/api/addUserMedia",{method:"post",body:{media:[t],folder:r.db_slug.replace(/_/g,"-"),type:"image"}},!0);r.db_image=e?.mediaData[0]?.media_thumbnail_url?e.mediaData[0].media_thumbnail_url:"/images/database-placeholder.png"}}i?.duplicate&&(r.duplicate=i.duplicate,r.keepUpdated=!!s&&s,r.keepDataUpdated=!!l&&l),(0,c.A)("/api/createUserDatabase",{method:"post",body:r},!0).then(e=>{e.success?(0,o.A)().then(()=>{window.location.pathname=`/admin/${n?.id}/databases/${r.db_slug}`}):setTimeout(()=>{a(!1)},1e3)})}var g=t(58403);function p({user:e,dbImage:a,setDbImage:t,database:i,setLoading:c,query:o}){async function p(a){return i?.db_slug===a?null:await (0,r.A)({tableName:"user_databases",columnName:"db_slug",value:a,userId:e.id})}let[h,b]=n().useState(null),[x,f]=n().useState(!1),[_,w]=n().useState(!1);return(0,l.jsxs)("form",{className:"w-full max-w-none xl:max-w-lg flex flex-col items-start gap-4 relative",onSubmit:t=>{t.preventDefault(),i?u({e:t,setLoading:c,dbImage:a,database:i,user:e}):m({e:t,setLoading:c,dbImage:a,keepUpdated:x,keepDataUpdated:_,query:o,user:e})},children:[h&&(0,l.jsx)(d.A,{message:h}),(0,l.jsxs)("div",{className:"flex flex-col items-start gap-0.5 w-full",children:[(0,l.jsx)("label",{htmlFor:"database_name",children:"Name"}),(0,l.jsx)("input",{type:"text",name:"database_name",id:"database_name",placeholder:"Database Name",onInput:async e=>{let a=e.target,t=a.closest("form");if(a.value.match(/./)?(a.classList.remove("warning"),b(null)):a.classList.add("warning"),t){t.database_slug.value=a.value.toLowerCase().replace(/ /g,"_").replace(/[^a-zA-Z0-9-_]/g,"");let e=t.database_slug.value;window.clearTimeout(s),s=setTimeout(()=>{p(e).then(e=>{e?.result?(t.database_slug.classList.add("warning"),b("Duplicate Database Name")):(t.database_slug.classList.remove("warning"),b(null))})},300)}},defaultValue:i?i.db_name:"",required:!0})]}),(0,l.jsxs)("div",{className:"flex flex-col items-start gap-0.5 w-full",children:[(0,l.jsx)("label",{htmlFor:"database_slug",children:"Slug"}),(0,l.jsx)("input",{type:"text",name:"database_slug",id:"database_slug",placeholder:"Database Slug",onInput:e=>{let a=e.target;a.value.match(/./)?(a.classList.remove("warning"),b(null)):a.classList.add("warning");let t=a.value;window.clearTimeout(s),s=setTimeout(()=>{p(t).then(e=>{e?.result?(a.classList.add("warning"),b("Duplicate Database Name")):(a.classList.remove("warning"),b(null))})},300)},defaultValue:i?i.db_slug:"",required:!0})]}),(0,l.jsxs)("div",{className:"flex flex-col items-start gap-0.5 w-full",children:[(0,l.jsx)("label",{htmlFor:"database_description",children:"Description (optional)"}),(0,l.jsx)("textarea",{name:"database_description",id:"database_description",placeholder:"Short Description of database",defaultValue:i?i.db_description:""})]}),o?.duplicate&&(0,l.jsx)(n().Fragment,{children:(0,l.jsxs)("div",{className:"card col items-start",children:[(0,l.jsx)(g.A,{checkBoxValues:[{payload:"Keep New Database Updated?",title:"Keep New Database Updated?",name:"clone_database",onChangeHandler:e=>{e.target.checked?f(!0):f(!1)}}]}),(0,l.jsx)("span",{className:"text-sm italic text-slate-600 dark:text-slate-300",children:"By checking this box the new Database stays up to date with changes to the structure of the parent Database"})]})}),(0,l.jsxs)("button",{type:"submit",className:"w-full",children:[i?"Update":"Create"," Database"]})]})}},12042:(e,a,t)=>{t.d(a,{A:()=>c});var s=t(8732),l=t(82015),i=t.n(l),n=t(79100),r=t(55158),d=t(62506);function c({user:e,dbImage:a,setDbImage:t,database:l}){let c=i().useRef();return(0,s.jsxs)("div",{className:"card no-hover col items-center justify-center",children:[(0,s.jsx)("h4",{className:"m-0",children:"Database Image"}),(0,s.jsx)("div",{className:"bg-white rounded-full overflow-hidden w-24 h-24",children:(0,s.jsx)("img",{src:a?"string"==typeof a?a:a.imageBase64Full:"/images/database-placeholder.png",alt:"Database Image",width:100,className:"w-full h-full object-cover",ref:c,"data-imagepreview":"image"})}),(0,s.jsxs)("div",{className:"w-full flex-col",id:"edit-database-image-option-wrapper",children:[(0,s.jsxs)(d.A,{column:!0,className:"w-full",children:[(0,s.jsx)("button",{className:"outlined w-full whitespace-normal",onClick:e=>{(0,r.PB)("user-media-popup")},children:"Choose Image from uploads"}),(0,s.jsx)("button",{className:"outlined secondary w-full whitespace-normal",onClick:e=>{let a=e.target;a?.closest("#edit-database-image-option-wrapper")?.querySelector("input")?.click()},children:"Choose Image from Computer"})]}),(0,s.jsx)("input",{type:"file",accept:".png,.jpg,.jpeg,.webp",placeholder:"Choose Database Image",className:"hidden",onChange:async e=>{let a=e.target.files[0];t(await (0,n.A)({imageInputFile:a,maxWidth:400}))}})]})]})}},42105:(e,a,t)=>{t.d(a,{A:()=>d});var s=t(8732),l=t(82015),i=t.n(l);t(63715);var n=t(55158),r=t(32415);function d({setDbImage:e}){let[a,t]=i().useState(!0),[l,d]=i().useState(null);return(0,s.jsxs)(n.Ay,{title:"user-media-popup",children:[a&&(0,s.jsx)(r.A,{width:"20px"}),(0,s.jsx)("h4",{className:"m-0 text-lg",children:"Your Uploaded Images"}),(0,s.jsx)("div",{className:"grid grid-cols-2",children:l&&l[0]&&l.filter(e=>"image"==e.media_type).map((a,t)=>(0,s.jsx)("div",{className:"w-full h-44 cursor-pointer hover:opacity-70",onClick:t=>{e(a.media_thumbnail_url||null),(0,n.fs)()},children:(0,s.jsx)("img",{src:a.media_thumbnail_url,alt:"User Media",className:"w-full h-full object-contain"})},t+1))})]})}t(64417)},64535:(e,a,t)=>{t.d(a,{A:()=>l});var s=t(8732);function l({message:e}){return(0,s.jsxs)("div",{className:"p-2 bg-orange-50 w-full justify-center rounded text-sm text-orange-700 border border-orange-400 border-solid",children:[(0,s.jsx)("img",{src:"/images/warning.png",alt:"Warning Image Icon",width:22,className:"-my-2"}),(0,s.jsx)("span",{children:e})]})}t(82015)},27462:(e,a,t)=>{t.d(a,{A:()=>l}),t(22326);var s=t(63715);async function l({tableName:e,columnName:a,value:t,dbFullName:l,dispatch:i,userId:n,dbId:r}){return await (0,s.A)(`/api/checkDuplicateData?tableName=${e}&type=${a}&value=${t}${l?"&dbFullName="+l:""}${n?"&userId="+n:""}${r?"&dbId="+r:""}`)}},79100:(e,a,t)=>{t.d(a,{A:()=>s});async function s({imageInputFile:e,maxWidth:a}){let t,s,l=document.querySelector("[data-imagepreview='image']"),i=e.name.replace(/\..*/,""),n=document.createElement("canvas"),r=e.type,d=a||null,c=URL.createObjectURL(e),o=new Image;return o.src=c,t=await new Promise((e,a)=>{o.onerror=function(){URL.revokeObjectURL(this.src),console.log("Cannot load image")},o.onload=function(){if(URL.revokeObjectURL(this.src),d){let e=d/o.naturalWidth;n.width=o.naturalWidth<d?o.naturalWidth:d,n.height=o.naturalWidth<d?o.naturalHeight:o.naturalHeight*e}else n.width=o.naturalWidth,n.height=o.naturalHeight;let a=n.getContext("2d");a?.drawImage(o,0,0,n.width,n.height);let t=n.toDataURL(r,.95);l&&document.querySelectorAll("[data-imagepreview='image']").forEach(e=>{e.src=t}),e(t)}}),s=await new Promise((e,a)=>{n.toBlob(a=>{e(a?.size)},r,.95)}),{imageBase64:t.replace(/.*?base64,/,""),imageBase64Full:t,imageName:i,imageSize:s}}},41658:(e,a,t)=>{let s=t(29021),l=t(22373),i=t(13608);e.exports=async function(e){let a;"production".match(/dev/)&&s.appendFileSync("./.tmp/sqlQuery.sql",e+"\n"+Date()+"\n\n\n","utf8");try{a=await i(e)}catch(e){l({component:"noDatabaseDbHandler",message:e.message}),console.log("ERROR in noDatabaseDbHandler =>",e.message)}return a||null}},13608:(e,a,t)=>{let s=t(9973),l=t(31976),i=s({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,charset:"utf8mb4",ssl:l()}});e.exports=function(...e){try{return new Promise((a,t)=>{i.query(...e).then(e=>{i.end(),a(JSON.parse(JSON.stringify(e)))}).catch(e=>{i.end(),a({error:e.message,sql:e.sql})})})}catch(e){return{success:!1,error:e.message}}}},2471:(e,a,t)=>{t.r(a),t.d(a,{AddDatabaseContext:()=>f,default:()=>_,getServerSideProps:()=>w});var s=t(8732),l=t(82015),i=t.n(l),n=t(95222),r=t(20048),d=t.n(r),c=t(41658),o=t.n(c),u=t(46437),m=t(58548),g=t(55312);function p(e){let{query:a,duplicateDb:t,user:l}=i().useContext(f),n=a?.duplicate?(0,s.jsxs)("span",{children:["Duplicate"," ",(0,s.jsx)("a",{href:`/admin/${l?.id}/databases/${t?.db_slug}`,target:"_blank",children:t?.db_name})," ","Database"]}):"Add A New Database";return(0,s.jsxs)(i().Fragment,{children:[(0,s.jsxs)("div",{className:"flex-col items-start gap-0 w-full",children:[(0,s.jsxs)("div",{className:"flex-wrap",children:[(0,s.jsx)(u.A,{}),(0,s.jsx)("h1",{className:"text-3xl m-0",children:n})]}),(0,s.jsx)(m.A,{user:l})]}),(0,s.jsx)(g.A,{})]})}var h=t(42105),b=t(67216),x=t.n(b);let f=i().createContext({});function _({user:e,databases:a,query:t,duplicateDb:l}){let r=(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("title",{children:"Add Database | Datasquirel"}),(0,s.jsx)("meta",{name:"description",content:"Add a database"})]}),[d,c]=i().useState("/images/database-placeholder.png");return(0,s.jsx)(f.Provider,{value:{user:e,databases:a,dbImage:d,setDbImage:c,query:t,duplicateDb:l},children:(0,s.jsxs)(i().Fragment,{children:[(0,s.jsx)(n.A,{head:r,user:e,children:(0,s.jsx)(p,{})}),(0,s.jsx)(h.A,{setDbImage:c})]})})}async function w({req:e,res:a,query:t}){let s=await d()(e,a);if(!s?.logged_in_status)return{redirect:{destination:"/login",permanent:!1}};if(!s?.verification_status?.toString()?.match(/1/))return{redirect:{destination:"/admin",permanent:!1}};let l=await o()(`SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME LIKE 'user_${s.id}%'`),i={};if(t.duplicate){let e=await x()(`SELECT * FROM user_databases WHERE db_full_name='${t.duplicate}'`);e&&e[0]&&(i=e[0])}return{props:{user:s,databases:l,query:t,duplicateDb:i}}}}}; |