mstate-react 1.5.2
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 +38 -0
- package/dist/index.esm.js +1 -0
- package/dist/index.js +1 -0
- package/dist/types/index.d.ts +51 -0
- package/package.json +57 -0
package/README.md
ADDED
@@ -0,0 +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
|
+
```
|
@@ -0,0 +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};
|
package/dist/index.js
ADDED
@@ -0,0 +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;
|
@@ -0,0 +1,51 @@
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
2
|
+
|
3
|
+
interface Props {
|
4
|
+
token: string;
|
5
|
+
workflow: string;
|
6
|
+
instanceID?: string;
|
7
|
+
/**
|
8
|
+
* @type {boolean}
|
9
|
+
* @default true
|
10
|
+
* @description Trigger if to start workflow.
|
11
|
+
*/
|
12
|
+
start?: boolean;
|
13
|
+
/**
|
14
|
+
* @type {'dark' | 'light' | 'system'}
|
15
|
+
* @default 'light'
|
16
|
+
* @description To change the color theme of mstate.
|
17
|
+
*/
|
18
|
+
theme?: 'dark' | 'light' | 'system';
|
19
|
+
/**
|
20
|
+
* @type {string}
|
21
|
+
* @description To Validate who are allowed to perform or see action.
|
22
|
+
*/
|
23
|
+
who?: string;
|
24
|
+
/**
|
25
|
+
* Triggers whenever your current workflow moved to other.
|
26
|
+
* @param {string} newInstanceID - The new instance ID on which your current workflow moved.
|
27
|
+
* @returns {void}.
|
28
|
+
*/
|
29
|
+
onInstanceChange?: (newInstanceID: string) => void;
|
30
|
+
/**
|
31
|
+
* Triggers when The Action execution is completed.
|
32
|
+
* @param {string} actionName - action name which you have executed at this moment.
|
33
|
+
* @returns {void}.
|
34
|
+
*/
|
35
|
+
onSuccess?: (actionName: string) => void;
|
36
|
+
/**
|
37
|
+
* Triggers when Error occurs while performing any action.
|
38
|
+
* @param {unknown[]} errors - Error response from the api call done by Mstate.
|
39
|
+
* @returns {void}.
|
40
|
+
*/
|
41
|
+
onError?: (errors: unknown[]) => void;
|
42
|
+
/**
|
43
|
+
* Handles your steps which contain Custom field module.
|
44
|
+
* @param {string} key - Which key you expect for getting the value.
|
45
|
+
* @returns {unknown} - whatever value you want to save in that custom field.
|
46
|
+
*/
|
47
|
+
handleCustomField?: (key: string) => unknown;
|
48
|
+
}
|
49
|
+
declare function Mstate(props: Props): react_jsx_runtime.JSX.Element;
|
50
|
+
|
51
|
+
export { Mstate as default };
|
package/package.json
ADDED
@@ -0,0 +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
|
+
}
|