mstate-react 1.5.2 → 1.5.4

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/README.md CHANGED
@@ -1,38 +1,38 @@
1
- MState is a library designed to automate workflows by managing state transitions and actions efficiently. It enables developers to create state machines, which are useful for defining and controlling the various states an object or process can pass through, especially in complex workflows.
2
-
3
- ```ts
4
- interface MStateProps {
5
- token: string;
6
- workflow: string;
7
- theme?: 'dark' | 'light' | 'system'; // default system
8
- start?: boolean; // default true
9
- who?: string;
10
- instanceID?: string;
11
- onInstanceChange?: (value: string) => void;
12
- updateCustomFieldValue?: (key: string) => unknown;
13
- }
14
- ```
15
-
16
- Once the package is installed, you can import the library using import or require approach:
17
-
18
- ```ts
19
- import { Mstate } from 'mstate-react';
20
- ```
21
-
22
- You can also use the default export, since the named export is just a re-export from the Axios factory:
23
-
24
- ```ts
25
- import Mstate from 'mstate-react';
26
- ```
27
-
28
- ## Example
29
-
30
- ```ts
31
- import Mstate from 'mstate-react';
32
-
33
- <Mstate
34
- token="your-secret-key"
35
- instanceID="instance-id"
36
- workflow="workflow-name-with-path"
37
- />;
38
- ```
1
+ MState is a library designed to automate workflows by managing state transitions and actions efficiently. It enables developers to create state machines, which are useful for defining and controlling the various states an object or process can pass through, especially in complex workflows.
2
+
3
+ ```ts
4
+ interface MStateProps {
5
+ token: string;
6
+ workflow: string;
7
+ theme?: 'dark' | 'light' | 'system'; // default system
8
+ start?: boolean; // default true
9
+ who?: string;
10
+ instanceID?: string;
11
+ onInstanceChange?: (value: string) => void;
12
+ updateCustomFieldValue?: (key: string) => unknown;
13
+ }
14
+ ```
15
+
16
+ Once the package is installed, you can import the library using import or require approach:
17
+
18
+ ```ts
19
+ import { Mstate } from 'mstate-react';
20
+ ```
21
+
22
+ You can also use the default export, since the named export is just a re-export from the Axios factory:
23
+
24
+ ```ts
25
+ import Mstate from 'mstate-react';
26
+ ```
27
+
28
+ ## Example
29
+
30
+ ```ts
31
+ import Mstate from 'mstate-react';
32
+
33
+ <Mstate
34
+ token="your-secret-key"
35
+ instanceID="instance-id"
36
+ workflow="workflow-name-with-path"
37
+ />;
38
+ ```
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 I,List as b,ListItem as y}from"@mui/material";import{jsx as k,jsxs as T,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 O,useForm as S}from"react-hook-form";import{createTheme as D}from"@mui/material/styles";const P="https://api.mstate.mobioffice.io/api",v="secret-key",N={errorMessage:{m:0,mt:1,ml:.5},label:{fontSize:14,mb:-2},textInput:{"& .MuiOutlinedInput-root":{"& fieldset":{border:"none"},borderRadius:"8px"}},radio:{"& fieldset":{border:"none"},borderRadius:"8px"},logo:{width:"70%",margin:"auto",maxWidth:"300px",my:2},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/companies_logo%2Flogo.png?alt=media&token=e5f238f0-c373-4c76-9168-37fbe7da1de0",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}({}),j=function(e){return e.CONTEXT_FIELD="CONTEXT_FIELD",e.CUSTOM_FIELD="CUSTOM_FIELD",e.USER_INPUT="USER_INPUT",e}({});function q(){return k(i,{children:k(B,{})})}function B(){return T(C,{children:[k("svg",{width:0,height:0,children:k("defs",{children:T("linearGradient",{id:"my_gradient",x1:"0%",y1:"0%",x2:"0%",y2:"100%",children:[k("stop",{offset:"0%",stopColor:"#e01cd5"}),k("stop",{offset:"100%",stopColor:"#1CB5E0"})]})})}),k(a,{sx:{"svg circle":{stroke:"url(#my_gradient)"}}})]})}function W(e=""){return e.toUpperCase().replace(/_/gi," ")}function X(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 k(w,{sx:{maxWidth:250,width:"100%",m:2,borderRadius:5},onClick:o,children:T(F,{children:[k(i,{children:k(A,{component:"img",sx:{m:"auto",objectFit:"contain",width:"200px",borderRadius:"50%",my:2},image:r?.logo??L,alt:"action"})}),k("hr",{style:{marginInline:"10px"}}),T(E,{children:[k(_,{color:"text.primary",gutterBottom:!0,variant:"h5",component:"div",sx:{fontSize:"1.3rem",textAlign:"center"},children:r?.title??W(t)}),k(_,{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,I=x.field??{},b=I.mapper,[y,w]=e(I.itemList??[]);switch(t((()=>{I.request&&async function(e,t){const{data:r}=await n(e);return X(r,t?.target??"")??[]}(I.request,b).then((e=>{w(e)})).catch((e=>{console.log(e)}))}),[]),x.fieldType){case M.INPUT_FIELD:case M.TEXT_AREA:return T(C,{children:[k(l,{sx:N.label,htmlFor:`text-${I.keyForAPI}`,children:I.label}),k(c,{multiline:x.fieldType===M.TEXT_AREA,minRows:3,id:`text-${I.keyForAPI}`,type:x.inputType,placeholder:I.placeholder,slotProps:{inputLabel:{shrink:"date"===x.inputType||void 0},formHelperText:{sx:N.errorMessage}},sx:N.textInput,defaultValue:"",...f(I.keyForAPI,{required:I?.isRequired&&`${I.label} is required`}),error:Boolean(g[I.keyForAPI]),helperText:g?.[I.keyForAPI]?.message},`input-${I.keyForAPI}`)]});case M.DROP_DOWN:case M.RADIO_LIST:return T(C,{children:[k(l,{sx:N.label,htmlFor:`dropdown-${I.keyForAPI}`,children:I.label}),T(m,{children:[k(s,{id:`dropdown-${I.keyForAPI}`,options:y,...f(I.keyForAPI,{required:I?.isRequired&&`${I.label} is required`}),onChange:(e,t)=>{p(I.keyForAPI,t,{shouldValidate:!0})},renderInput:e=>r(c,{...e,sx:N.textInput,key:`input-${I.keyForAPI}`,placeholder:I.placeholder,error:Boolean(g[I.keyForAPI])}),getOptionLabel:e=>X(e,b?.label??"label")??"",renderOption:(e,t)=>{const o=X(t,b?.id??"id"),n=X(t,b?.label??"label");return r("li",{...e,key:o?.toString()},n?.toString())}}),Boolean(g[I.keyForAPI])?T(u,{error:!0,sx:N.errorMessage,children:[I.label," is required"]}):k(C,{})]})]});case M.CHECKBOX:return T(C,{children:[k(l,{sx:N.label,htmlFor:`checkbox-${I.keyForAPI}`,children:I.label}),k(O,{name:I.keyForAPI,control:h,defaultValue:[],rules:{required:I?.isRequired&&`${I.label} is required`},render:({field:{value:e},fieldState:{error:t}})=>k(C,{children:k(s,{multiple:!0,id:`checkbox-${I.keyForAPI}`,options:y,value:e||[],onChange:(e,t)=>{const r=b?.id??"id",o=new Set,n=t.find((e=>{const t=X(e,r);return!!o.has(t)||(o.add(t),!1)}));if(n){const e=X(n,r);t=t.filter((t=>X(t,r)!==e))}p(I.keyForAPI,t,{shouldValidate:!0})},renderInput:e=>k(c,{...e,sx:N.textInput,placeholder:I.placeholder,slotProps:{formHelperText:{sx:N.errorMessage}},error:!!t,helperText:t?t.message:""}),getOptionLabel:e=>X(e,b?.label??"label")??"",renderOption:(t,o)=>{const n=X(o,b?.id??"id"),i=X(o,b?.label??"label"),a=e.some((e=>X(e,b?.id??"id")===n));return r("li",{...t,key:n?.toString()},k(d,{style:{marginRight:8},checked:a}),i?.toString())}})})})]})}return k(C,{})}function H(r){const{config:o,onSuccess:a,onExecuteAction:l,updateCustomFieldValue:s}=r,[c,d]=e(!1),m=S({defaultValues:{}}),{handleSubmit:u,setValue:x}=m;t((()=>!1===Boolean(o?.steps?.length)?(I({}),()=>{}):(b(),()=>{})),[]);const I=async e=>{try{d(!0);const t=await async function(e){const{data:t}=await n({url:`${P}/${U.EXECUTE_ACTION}`,method:"POST",headers:{"Content-Type":"application/json",[v]: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)}},b=()=>{const e=new Set;o.steps=o?.steps?.filter((t=>{const r=t;switch(r?.module){case j.CUSTOM_FIELD:return x(r?.context?.as,s(r?.context?.key)),!0;case j.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 k(p,{sx:{p:2,justifyContent:"center"},children:c?T(i,{sx:Object.assign({p:3},N.centerContent),children:[k(q,{}),k(h,{color:"text.primary",variant:"h3",sx:N.title,children:"Loading..."})]}):T("form",{onSubmit:u(I),noValidate:!0,children:[k(h,{color:"text.primary",variant:"h4",sx:Object.assign({mb:2},N.title),children:o?.meta?.title??W(o?.name)}),k(f,{direction:"column",sx:{gap:3},children:o.steps.map(((e,t)=>k(V,{form:m,step:e},`step-input-${t}`)))}),k(f,{sx:{mt:3},children:k(g,{variant:"contained",type:"submit",sx:N.button,children:"Submit"})})]})})}function K(r){const{who:a,token:l,start:s=!0,theme:c="light",instanceID:d,workflow:m,onInstanceChange:u,onError:p,onSuccess:w,handleCustomField:E=()=>""}=r,A=x("(prefers-color-scheme: dark)")?"dark":"light",_="system"===c?A:c,[F,O]=e(d),[S,L]=e(!0),[M,j]=e([]),[B,W]=e([]),[X,V]=e(!1);t((()=>!1===s?()=>{}:(L(!0),F?(async function(e){const{data:t}=await n({url:`${P}/${U.GET_CURRENT_ACTIONS_WITH_INSTANCE}/${e.instanceID}`,method:"GET",headers:{[v]:e.token}});return t.data}({instanceID:F,token:l}).then((e=>{j(Object.values(e))})).catch((e=>{n.isAxiosError(e)&&e.response?W(e.response?.data?.errors??["Something went wrong"]):W(["Something went wrong"])})).finally((()=>{L(!1)})),()=>{}):(async function(e){const{data:t}=await n({url:`${P}/${U.GET_CURRENT_ACTIONS_WITHOUT_INSTANCE}?name=${e.workflow}`,method:"GET",headers:{[v]:e.token}});return t.data}({token:l,workflow:m}).then((e=>{j(Object.values(e))})).catch((e=>{n.isAxiosError(e)&&e.response?W(e.response?.data?.errors??["Something went wrong"]):W(["Something went wrong"])})).finally((()=>{L(!1)})),()=>{}))),[s,X]),t((()=>{O(d)}),[d]),t((()=>{p&&B?.length&&p(B)}),[B]);const K=o((()=>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"}})}(_)),[_]);return!1===s?k(C,{}):k(I,{theme:K,children:T(i,{sx:{backgroundColor:"background.default",height:"100%",overflow:"auto"},children:[k(i,{sx:N.logo,children:k("img",{src:R,alt:"logo",width:"100%"})}),k("hr",{style:{marginInline:"16px"}}),S?T(i,{sx:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column",p:3},children:[k(q,{}),k(h,{color:"text.primary",variant:"h3",sx:N.title,children:"Loading..."})]}):B.length?T(i,{sx:Object.assign({p:3},N.centerContent),children:[k(i,{sx:{width:"100px",m:"auto"},children:k("img",{width:"100%",src:$,alt:"error"})}),k(b,{children:B.filter((e=>"string"==typeof e)).map(((e,t)=>k(y,{children:k(h,{color:"error",variant:"subtitle1",sx:N.title,children:e.replace(/instanceID='.*'/i,"")})},`error ${t}`)))}),k(g,{variant:"contained",onClick:()=>{M?.length||V(!X),W([])},sx:N.button,children:"Try Again"})]}):k(i,{children:M?.length?1===M.length?k(H,{token:l,onSuccess:w,instanceID:F,workflow:m,config:M[0],onExecuteAction:(e,t)=>{t?.length?(W(t),e.instanceID=t[0]?.split("instanceID='")?.pop()?.replace("'","")):j(e.actions),e?.instanceID&&e?.instanceID!==F&&!t?.length&&(O(e?.instanceID),u&&u(e?.instanceID))},updateCustomFieldValue:E}):k(i,{children:k(f,{sx:{flexDirection:"row",flexWrap:"wrap",justifyContent:"center"},children:M.map((e=>!e?.who?.length||e?.who?.includes(a??"")?k(G,{meta:e?.meta,name:e?.name,onClick:()=>j([e])},`action-${e.name}`):k(C,{})))})}):k(i,{className:"box_center_to_modal",children:k(h,{color:"text.primary",variant:"h3",sx:N.title,children:"Workflow Ended"})})})]})})}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,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 C,jsxs as T,Fragment as k}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 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"}},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"}},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 C(i,{children:C(B,{})})}function B(){return T(k,{children:[C("svg",{width:0,height:0,children:C("defs",{children:T("linearGradient",{id:"my_gradient",x1:"0%",y1:"0%",x2:"0%",y2:"100%",children:[C("stop",{offset:"0%",stopColor:"#e01cd5"}),C("stop",{offset:"100%",stopColor:"#1CB5E0"})]})})}),C(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 C(w,{sx:{maxWidth:250,width:"100%",m:2,borderRadius:5},onClick:r,children:T(F,{children:[C(i,{children:C(A,{component:"img",sx:{m:"auto",objectFit:"contain",height:"150px",width:"auto",borderRadius:"50%",my:2},image:o?.logo??P.icon,alt:"action"})}),C("hr",{style:{marginInline:"10px"}}),T(E,{children:[C(_,{color:"text.primary",gutterBottom:!0,variant:"h5",component:"div",sx:{fontSize:"1.3rem",textAlign:"center"},children:o?.title??j(t)}),C(_,{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,b=x.field??{},y=b.mapper,[I,w]=e(b.itemList??[]);switch(t((()=>{b.request&&async function(e,t){const{data:o}=await n(e);return q(o,t?.target??"")??[]}(b.request,y).then((e=>{w(e)})).catch((e=>{console.log(e)}))}),[]),x.fieldType){case $.INPUT_FIELD:case $.TEXT_AREA:return T(k,{children:[C(l,{sx:N.label,htmlFor:`text-${b.keyForAPI}`,children:b.label}),C(c,{multiline:x.fieldType===$.TEXT_AREA,minRows:3,id:`text-${b.keyForAPI}`,type:x.inputType,placeholder:b.placeholder,slotProps:{inputLabel:{shrink:"date"===x.inputType||void 0},formHelperText:{sx:N.errorMessage}},sx:N.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 $.DROP_DOWN:case $.RADIO_LIST:return T(k,{children:[C(l,{sx:N.label,htmlFor:`dropdown-${b.keyForAPI}`,children:b.label}),T(m,{children:[C(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=>o(c,{...e,sx:N.textInput,key:`input-${b.keyForAPI}`,placeholder:b.placeholder,error:Boolean(g[b.keyForAPI])}),getOptionLabel:e=>q(e,y?.label??"label")??"",renderOption:(e,t)=>{const r=q(t,y?.id??"id"),n=q(t,y?.label??"label");return o("li",{...e,key:r?.toString()},n?.toString())}}),Boolean(g[b.keyForAPI])?T(u,{error:!0,sx:N.errorMessage,children:[b.label," is required"]}):C(k,{})]})]});case $.CHECKBOX:return T(k,{children:[C(l,{sx:N.label,htmlFor:`checkbox-${b.keyForAPI}`,children:b.label}),C(O,{name:b.keyForAPI,control:h,defaultValue:[],rules:{required:b?.isRequired&&`${b.label} is required`},render:({field:{value:e},fieldState:{error:t}})=>C(k,{children:C(s,{multiple:!0,id:`checkbox-${b.keyForAPI}`,options:I,value:e||[],onChange:(e,t)=>{const o=y?.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(b.keyForAPI,t,{shouldValidate:!0})},renderInput:e=>C(c,{...e,sx:N.textInput,placeholder:b.placeholder,slotProps:{formHelperText:{sx:N.errorMessage}},error:!!t,helperText:t?t.message:""}),getOptionLabel:e=>q(e,y?.label??"label")??"",renderOption:(t,r)=>{const n=q(r,y?.id??"id"),i=q(r,y?.label??"label"),a=e.some((e=>q(e,y?.id??"id")===n));return o("li",{...t,key:n?.toString()},C(d,{style:{marginRight:8},checked:a}),i?.toString())}})})})]})}return C(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:b,reset:y}=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),y()}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 b(o?.context?.as,c(o?.context?.key)),!0;case U.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 C(p,{sx:{p:2,justifyContent:"center"},children:d?T(i,{sx:Object.assign({p:3},N.centerContent),children:[C(M,{}),C(h,{color:"text.primary",variant:"h3",sx:N.title,children:"Loading..."})]}):T("form",{onSubmit:x(I),noValidate:!0,children:[C(h,{color:"text.primary",variant:"h4",sx:N.title,children:r?.meta?.title??j(r?.name)}),C(h,{color:"text.primary",variant:"subtitle1",sx:{mb:2,textAlign:"center"},children:r?.meta?.desc??""}),C(f,{direction:"column",sx:{gap:3},children:r.steps.map(((e,t)=>C(X,{form:u,step:e},`step-input-${t}`)))}),C(f,{sx:{mt:3},children:C(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:w,onSuccess:E,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((()=>{w&&X?.length&&w(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?C(k,{}):C(b,{theme:z,children:T(i,{sx:{backgroundColor:"var(--mstate-bg-color)",height:"100%",overflow:"auto"},children:[C(i,{sx:N.logo,onClick:()=>window.open("https://mobioffice.io"),children:C("img",{src:P.logo,alt:"logo",width:"100%"})}),U?T(i,{sx:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column",p:3},children:[C(M,{}),C(h,{color:"text.primary",variant:"h3",sx:N.title,children:"Loading..."})]}):X.length?T(i,{sx:Object.assign({p:3},N.centerContent),children:[C(i,{sx:{width:"100px",m:"auto"},children:C("img",{width:"100%",src:P.error,alt:"error"})}),C(y,{children:X.filter((e=>"string"==typeof e)).map(((e,t)=>C(I,{children:C(h,{color:"error",variant:"subtitle1",sx:N.title,children:e.replace(/instanceID='.*'/i,"")})},`error ${t}`)))}),C(g,{variant:"contained",onClick:()=>{j?.length||K(!H),V([])},sx:N.button,children:"Try Again"})]}):C(i,{children:j?.length?1===j.length?C(G,{token:l,onSuccess:E,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:_}):C(i,{children:C(f,{sx:{flexDirection:"row",flexWrap:"wrap",justifyContent:"center"},children:j.map((e=>!e?.who?.length||e?.who?.includes(a??"")?C(W,{meta:e?.meta,name:e?.name,onClick:()=>q([e])},`action-${e.name}`):C(k,{})))})}):C(i,{className:"box_center_to_modal",children:C(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",m={errorMessage:{m:0,mt:1,ml:.5},label:{fontSize:14,mb:-2},textInput:{"& .MuiOutlinedInput-root":{"& fieldset":{border:"none"},borderRadius:"8px"}},radio:{"& fieldset":{border:"none"},borderRadius:"8px"},logo:{width:"70%",margin:"auto",maxWidth:"300px",my:2},title:{textAlign:"center",fontSize:{xs:"1.5rem",sm:"2rem"}},button:{borderRadius:100},centerContent:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column"}},p="https://firebasestorage.googleapis.com/v0/b/mobioffice-dev.appspot.com/o/companies_logo%2Flogo.png?alt=media&token=e5f238f0-c373-4c76-9168-37fbe7da1de0",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}({}),I=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}({}),y=function(e){return e.CONTEXT_FIELD="CONTEXT_FIELD",e.CUSTOM_FIELD="CUSTOM_FIELD",e.USER_INPUT="USER_INPUT",e}({});function b(){return o.jsx(r.Box,{children:o.jsx(j,{})})}function j(){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 T(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??T(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,p=x.field??{},h=p.mapper,[f,g]=e.useState(p.itemList??[]);switch(e.useEffect((()=>{p.request&&async function(e,r){const{data:o}=await t(e);return k(o,r?.target??"")??[]}(p.request,h).then((e=>{g(e)})).catch((e=>{console.log(e)}))}),[]),x.fieldType){case I.INPUT_FIELD:case I.TEXT_AREA:return o.jsxs(o.Fragment,{children:[o.jsx(r.InputLabel,{sx:m.label,htmlFor:`text-${p.keyForAPI}`,children:p.label}),o.jsx(r.TextField,{multiline:x.fieldType===I.TEXT_AREA,minRows:3,id:`text-${p.keyForAPI}`,type:x.inputType,placeholder:p.placeholder,slotProps:{inputLabel:{shrink:"date"===x.inputType||void 0},formHelperText:{sx:m.errorMessage}},sx:m.textInput,defaultValue:"",...d(p.keyForAPI,{required:p?.isRequired&&`${p.label} is required`}),error:Boolean(u[p.keyForAPI]),helperText:u?.[p.keyForAPI]?.message},`input-${p.keyForAPI}`)]});case I.DROP_DOWN:case I.RADIO_LIST:return o.jsxs(o.Fragment,{children:[o.jsx(r.InputLabel,{sx:m.label,htmlFor:`dropdown-${p.keyForAPI}`,children:p.label}),o.jsxs(r.FormControl,{children:[o.jsx(r.Autocomplete,{id:`dropdown-${p.keyForAPI}`,options:f,...d(p.keyForAPI,{required:p?.isRequired&&`${p.label} is required`}),onChange:(e,t)=>{a(p.keyForAPI,t,{shouldValidate:!0})},renderInput:t=>e.createElement(r.TextField,{...t,sx:m.textInput,key:`input-${p.keyForAPI}`,placeholder:p.placeholder,error:Boolean(u[p.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[p.keyForAPI])?o.jsxs(r.FormHelperText,{error:!0,sx:m.errorMessage,children:[p.label," is required"]}):o.jsx(o.Fragment,{})]})]});case I.CHECKBOX:return o.jsxs(o.Fragment,{children:[o.jsx(r.InputLabel,{sx:m.label,htmlFor:`checkbox-${p.keyForAPI}`,children:p.label}),o.jsx(c.Controller,{name:p.keyForAPI,control:l,defaultValue:[],rules:{required:p?.isRequired&&`${p.label} is required`},render:({field:{value:t},fieldState:{error:n}})=>o.jsx(o.Fragment,{children:o.jsx(r.Autocomplete,{multiple:!0,id:`checkbox-${p.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(p.keyForAPI,t,{shouldValidate:!0})},renderInput:e=>o.jsx(r.TextField,{...e,sx:m.textInput,placeholder:p.placeholder,slotProps:{formHelperText:{sx:m.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,p]=e.useState(!1),h=c.useForm({defaultValues:{}}),{handleSubmit:f,setValue:I}=h;e.useEffect((()=>!1===Boolean(i?.steps?.length)?(j({}),()=>{}):(k(),()=>{})),[]);const j=async e=>{try{p(!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{p(!1)}},k=()=>{const e=new Set;i.steps=i?.steps?.filter((t=>{const r=t;switch(r?.module){case y.CUSTOM_FIELD:return I(r?.context?.as,l(r?.context?.key)),!0;case y.CONTEXT_FIELD:return I(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},m.centerContent),children:[o.jsx(b,{}),o.jsx(r.Typography,{color:"text.primary",variant:"h3",sx:m.title,children:"Loading..."})]}):o.jsxs("form",{onSubmit:f(j),noValidate:!0,children:[o.jsx(r.Typography,{color:"text.primary",variant:"h4",sx:Object.assign({mb:2},m.title),children:i?.meta?.title??T(i?.name)}),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:m.button,children:"Submit"})})]})})}function F(n){const{who:i,token:s,start:a=!0,theme:l="light",instanceID:c,workflow:h,onInstanceChange:I,onError:y,onSuccess:j,handleCustomField:T=()=>""}=n,k=r.useMediaQuery("(prefers-color-scheme: dark)")?"dark":"light",E="system"===l?k:l,[F,A]=e.useState(c),[_,S]=e.useState(!0),[O,D]=e.useState([]),[P,v]=e.useState([]),[N,R]=e.useState(!1);e.useEffect((()=>!1===a?()=>{}:(S(!0),F?(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:F,token:s}).then((e=>{D(Object.values(e))})).catch((e=>{t.isAxiosError(e)&&e.response?v(e.response?.data?.errors??["Something went wrong"]):v(["Something went wrong"])})).finally((()=>{S(!1)})),()=>{}):(async function(e){const{data:r}=await t({url:`${u}/${g.GET_CURRENT_ACTIONS_WITHOUT_INSTANCE}?name=${e.workflow}`,method:"GET",headers:{[x]:e.token}});return r.data}({token:s,workflow:h}).then((e=>{D(Object.values(e))})).catch((e=>{t.isAxiosError(e)&&e.response?v(e.response?.data?.errors??["Something went wrong"]):v(["Something went wrong"])})).finally((()=>{S(!1)})),()=>{}))),[a,N]),e.useEffect((()=>{A(c)}),[c]),e.useEffect((()=>{y&&P?.length&&y(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"}})}(E)),[E]);return!1===a?o.jsx(o.Fragment,{}):o.jsx(r.ThemeProvider,{theme:L,children:o.jsxs(r.Box,{sx:{backgroundColor:"background.default",height:"100%",overflow:"auto"},children:[o.jsx(r.Box,{sx:m.logo,children:o.jsx("img",{src:p,alt:"logo",width:"100%"})}),o.jsx("hr",{style:{marginInline:"16px"}}),_?o.jsxs(r.Box,{sx:{display:"flex",justifyContent:"center",alignItems:"center",flexDirection:"column",p:3},children:[o.jsx(b,{}),o.jsx(r.Typography,{color:"text.primary",variant:"h3",sx:m.title,children:"Loading..."})]}):P.length?o.jsxs(r.Box,{sx:Object.assign({p:3},m.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:m.title,children:e.replace(/instanceID='.*'/i,"")})},`error ${t}`)))}),o.jsx(r.Button,{variant:"contained",onClick:()=>{O?.length||R(!N),v([])},sx:m.button,children:"Try Again"})]}):o.jsx(r.Box,{children:O?.length?1===O.length?o.jsx(w,{token:s,onSuccess:j,instanceID:F,workflow:h,config:O[0],onExecuteAction:(e,t)=>{t?.length?(v(t),e.instanceID=t[0]?.split("instanceID='")?.pop()?.replace("'","")):D(e.actions),e?.instanceID&&e?.instanceID!==F&&!t?.length&&(A(e?.instanceID),I&&I(e?.instanceID))},updateCustomFieldValue:T}):o.jsx(r.Box,{children:o.jsx(r.Stack,{sx:{flexDirection:"row",flexWrap:"wrap",justifyContent:"center"},children:O.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:m.title,children:"Workflow Ended"})})})]})})}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",p="secret-key",m={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"}},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,p=function(e){return void 0!==e?.config}(i)?i?.config?.data:i,x=p.field??{},h=x.mapper,[g,y]=e.useState(x.itemList??[]);switch(e.useEffect((()=>{x.request&&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)}))}),[]),p.fieldType){case f.INPUT_FIELD:case f.TEXT_AREA:return o.jsxs(o.Fragment,{children:[o.jsx(r.InputLabel,{sx:m.label,htmlFor:`text-${x.keyForAPI}`,children:x.label}),o.jsx(r.TextField,{multiline:p.fieldType===f.TEXT_AREA,minRows:3,id:`text-${x.keyForAPI}`,type:p.inputType,placeholder:x.placeholder,slotProps:{inputLabel:{shrink:"date"===p.inputType||void 0},formHelperText:{sx:m.errorMessage}},sx:m.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:m.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:m.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:m.errorMessage,children:[x.label," is required"]}):o.jsx(o.Fragment,{})]})]});case f.CHECKBOX:return o.jsxs(o.Fragment,{children:[o.jsx(r.InputLabel,{sx:m.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:m.textInput,placeholder:x.placeholder,slotProps:{formHelperText:{sx:m.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",[p]: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},m.centerContent),children:[o.jsx(y,{}),o.jsx(r.Typography,{color:"text.primary",variant:"h3",sx:m.title,children:"Loading..."})]}):o.jsxs("form",{onSubmit:T(E),noValidate:!0,children:[o.jsx(r.Typography,{color:"text.primary",variant:"h4",sx:m.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:m.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([]),[R,P]=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:{[p]:e.token}});return r.data}({instanceID:A,token:s}).then((e=>{D(Object.values(e))})).catch((e=>{t.isAxiosError(e)&&e.response?P(e.response?.data?.errors??["Something went wrong"]):P(["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:{[p]:e.token}});return r.data}({token:s,workflow:g,type:l}).then((e=>{D(Object.values(e))})).catch((e=>{t.isAxiosError(e)&&e.response?P(e.response?.data?.errors??["Something went wrong"]):P(["Something went wrong"])})).finally((()=>{O(!1)})),()=>{}))),[a,N]),e.useEffect((()=>{S(f)}),[f]),e.useEffect((()=>{I&&R?.length&&I(R)}),[R]);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:m.logo,onClick:()=>window.open("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:m.title,children:"Loading..."})]}):R.length?o.jsxs(r.Box,{sx:Object.assign({p:3},m.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:R.filter((e=>"string"==typeof e)).map(((e,t)=>o.jsx(r.ListItem,{children:o.jsx(r.Typography,{color:"error",variant:"subtitle1",sx:m.title,children:e.replace(/instanceID='.*'/i,"")})},`error ${t}`)))}),o.jsx(r.Button,{variant:"contained",onClick:()=>{v?.length||B(!N),P([])},sx:m.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?(P(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:m.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;
@@ -4,6 +4,12 @@ interface Props {
4
4
  token: string;
5
5
  workflow: string;
6
6
  instanceID?: string;
7
+ /**
8
+ * @type {'public' | 'private'}
9
+ * @default 'private'
10
+ * @description Define the workflow type which you want to execute.
11
+ */
12
+ type?: 'public' | 'private';
7
13
  /**
8
14
  * @type {boolean}
9
15
  * @default true
@@ -39,6 +45,12 @@ interface Props {
39
45
  * @returns {void}.
40
46
  */
41
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;
42
54
  /**
43
55
  * Handles your steps which contain Custom field module.
44
56
  * @param {string} key - Which key you expect for getting the value.
package/package.json CHANGED
@@ -1,57 +1,57 @@
1
- {
2
- "name": "mstate-react",
3
- "version": "1.5.2",
4
- "main": "dist/index.js",
5
- "module": "dist/index.esm.js",
6
- "types": "dist/types/index.d.ts",
7
- "files": [
8
- "dist"
9
- ],
10
- "keywords": [
11
- "mstate",
12
- "workflow",
13
- "mobioffice"
14
- ],
15
- "scripts": {
16
- "prod": "npm run build",
17
- "dev": "rollup -c --watch",
18
- "build": "rollup -c"
19
- },
20
- "peerDependencies": {
21
- "@emotion/react": "^11.13.3",
22
- "@emotion/styled": "^11.13.0",
23
- "@mui/material": "^6.1.5",
24
- "axios": "^1.7.7",
25
- "react": "^18.3.1",
26
- "react-dom": "^18.3.1",
27
- "react-hook-form": "^7.53.1"
28
- },
29
- "dependencies": {
30
- "@emotion/react": "^11.13.3",
31
- "@emotion/styled": "^11.13.0",
32
- "@mui/material": "^6.1.5",
33
- "@rollup/plugin-typescript": "^11.1.6",
34
- "@rollup/plugin-url": "^8.0.2",
35
- "@svgr/rollup": "^8.1.0",
36
- "axios": "^1.7.7",
37
- "react": "^18.3.1",
38
- "react-dom": "^18.3.1",
39
- "react-hook-form": "^7.53.1",
40
- "rollup-plugin-dts": "^6.1.1",
41
- "rollup-plugin-postcss": "^4.0.2"
42
- },
43
- "devDependencies": {
44
- "@rollup/plugin-alias": "^5.1.1",
45
- "@rollup/plugin-babel": "^6.0.4",
46
- "@rollup/plugin-commonjs": "^25.0.8",
47
- "@rollup/plugin-json": "^6.1.0",
48
- "@rollup/plugin-node-resolve": "^15.2.3",
49
- "@rollup/plugin-terser": "^0.4.4",
50
- "@types/react": "^18.3.3",
51
- "@types/react-dom": "^18.3.0",
52
- "rollup": "^4.18.0",
53
- "rollup-plugin-peer-deps-external": "^2.2.4",
54
- "rollup-plugin-typescript2": "^0.36.0",
55
- "typescript": "^5.4.5"
56
- }
57
- }
1
+ {
2
+ "name": "mstate-react",
3
+ "version": "1.5.4",
4
+ "main": "dist/index.js",
5
+ "module": "dist/index.esm.js",
6
+ "types": "dist/types/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "keywords": [
11
+ "mstate",
12
+ "workflow",
13
+ "mobioffice"
14
+ ],
15
+ "scripts": {
16
+ "prod": "npm run build",
17
+ "dev": "rollup -c --watch",
18
+ "build": "rollup -c"
19
+ },
20
+ "peerDependencies": {
21
+ "@emotion/react": "^11.13.3",
22
+ "@emotion/styled": "^11.13.0",
23
+ "@mui/material": "^6.1.5",
24
+ "axios": "^1.7.7",
25
+ "react": "^18.3.1",
26
+ "react-dom": "^18.3.1",
27
+ "react-hook-form": "^7.53.1"
28
+ },
29
+ "dependencies": {
30
+ "@emotion/react": "^11.13.3",
31
+ "@emotion/styled": "^11.13.0",
32
+ "@mui/material": "^6.1.5",
33
+ "@rollup/plugin-typescript": "^11.1.6",
34
+ "@rollup/plugin-url": "^8.0.2",
35
+ "@svgr/rollup": "^8.1.0",
36
+ "axios": "^1.7.7",
37
+ "react": "^18.3.1",
38
+ "react-dom": "^18.3.1",
39
+ "react-hook-form": "^7.53.1",
40
+ "rollup-plugin-dts": "^6.1.1",
41
+ "rollup-plugin-postcss": "^4.0.2"
42
+ },
43
+ "devDependencies": {
44
+ "@rollup/plugin-alias": "^5.1.1",
45
+ "@rollup/plugin-babel": "^6.0.4",
46
+ "@rollup/plugin-commonjs": "^25.0.8",
47
+ "@rollup/plugin-json": "^6.1.0",
48
+ "@rollup/plugin-node-resolve": "^15.2.3",
49
+ "@rollup/plugin-terser": "^0.4.4",
50
+ "@types/react": "^18.3.3",
51
+ "@types/react-dom": "^18.3.0",
52
+ "rollup": "^4.18.0",
53
+ "rollup-plugin-peer-deps-external": "^2.2.4",
54
+ "rollup-plugin-typescript2": "^0.36.0",
55
+ "typescript": "^5.4.5"
56
+ }
57
+ }