@fctc/widget-logic 1.1.7 → 1.1.8

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/hooks.d.mts CHANGED
@@ -918,6 +918,41 @@ declare const useAuth: () => {
918
918
  };
919
919
  type useAuthType = ReturnType<typeof useAuth>;
920
920
 
921
+ declare const useCompany: (accessToken: string | null) => {
922
+ currentCompany: _tanstack_react_query.UseQueryResult<CurrentCompany | undefined, Error>;
923
+ companyInfo: _tanstack_react_query.UseQueryResult<any, Error>;
924
+ context: {
925
+ allowed_company_ids: (number | undefined)[];
926
+ };
927
+ };
928
+
929
+ interface CompanyTuple extends Array<number | string> {
930
+ 0: number;
931
+ 1: string;
932
+ }
933
+ interface CurrentCompany {
934
+ allowed_companies: CompanyTuple[];
935
+ current_company: CompanyTuple;
936
+ current_company_id: number;
937
+ is_allowed: boolean;
938
+ }
939
+ type useCompanyType = ReturnType<typeof useCompany>;
940
+
941
+ type AppProviderType = {
942
+ config: useConfigType;
943
+ user: useUserType;
944
+ auth: useAuthType;
945
+ company: useCompanyType;
946
+ action: ActionResultType;
947
+ menu: useMenuType;
948
+ view: useViewV2Type;
949
+ list: useListDataType;
950
+ };
951
+ declare const AppProvider: ({ children }: {
952
+ children: React.ReactNode;
953
+ }) => JSX.Element;
954
+ declare const useAppProvider: () => AppProviderType;
955
+
921
956
  declare const DEFAULT_EVENTS: readonly ["mousedown", "touchstart"];
922
957
  type EventType = (typeof DEFAULT_EVENTS)[number];
923
958
  interface UseClickOutsideOptions {
@@ -930,4 +965,45 @@ declare const useClickOutside: ({ handler, events, nodes, refs, }: UseClickOutsi
930
965
 
931
966
  declare function useDebounce<T>(value: T, delay: number): [T];
932
967
 
933
- export { type ActionResultType, type Context, type Record, type ViewResponse, useAuth, type useAuthType, useCallAction, type useCallActionType, useClickOutside, useConfig, type useConfigType, useDebounce, useDetail, useListData, type useListDataType, useMenu, type useMenuType, useProfile, useUser, type useUserType, useViewV2, type useViewV2Type };
968
+ declare const useSwitchLocaleHandler: () => {
969
+ switchLocale: (langId: string) => Promise<void>;
970
+ isLoading: boolean;
971
+ error: Error | null;
972
+ };
973
+
974
+ declare const useLoginHandler: () => {
975
+ login: ({ email, password, path, }: {
976
+ email: string;
977
+ password: string;
978
+ path: string;
979
+ }, { onSuccess, onError, }: {
980
+ onSuccess?: (res: any) => void;
981
+ onError?: (err: any) => void;
982
+ }) => void;
983
+ isLoading: boolean;
984
+ error: Error | null;
985
+ };
986
+
987
+ declare const useForgotPasswordHandler: () => {
988
+ sendForgotPassword: (email: string, { onSuccess, onError, }?: {
989
+ onSuccess?: () => void;
990
+ onError?: (err: any) => void;
991
+ }) => void;
992
+ isLoading: boolean;
993
+ error: Error | null;
994
+ };
995
+
996
+ declare const useResetPasswordHandler: () => {
997
+ resetPassword: ({ password, confirmPassword, token, }: {
998
+ password: string;
999
+ confirmPassword: string;
1000
+ token: string;
1001
+ }, { onSuccess, onError, }: {
1002
+ onSuccess?: () => void;
1003
+ onError?: () => void;
1004
+ }) => void;
1005
+ isLoading: boolean;
1006
+ error: Error | null;
1007
+ };
1008
+
1009
+ export { type ActionResultType, AppProvider, type Context, type Record, type ViewResponse, useAppProvider, useAuth, type useAuthType, useCallAction, type useCallActionType, useClickOutside, useConfig, type useConfigType, useDebounce, useDetail, useForgotPasswordHandler, useListData, type useListDataType, useLoginHandler, useMenu, type useMenuType, useProfile, useResetPasswordHandler, useSwitchLocaleHandler, useUser, type useUserType, useViewV2, type useViewV2Type };
package/dist/hooks.d.ts CHANGED
@@ -918,6 +918,41 @@ declare const useAuth: () => {
918
918
  };
919
919
  type useAuthType = ReturnType<typeof useAuth>;
920
920
 
921
+ declare const useCompany: (accessToken: string | null) => {
922
+ currentCompany: _tanstack_react_query.UseQueryResult<CurrentCompany | undefined, Error>;
923
+ companyInfo: _tanstack_react_query.UseQueryResult<any, Error>;
924
+ context: {
925
+ allowed_company_ids: (number | undefined)[];
926
+ };
927
+ };
928
+
929
+ interface CompanyTuple extends Array<number | string> {
930
+ 0: number;
931
+ 1: string;
932
+ }
933
+ interface CurrentCompany {
934
+ allowed_companies: CompanyTuple[];
935
+ current_company: CompanyTuple;
936
+ current_company_id: number;
937
+ is_allowed: boolean;
938
+ }
939
+ type useCompanyType = ReturnType<typeof useCompany>;
940
+
941
+ type AppProviderType = {
942
+ config: useConfigType;
943
+ user: useUserType;
944
+ auth: useAuthType;
945
+ company: useCompanyType;
946
+ action: ActionResultType;
947
+ menu: useMenuType;
948
+ view: useViewV2Type;
949
+ list: useListDataType;
950
+ };
951
+ declare const AppProvider: ({ children }: {
952
+ children: React.ReactNode;
953
+ }) => JSX.Element;
954
+ declare const useAppProvider: () => AppProviderType;
955
+
921
956
  declare const DEFAULT_EVENTS: readonly ["mousedown", "touchstart"];
922
957
  type EventType = (typeof DEFAULT_EVENTS)[number];
923
958
  interface UseClickOutsideOptions {
@@ -930,4 +965,45 @@ declare const useClickOutside: ({ handler, events, nodes, refs, }: UseClickOutsi
930
965
 
931
966
  declare function useDebounce<T>(value: T, delay: number): [T];
932
967
 
933
- export { type ActionResultType, type Context, type Record, type ViewResponse, useAuth, type useAuthType, useCallAction, type useCallActionType, useClickOutside, useConfig, type useConfigType, useDebounce, useDetail, useListData, type useListDataType, useMenu, type useMenuType, useProfile, useUser, type useUserType, useViewV2, type useViewV2Type };
968
+ declare const useSwitchLocaleHandler: () => {
969
+ switchLocale: (langId: string) => Promise<void>;
970
+ isLoading: boolean;
971
+ error: Error | null;
972
+ };
973
+
974
+ declare const useLoginHandler: () => {
975
+ login: ({ email, password, path, }: {
976
+ email: string;
977
+ password: string;
978
+ path: string;
979
+ }, { onSuccess, onError, }: {
980
+ onSuccess?: (res: any) => void;
981
+ onError?: (err: any) => void;
982
+ }) => void;
983
+ isLoading: boolean;
984
+ error: Error | null;
985
+ };
986
+
987
+ declare const useForgotPasswordHandler: () => {
988
+ sendForgotPassword: (email: string, { onSuccess, onError, }?: {
989
+ onSuccess?: () => void;
990
+ onError?: (err: any) => void;
991
+ }) => void;
992
+ isLoading: boolean;
993
+ error: Error | null;
994
+ };
995
+
996
+ declare const useResetPasswordHandler: () => {
997
+ resetPassword: ({ password, confirmPassword, token, }: {
998
+ password: string;
999
+ confirmPassword: string;
1000
+ token: string;
1001
+ }, { onSuccess, onError, }: {
1002
+ onSuccess?: () => void;
1003
+ onError?: () => void;
1004
+ }) => void;
1005
+ isLoading: boolean;
1006
+ error: Error | null;
1007
+ };
1008
+
1009
+ export { type ActionResultType, AppProvider, type Context, type Record, type ViewResponse, useAppProvider, useAuth, type useAuthType, useCallAction, type useCallActionType, useClickOutside, useConfig, type useConfigType, useDebounce, useDetail, useForgotPasswordHandler, useListData, type useListDataType, useLoginHandler, useMenu, type useMenuType, useProfile, useResetPasswordHandler, useSwitchLocaleHandler, useUser, type useUserType, useViewV2, type useViewV2Type };
package/dist/hooks.js CHANGED
@@ -20,15 +20,21 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/hooks.ts
21
21
  var hooks_exports = {};
22
22
  __export(hooks_exports, {
23
+ AppProvider: () => AppProvider,
24
+ useAppProvider: () => useAppProvider,
23
25
  useAuth: () => useAuth,
24
26
  useCallAction: () => useCallAction,
25
27
  useClickOutside: () => useClickOutside,
26
28
  useConfig: () => useConfig,
27
29
  useDebounce: () => useDebounce,
28
30
  useDetail: () => useDetail,
31
+ useForgotPasswordHandler: () => useForgotPasswordHandler,
29
32
  useListData: () => useListData,
33
+ useLoginHandler: () => useLoginHandler,
30
34
  useMenu: () => useMenu,
31
35
  useProfile: () => useProfile,
36
+ useResetPasswordHandler: () => useResetPasswordHandler,
37
+ useSwitchLocaleHandler: () => useSwitchLocaleHandler,
32
38
  useUser: () => useUser,
33
39
  useViewV2: () => useViewV2
34
40
  });
@@ -238,6 +244,16 @@ var convertFieldsToArray = (fields) => {
238
244
  const inputFields = fields.filter((field) => field && field.type_co === "field").map((field) => field.name);
239
245
  return [...defaultFields, ...inputFields];
240
246
  };
247
+ function combineContexts(contexts) {
248
+ if (contexts.some((context) => !context)) {
249
+ return void 0;
250
+ } else {
251
+ const res = contexts.reduce((acc, context) => {
252
+ return { ...acc, ...context };
253
+ }, {});
254
+ return res;
255
+ }
256
+ }
241
257
  function useAsyncState(initialValue = [true, null]) {
242
258
  return (0, import_react4.useReducer)(
243
259
  (_state, action = null) => [false, action],
@@ -537,8 +553,125 @@ var useAuth = () => {
537
553
  };
538
554
  };
539
555
 
540
- // src/hooks/utils/use-click-outside.ts
556
+ // src/hooks/core/use-app-provider.tsx
557
+ var import_react10 = require("react");
558
+
559
+ // src/hooks/core/use-company.ts
560
+ var import_interface_logic9 = require("@fctc/interface-logic");
561
+ var import_react_query3 = require("@tanstack/react-query");
541
562
  var import_react9 = require("react");
563
+ var useCompany = (accessToken) => {
564
+ const getCurrentCompany = (0, import_interface_logic9.useGetCurrentCompany)();
565
+ const fetchCurrentCompany = async () => {
566
+ return await getCurrentCompany.mutateAsync();
567
+ };
568
+ const currentCompany = (0, import_react_query3.useQuery)({
569
+ queryKey: ["currentCompany", accessToken],
570
+ queryFn: fetchCurrentCompany,
571
+ enabled: !!accessToken
572
+ });
573
+ const current_company_id = (0, import_react9.useMemo)(() => {
574
+ return currentCompany.data?.current_company_id;
575
+ }, [currentCompany.data]);
576
+ (0, import_react9.useEffect)(() => {
577
+ if (current_company_id) {
578
+ const companyIDs = [current_company_id];
579
+ const env = (0, import_interface_logic9.getEnv)();
580
+ env.setAllowCompanies([...companyIDs]);
581
+ env.setCompanies(companyIDs);
582
+ }
583
+ }, [current_company_id]);
584
+ const getCompanyInfo = (0, import_interface_logic9.useGetCompanyInfo)();
585
+ const companyInfo = (0, import_react_query3.useQuery)({
586
+ queryKey: ["companyInfoQuery", current_company_id, accessToken],
587
+ queryFn: () => getCompanyInfo.mutateAsync(Number(current_company_id)),
588
+ enabled: !!current_company_id && !!accessToken
589
+ });
590
+ (0, import_react9.useEffect)(() => {
591
+ if (companyInfo.data) {
592
+ const companyInfoData = companyInfo.data;
593
+ if (companyInfoData?.length) {
594
+ const env = (0, import_interface_logic9.getEnv)();
595
+ env.setDefaultCompany(companyInfoData[0]);
596
+ }
597
+ }
598
+ }, [companyInfo.data]);
599
+ return {
600
+ currentCompany,
601
+ companyInfo,
602
+ context: { allowed_company_ids: [current_company_id] }
603
+ };
604
+ };
605
+ var use_company_default = useCompany;
606
+
607
+ // src/hooks/core/use-app-provider.tsx
608
+ var import_interface_logic10 = require("@fctc/interface-logic");
609
+ var import_jsx_runtime = require("react/jsx-runtime");
610
+ var AppProviderInitialValue = {
611
+ config: {},
612
+ user: {},
613
+ auth: {},
614
+ company: {},
615
+ action: {},
616
+ menu: {},
617
+ view: {},
618
+ list: {}
619
+ };
620
+ var ReactContext = (0, import_react10.createContext)(AppProviderInitialValue);
621
+ var AppProvider = ({ children }) => {
622
+ const config = useConfig({});
623
+ const auth = useAuth();
624
+ const user = useUser(auth.accessToken);
625
+ const company = use_company_default(auth.accessToken);
626
+ const menuContext = (0, import_react10.useMemo)(() => {
627
+ return combineContexts([user.context, company.context]);
628
+ }, [user.context, company.context]);
629
+ const menu = useMenu({ context: menuContext });
630
+ const action = (0, import_react10.useMemo)(() => {
631
+ return menu.state.action;
632
+ }, [menu.state.action]);
633
+ const viewContext = (0, import_react10.useMemo)(() => {
634
+ return combineContexts([
635
+ menuContext,
636
+ { ...(0, import_interface_logic10.evalJSONContext)(action?.result?.context) }
637
+ ]);
638
+ }, [menuContext, action?.result?.context]);
639
+ const view = useViewV2({
640
+ action,
641
+ context: viewContext
642
+ });
643
+ const list = useListData({
644
+ action,
645
+ viewResponse: view.data,
646
+ context: viewContext
647
+ });
648
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
649
+ ReactContext.Provider,
650
+ {
651
+ value: {
652
+ config,
653
+ auth,
654
+ user,
655
+ company,
656
+ menu,
657
+ list,
658
+ action,
659
+ view
660
+ },
661
+ children
662
+ }
663
+ );
664
+ };
665
+ var useAppProvider = () => {
666
+ const context = (0, import_react10.useContext)(ReactContext);
667
+ if (!context) {
668
+ return AppProviderInitialValue;
669
+ }
670
+ return context;
671
+ };
672
+
673
+ // src/hooks/utils/use-click-outside.ts
674
+ var import_react11 = require("react");
542
675
  var DEFAULT_EVENTS = ["mousedown", "touchstart"];
543
676
  var useClickOutside = ({
544
677
  handler,
@@ -546,8 +679,8 @@ var useClickOutside = ({
546
679
  nodes = [],
547
680
  refs
548
681
  }) => {
549
- const ref = (0, import_react9.useRef)(null);
550
- (0, import_react9.useEffect)(() => {
682
+ const ref = (0, import_react11.useRef)(null);
683
+ (0, import_react11.useEffect)(() => {
551
684
  const listener = (event) => {
552
685
  const { target } = event;
553
686
  if (refs && refs?.length > 0 && refs?.some((r) => r.current?.contains(target))) {
@@ -569,10 +702,10 @@ var useClickOutside = ({
569
702
  };
570
703
 
571
704
  // src/hooks/utils/use-debounce.ts
572
- var import_react10 = require("react");
705
+ var import_react12 = require("react");
573
706
  function useDebounce(value, delay) {
574
- const [debouncedValue, setDebouncedValue] = (0, import_react10.useState)(value);
575
- (0, import_react10.useEffect)(() => {
707
+ const [debouncedValue, setDebouncedValue] = (0, import_react12.useState)(value);
708
+ (0, import_react12.useEffect)(() => {
576
709
  const handler = setTimeout(() => {
577
710
  setDebouncedValue(value);
578
711
  }, delay);
@@ -582,17 +715,146 @@ function useDebounce(value, delay) {
582
715
  }, [value, delay]);
583
716
  return [debouncedValue];
584
717
  }
718
+
719
+ // src/hooks/api/use-switch-locale.ts
720
+ var import_interface_logic11 = require("@fctc/interface-logic");
721
+ var import_react13 = require("react");
722
+ var useSwitchLocaleHandler = () => {
723
+ const switchUserLocale = (0, import_interface_logic11.useSwitchLocale)();
724
+ const env = (0, import_interface_logic11.getEnv)();
725
+ const { context } = (0, import_interface_logic11.useAppSelector)(import_interface_logic11.selectEnv);
726
+ const switchLocale = (0, import_react13.useCallback)(
727
+ async (langId) => {
728
+ if (!langId) return;
729
+ await switchUserLocale.mutateAsync({
730
+ data: {
731
+ id: parseInt(context?.uid),
732
+ values: { lang: langId }
733
+ }
734
+ });
735
+ env.setLang(langId);
736
+ },
737
+ [switchUserLocale]
738
+ );
739
+ return {
740
+ switchLocale,
741
+ isLoading: switchUserLocale.isPending,
742
+ error: switchUserLocale.error
743
+ };
744
+ };
745
+
746
+ // src/hooks/api/use-login.ts
747
+ var import_interface_logic12 = require("@fctc/interface-logic");
748
+ var import_react14 = require("react");
749
+ var useLoginHandler = () => {
750
+ const loginMutate = (0, import_interface_logic12.useLoginCredential)();
751
+ const login = (0, import_react14.useCallback)(
752
+ ({
753
+ email,
754
+ password,
755
+ path
756
+ }, {
757
+ onSuccess,
758
+ onError
759
+ }) => {
760
+ loginMutate.mutate(
761
+ {
762
+ email,
763
+ password,
764
+ path
765
+ },
766
+ {
767
+ onSuccess,
768
+ onError
769
+ }
770
+ );
771
+ },
772
+ [loginMutate]
773
+ );
774
+ return {
775
+ login,
776
+ isLoading: loginMutate.isPending,
777
+ error: loginMutate.error
778
+ };
779
+ };
780
+
781
+ // src/hooks/api/use-forgot-password.ts
782
+ var import_interface_logic13 = require("@fctc/interface-logic");
783
+ var import_react15 = require("react");
784
+ var useForgotPasswordHandler = () => {
785
+ const forgotPasswordMutate = (0, import_interface_logic13.useForgotPassword)();
786
+ const sendForgotPassword = (0, import_react15.useCallback)(
787
+ (email, {
788
+ onSuccess,
789
+ onError
790
+ } = {}) => {
791
+ forgotPasswordMutate.mutate(email, {
792
+ onSuccess,
793
+ onError
794
+ });
795
+ },
796
+ [forgotPasswordMutate]
797
+ );
798
+ return {
799
+ sendForgotPassword,
800
+ isLoading: forgotPasswordMutate.isPending,
801
+ error: forgotPasswordMutate.error
802
+ };
803
+ };
804
+
805
+ // src/hooks/api/use-reset-password.ts
806
+ var import_interface_logic14 = require("@fctc/interface-logic");
807
+ var import_react16 = require("react");
808
+ var useResetPasswordHandler = () => {
809
+ const resetPasswordMutate = (0, import_interface_logic14.useResetPassword)();
810
+ const resetPassword = (0, import_react16.useCallback)(
811
+ ({
812
+ password,
813
+ confirmPassword,
814
+ token
815
+ }, {
816
+ onSuccess,
817
+ onError
818
+ }) => {
819
+ resetPasswordMutate.mutate(
820
+ {
821
+ data: {
822
+ password,
823
+ confirmPassword
824
+ },
825
+ token
826
+ },
827
+ {
828
+ onSuccess,
829
+ onError
830
+ }
831
+ );
832
+ },
833
+ [resetPasswordMutate]
834
+ );
835
+ return {
836
+ resetPassword,
837
+ isLoading: resetPasswordMutate.isPending,
838
+ error: resetPasswordMutate.error
839
+ };
840
+ };
585
841
  // Annotate the CommonJS export names for ESM import in node:
586
842
  0 && (module.exports = {
843
+ AppProvider,
844
+ useAppProvider,
587
845
  useAuth,
588
846
  useCallAction,
589
847
  useClickOutside,
590
848
  useConfig,
591
849
  useDebounce,
592
850
  useDetail,
851
+ useForgotPasswordHandler,
593
852
  useListData,
853
+ useLoginHandler,
594
854
  useMenu,
595
855
  useProfile,
856
+ useResetPasswordHandler,
857
+ useSwitchLocaleHandler,
596
858
  useUser,
597
859
  useViewV2
598
860
  });