mstate-react 1.5.3 → 1.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- import{useState as e,useEffect as t,createElement as r,useMemo as o}from"react";import n from"axios";import{Box as i,CircularProgress as a,InputLabel as l,Autocomplete as s,TextField as c,Checkbox as d,FormControl as m,FormHelperText as u,Container as p,Typography as h,Stack as f,Button as g,useMediaQuery as x,ThemeProvider as b,List as y,ListItem as I}from"@mui/material";import{jsx as T,jsxs as k,Fragment as C}from"react/jsx-runtime";import w from"@mui/material/Card";import E from"@mui/material/CardContent";import A from"@mui/material/CardMedia";import _ from"@mui/material/Typography";import F from"@mui/material/CardActionArea";import{Controller as S,useForm as O}from"react-hook-form";import{createTheme as v}from"@mui/material/styles";const D="https://api.mstate.mobioffice.io/api",N="secret-key",P={errorMessage:{m:0,mt:1,ml:.5},label:{fontSize:14,mb:-2},textInput:{"& .MuiOutlinedInput-root":{"& fieldset":{border:"none"},borderRadius:"8px"},"& .MuiInputBase-input":{height:14},"& .MuiInputBase-multiline":{paddingBlock:"16px"}},radio:{"& fieldset":{border:"none"},borderRadius:"8px"},logo:{width:"100px",margin:"auto",maxWidth:"300px",my:2,cursor:"pointer"},title:{textAlign:"center",fontSize:{xs:"1.5rem",sm:"2rem"}},button:{borderRadius:100},centerContent:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column"}},R="https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/mstate-mobioiifce-logo.svg?alt=media&token=fa74ace6-0938-4df5-b5b7-cb93115cf7b2",L="https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/companies_logo%2FMobiOffice%20Logo.png?alt=media&token=575b9568-0b4c-468f-ba99-4b613bd4e40b",$="https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/companies_logo%2Ferror.png?alt=media&token=bb494b33-eb79-471a-abca-1031524739bc";let U=function(e){return e.EXECUTE_ACTION="workflow/v2/execute",e.GET_WORKFLOW_CONFIG="workflow/config",e.GET_CURRENT_ACTIONS_WITHOUT_INSTANCE="workflow/v2/actions",e.GET_CURRENT_ACTIONS_WITH_INSTANCE="workflow/actions",e}({}),M=function(e){return e.DATE_PICKER="datePicker",e.TIME_PICKER="timePicker",e.INPUT_FIELD="inputField",e.TEXT_AREA="textArea",e.CHECKBOX="checkBox",e.RADIO_LIST="radioList",e.DROP_DOWN="dropDown",e.SUMMARY="summary",e.CONTEXT_FIELD="CONTEXT_FIELD",e.CUSTOM_FIELD="CUSTOM_FIELD",e}({}),B=function(e){return e.CONTEXT_FIELD="CONTEXT_FIELD",e.CUSTOM_FIELD="CUSTOM_FIELD",e.USER_INPUT="USER_INPUT",e}({});function q(){return T(i,{children:T(W,{})})}function W(){return k(C,{children:[T("svg",{width:0,height:0,children:T("defs",{children:k("linearGradient",{id:"my_gradient",x1:"0%",y1:"0%",x2:"0%",y2:"100%",children:[T("stop",{offset:"0%",stopColor:"#e01cd5"}),T("stop",{offset:"100%",stopColor:"#1CB5E0"})]})})}),T(a,{sx:{"svg circle":{stroke:"url(#my_gradient)"}}})]})}function X(e=""){return e.toUpperCase().replace(/_/gi," ")}function j(e,t){const r=t?t.split("."):[];let o=e;for(const e of r)o=o?.[e];return o}function G(e){const{name:t,meta:r,onClick:o}=e;return T(w,{sx:{maxWidth:250,width:"100%",m:2,borderRadius:5},onClick:o,children:k(F,{children:[T(i,{children:T(A,{component:"img",sx:{m:"auto",objectFit:"contain",width:"200px",borderRadius:"50%",my:2},image:r?.logo??L,alt:"action"})}),T("hr",{style:{marginInline:"10px"}}),k(E,{children:[T(_,{color:"text.primary",gutterBottom:!0,variant:"h5",component:"div",sx:{fontSize:"1.3rem",textAlign:"center"},children:r?.title??X(t)}),T(_,{color:"text.primary",variant:"body2",sx:{color:"text.secondary",textAlign:"center"},children:r?.desc})]})]})})}function V(o){const{step:i,form:a}=o,{setValue:p,control:h,register:f,formState:{errors:g}}=a,x=function(e){return void 0!==e?.config}(i)?i?.config?.data:i,b=x.field??{},y=b.mapper,[I,w]=e(b.itemList??[]);switch(t((()=>{b.request&&async function(e,t){const{data:r}=await n(e);return j(r,t?.target??"")??[]}(b.request,y).then((e=>{w(e)})).catch((e=>{console.log(e)}))}),[]),x.fieldType){case M.INPUT_FIELD:case M.TEXT_AREA:return k(C,{children:[T(l,{sx:P.label,htmlFor:`text-${b.keyForAPI}`,children:b.label}),T(c,{multiline:x.fieldType===M.TEXT_AREA,minRows:3,id:`text-${b.keyForAPI}`,type:x.inputType,placeholder:b.placeholder,slotProps:{inputLabel:{shrink:"date"===x.inputType||void 0},formHelperText:{sx:P.errorMessage}},sx:P.textInput,defaultValue:"",...f(b.keyForAPI,{required:b?.isRequired&&`${b.label} is required`}),error:Boolean(g[b.keyForAPI]),helperText:g?.[b.keyForAPI]?.message},`input-${b.keyForAPI}`)]});case M.DROP_DOWN:case M.RADIO_LIST:return k(C,{children:[T(l,{sx:P.label,htmlFor:`dropdown-${b.keyForAPI}`,children:b.label}),k(m,{children:[T(s,{id:`dropdown-${b.keyForAPI}`,options:I,...f(b.keyForAPI,{required:b?.isRequired&&`${b.label} is required`}),onChange:(e,t)=>{p(b.keyForAPI,t,{shouldValidate:!0})},renderInput:e=>r(c,{...e,sx:P.textInput,key:`input-${b.keyForAPI}`,placeholder:b.placeholder,error:Boolean(g[b.keyForAPI])}),getOptionLabel:e=>j(e,y?.label??"label")??"",renderOption:(e,t)=>{const o=j(t,y?.id??"id"),n=j(t,y?.label??"label");return r("li",{...e,key:o?.toString()},n?.toString())}}),Boolean(g[b.keyForAPI])?k(u,{error:!0,sx:P.errorMessage,children:[b.label," is required"]}):T(C,{})]})]});case M.CHECKBOX:return k(C,{children:[T(l,{sx:P.label,htmlFor:`checkbox-${b.keyForAPI}`,children:b.label}),T(S,{name:b.keyForAPI,control:h,defaultValue:[],rules:{required:b?.isRequired&&`${b.label} is required`},render:({field:{value:e},fieldState:{error:t}})=>T(C,{children:T(s,{multiple:!0,id:`checkbox-${b.keyForAPI}`,options:I,value:e||[],onChange:(e,t)=>{const r=y?.id??"id",o=new Set,n=t.find((e=>{const t=j(e,r);return!!o.has(t)||(o.add(t),!1)}));if(n){const e=j(n,r);t=t.filter((t=>j(t,r)!==e))}p(b.keyForAPI,t,{shouldValidate:!0})},renderInput:e=>T(c,{...e,sx:P.textInput,placeholder:b.placeholder,slotProps:{formHelperText:{sx:P.errorMessage}},error:!!t,helperText:t?t.message:""}),getOptionLabel:e=>j(e,y?.label??"label")??"",renderOption:(t,o)=>{const n=j(o,y?.id??"id"),i=j(o,y?.label??"label"),a=e.some((e=>j(e,y?.id??"id")===n));return r("li",{...t,key:n?.toString()},T(d,{style:{marginRight:8},checked:a}),i?.toString())}})})})]})}return T(C,{})}function H(r){const{config:o,onSuccess:a,onExecuteAction:l,updateCustomFieldValue:s}=r,[c,d]=e(!1),m=O({defaultValues:{}}),{handleSubmit:u,setValue:x}=m;t((()=>!1===Boolean(o?.steps?.length)?(b({}),()=>{}):(y(),()=>{})),[]);const b=async e=>{try{d(!0);const t=await async function(e){const{data:t}=await n({url:`${D}/${U.EXECUTE_ACTION}`,method:"POST",headers:{"Content-Type":"application/json",[N]:e.token},data:{instanceID:e.instanceID??"",name:e.workflow,action:e.action}});return t.data}({workflow:r.workflow,instanceID:r.instanceID,token:r.token,action:{name:o.name,payload:e}});a&&a(o.name),l(t)}catch(e){n.isAxiosError(e)&&e.response?l({},e.response?.data?.errors??["Something went wrong"]):l({},["Something went wrong"])}finally{d(!1)}},y=()=>{const e=new Set;o.steps=o?.steps?.filter((t=>{const r=t;switch(r?.module){case B.CUSTOM_FIELD:return x(r?.context?.as,s(r?.context?.key)),!0;case B.CONTEXT_FIELD:return x(r?.context?.as,r?.context?.value),!0;default:{const t=e.has(r?.field?.keyForAPI);return e.add(r?.field?.keyForAPI),!1===t}}}))};return T(p,{sx:{p:2,justifyContent:"center"},children:c?k(i,{sx:Object.assign({p:3},P.centerContent),children:[T(q,{}),T(h,{color:"text.primary",variant:"h3",sx:P.title,children:"Loading..."})]}):k("form",{onSubmit:u(b),noValidate:!0,children:[T(h,{color:"text.primary",variant:"h4",sx:P.title,children:o?.meta?.title??X(o?.name)}),T(h,{color:"text.primary",variant:"subtitle1",sx:{mb:2,textAlign:"center"},children:o?.meta?.desc??""}),T(f,{direction:"column",sx:{gap:3},children:o.steps.map(((e,t)=>T(V,{form:m,step:e},`step-input-${t}`)))}),T(f,{sx:{mt:3},children:T(g,{variant:"contained",type:"submit",sx:P.button,children:"Submit"})})]})})}function K(r){const{who:a,token:l,start:s=!0,type:c="private",theme:d="light",instanceID:m,workflow:u,onInstanceChange:p,onError:w,onSuccess:E,handleCustomField:A=()=>""}=r,_=x("(prefers-color-scheme: dark)")?"dark":"light",F="system"===d?_:d,[S,O]=e(m),[L,M]=e(!0),[B,W]=e([]),[X,j]=e([]),[V,K]=e(!1);t((()=>!1===s?()=>{}:(M(!0),S?(async function(e){const{data:t}=await n({url:`${D}/${U.GET_CURRENT_ACTIONS_WITH_INSTANCE}/${e.instanceID}`,method:"GET",headers:{[N]:e.token}});return t.data}({instanceID:S,token:l}).then((e=>{W(Object.values(e))})).catch((e=>{n.isAxiosError(e)&&e.response?j(e.response?.data?.errors??["Something went wrong"]):j(["Something went wrong"])})).finally((()=>{M(!1)})),()=>{}):(async function(e){const{data:t}=await n({url:`${D}/${U.GET_CURRENT_ACTIONS_WITHOUT_INSTANCE}?name=${e.workflow}&type=${e.type}`,method:"GET",headers:{[N]:e.token}});return t.data}({token:l,workflow:u,type:c}).then((e=>{W(Object.values(e))})).catch((e=>{n.isAxiosError(e)&&e.response?j(e.response?.data?.errors??["Something went wrong"]):j(["Something went wrong"])})).finally((()=>{M(!1)})),()=>{}))),[s,V]),t((()=>{O(m)}),[m]),t((()=>{w&&X?.length&&w(X)}),[X]);const z=o((()=>function(e){return v({components:{MuiInputBase:{styleOverrides:{root:{backgroundColor:"dark"===e?"#353539":"white"}}},MuiButton:{styleOverrides:{root:{background:"linear-gradient(90deg, #7263E2 0%, #473C9A 105.07%)",color:"#ffffff"}}},MuiCard:{styleOverrides:{root:{backgroundColor:"dark"===e?"#353539":"white"}}}},palette:{mode:e,background:{default:"light"===e?"#F1EFFC":"#161618"},text:{primary:"light"===e?"#000":"#fff",secondary:"light"===e?"#666":"#bbb"}}},{customElements:{inputBG:"white"}})}(F)),[F]);return!1===s?T(C,{}):T(b,{theme:z,children:k(i,{sx:{backgroundColor:"var(--mstate-bg-color)",height:"100%",overflow:"auto"},children:[T(i,{sx:P.logo,onClick:()=>window.open("https://mobioffice.io"),children:T("img",{src:R,alt:"logo",width:"100%"})}),L?k(i,{sx:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column",p:3},children:[T(q,{}),T(h,{color:"text.primary",variant:"h3",sx:P.title,children:"Loading..."})]}):X.length?k(i,{sx:Object.assign({p:3},P.centerContent),children:[T(i,{sx:{width:"100px",m:"auto"},children:T("img",{width:"100%",src:$,alt:"error"})}),T(y,{children:X.filter((e=>"string"==typeof e)).map(((e,t)=>T(I,{children:T(h,{color:"error",variant:"subtitle1",sx:P.title,children:e.replace(/instanceID='.*'/i,"")})},`error ${t}`)))}),T(g,{variant:"contained",onClick:()=>{B?.length||K(!V),j([])},sx:P.button,children:"Try Again"})]}):T(i,{children:B?.length?1===B.length?T(H,{token:l,onSuccess:E,instanceID:S,workflow:u,config:B[0],onExecuteAction:(e,t)=>{t?.length?(j(t),e.instanceID=t[0]?.split("instanceID='")?.pop()?.replace("'","")):W(e.actions),e?.instanceID&&e?.instanceID!==S&&!t?.length&&(O(e?.instanceID),p&&p(e?.instanceID))},updateCustomFieldValue:A}):T(i,{children:T(f,{sx:{flexDirection:"row",flexWrap:"wrap",justifyContent:"center"},children:B.map((e=>!e?.who?.length||e?.who?.includes(a??"")?T(G,{meta:e?.meta,name:e?.name,onClick:()=>W([e])},`action-${e.name}`):T(C,{})))})}):T(i,{className:"box_center_to_modal",children:T(h,{color:"text.primary",variant:"h3",sx:P.title,children:"Workflow Ended"})})})]})})}!function(e,t){void 0===t&&(t={});var r=t.insertAt;if("undefined"!=typeof document){var o=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css","top"===r&&o.firstChild?o.insertBefore(n,o.firstChild):o.appendChild(n),n.styleSheet?n.styleSheet.cssText=e:n.appendChild(document.createTextNode(e))}}(":root{--mstate-input:#fff;--mstate-bg-color:#f1effc}");export{K as Mstate,K as default};
1
+ import{useState as e,useEffect as t,createElement as o,useMemo as r}from"react";import n from"axios";import{Box as i,CircularProgress as a,InputLabel as l,Autocomplete as s,Checkbox as c,TextField as d,FormControl as m,FormHelperText as u,Container as p,Typography as h,Stack as f,Button as g,useMediaQuery as x,ThemeProvider as y,List as b,ListItem as I}from"@mui/material";import{jsx as T,jsxs as C,Fragment as k}from"react/jsx-runtime";import E from"@mui/material/Card";import w from"@mui/material/CardContent";import A from"@mui/material/CardMedia";import _ from"@mui/material/Typography";import F from"@mui/material/CardActionArea";import{Controller as O,useForm as S}from"react-hook-form";import{createTheme as v}from"@mui/material/styles";const D="https://api.mstate.mobioffice.io/api",R="secret-key",N={errorMessage:{m:0,mt:1,ml:.5},label:{fontSize:14,mb:-2},textInput:{"& .MuiOutlinedInput-root":{"& fieldset":{border:"none"},borderRadius:"8px"},"& .MuiInputBase-input":{height:14},"& .MuiInputBase-multiline":{paddingBlock:"16px"}},logo:{width:"100px",margin:"auto",maxWidth:"300px",my:2,cursor:"pointer"},title:{textAlign:"center",fontSize:{xs:"1.5rem",sm:"2rem"}},button:{borderRadius:100},centerContent:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column"}},P={logo:"https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/mstate-mobioiifce-logo.svg?alt=media&token=fa74ace6-0938-4df5-b5b7-cb93115cf7b2",icon:"https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/companies_logo%2FMobiOffice%20Logo.png?alt=media&token=575b9568-0b4c-468f-ba99-4b613bd4e40b",error:"https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/companies_logo%2Ferror.png?alt=media&token=bb494b33-eb79-471a-abca-1031524739bc"};let L=function(e){return e.EXECUTE_ACTION="workflow/v2/execute",e.GET_WORKFLOW_CONFIG="workflow/config",e.GET_CURRENT_ACTIONS_WITHOUT_INSTANCE="workflow/v2/actions",e.GET_CURRENT_ACTIONS_WITH_INSTANCE="workflow/actions",e}({}),$=function(e){return e.DATE_PICKER="datePicker",e.TIME_PICKER="timePicker",e.INPUT_FIELD="inputField",e.TEXT_AREA="textArea",e.CHECKBOX="checkBox",e.RADIO_LIST="radioList",e.DROP_DOWN="dropDown",e.SUMMARY="summary",e.CONTEXT_FIELD="CONTEXT_FIELD",e.CUSTOM_FIELD="CUSTOM_FIELD",e}({}),U=function(e){return e.CONTEXT_FIELD="CONTEXT_FIELD",e.CUSTOM_FIELD="CUSTOM_FIELD",e.USER_INPUT="USER_INPUT",e}({});function M(){return T(i,{children:T(B,{})})}function B(){return C(k,{children:[T("svg",{width:0,height:0,children:T("defs",{children:C("linearGradient",{id:"my_gradient",x1:"0%",y1:"0%",x2:"0%",y2:"100%",children:[T("stop",{offset:"0%",stopColor:"#e01cd5"}),T("stop",{offset:"100%",stopColor:"#1CB5E0"})]})})}),T(a,{sx:{"svg circle":{stroke:"url(#my_gradient)"}}})]})}function j(e=""){return e.toUpperCase().replace(/_/gi," ")}function q(e,t){const o=t?t.split("."):[];let r=e;for(const e of o)r=r?.[e];return r}function W(e){const{name:t,meta:o,onClick:r}=e;return T(E,{sx:{maxWidth:250,width:"100%",m:2,borderRadius:5},onClick:r,children:C(F,{children:[T(i,{children:T(A,{component:"img",sx:{m:"auto",objectFit:"contain",height:"150px",width:"auto",borderRadius:"50%",my:2},image:o?.logo??P.icon,alt:"action"})}),T("hr",{style:{marginInline:"10px"}}),C(w,{children:[T(_,{color:"text.primary",gutterBottom:!0,variant:"h5",component:"div",sx:{fontSize:"1.3rem",textAlign:"center"},children:o?.title??j(t)}),T(_,{color:"text.primary",variant:"body2",sx:{color:"text.secondary",textAlign:"center"},children:o?.desc})]})]})})}function X(r){const{step:i,form:a}=r,{setValue:p,control:h,register:f,formState:{errors:g}}=a,x=function(e){return void 0!==e?.config}(i)?i?.config?.data:i,y=x.field??{},b=y.mapper,[I,E]=e(y.itemList??[]);switch(t((()=>{y.request?.url&&async function(e,t){const{data:o}=await n(e);return q(o,t?.target??"")??[]}(y.request,b).then((e=>{E(e)})).catch((e=>{console.log(e)}))}),[]),x.fieldType){case $.INPUT_FIELD:case $.TEXT_AREA:return C(k,{children:[T(l,{sx:N.label,htmlFor:`text-${y.keyForAPI}`,children:y.label}),T(d,{multiline:x.fieldType===$.TEXT_AREA,minRows:3,id:`text-${y.keyForAPI}`,type:x.inputType,placeholder:y.placeholder,slotProps:{inputLabel:{shrink:"date"===x.inputType||void 0},formHelperText:{sx:N.errorMessage}},sx:N.textInput,defaultValue:"",...f(y.keyForAPI,{required:y?.isRequired&&`${y.label} is required`}),error:Boolean(g[y.keyForAPI]),helperText:g?.[y.keyForAPI]?.message},`input-${y.keyForAPI}`)]});case $.DROP_DOWN:case $.RADIO_LIST:return C(k,{children:[T(l,{sx:N.label,htmlFor:`dropdown-${y.keyForAPI}`,children:y.label}),C(m,{children:[T(s,{id:`dropdown-${y.keyForAPI}`,options:I,...f(y.keyForAPI,{required:y?.isRequired&&`${y.label} is required`}),onChange:(e,t)=>{p(y.keyForAPI,t,{shouldValidate:!0})},renderInput:e=>o(d,{...e,sx:N.textInput,key:`input-${y.keyForAPI}`,placeholder:y.placeholder,error:Boolean(g[y.keyForAPI])}),getOptionLabel:e=>q(e,b?.label||"label")||"",renderOption:(e,t)=>{const r=q(t,b?.id||"id"),n=q(t,b?.label||"label");return o("li",{...e,key:r?.toString()},n?.toString())}}),Boolean(g[y.keyForAPI])?C(u,{error:!0,sx:N.errorMessage,children:[y.label," is required"]}):T(k,{})]})]});case $.CHECKBOX:return C(k,{children:[T(l,{sx:N.label,htmlFor:`checkbox-${y.keyForAPI}`,children:y.label}),T(O,{name:y.keyForAPI,control:h,defaultValue:[],rules:{required:y?.isRequired&&`${y.label} is required`},render:({field:{value:e},fieldState:{error:t}})=>T(k,{children:T(s,{multiple:!0,id:`checkbox-${y.keyForAPI}`,options:I,value:e||[],onChange:(e,t)=>{const o=b?.id||"id",r=new Set,n=t.find((e=>{const t=q(e,o);return!!r.has(t)||(r.add(t),!1)}));if(n){const e=q(n,o);t=t.filter((t=>q(t,o)!==e))}p(y.keyForAPI,t,{shouldValidate:!0})},renderInput:e=>T(d,{...e,sx:N.textInput,placeholder:y.placeholder,slotProps:{formHelperText:{sx:N.errorMessage}},error:!!t,helperText:t?t.message:""}),getOptionLabel:e=>q(e,b?.label||"label")||"",renderOption:(t,r)=>{const n=q(r,b?.id||"id"),i=q(r,b?.label||"label"),a=e.some((e=>q(e,b?.id||"id")===n));return o("li",{...t,key:n?.toString()},T(c,{style:{marginRight:8},checked:a}),i?.toString())}})})})]})}return T(k,{})}function G(o){const{config:r,onSuccess:a,onExecuteAction:l,onCustomResponse:s,updateCustomFieldValue:c}=o,[d,m]=e(!1),u=S({defaultValues:{}}),{handleSubmit:x,setValue:y,reset:b}=u;t((()=>!1===Boolean(r?.steps?.length)?(I({}),()=>{}):(k(),()=>{})),[]);const I=async e=>{try{m(!0);const t=await async function(e){const{data:t}=await n({url:`${D}/${L.EXECUTE_ACTION}`,method:"POST",headers:{"Content-Type":"application/json",[R]:e.token},data:{instanceID:e.instanceID??"",name:e.workflow,action:e.action}});return t.data}({workflow:o.workflow,instanceID:o.instanceID,token:o.token,action:{name:r.name,payload:e}});a&&a(r.name),s&&Object.keys(t?.customResponse??{}).length&&s(t?.customResponse),l(t),b()}catch(e){n.isAxiosError(e)&&e.response?l({},e.response?.data?.errors??["Something went wrong"]):l({},["Something went wrong"])}finally{m(!1)}},k=()=>{const e=new Set;r.steps=r?.steps?.filter((t=>{const o=t;switch(o?.module){case U.CUSTOM_FIELD:return y(o?.context?.as,c(o?.context?.key)),!0;case U.CONTEXT_FIELD:return y(o?.context?.as,o?.context?.value),!0;default:{const t=e.has(o?.field?.keyForAPI);return e.add(o?.field?.keyForAPI),!1===t}}}))};return T(p,{sx:{p:2,justifyContent:"center"},children:d?C(i,{sx:Object.assign({p:3},N.centerContent),children:[T(M,{}),T(h,{color:"text.primary",variant:"h3",sx:N.title,children:"Loading..."})]}):C("form",{onSubmit:x(I),noValidate:!0,children:[T(h,{color:"text.primary",variant:"h4",sx:N.title,children:r?.meta?.title??j(r?.name)}),T(h,{color:"text.primary",variant:"subtitle1",sx:{mb:2,textAlign:"center"},children:r?.meta?.desc??""}),T(f,{direction:"column",sx:{gap:3},children:r.steps.map(((e,t)=>T(X,{form:u,step:e},`step-input-${t}`)))}),T(f,{sx:{mt:3},children:T(g,{variant:"contained",type:"submit",sx:N.button,children:"Submit"})})]})})}function V(o){const{who:a,token:l,start:s=!0,type:c="private",theme:d="light",instanceID:m,workflow:u,onInstanceChange:p,onError:E,onSuccess:w,onCustomResponse:A,handleCustomField:_=(()=>"")}=o,F=x("(prefers-color-scheme: dark)")?"dark":"light",O="system"===d?F:d,[S,$]=e(m),[U,B]=e(!0),[j,q]=e([]),[X,V]=e([]),[H,K]=e(!1);t((()=>!1===s?()=>{}:(B(!0),S?(async function(e){const{data:t}=await n({url:`${D}/${L.GET_CURRENT_ACTIONS_WITH_INSTANCE}/${e.instanceID}`,method:"GET",headers:{[R]:e.token}});return t.data}({instanceID:S,token:l}).then((e=>{q(Object.values(e))})).catch((e=>{n.isAxiosError(e)&&e.response?V(e.response?.data?.errors??["Something went wrong"]):V(["Something went wrong"])})).finally((()=>{B(!1)})),()=>{}):(async function(e){const{data:t}=await n({url:`${D}/${L.GET_CURRENT_ACTIONS_WITHOUT_INSTANCE}?name=${e.workflow}&type=${e.type}`,method:"GET",headers:{[R]:e.token}});return t.data}({token:l,workflow:u,type:c}).then((e=>{q(Object.values(e))})).catch((e=>{n.isAxiosError(e)&&e.response?V(e.response?.data?.errors??["Something went wrong"]):V(["Something went wrong"])})).finally((()=>{B(!1)})),()=>{}))),[s,H]),t((()=>{$(m)}),[m]),t((()=>{E&&X?.length&&E(X)}),[X]);const z=r((()=>function(e){return v({components:{MuiInputBase:{styleOverrides:{root:{backgroundColor:"dark"===e?"#353539":"white"}}},MuiButton:{styleOverrides:{root:{background:"linear-gradient(90deg, #7263E2 0%, #473C9A 105.07%)",color:"#ffffff"}}},MuiCard:{styleOverrides:{root:{backgroundColor:"dark"===e?"#353539":"white"}}}},palette:{mode:e,background:{default:"light"===e?"#F1EFFC":"#161618"},text:{primary:"light"===e?"#000":"#fff",secondary:"light"===e?"#666":"#bbb"}}},{customElements:{inputBG:"white"}})}(O)),[O]);return!1===s?T(k,{}):T(y,{theme:z,children:C(i,{sx:{backgroundColor:"var(--mstate-bg-color)",height:"100%",overflow:"auto"},children:[T(i,{sx:N.logo,children:T("a",{href:"https://mobioffice.io",children:T("img",{src:P.logo,alt:"logo",width:"100%"})})}),U?C(i,{sx:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column",p:3},children:[T(M,{}),T(h,{color:"text.primary",variant:"h3",sx:N.title,children:"Loading..."})]}):X.length?C(i,{sx:Object.assign({p:3},N.centerContent),children:[T(i,{sx:{width:"100px",m:"auto"},children:T("img",{width:"100%",src:P.error,alt:"error"})}),T(b,{children:X.filter((e=>"string"==typeof e)).map(((e,t)=>T(I,{children:T(h,{color:"error",variant:"subtitle1",sx:N.title,children:e.replace(/instanceID='.*'/i,"")})},`error ${t}`)))}),T(g,{variant:"contained",onClick:()=>{j?.length||K(!H),V([])},sx:N.button,children:"Try Again"})]}):T(i,{children:j?.length?1===j.length?T(G,{token:l,onSuccess:w,onCustomResponse:A,instanceID:S,workflow:u,config:j[0],onExecuteAction:(e,t)=>{t?.length?(V(t),e.instanceID=t[0]?.split("instanceID='")?.pop()?.replace("'","")):q(e.actions),e?.instanceID&&e?.instanceID!==S&&!t?.length&&($(e?.instanceID),p&&p(e?.instanceID))},updateCustomFieldValue:_}):T(i,{children:T(f,{sx:{flexDirection:"row",flexWrap:"wrap",justifyContent:"center"},children:j.map((e=>!e?.who?.length||e?.who?.includes(a??"")?T(W,{meta:e?.meta,name:e?.name,onClick:()=>q([e])},`action-${e.name}`):T(k,{})))})}):T(i,{className:"box_center_to_modal",children:T(h,{color:"text.primary",variant:"h3",sx:N.title,children:"Workflow Ended"})})})]})})}!function(e,t){void 0===t&&(t={});var o=t.insertAt;if("undefined"!=typeof document){var r=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css","top"===o&&r.firstChild?r.insertBefore(n,r.firstChild):r.appendChild(n),n.styleSheet?n.styleSheet.cssText=e:n.appendChild(document.createTextNode(e))}}(":root{--mstate-input:#fff;--mstate-bg-color:#f1effc}");export{V as Mstate,V as default};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("axios"),r=require("@mui/material"),o=require("react/jsx-runtime"),n=require("@mui/material/Card"),i=require("@mui/material/CardContent"),s=require("@mui/material/CardMedia"),a=require("@mui/material/Typography"),l=require("@mui/material/CardActionArea"),c=require("react-hook-form"),d=require("@mui/material/styles");const u="https://api.mstate.mobioffice.io/api",x="secret-key",p={errorMessage:{m:0,mt:1,ml:.5},label:{fontSize:14,mb:-2},textInput:{"& .MuiOutlinedInput-root":{"& fieldset":{border:"none"},borderRadius:"8px"},"& .MuiInputBase-input":{height:14},"& .MuiInputBase-multiline":{paddingBlock:"16px"}},radio:{"& fieldset":{border:"none"},borderRadius:"8px"},logo:{width:"100px",margin:"auto",maxWidth:"300px",my:2,cursor:"pointer"},title:{textAlign:"center",fontSize:{xs:"1.5rem",sm:"2rem"}},button:{borderRadius:100},centerContent:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column"}},m="https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/mstate-mobioiifce-logo.svg?alt=media&token=fa74ace6-0938-4df5-b5b7-cb93115cf7b2",h="https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/companies_logo%2FMobiOffice%20Logo.png?alt=media&token=575b9568-0b4c-468f-ba99-4b613bd4e40b",f="https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/companies_logo%2Ferror.png?alt=media&token=bb494b33-eb79-471a-abca-1031524739bc";let g=function(e){return e.EXECUTE_ACTION="workflow/v2/execute",e.GET_WORKFLOW_CONFIG="workflow/config",e.GET_CURRENT_ACTIONS_WITHOUT_INSTANCE="workflow/v2/actions",e.GET_CURRENT_ACTIONS_WITH_INSTANCE="workflow/actions",e}({}),y=function(e){return e.DATE_PICKER="datePicker",e.TIME_PICKER="timePicker",e.INPUT_FIELD="inputField",e.TEXT_AREA="textArea",e.CHECKBOX="checkBox",e.RADIO_LIST="radioList",e.DROP_DOWN="dropDown",e.SUMMARY="summary",e.CONTEXT_FIELD="CONTEXT_FIELD",e.CUSTOM_FIELD="CUSTOM_FIELD",e}({}),b=function(e){return e.CONTEXT_FIELD="CONTEXT_FIELD",e.CUSTOM_FIELD="CUSTOM_FIELD",e.USER_INPUT="USER_INPUT",e}({});function I(){return o.jsx(r.Box,{children:o.jsx(T,{})})}function T(){return o.jsxs(o.Fragment,{children:[o.jsx("svg",{width:0,height:0,children:o.jsx("defs",{children:o.jsxs("linearGradient",{id:"my_gradient",x1:"0%",y1:"0%",x2:"0%",y2:"100%",children:[o.jsx("stop",{offset:"0%",stopColor:"#e01cd5"}),o.jsx("stop",{offset:"100%",stopColor:"#1CB5E0"})]})})}),o.jsx(r.CircularProgress,{sx:{"svg circle":{stroke:"url(#my_gradient)"}}})]})}function j(e=""){return e.toUpperCase().replace(/_/gi," ")}function k(e,t){const r=t?t.split("."):[];let o=e;for(const e of r)o=o?.[e];return o}function C(e){const{name:t,meta:c,onClick:d}=e;return o.jsx(n,{sx:{maxWidth:250,width:"100%",m:2,borderRadius:5},onClick:d,children:o.jsxs(l,{children:[o.jsx(r.Box,{children:o.jsx(s,{component:"img",sx:{m:"auto",objectFit:"contain",width:"200px",borderRadius:"50%",my:2},image:c?.logo??h,alt:"action"})}),o.jsx("hr",{style:{marginInline:"10px"}}),o.jsxs(i,{children:[o.jsx(a,{color:"text.primary",gutterBottom:!0,variant:"h5",component:"div",sx:{fontSize:"1.3rem",textAlign:"center"},children:c?.title??j(t)}),o.jsx(a,{color:"text.primary",variant:"body2",sx:{color:"text.secondary",textAlign:"center"},children:c?.desc})]})]})})}function E(n){const{step:i,form:s}=n,{setValue:a,control:l,register:d,formState:{errors:u}}=s,x=function(e){return void 0!==e?.config}(i)?i?.config?.data:i,m=x.field??{},h=m.mapper,[f,g]=e.useState(m.itemList??[]);switch(e.useEffect((()=>{m.request&&async function(e,r){const{data:o}=await t(e);return k(o,r?.target??"")??[]}(m.request,h).then((e=>{g(e)})).catch((e=>{console.log(e)}))}),[]),x.fieldType){case y.INPUT_FIELD:case y.TEXT_AREA:return o.jsxs(o.Fragment,{children:[o.jsx(r.InputLabel,{sx:p.label,htmlFor:`text-${m.keyForAPI}`,children:m.label}),o.jsx(r.TextField,{multiline:x.fieldType===y.TEXT_AREA,minRows:3,id:`text-${m.keyForAPI}`,type:x.inputType,placeholder:m.placeholder,slotProps:{inputLabel:{shrink:"date"===x.inputType||void 0},formHelperText:{sx:p.errorMessage}},sx:p.textInput,defaultValue:"",...d(m.keyForAPI,{required:m?.isRequired&&`${m.label} is required`}),error:Boolean(u[m.keyForAPI]),helperText:u?.[m.keyForAPI]?.message},`input-${m.keyForAPI}`)]});case y.DROP_DOWN:case y.RADIO_LIST:return o.jsxs(o.Fragment,{children:[o.jsx(r.InputLabel,{sx:p.label,htmlFor:`dropdown-${m.keyForAPI}`,children:m.label}),o.jsxs(r.FormControl,{children:[o.jsx(r.Autocomplete,{id:`dropdown-${m.keyForAPI}`,options:f,...d(m.keyForAPI,{required:m?.isRequired&&`${m.label} is required`}),onChange:(e,t)=>{a(m.keyForAPI,t,{shouldValidate:!0})},renderInput:t=>e.createElement(r.TextField,{...t,sx:p.textInput,key:`input-${m.keyForAPI}`,placeholder:m.placeholder,error:Boolean(u[m.keyForAPI])}),getOptionLabel:e=>k(e,h?.label??"label")??"",renderOption:(t,r)=>{const o=k(r,h?.id??"id"),n=k(r,h?.label??"label");return e.createElement("li",{...t,key:o?.toString()},n?.toString())}}),Boolean(u[m.keyForAPI])?o.jsxs(r.FormHelperText,{error:!0,sx:p.errorMessage,children:[m.label," is required"]}):o.jsx(o.Fragment,{})]})]});case y.CHECKBOX:return o.jsxs(o.Fragment,{children:[o.jsx(r.InputLabel,{sx:p.label,htmlFor:`checkbox-${m.keyForAPI}`,children:m.label}),o.jsx(c.Controller,{name:m.keyForAPI,control:l,defaultValue:[],rules:{required:m?.isRequired&&`${m.label} is required`},render:({field:{value:t},fieldState:{error:n}})=>o.jsx(o.Fragment,{children:o.jsx(r.Autocomplete,{multiple:!0,id:`checkbox-${m.keyForAPI}`,options:f,value:t||[],onChange:(e,t)=>{const r=h?.id??"id",o=new Set,n=t.find((e=>{const t=k(e,r);return!!o.has(t)||(o.add(t),!1)}));if(n){const e=k(n,r);t=t.filter((t=>k(t,r)!==e))}a(m.keyForAPI,t,{shouldValidate:!0})},renderInput:e=>o.jsx(r.TextField,{...e,sx:p.textInput,placeholder:m.placeholder,slotProps:{formHelperText:{sx:p.errorMessage}},error:!!n,helperText:n?n.message:""}),getOptionLabel:e=>k(e,h?.label??"label")??"",renderOption:(n,i)=>{const s=k(i,h?.id??"id"),a=k(i,h?.label??"label"),l=t.some((e=>k(e,h?.id??"id")===s));return e.createElement("li",{...n,key:s?.toString()},o.jsx(r.Checkbox,{style:{marginRight:8},checked:l}),a?.toString())}})})})]})}return o.jsx(o.Fragment,{})}function w(n){const{config:i,onSuccess:s,onExecuteAction:a,updateCustomFieldValue:l}=n,[d,m]=e.useState(!1),h=c.useForm({defaultValues:{}}),{handleSubmit:f,setValue:y}=h;e.useEffect((()=>!1===Boolean(i?.steps?.length)?(T({}),()=>{}):(k(),()=>{})),[]);const T=async e=>{try{m(!0);const r=await async function(e){const{data:r}=await t({url:`${u}/${g.EXECUTE_ACTION}`,method:"POST",headers:{"Content-Type":"application/json",[x]:e.token},data:{instanceID:e.instanceID??"",name:e.workflow,action:e.action}});return r.data}({workflow:n.workflow,instanceID:n.instanceID,token:n.token,action:{name:i.name,payload:e}});s&&s(i.name),a(r)}catch(e){t.isAxiosError(e)&&e.response?a({},e.response?.data?.errors??["Something went wrong"]):a({},["Something went wrong"])}finally{m(!1)}},k=()=>{const e=new Set;i.steps=i?.steps?.filter((t=>{const r=t;switch(r?.module){case b.CUSTOM_FIELD:return y(r?.context?.as,l(r?.context?.key)),!0;case b.CONTEXT_FIELD:return y(r?.context?.as,r?.context?.value),!0;default:{const t=e.has(r?.field?.keyForAPI);return e.add(r?.field?.keyForAPI),!1===t}}}))};return o.jsx(r.Container,{sx:{p:2,justifyContent:"center"},children:d?o.jsxs(r.Box,{sx:Object.assign({p:3},p.centerContent),children:[o.jsx(I,{}),o.jsx(r.Typography,{color:"text.primary",variant:"h3",sx:p.title,children:"Loading..."})]}):o.jsxs("form",{onSubmit:f(T),noValidate:!0,children:[o.jsx(r.Typography,{color:"text.primary",variant:"h4",sx:p.title,children:i?.meta?.title??j(i?.name)}),o.jsx(r.Typography,{color:"text.primary",variant:"subtitle1",sx:{mb:2,textAlign:"center"},children:i?.meta?.desc??""}),o.jsx(r.Stack,{direction:"column",sx:{gap:3},children:i.steps.map(((e,t)=>o.jsx(E,{form:h,step:e},`step-input-${t}`)))}),o.jsx(r.Stack,{sx:{mt:3},children:o.jsx(r.Button,{variant:"contained",type:"submit",sx:p.button,children:"Submit"})})]})})}function F(n){const{who:i,token:s,start:a=!0,type:l="private",theme:c="light",instanceID:h,workflow:y,onInstanceChange:b,onError:T,onSuccess:j,handleCustomField:k=()=>""}=n,E=r.useMediaQuery("(prefers-color-scheme: dark)")?"dark":"light",F="system"===c?E:c,[A,S]=e.useState(h),[_,O]=e.useState(!0),[v,D]=e.useState([]),[P,N]=e.useState([]),[R,B]=e.useState(!1);e.useEffect((()=>!1===a?()=>{}:(O(!0),A?(async function(e){const{data:r}=await t({url:`${u}/${g.GET_CURRENT_ACTIONS_WITH_INSTANCE}/${e.instanceID}`,method:"GET",headers:{[x]:e.token}});return r.data}({instanceID:A,token:s}).then((e=>{D(Object.values(e))})).catch((e=>{t.isAxiosError(e)&&e.response?N(e.response?.data?.errors??["Something went wrong"]):N(["Something went wrong"])})).finally((()=>{O(!1)})),()=>{}):(async function(e){const{data:r}=await t({url:`${u}/${g.GET_CURRENT_ACTIONS_WITHOUT_INSTANCE}?name=${e.workflow}&type=${e.type}`,method:"GET",headers:{[x]:e.token}});return r.data}({token:s,workflow:y,type:l}).then((e=>{D(Object.values(e))})).catch((e=>{t.isAxiosError(e)&&e.response?N(e.response?.data?.errors??["Something went wrong"]):N(["Something went wrong"])})).finally((()=>{O(!1)})),()=>{}))),[a,R]),e.useEffect((()=>{S(h)}),[h]),e.useEffect((()=>{T&&P?.length&&T(P)}),[P]);const L=e.useMemo((()=>function(e){return d.createTheme({components:{MuiInputBase:{styleOverrides:{root:{backgroundColor:"dark"===e?"#353539":"white"}}},MuiButton:{styleOverrides:{root:{background:"linear-gradient(90deg, #7263E2 0%, #473C9A 105.07%)",color:"#ffffff"}}},MuiCard:{styleOverrides:{root:{backgroundColor:"dark"===e?"#353539":"white"}}}},palette:{mode:e,background:{default:"light"===e?"#F1EFFC":"#161618"},text:{primary:"light"===e?"#000":"#fff",secondary:"light"===e?"#666":"#bbb"}}},{customElements:{inputBG:"white"}})}(F)),[F]);return!1===a?o.jsx(o.Fragment,{}):o.jsx(r.ThemeProvider,{theme:L,children:o.jsxs(r.Box,{sx:{backgroundColor:"var(--mstate-bg-color)",height:"100%",overflow:"auto"},children:[o.jsx(r.Box,{sx:p.logo,onClick:()=>window.open("https://mobioffice.io"),children:o.jsx("img",{src:m,alt:"logo",width:"100%"})}),_?o.jsxs(r.Box,{sx:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column",p:3},children:[o.jsx(I,{}),o.jsx(r.Typography,{color:"text.primary",variant:"h3",sx:p.title,children:"Loading..."})]}):P.length?o.jsxs(r.Box,{sx:Object.assign({p:3},p.centerContent),children:[o.jsx(r.Box,{sx:{width:"100px",m:"auto"},children:o.jsx("img",{width:"100%",src:f,alt:"error"})}),o.jsx(r.List,{children:P.filter((e=>"string"==typeof e)).map(((e,t)=>o.jsx(r.ListItem,{children:o.jsx(r.Typography,{color:"error",variant:"subtitle1",sx:p.title,children:e.replace(/instanceID='.*'/i,"")})},`error ${t}`)))}),o.jsx(r.Button,{variant:"contained",onClick:()=>{v?.length||B(!R),N([])},sx:p.button,children:"Try Again"})]}):o.jsx(r.Box,{children:v?.length?1===v.length?o.jsx(w,{token:s,onSuccess:j,instanceID:A,workflow:y,config:v[0],onExecuteAction:(e,t)=>{t?.length?(N(t),e.instanceID=t[0]?.split("instanceID='")?.pop()?.replace("'","")):D(e.actions),e?.instanceID&&e?.instanceID!==A&&!t?.length&&(S(e?.instanceID),b&&b(e?.instanceID))},updateCustomFieldValue:k}):o.jsx(r.Box,{children:o.jsx(r.Stack,{sx:{flexDirection:"row",flexWrap:"wrap",justifyContent:"center"},children:v.map((e=>!e?.who?.length||e?.who?.includes(i??"")?o.jsx(C,{meta:e?.meta,name:e?.name,onClick:()=>D([e])},`action-${e.name}`):o.jsx(o.Fragment,{})))})}):o.jsx(r.Box,{className:"box_center_to_modal",children:o.jsx(r.Typography,{color:"text.primary",variant:"h3",sx:p.title,children:"Workflow Ended"})})})]})})}!function(e,t){void 0===t&&(t={});var r=t.insertAt;if("undefined"!=typeof document){var o=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css","top"===r&&o.firstChild?o.insertBefore(n,o.firstChild):o.appendChild(n),n.styleSheet?n.styleSheet.cssText=e:n.appendChild(document.createTextNode(e))}}(":root{--mstate-input:#fff;--mstate-bg-color:#f1effc}"),exports.Mstate=F,exports.default=F;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("axios"),r=require("@mui/material"),o=require("react/jsx-runtime"),n=require("@mui/material/Card"),i=require("@mui/material/CardContent"),s=require("@mui/material/CardMedia"),a=require("@mui/material/Typography"),l=require("@mui/material/CardActionArea"),c=require("react-hook-form"),d=require("@mui/material/styles");const u="https://api.mstate.mobioffice.io/api",m="secret-key",p={errorMessage:{m:0,mt:1,ml:.5},label:{fontSize:14,mb:-2},textInput:{"& .MuiOutlinedInput-root":{"& fieldset":{border:"none"},borderRadius:"8px"},"& .MuiInputBase-input":{height:14},"& .MuiInputBase-multiline":{paddingBlock:"16px"}},logo:{width:"100px",margin:"auto",maxWidth:"300px",my:2,cursor:"pointer"},title:{textAlign:"center",fontSize:{xs:"1.5rem",sm:"2rem"}},button:{borderRadius:100},centerContent:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column"}},x={logo:"https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/mstate-mobioiifce-logo.svg?alt=media&token=fa74ace6-0938-4df5-b5b7-cb93115cf7b2",icon:"https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/companies_logo%2FMobiOffice%20Logo.png?alt=media&token=575b9568-0b4c-468f-ba99-4b613bd4e40b",error:"https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/companies_logo%2Ferror.png?alt=media&token=bb494b33-eb79-471a-abca-1031524739bc"};let h=function(e){return e.EXECUTE_ACTION="workflow/v2/execute",e.GET_WORKFLOW_CONFIG="workflow/config",e.GET_CURRENT_ACTIONS_WITHOUT_INSTANCE="workflow/v2/actions",e.GET_CURRENT_ACTIONS_WITH_INSTANCE="workflow/actions",e}({}),f=function(e){return e.DATE_PICKER="datePicker",e.TIME_PICKER="timePicker",e.INPUT_FIELD="inputField",e.TEXT_AREA="textArea",e.CHECKBOX="checkBox",e.RADIO_LIST="radioList",e.DROP_DOWN="dropDown",e.SUMMARY="summary",e.CONTEXT_FIELD="CONTEXT_FIELD",e.CUSTOM_FIELD="CUSTOM_FIELD",e}({}),g=function(e){return e.CONTEXT_FIELD="CONTEXT_FIELD",e.CUSTOM_FIELD="CUSTOM_FIELD",e.USER_INPUT="USER_INPUT",e}({});function y(){return o.jsx(r.Box,{children:o.jsx(b,{})})}function b(){return o.jsxs(o.Fragment,{children:[o.jsx("svg",{width:0,height:0,children:o.jsx("defs",{children:o.jsxs("linearGradient",{id:"my_gradient",x1:"0%",y1:"0%",x2:"0%",y2:"100%",children:[o.jsx("stop",{offset:"0%",stopColor:"#e01cd5"}),o.jsx("stop",{offset:"100%",stopColor:"#1CB5E0"})]})})}),o.jsx(r.CircularProgress,{sx:{"svg circle":{stroke:"url(#my_gradient)"}}})]})}function I(e=""){return e.toUpperCase().replace(/_/gi," ")}function T(e,t){const r=t?t.split("."):[];let o=e;for(const e of r)o=o?.[e];return o}function j(e){const{name:t,meta:c,onClick:d}=e;return o.jsx(n,{sx:{maxWidth:250,width:"100%",m:2,borderRadius:5},onClick:d,children:o.jsxs(l,{children:[o.jsx(r.Box,{children:o.jsx(s,{component:"img",sx:{m:"auto",objectFit:"contain",height:"150px",width:"auto",borderRadius:"50%",my:2},image:c?.logo??x.icon,alt:"action"})}),o.jsx("hr",{style:{marginInline:"10px"}}),o.jsxs(i,{children:[o.jsx(a,{color:"text.primary",gutterBottom:!0,variant:"h5",component:"div",sx:{fontSize:"1.3rem",textAlign:"center"},children:c?.title??I(t)}),o.jsx(a,{color:"text.primary",variant:"body2",sx:{color:"text.secondary",textAlign:"center"},children:c?.desc})]})]})})}function C(n){const{step:i,form:s}=n,{setValue:a,control:l,register:d,formState:{errors:u}}=s,m=function(e){return void 0!==e?.config}(i)?i?.config?.data:i,x=m.field??{},h=x.mapper,[g,y]=e.useState(x.itemList??[]);switch(e.useEffect((()=>{x.request?.url&&async function(e,r){const{data:o}=await t(e);return T(o,r?.target??"")??[]}(x.request,h).then((e=>{y(e)})).catch((e=>{console.log(e)}))}),[]),m.fieldType){case f.INPUT_FIELD:case f.TEXT_AREA:return o.jsxs(o.Fragment,{children:[o.jsx(r.InputLabel,{sx:p.label,htmlFor:`text-${x.keyForAPI}`,children:x.label}),o.jsx(r.TextField,{multiline:m.fieldType===f.TEXT_AREA,minRows:3,id:`text-${x.keyForAPI}`,type:m.inputType,placeholder:x.placeholder,slotProps:{inputLabel:{shrink:"date"===m.inputType||void 0},formHelperText:{sx:p.errorMessage}},sx:p.textInput,defaultValue:"",...d(x.keyForAPI,{required:x?.isRequired&&`${x.label} is required`}),error:Boolean(u[x.keyForAPI]),helperText:u?.[x.keyForAPI]?.message},`input-${x.keyForAPI}`)]});case f.DROP_DOWN:case f.RADIO_LIST:return o.jsxs(o.Fragment,{children:[o.jsx(r.InputLabel,{sx:p.label,htmlFor:`dropdown-${x.keyForAPI}`,children:x.label}),o.jsxs(r.FormControl,{children:[o.jsx(r.Autocomplete,{id:`dropdown-${x.keyForAPI}`,options:g,...d(x.keyForAPI,{required:x?.isRequired&&`${x.label} is required`}),onChange:(e,t)=>{a(x.keyForAPI,t,{shouldValidate:!0})},renderInput:t=>e.createElement(r.TextField,{...t,sx:p.textInput,key:`input-${x.keyForAPI}`,placeholder:x.placeholder,error:Boolean(u[x.keyForAPI])}),getOptionLabel:e=>T(e,h?.label||"label")||"",renderOption:(t,r)=>{const o=T(r,h?.id||"id"),n=T(r,h?.label||"label");return e.createElement("li",{...t,key:o?.toString()},n?.toString())}}),Boolean(u[x.keyForAPI])?o.jsxs(r.FormHelperText,{error:!0,sx:p.errorMessage,children:[x.label," is required"]}):o.jsx(o.Fragment,{})]})]});case f.CHECKBOX:return o.jsxs(o.Fragment,{children:[o.jsx(r.InputLabel,{sx:p.label,htmlFor:`checkbox-${x.keyForAPI}`,children:x.label}),o.jsx(c.Controller,{name:x.keyForAPI,control:l,defaultValue:[],rules:{required:x?.isRequired&&`${x.label} is required`},render:({field:{value:t},fieldState:{error:n}})=>o.jsx(o.Fragment,{children:o.jsx(r.Autocomplete,{multiple:!0,id:`checkbox-${x.keyForAPI}`,options:g,value:t||[],onChange:(e,t)=>{const r=h?.id||"id",o=new Set,n=t.find((e=>{const t=T(e,r);return!!o.has(t)||(o.add(t),!1)}));if(n){const e=T(n,r);t=t.filter((t=>T(t,r)!==e))}a(x.keyForAPI,t,{shouldValidate:!0})},renderInput:e=>o.jsx(r.TextField,{...e,sx:p.textInput,placeholder:x.placeholder,slotProps:{formHelperText:{sx:p.errorMessage}},error:!!n,helperText:n?n.message:""}),getOptionLabel:e=>T(e,h?.label||"label")||"",renderOption:(n,i)=>{const s=T(i,h?.id||"id"),a=T(i,h?.label||"label"),l=t.some((e=>T(e,h?.id||"id")===s));return e.createElement("li",{...n,key:s?.toString()},o.jsx(r.Checkbox,{style:{marginRight:8},checked:l}),a?.toString())}})})})]})}return o.jsx(o.Fragment,{})}function k(n){const{config:i,onSuccess:s,onExecuteAction:a,onCustomResponse:l,updateCustomFieldValue:d}=n,[x,f]=e.useState(!1),b=c.useForm({defaultValues:{}}),{handleSubmit:T,setValue:j,reset:k}=b;e.useEffect((()=>!1===Boolean(i?.steps?.length)?(E({}),()=>{}):(w(),()=>{})),[]);const E=async e=>{try{f(!0);const r=await async function(e){const{data:r}=await t({url:`${u}/${h.EXECUTE_ACTION}`,method:"POST",headers:{"Content-Type":"application/json",[m]:e.token},data:{instanceID:e.instanceID??"",name:e.workflow,action:e.action}});return r.data}({workflow:n.workflow,instanceID:n.instanceID,token:n.token,action:{name:i.name,payload:e}});s&&s(i.name),l&&Object.keys(r?.customResponse??{}).length&&l(r?.customResponse),a(r),k()}catch(e){t.isAxiosError(e)&&e.response?a({},e.response?.data?.errors??["Something went wrong"]):a({},["Something went wrong"])}finally{f(!1)}},w=()=>{const e=new Set;i.steps=i?.steps?.filter((t=>{const r=t;switch(r?.module){case g.CUSTOM_FIELD:return j(r?.context?.as,d(r?.context?.key)),!0;case g.CONTEXT_FIELD:return j(r?.context?.as,r?.context?.value),!0;default:{const t=e.has(r?.field?.keyForAPI);return e.add(r?.field?.keyForAPI),!1===t}}}))};return o.jsx(r.Container,{sx:{p:2,justifyContent:"center"},children:x?o.jsxs(r.Box,{sx:Object.assign({p:3},p.centerContent),children:[o.jsx(y,{}),o.jsx(r.Typography,{color:"text.primary",variant:"h3",sx:p.title,children:"Loading..."})]}):o.jsxs("form",{onSubmit:T(E),noValidate:!0,children:[o.jsx(r.Typography,{color:"text.primary",variant:"h4",sx:p.title,children:i?.meta?.title??I(i?.name)}),o.jsx(r.Typography,{color:"text.primary",variant:"subtitle1",sx:{mb:2,textAlign:"center"},children:i?.meta?.desc??""}),o.jsx(r.Stack,{direction:"column",sx:{gap:3},children:i.steps.map(((e,t)=>o.jsx(C,{form:b,step:e},`step-input-${t}`)))}),o.jsx(r.Stack,{sx:{mt:3},children:o.jsx(r.Button,{variant:"contained",type:"submit",sx:p.button,children:"Submit"})})]})})}function E(n){const{who:i,token:s,start:a=!0,type:l="private",theme:c="light",instanceID:f,workflow:g,onInstanceChange:b,onError:I,onSuccess:T,onCustomResponse:C,handleCustomField:E=(()=>"")}=n,w=r.useMediaQuery("(prefers-color-scheme: dark)")?"dark":"light",F="system"===c?w:c,[A,S]=e.useState(f),[_,O]=e.useState(!0),[v,D]=e.useState([]),[P,R]=e.useState([]),[N,B]=e.useState(!1);e.useEffect((()=>!1===a?()=>{}:(O(!0),A?(async function(e){const{data:r}=await t({url:`${u}/${h.GET_CURRENT_ACTIONS_WITH_INSTANCE}/${e.instanceID}`,method:"GET",headers:{[m]:e.token}});return r.data}({instanceID:A,token:s}).then((e=>{D(Object.values(e))})).catch((e=>{t.isAxiosError(e)&&e.response?R(e.response?.data?.errors??["Something went wrong"]):R(["Something went wrong"])})).finally((()=>{O(!1)})),()=>{}):(async function(e){const{data:r}=await t({url:`${u}/${h.GET_CURRENT_ACTIONS_WITHOUT_INSTANCE}?name=${e.workflow}&type=${e.type}`,method:"GET",headers:{[m]:e.token}});return r.data}({token:s,workflow:g,type:l}).then((e=>{D(Object.values(e))})).catch((e=>{t.isAxiosError(e)&&e.response?R(e.response?.data?.errors??["Something went wrong"]):R(["Something went wrong"])})).finally((()=>{O(!1)})),()=>{}))),[a,N]),e.useEffect((()=>{S(f)}),[f]),e.useEffect((()=>{I&&P?.length&&I(P)}),[P]);const L=e.useMemo((()=>function(e){return d.createTheme({components:{MuiInputBase:{styleOverrides:{root:{backgroundColor:"dark"===e?"#353539":"white"}}},MuiButton:{styleOverrides:{root:{background:"linear-gradient(90deg, #7263E2 0%, #473C9A 105.07%)",color:"#ffffff"}}},MuiCard:{styleOverrides:{root:{backgroundColor:"dark"===e?"#353539":"white"}}}},palette:{mode:e,background:{default:"light"===e?"#F1EFFC":"#161618"},text:{primary:"light"===e?"#000":"#fff",secondary:"light"===e?"#666":"#bbb"}}},{customElements:{inputBG:"white"}})}(F)),[F]);return!1===a?o.jsx(o.Fragment,{}):o.jsx(r.ThemeProvider,{theme:L,children:o.jsxs(r.Box,{sx:{backgroundColor:"var(--mstate-bg-color)",height:"100%",overflow:"auto"},children:[o.jsx(r.Box,{sx:p.logo,children:o.jsx("a",{href:"https://mobioffice.io",children:o.jsx("img",{src:x.logo,alt:"logo",width:"100%"})})}),_?o.jsxs(r.Box,{sx:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column",p:3},children:[o.jsx(y,{}),o.jsx(r.Typography,{color:"text.primary",variant:"h3",sx:p.title,children:"Loading..."})]}):P.length?o.jsxs(r.Box,{sx:Object.assign({p:3},p.centerContent),children:[o.jsx(r.Box,{sx:{width:"100px",m:"auto"},children:o.jsx("img",{width:"100%",src:x.error,alt:"error"})}),o.jsx(r.List,{children:P.filter((e=>"string"==typeof e)).map(((e,t)=>o.jsx(r.ListItem,{children:o.jsx(r.Typography,{color:"error",variant:"subtitle1",sx:p.title,children:e.replace(/instanceID='.*'/i,"")})},`error ${t}`)))}),o.jsx(r.Button,{variant:"contained",onClick:()=>{v?.length||B(!N),R([])},sx:p.button,children:"Try Again"})]}):o.jsx(r.Box,{children:v?.length?1===v.length?o.jsx(k,{token:s,onSuccess:T,onCustomResponse:C,instanceID:A,workflow:g,config:v[0],onExecuteAction:(e,t)=>{t?.length?(R(t),e.instanceID=t[0]?.split("instanceID='")?.pop()?.replace("'","")):D(e.actions),e?.instanceID&&e?.instanceID!==A&&!t?.length&&(S(e?.instanceID),b&&b(e?.instanceID))},updateCustomFieldValue:E}):o.jsx(r.Box,{children:o.jsx(r.Stack,{sx:{flexDirection:"row",flexWrap:"wrap",justifyContent:"center"},children:v.map((e=>!e?.who?.length||e?.who?.includes(i??"")?o.jsx(j,{meta:e?.meta,name:e?.name,onClick:()=>D([e])},`action-${e.name}`):o.jsx(o.Fragment,{})))})}):o.jsx(r.Box,{className:"box_center_to_modal",children:o.jsx(r.Typography,{color:"text.primary",variant:"h3",sx:p.title,children:"Workflow Ended"})})})]})})}!function(e,t){void 0===t&&(t={});var r=t.insertAt;if("undefined"!=typeof document){var o=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css","top"===r&&o.firstChild?o.insertBefore(n,o.firstChild):o.appendChild(n),n.styleSheet?n.styleSheet.cssText=e:n.appendChild(document.createTextNode(e))}}(":root{--mstate-input:#fff;--mstate-bg-color:#f1effc}"),exports.Mstate=E,exports.default=E;
@@ -45,6 +45,12 @@ interface Props {
45
45
  * @returns {void}.
46
46
  */
47
47
  onError?: (errors: unknown[]) => void;
48
+ /**
49
+ * Triggers when Custom response is received by mstate on action perform.
50
+ * @param {any} data - data which is send by mstate by performing CUSTOM_RESPONSE step.
51
+ * @returns {void}.
52
+ */
53
+ onCustomResponse?: (data: any) => void;
48
54
  /**
49
55
  * Handles your steps which contain Custom field module.
50
56
  * @param {string} key - Which key you expect for getting the value.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mstate-react",
3
- "version": "1.5.3",
3
+ "version": "1.5.5",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -15,6 +15,8 @@
15
15
  "scripts": {
16
16
  "prod": "npm run build",
17
17
  "dev": "rollup -c --watch",
18
+ "link": "npm link",
19
+ "publish": "npm run build && npm publish",
18
20
  "build": "rollup -c"
19
21
  },
20
22
  "peerDependencies": {