mstate-react 1.5.8 → 1.6.0
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 +1 -1
- package/dist/index.js +1 -1
- package/dist/types/index.d.ts +0 -0
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
@@ -1 +1 @@
|
|
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 s,Autocomplete as l,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 I,ListItem as b}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}({}),U=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}({}),$=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??{},I=y.mapper,[b,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,I).then((e=>{E(e)})).catch((e=>{console.log(e)}))}),[]),x.fieldType){case U.INPUT_FIELD:case U.TEXT_AREA:return C(k,{children:[T(s,{sx:N.label,htmlFor:`text-${y.keyForAPI}`,children:y.label}),T(d,{multiline:x.fieldType===U.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 U.DROP_DOWN:case U.RADIO_LIST:return C(k,{children:[T(s,{sx:N.label,htmlFor:`dropdown-${y.keyForAPI}`,children:y.label}),C(m,{children:[T(l,{id:`dropdown-${y.keyForAPI}`,options:b,...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,I?.label??"")||"",renderOption:(e,t)=>{const r=q(t,I?.id??""),n=q(t,I?.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 U.CHECKBOX:return C(k,{children:[T(s,{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(l,{multiple:!0,id:`checkbox-${y.keyForAPI}`,options:b,value:e||[],onChange:(e,t)=>{const o=I?.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,I?.label??"")||"",renderOption:(t,r)=>{const n=q(r,I?.id??""),i=q(r,I?.label??""),a=e.some((e=>q(e,I?.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:s,onCustomResponse:l,updateCustomFieldValue:c}=o,[d,m]=e(!1),[u,x]=e([]),y=S({defaultValues:{}}),{handleSubmit:I,setValue:b,reset:k}=y;t((()=>!1===Boolean(r?.steps?.length)?(E({}),()=>{}):(w(),()=>{})),[]),t((()=>{!1===r.steps.some((e=>e.module===$.USER_INPUT))&&E({})}),[u]);const E=async e=>{try{m(!0);let t={};if(c)for(const{key:e,as:o}of u)t[o]=c(e);const i=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:Object.assign(e??{},t)}});a&&a(r.name),l&&Object.keys(i?.customResponse??{}).length&&l(i?.customResponse),x([]),s(i),k()}catch(e){n.isAxiosError(e)&&e.response?s({},e.response?.data?.errors??["Something went wrong"]):s({},["Something went wrong"])}finally{m(!1)}},w=()=>{const e=new Set;r.steps=r?.steps?.filter((t=>{const o=t;switch(o?.module){case $.CUSTOM_FIELD:return x(u.concat(o?.context)),!0;case $.CONTEXT_FIELD:return b(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:I(E),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:y,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:s,start:l=!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,U]=e(m),[$,B]=e(!0),[j,q]=e([]),[X,V]=e([]),[H,K]=e(!1);t((()=>!1===l?()=>{}:(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:s}).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:s,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)})),()=>{}))),[l,H]),t((()=>{U(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===l?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%"})})}),$?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(I,{children:X.filter((e=>"string"==typeof e)).map(((e,t)=>T(b,{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:s,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&&(U(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};
|
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 s,Autocomplete as l,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 I,ListItem as b}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 D}from"@mui/material/styles";const v="https://api.mstate.mobioffice.io/api",R="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"}},N={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}({}),M=function(e){return e.CONTEXT_FIELD="CONTEXT_FIELD",e.CUSTOM_FIELD="CUSTOM_FIELD",e.USER_INPUT="USER_INPUT",e}({});function U(){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??N.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?.displayName??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??{},I=y.mapper,[b,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,I).then((e=>{E(e)})).catch((e=>{console.log(e)}))}),[]),x.fieldType){case $.INPUT_FIELD:case $.TEXT_AREA:case $.DATE_PICKER:case $.TIME_PICKER:return C(k,{children:[T(s,{sx:P.label,htmlFor:`text-${y.keyForAPI}`,children:y.label}),T(d,{multiline:x.fieldType===$.TEXT_AREA,minRows:3,id:`text-${y.keyForAPI}`,type:x.fieldType===$.TIME_PICKER?"time":x.inputType,placeholder:y.placeholder,slotProps:{inputLabel:{shrink:"date"===x.inputType||void 0},formHelperText:{sx:P.errorMessage}},sx:P.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(s,{sx:P.label,htmlFor:`dropdown-${y.keyForAPI}`,children:y.label}),C(m,{children:[T(l,{id:`dropdown-${y.keyForAPI}`,options:b,...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:P.textInput,key:`input-${y.keyForAPI}`,placeholder:y.placeholder,error:Boolean(g[y.keyForAPI])}),getOptionLabel:e=>q(e,I?.label??"")||"",renderOption:(e,t)=>{const r=q(t,I?.id??""),n=q(t,I?.label??"");return o("li",{...e,key:r?.toString()},n?.toString())}}),Boolean(g[y.keyForAPI])?C(u,{error:!0,sx:P.errorMessage,children:[y.label," is required"]}):T(k,{})]})]});case $.CHECKBOX:return C(k,{children:[T(s,{sx:P.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(l,{multiple:!0,id:`checkbox-${y.keyForAPI}`,options:b,value:e||[],onChange:(e,t)=>{const o=I?.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:P.textInput,placeholder:y.placeholder,slotProps:{formHelperText:{sx:P.errorMessage}},error:!!t,helperText:t?t.message:""}),getOptionLabel:e=>q(e,I?.label??"")||"",renderOption:(t,r)=>{const n=q(r,I?.id??""),i=q(r,I?.label??""),a=e.some((e=>q(e,I?.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:s,onCustomResponse:l,updateCustomFieldValue:c}=o,[d,m]=e(!1),[u,x]=e([]),y=S({defaultValues:{}}),{handleSubmit:I,setValue:b,reset:k}=y;t((()=>!1===Boolean(r?.steps?.length)?(E({}),()=>{}):(w(),()=>{})),[]);const E=async e=>{try{m(!0);let t={};if(c)for(const{key:e,as:o}of u)t[o]=c(e);const i=await async function(e){const{data:t}=await n({url:`${v}/${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:Object.assign(e??{},t)}});a&&a(r.name),l&&Object.keys(i?.customResponse??{}).length&&l(i?.customResponse),x([]),s(i),k()}catch(e){n.isAxiosError(e)&&e.response?s({},e.response?.data?.errors??["Something went wrong"]):s({},["Something went wrong"])}finally{m(!1)}},w=()=>{const e=new Set;r.steps=r?.steps?.filter((t=>{const o=t;switch(o?.module){case M.CUSTOM_FIELD:return x(u.concat(o?.context)),!0;case M.CONTEXT_FIELD:return b(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},P.centerContent),children:[T(U,{}),T(h,{color:"text.primary",variant:"h3",sx:P.title,children:"Loading..."})]}):C("form",{onSubmit:I(E),noValidate:!0,children:[T(h,{color:"text.primary",variant:"h4",sx:P.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:y,step:e},`step-input-${t}`)))}),T(f,{sx:{mt:3},children:T(g,{variant:"contained",type:"submit",sx:P.button,children:"Submit"})})]})})}function V(o){const{who:a,token:s,start:l=!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),[M,B]=e(!0),[j,q]=e([]),[X,V]=e([]),[H,K]=e(!1);t((()=>!1===l?()=>{}:(B(!0),S?(async function(e){const{data:t}=await n({url:`${v}/${L.GET_CURRENT_ACTIONS_WITH_INSTANCE}/${e.instanceID}`,method:"GET",headers:{[R]:e.token}});return t.data}({instanceID:S,token:s}).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:`${v}/${L.GET_CURRENT_ACTIONS_WITHOUT_INSTANCE}?name=${e.workflow}&type=${e.type}`,method:"GET",headers:{[R]:e.token}});return t.data}({token:s,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)})),()=>{}))),[l,H]),t((()=>{$(m)}),[m]),t((()=>{E&&X?.length&&E(X)}),[X]);const z=r((()=>function(e){return D({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===l?T(k,{}):T(y,{theme:z,children:C(i,{sx:{backgroundColor:"var(--mstate-bg-color)",height:"100%",overflow:"auto"},children:[T(i,{sx:P.logo,children:T("a",{href:"https://mobioffice.io",children:T("img",{src:N.logo,alt:"logo",width:"100%"})})}),M?C(i,{sx:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column",p:3},children:[T(U,{}),T(h,{color:"text.primary",variant:"h3",sx:P.title,children:"Loading..."})]}):X.length?C(i,{sx:Object.assign({p:3},P.centerContent),children:[T(i,{sx:{width:"100px",m:"auto"},children:T("img",{width:"100%",src:N.error,alt:"error"})}),T(I,{children:X.filter((e=>"string"==typeof e)).map(((e,t)=>T(b,{children:T(h,{color:"error",variant:"subtitle1",sx:P.title,children:e.replace(/instanceID='.*'/i,"")})},`error ${t}`)))}),T(g,{variant:"contained",onClick:()=>{j?.length||K(!H),V([])},sx:P.button,children:"Try Again"})]}):T(i,{children:j?.length?1===j.length?T(G,{token:s,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&&($(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:P.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"),s=require("@mui/material/CardContent"),i=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(I,{})})}function I(){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 b(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(i,{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(s,{children:[o.jsx(a,{color:"text.primary",gutterBottom:!0,variant:"h5",component:"div",sx:{fontSize:"1.3rem",textAlign:"center"},children:c?.title??b(t)}),o.jsx(a,{color:"text.primary",variant:"body2",sx:{color:"text.secondary",textAlign:"center"},children:c?.desc})]})]})})}function C(n){const{step:s,form:i}=n,{setValue:a,control:l,register:d,formState:{errors:u}}=i,m=function(e){return void 0!==e?.config}(s)?s?.config?.data:s,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??"")||"",renderOption:(t,r)=>{const o=T(r,h?.id??""),n=T(r,h?.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??"",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??"")||"",renderOption:(n,s)=>{const i=T(s,h?.id??""),a=T(s,h?.label??""),l=t.some((e=>T(e,h?.id??"")===i));return e.createElement("li",{...n,key:i?.toString()},o.jsx(r.Checkbox,{style:{marginRight:8},checked:l}),a?.toString())}})})})]})}return o.jsx(o.Fragment,{})}function k(n){const{config:s,onSuccess:i,onExecuteAction:a,onCustomResponse:l,updateCustomFieldValue:d}=n,[x,f]=e.useState(!1),[I,T]=e.useState([]),j=c.useForm({defaultValues:{}}),{handleSubmit:k,setValue:E,reset:w}=j;e.useEffect((()=>!1===Boolean(s?.steps?.length)?(F({}),()=>{}):(A(),()=>{})),[]),e.useEffect((()=>{!1===s.steps.some((e=>e.module===g.USER_INPUT))&&F({})}),[I]);const F=async e=>{try{f(!0);let r={};if(d)for(const{key:e,as:t}of I)r[t]=d(e);const o=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:s.name,payload:Object.assign(e??{},r)}});i&&i(s.name),l&&Object.keys(o?.customResponse??{}).length&&l(o?.customResponse),T([]),a(o),w()}catch(e){t.isAxiosError(e)&&e.response?a({},e.response?.data?.errors??["Something went wrong"]):a({},["Something went wrong"])}finally{f(!1)}},A=()=>{const e=new Set;s.steps=s?.steps?.filter((t=>{const r=t;switch(r?.module){case g.CUSTOM_FIELD:return T(I.concat(r?.context)),!0;case g.CONTEXT_FIELD:return E(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:k(F),noValidate:!0,children:[o.jsx(r.Typography,{color:"text.primary",variant:"h4",sx:p.title,children:s?.meta?.title??b(s?.name)}),o.jsx(r.Typography,{color:"text.primary",variant:"subtitle1",sx:{mb:2,textAlign:"center"},children:s?.meta?.desc??""}),o.jsx(r.Stack,{direction:"column",sx:{gap:3},children:s.steps.map(((e,t)=>o.jsx(C,{form:j,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:s,token:i,start:a=!0,type:l="private",theme:c="light",instanceID:f,workflow:g,onInstanceChange:I,onError:b,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:i}).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:i,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((()=>{b&&P?.length&&b(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:i,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&&(S(e?.instanceID),I&&I(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(s??"")?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;
|
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"),s=require("@mui/material/CardContent"),i=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(I,{})})}function I(){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 b(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(i,{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(s,{children:[o.jsx(a,{color:"text.primary",gutterBottom:!0,variant:"h5",component:"div",sx:{fontSize:"1.3rem",textAlign:"center"},children:c?.displayName??c?.title??b(t)}),o.jsx(a,{color:"text.primary",variant:"body2",sx:{color:"text.secondary",textAlign:"center"},children:c?.desc})]})]})})}function C(n){const{step:s,form:i}=n,{setValue:a,control:l,register:d,formState:{errors:u}}=i,m=function(e){return void 0!==e?.config}(s)?s?.config?.data:s,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:case f.DATE_PICKER:case f.TIME_PICKER: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.fieldType===f.TIME_PICKER?"time":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??"")||"",renderOption:(t,r)=>{const o=T(r,h?.id??""),n=T(r,h?.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??"",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??"")||"",renderOption:(n,s)=>{const i=T(s,h?.id??""),a=T(s,h?.label??""),l=t.some((e=>T(e,h?.id??"")===i));return e.createElement("li",{...n,key:i?.toString()},o.jsx(r.Checkbox,{style:{marginRight:8},checked:l}),a?.toString())}})})})]})}return o.jsx(o.Fragment,{})}function E(n){const{config:s,onSuccess:i,onExecuteAction:a,onCustomResponse:l,updateCustomFieldValue:d}=n,[x,f]=e.useState(!1),[I,T]=e.useState([]),j=c.useForm({defaultValues:{}}),{handleSubmit:E,setValue:k,reset:w}=j;e.useEffect((()=>!1===Boolean(s?.steps?.length)?(F({}),()=>{}):(A(),()=>{})),[]);const F=async e=>{try{f(!0);let r={};if(d)for(const{key:e,as:t}of I)r[t]=d(e);const o=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:s.name,payload:Object.assign(e??{},r)}});i&&i(s.name),l&&Object.keys(o?.customResponse??{}).length&&l(o?.customResponse),T([]),a(o),w()}catch(e){t.isAxiosError(e)&&e.response?a({},e.response?.data?.errors??["Something went wrong"]):a({},["Something went wrong"])}finally{f(!1)}},A=()=>{const e=new Set;s.steps=s?.steps?.filter((t=>{const r=t;switch(r?.module){case g.CUSTOM_FIELD:return T(I.concat(r?.context)),!0;case g.CONTEXT_FIELD:return k(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:E(F),noValidate:!0,children:[o.jsx(r.Typography,{color:"text.primary",variant:"h4",sx:p.title,children:s?.meta?.title??b(s?.name)}),o.jsx(r.Typography,{color:"text.primary",variant:"subtitle1",sx:{mb:2,textAlign:"center"},children:s?.meta?.desc??""}),o.jsx(r.Stack,{direction:"column",sx:{gap:3},children:s.steps.map(((e,t)=>o.jsx(C,{form:j,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 k(n){const{who:s,token:i,start:a=!0,type:l="private",theme:c="light",instanceID:f,workflow:g,onInstanceChange:I,onError:b,onSuccess:T,onCustomResponse:C,handleCustomField:k=(()=>"")}=n,w=r.useMediaQuery("(prefers-color-scheme: dark)")?"dark":"light",F="system"===c?w:c,[A,_]=e.useState(f),[S,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:i}).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:i,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((()=>{_(f)}),[f]),e.useEffect((()=>{b&&P?.length&&b(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%"})})}),S?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(E,{token:i,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&&(_(e?.instanceID),I&&I(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(s??"")?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=k,exports.default=k;
|
package/dist/types/index.d.ts
CHANGED
File without changes
|