@greatapps/common 1.1.97 → 1.1.99
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/components/account/hooks/useTwoFactorAuthForm.mjs +5 -13
- package/dist/components/account/hooks/useTwoFactorAuthForm.mjs.map +1 -1
- package/dist/components/account/sections/MyProfileSection.mjs +30 -19
- package/dist/components/account/sections/MyProfileSection.mjs.map +1 -1
- package/dist/components/account/sections/PreferencesSection.mjs +11 -10
- package/dist/components/account/sections/PreferencesSection.mjs.map +1 -1
- package/dist/index.mjs +3 -1
- package/dist/index.mjs.map +1 -1
- package/dist/modules/accounts/actions/account-management.action.mjs +36 -168
- package/dist/modules/accounts/actions/account-management.action.mjs.map +1 -1
- package/dist/modules/accounts/actions/find-current-account.action.mjs +3 -2
- package/dist/modules/accounts/actions/find-current-account.action.mjs.map +1 -1
- package/dist/modules/accounts/actions/list-currencies.action.mjs +3 -2
- package/dist/modules/accounts/actions/list-currencies.action.mjs.map +1 -1
- package/dist/modules/accounts/actions/list-timezones.action.mjs +3 -2
- package/dist/modules/accounts/actions/list-timezones.action.mjs.map +1 -1
- package/dist/modules/accounts/hooks/current-account.hook.mjs +2 -1
- package/dist/modules/accounts/hooks/current-account.hook.mjs.map +1 -1
- package/dist/modules/accounts/hooks/useAccountManagement.mjs +8 -5
- package/dist/modules/accounts/hooks/useAccountManagement.mjs.map +1 -1
- package/dist/modules/accounts/hooks/useCurrencies.mjs +2 -1
- package/dist/modules/accounts/hooks/useCurrencies.mjs.map +1 -1
- package/dist/modules/accounts/hooks/useTimezones.mjs +2 -1
- package/dist/modules/accounts/hooks/useTimezones.mjs.map +1 -1
- package/dist/modules/auth/actions/logout.action.mjs +6 -3
- package/dist/modules/auth/actions/logout.action.mjs.map +1 -1
- package/dist/modules/auth/actions/register.action.mjs +7 -2
- package/dist/modules/auth/actions/register.action.mjs.map +1 -1
- package/dist/modules/auth/actions/verify-two-factor.action.mjs +5 -10
- package/dist/modules/auth/actions/verify-two-factor.action.mjs.map +1 -1
- package/dist/modules/auth/hooks/logout.hook.mjs +2 -1
- package/dist/modules/auth/hooks/logout.hook.mjs.map +1 -1
- package/dist/modules/auth/hooks/register.hook.mjs +2 -1
- package/dist/modules/auth/hooks/register.hook.mjs.map +1 -1
- package/dist/modules/auth/hooks/useUserQuery.mjs +2 -2
- package/dist/modules/auth/hooks/useUserQuery.mjs.map +1 -1
- package/dist/modules/auth/services/auth.service.mjs +9 -4
- package/dist/modules/auth/services/auth.service.mjs.map +1 -1
- package/dist/modules/ia-credits/actions/list-ia-credits.action.mjs +3 -2
- package/dist/modules/ia-credits/actions/list-ia-credits.action.mjs.map +1 -1
- package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs +2 -1
- package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs.map +1 -1
- package/dist/modules/projects/actions/add-project-user.action.mjs +3 -2
- package/dist/modules/projects/actions/add-project-user.action.mjs.map +1 -1
- package/dist/modules/projects/actions/list-available-users.action.mjs +3 -2
- package/dist/modules/projects/actions/list-available-users.action.mjs.map +1 -1
- package/dist/modules/projects/actions/list-project-users.action.mjs +3 -2
- package/dist/modules/projects/actions/list-project-users.action.mjs.map +1 -1
- package/dist/modules/projects/actions/remove-project-user.action.mjs +3 -2
- package/dist/modules/projects/actions/remove-project-user.action.mjs.map +1 -1
- package/dist/modules/projects/hooks/list-available-users.hook.mjs +8 -1
- package/dist/modules/projects/hooks/list-available-users.hook.mjs.map +1 -1
- package/dist/modules/projects/hooks/list-project-users.hook.mjs +8 -1
- package/dist/modules/projects/hooks/list-project-users.hook.mjs.map +1 -1
- package/dist/modules/subscriptions/actions/list-subscriptions.action.mjs +3 -2
- package/dist/modules/subscriptions/actions/list-subscriptions.action.mjs.map +1 -1
- package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs +5 -1
- package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs.map +1 -1
- package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs +2 -1
- package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs.map +1 -1
- package/dist/modules/users/action/find-user-by-id.action.mjs +3 -14
- package/dist/modules/users/action/find-user-by-id.action.mjs.map +1 -1
- package/dist/modules/users/action/list-messages.action.mjs +6 -2
- package/dist/modules/users/action/list-messages.action.mjs.map +1 -1
- package/dist/modules/users/action/mark-all-messages-as-read.action.mjs +3 -2
- package/dist/modules/users/action/mark-all-messages-as-read.action.mjs.map +1 -1
- package/dist/modules/users/action/mark-message-as-read.action.mjs +3 -2
- package/dist/modules/users/action/mark-message-as-read.action.mjs.map +1 -1
- package/dist/modules/users/hooks/messages.hook.mjs +11 -9
- package/dist/modules/users/hooks/messages.hook.mjs.map +1 -1
- package/dist/providers/auth.provider.mjs +9 -15
- package/dist/providers/auth.provider.mjs.map +1 -1
- package/dist/utils/safeServerAction.mjs +10 -16
- package/dist/utils/safeServerAction.mjs.map +1 -1
- package/dist/utils/withAction.mjs +13 -0
- package/dist/utils/withAction.mjs.map +1 -0
- package/package.json +1 -1
- package/src/components/account/hooks/useTwoFactorAuthForm.tsx +101 -114
- package/src/components/account/sections/MyProfileSection.tsx +29 -26
- package/src/components/account/sections/PreferencesSection.tsx +10 -16
- package/src/index.ts +1 -0
- package/src/modules/accounts/actions/account-management.action.ts +90 -261
- package/src/modules/accounts/actions/find-current-account.action.ts +5 -4
- package/src/modules/accounts/actions/list-currencies.action.ts +8 -7
- package/src/modules/accounts/actions/list-timezones.action.ts +8 -7
- package/src/modules/accounts/hooks/current-account.hook.tsx +6 -5
- package/src/modules/accounts/hooks/useAccountManagement.ts +85 -83
- package/src/modules/accounts/hooks/useCurrencies.ts +15 -14
- package/src/modules/accounts/hooks/useTimezones.ts +15 -14
- package/src/modules/auth/actions/logout.action.ts +8 -7
- package/src/modules/auth/actions/register.action.ts +9 -4
- package/src/modules/auth/actions/verify-two-factor.action.ts +8 -21
- package/src/modules/auth/hooks/logout.hook.tsx +2 -1
- package/src/modules/auth/hooks/register.hook.tsx +2 -1
- package/src/modules/auth/hooks/useUserQuery.ts +2 -2
- package/src/modules/auth/services/auth.service.ts +11 -6
- package/src/modules/ia-credits/actions/list-ia-credits.action.ts +8 -11
- package/src/modules/ia-credits/hooks/ia-credits.hook.ts +2 -1
- package/src/modules/projects/actions/add-project-user.action.ts +9 -8
- package/src/modules/projects/actions/list-available-users.action.ts +9 -8
- package/src/modules/projects/actions/list-project-users.action.ts +9 -8
- package/src/modules/projects/actions/remove-project-user.action.ts +9 -8
- package/src/modules/projects/hooks/list-available-users.hook.ts +33 -26
- package/src/modules/projects/hooks/list-project-users.hook.ts +33 -26
- package/src/modules/subscriptions/actions/list-subscriptions.action.ts +9 -11
- package/src/modules/subscriptions/hooks/find-active-subscription.hook.ts +19 -15
- package/src/modules/subscriptions/hooks/list-subscriptions.hook.ts +17 -15
- package/src/modules/users/action/find-user-by-id.action.ts +8 -25
- package/src/modules/users/action/list-messages.action.ts +12 -10
- package/src/modules/users/action/mark-all-messages-as-read.action.ts +8 -8
- package/src/modules/users/action/mark-message-as-read.action.ts +8 -8
- package/src/modules/users/hooks/messages.hook.ts +60 -58
- package/src/providers/auth.provider.tsx +9 -16
- package/src/utils/safeServerAction.ts +18 -24
- package/src/utils/withAction.ts +13 -0
|
@@ -6,19 +6,16 @@ import {
|
|
|
6
6
|
useContext,
|
|
7
7
|
useMemo
|
|
8
8
|
} from "react";
|
|
9
|
+
import { useQueryClient } from "@tanstack/react-query";
|
|
9
10
|
import {
|
|
10
11
|
useUserQuery,
|
|
11
12
|
useSetUserData,
|
|
12
|
-
useInvalidateUser,
|
|
13
13
|
useUserValidateSession
|
|
14
14
|
} from "../modules/auth/hooks/useUserQuery";
|
|
15
15
|
import { useLogin } from "../modules/auth/hooks/login.hook";
|
|
16
16
|
import { useRegister } from "../modules/auth/hooks/register.hook";
|
|
17
17
|
import { useLogout } from "../modules/auth/hooks/logout.hook";
|
|
18
|
-
import {
|
|
19
|
-
useCurrentAccount,
|
|
20
|
-
useInvalidateAccount
|
|
21
|
-
} from "../modules/accounts/hooks/current-account.hook";
|
|
18
|
+
import { useCurrentAccount } from "../modules/accounts/hooks/current-account.hook";
|
|
22
19
|
import { useRouter } from "next/navigation";
|
|
23
20
|
const AuthContext = createContext(
|
|
24
21
|
void 0
|
|
@@ -27,9 +24,8 @@ function AuthProvider({ children }) {
|
|
|
27
24
|
const { data: user, isLoading: isQueryLoading } = useUserQuery();
|
|
28
25
|
const { data: account, isLoading: isAccountLoading } = useCurrentAccount();
|
|
29
26
|
const { isLoading: isSessionLoading } = useUserValidateSession();
|
|
27
|
+
const queryClient = useQueryClient();
|
|
30
28
|
const setUserData = useSetUserData();
|
|
31
|
-
const invalidateUser = useInvalidateUser();
|
|
32
|
-
const invalidateAccount = useInvalidateAccount();
|
|
33
29
|
const router = useRouter();
|
|
34
30
|
const { mutateAsync: loginMutate, isPending: isLogging } = useLogin();
|
|
35
31
|
const { mutateAsync: registerMutate, isPending: isRegistering } = useRegister();
|
|
@@ -42,33 +38,31 @@ function AuthProvider({ children }) {
|
|
|
42
38
|
if (data.result === "two_factor_required") {
|
|
43
39
|
return { result: "two_factor_required", cookie: data.cookie };
|
|
44
40
|
}
|
|
41
|
+
queryClient.clear();
|
|
45
42
|
setUserData(data.user);
|
|
46
|
-
invalidateUser();
|
|
47
|
-
invalidateAccount();
|
|
48
43
|
return { result: "success" };
|
|
49
44
|
},
|
|
50
|
-
[
|
|
45
|
+
[queryClient, setUserData]
|
|
51
46
|
);
|
|
52
47
|
const register = useCallback(
|
|
53
48
|
async (data) => {
|
|
54
49
|
await registerMutate(data, {
|
|
55
50
|
onSuccess: (data2) => {
|
|
56
|
-
|
|
57
|
-
invalidateAccount();
|
|
51
|
+
queryClient.clear();
|
|
58
52
|
setUserData(data2.user);
|
|
59
53
|
}
|
|
60
54
|
});
|
|
61
55
|
},
|
|
62
|
-
[
|
|
56
|
+
[queryClient, setUserData]
|
|
63
57
|
);
|
|
64
58
|
const logout = useCallback(async () => {
|
|
65
59
|
await logoutMutate(void 0, {
|
|
66
60
|
onSuccess: () => {
|
|
67
|
-
|
|
61
|
+
queryClient.clear();
|
|
68
62
|
router.push("/login");
|
|
69
63
|
}
|
|
70
64
|
});
|
|
71
|
-
}, [
|
|
65
|
+
}, [queryClient, router]);
|
|
72
66
|
const value = useMemo(
|
|
73
67
|
() => ({
|
|
74
68
|
user: user ?? null,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/providers/auth.provider.tsx"],"sourcesContent":["\"use client\";\r\n\r\nimport {\r\n createContext,\r\n ReactNode,\r\n useCallback,\r\n useContext,\r\n useMemo,\r\n} from \"react\";\r\nimport {\r\n useUserQuery,\r\n useSetUserData,\r\n
|
|
1
|
+
{"version":3,"sources":["../../src/providers/auth.provider.tsx"],"sourcesContent":["\"use client\";\r\n\r\nimport {\r\n createContext,\r\n ReactNode,\r\n useCallback,\r\n useContext,\r\n useMemo,\r\n} from \"react\";\r\nimport { useQueryClient } from \"@tanstack/react-query\";\r\nimport {\r\n useUserQuery,\r\n useSetUserData,\r\n useUserValidateSession,\r\n} from \"../modules/auth/hooks/useUserQuery\";\r\nimport { useLogin } from \"../modules/auth/hooks/login.hook\";\r\nimport { useRegister } from \"../modules/auth/hooks/register.hook\";\r\nimport { useLogout } from \"../modules/auth/hooks/logout.hook\";\r\nimport {\r\n AuthState,\r\n LoginRequest,\r\n RegisterRequest,\r\n} from \"../modules/auth/schema\";\r\n\r\nexport type LoginResult =\r\n | { result: \"success\" }\r\n | { result: \"two_factor_required\"; cookie: string }\r\n | { result: \"error\"; message: string };\r\nimport { useCurrentAccount } from \"../modules/accounts/hooks/current-account.hook\";\r\nimport { useRouter } from \"next/navigation\";\r\n\r\ninterface AuthContextProps extends AuthState {\r\n login: (credentials: LoginRequest) => Promise<LoginResult>;\r\n register: (data: RegisterRequest) => Promise<void>;\r\n logout: () => Promise<void>;\r\n}\r\n\r\nexport const AuthContext = createContext<AuthContextProps | undefined>(\r\n undefined\r\n);\r\n\r\ninterface AuthProviderProps {\r\n children: ReactNode;\r\n}\r\n\r\nexport function AuthProvider({ children }: AuthProviderProps) {\r\n const { data: user, isLoading: isQueryLoading } = useUserQuery();\r\n const { data: account, isLoading: isAccountLoading } = useCurrentAccount();\r\n const { isLoading: isSessionLoading } = useUserValidateSession();\r\n\r\n const queryClient = useQueryClient();\r\n const setUserData = useSetUserData();\r\n\r\n const router = useRouter();\r\n\r\n const { mutateAsync: loginMutate, isPending: isLogging } = useLogin();\r\n const { mutateAsync: registerMutate, isPending: isRegistering } =\r\n useRegister();\r\n const { mutateAsync: logoutMutate, isPending: isLoggingOut } = useLogout();\r\n\r\n const isAuthenticated = !!user;\r\n const isLoading =\r\n isLogging ||\r\n isRegistering ||\r\n isLoggingOut ||\r\n isQueryLoading ||\r\n isAccountLoading ||\r\n isSessionLoading;\r\n\r\n const login = useCallback(\r\n async (credentials: LoginRequest): Promise<LoginResult> => {\r\n const data = await loginMutate(credentials);\r\n\r\n if (data.result === \"two_factor_required\") {\r\n return { result: \"two_factor_required\", cookie: data.cookie };\r\n }\r\n\r\n queryClient.clear();\r\n setUserData(data.user);\r\n\r\n return { result: \"success\" };\r\n },\r\n [queryClient, setUserData]\r\n );\r\n\r\n const register = useCallback(\r\n async (data: RegisterRequest): Promise<void> => {\r\n await registerMutate(data, {\r\n onSuccess: (data) => {\r\n queryClient.clear();\r\n setUserData(data.user);\r\n },\r\n });\r\n },\r\n [queryClient, setUserData]\r\n );\r\n\r\n const logout = useCallback(async (): Promise<void> => {\r\n await logoutMutate(undefined, {\r\n onSuccess: () => {\r\n queryClient.clear();\r\n router.push(\"/login\");\r\n },\r\n });\r\n }, [queryClient, router]);\r\n\r\n const value: AuthContextProps = useMemo(\r\n () => ({\r\n user: user ?? null,\r\n account: account ?? null,\r\n isAuthenticated,\r\n isLoading,\r\n login,\r\n register,\r\n logout,\r\n }),\r\n [user, isAuthenticated, isLoading, login, register, logout]\r\n );\r\n\r\n return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\r\n}\r\n\r\nexport function useAuth(): AuthContextProps {\r\n const context = useContext(AuthContext);\r\n\r\n if (context === undefined) {\r\n throw new Error(\"useAuth deve ser usado dentro de um AuthProvider\");\r\n }\r\n\r\n return context;\r\n}\r\n"],"mappings":";AAuHS;AArHT;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,sBAAsB;AAC/B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAC5B,SAAS,iBAAiB;AAW1B,SAAS,yBAAyB;AAClC,SAAS,iBAAiB;AAQnB,MAAM,cAAc;AAAA,EACzB;AACF;AAMO,SAAS,aAAa,EAAE,SAAS,GAAsB;AAC5D,QAAM,EAAE,MAAM,MAAM,WAAW,eAAe,IAAI,aAAa;AAC/D,QAAM,EAAE,MAAM,SAAS,WAAW,iBAAiB,IAAI,kBAAkB;AACzE,QAAM,EAAE,WAAW,iBAAiB,IAAI,uBAAuB;AAE/D,QAAM,cAAc,eAAe;AACnC,QAAM,cAAc,eAAe;AAEnC,QAAM,SAAS,UAAU;AAEzB,QAAM,EAAE,aAAa,aAAa,WAAW,UAAU,IAAI,SAAS;AACpE,QAAM,EAAE,aAAa,gBAAgB,WAAW,cAAc,IAC5D,YAAY;AACd,QAAM,EAAE,aAAa,cAAc,WAAW,aAAa,IAAI,UAAU;AAEzE,QAAM,kBAAkB,CAAC,CAAC;AAC1B,QAAM,YACJ,aACA,iBACA,gBACA,kBACA,oBACA;AAEF,QAAM,QAAQ;AAAA,IACZ,OAAO,gBAAoD;AACzD,YAAM,OAAO,MAAM,YAAY,WAAW;AAE1C,UAAI,KAAK,WAAW,uBAAuB;AACzC,eAAO,EAAE,QAAQ,uBAAuB,QAAQ,KAAK,OAAO;AAAA,MAC9D;AAEA,kBAAY,MAAM;AAClB,kBAAY,KAAK,IAAI;AAErB,aAAO,EAAE,QAAQ,UAAU;AAAA,IAC7B;AAAA,IACA,CAAC,aAAa,WAAW;AAAA,EAC3B;AAEA,QAAM,WAAW;AAAA,IACf,OAAO,SAAyC;AAC9C,YAAM,eAAe,MAAM;AAAA,QACzB,WAAW,CAACA,UAAS;AACnB,sBAAY,MAAM;AAClB,sBAAYA,MAAK,IAAI;AAAA,QACvB;AAAA,MACF,CAAC;AAAA,IACH;AAAA,IACA,CAAC,aAAa,WAAW;AAAA,EAC3B;AAEA,QAAM,SAAS,YAAY,YAA2B;AACpD,UAAM,aAAa,QAAW;AAAA,MAC5B,WAAW,MAAM;AACf,oBAAY,MAAM;AAClB,eAAO,KAAK,QAAQ;AAAA,MACtB;AAAA,IACF,CAAC;AAAA,EACH,GAAG,CAAC,aAAa,MAAM,CAAC;AAExB,QAAM,QAA0B;AAAA,IAC9B,OAAO;AAAA,MACL,MAAM,QAAQ;AAAA,MACd,SAAS,WAAW;AAAA,MACpB;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,CAAC,MAAM,iBAAiB,WAAW,OAAO,UAAU,MAAM;AAAA,EAC5D;AAEA,SAAO,oBAAC,YAAY,UAAZ,EAAqB,OAAe,UAAS;AACvD;AAEO,SAAS,UAA4B;AAC1C,QAAM,UAAU,WAAW,WAAW;AAEtC,MAAI,YAAY,QAAW;AACzB,UAAM,IAAI,MAAM,kDAAkD;AAAA,EACpE;AAEA,SAAO;AACT;","names":["data"]}
|
|
@@ -1,22 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { isRedirectError } from "next/dist/client/components/redirect-error";
|
|
2
|
+
import { ApiError } from "../infra/api/types";
|
|
3
|
+
async function safeServerAction(fn) {
|
|
4
|
+
try {
|
|
5
|
+
const data = await fn();
|
|
6
|
+
return { success: true, data };
|
|
7
|
+
} catch (error) {
|
|
8
|
+
if (isRedirectError(error)) throw error;
|
|
9
|
+
const message = error instanceof ApiError ? error.message : error instanceof Error ? error.message : "Erro desconhecido";
|
|
10
|
+
return { success: false, error: message };
|
|
7
11
|
}
|
|
8
12
|
}
|
|
9
|
-
async function safeServerAction(action, ...args) {
|
|
10
|
-
const result = await action(...args);
|
|
11
|
-
if (result.status === 0 || result.result === "error" || result.error) {
|
|
12
|
-
const msg = result.message || result.error || "Erro na a\xE7\xE3o";
|
|
13
|
-
throw new ServerActionError(msg, result.status || 0);
|
|
14
|
-
}
|
|
15
|
-
const { error, ...successData } = result;
|
|
16
|
-
return successData;
|
|
17
|
-
}
|
|
18
13
|
export {
|
|
19
|
-
ServerActionError,
|
|
20
14
|
safeServerAction
|
|
21
15
|
};
|
|
22
16
|
//# sourceMappingURL=safeServerAction.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/safeServerAction.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../../src/utils/safeServerAction.ts"],"sourcesContent":["import { isRedirectError } from 'next/dist/client/components/redirect-error';\nimport { ApiError } from '../infra/api/types';\nimport type { ActionResult } from '../infra/api/types';\n\nexport async function safeServerAction<T>(\n fn: () => Promise<T>\n): Promise<ActionResult<T>> {\n try {\n const data = await fn();\n return { success: true, data };\n } catch (error) {\n if (isRedirectError(error)) throw error;\n const message =\n error instanceof ApiError\n ? error.message\n : error instanceof Error\n ? error.message\n : 'Erro desconhecido';\n return { success: false, error: message };\n }\n}\n"],"mappings":"AAAA,SAAS,uBAAuB;AAChC,SAAS,gBAAgB;AAGzB,eAAsB,iBACpB,IAC0B;AAC1B,MAAI;AACF,UAAM,OAAO,MAAM,GAAG;AACtB,WAAO,EAAE,SAAS,MAAM,KAAK;AAAA,EAC/B,SAAS,OAAO;AACd,QAAI,gBAAgB,KAAK,EAAG,OAAM;AAClC,UAAM,UACJ,iBAAiB,WACb,MAAM,UACN,iBAAiB,QACf,MAAM,UACN;AACR,WAAO,EAAE,SAAS,OAAO,OAAO,QAAQ;AAAA,EAC1C;AACF;","names":[]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function withAction(action) {
|
|
2
|
+
return async (...args) => {
|
|
3
|
+
const result = await action(...args);
|
|
4
|
+
if (!result.success) {
|
|
5
|
+
throw new Error(result.error ?? "Erro desconhecido");
|
|
6
|
+
}
|
|
7
|
+
return result.data;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
withAction
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=withAction.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/withAction.ts"],"sourcesContent":["import type { ActionResult } from '../infra/api/types';\n\nexport function withAction<TArgs extends any[], TData>(\n action: (...args: TArgs) => Promise<ActionResult<TData>>\n): (...args: TArgs) => Promise<TData> {\n return async (...args) => {\n const result = await action(...args);\n if (!result.success) {\n throw new Error(result.error ?? 'Erro desconhecido');\n }\n return result.data as TData;\n };\n}\n"],"mappings":"AAEO,SAAS,WACd,QACoC;AACpC,SAAO,UAAU,SAAS;AACxB,UAAM,SAAS,MAAM,OAAO,GAAG,IAAI;AACnC,QAAI,CAAC,OAAO,SAAS;AACnB,YAAM,IAAI,MAAM,OAAO,SAAS,mBAAmB;AAAA,IACrD;AACA,WAAO,OAAO;AAAA,EAChB;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,114 +1,101 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useState, useCallback, useEffect } from 'react';
|
|
4
|
-
import { useMutation } from '@tanstack/react-query';
|
|
5
|
-
import { toast } from 'sonner';
|
|
6
|
-
import { Toast } from '../../ui/feedback/Toast';
|
|
7
|
-
import { copyToClipboard } from '../../../utils/browser/clipboard';
|
|
8
|
-
import {
|
|
9
|
-
generateTwoFactorAction,
|
|
10
|
-
confirmTwoFactorAction,
|
|
11
|
-
} from '../../../modules/accounts/actions/account-management.action';
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const [
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
generateMutation.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
isGenerating: generateMutation.isPending,
|
|
103
|
-
isConfirming: confirmMutation.isPending,
|
|
104
|
-
generateFailed,
|
|
105
|
-
retryGenerate: triggerGenerate,
|
|
106
|
-
qrcode: generateMutation.data?.qrcode,
|
|
107
|
-
backupCodes: generateMutation.data?.backupCodes ?? [],
|
|
108
|
-
step,
|
|
109
|
-
handleSubmit,
|
|
110
|
-
handleFinalize,
|
|
111
|
-
handleCopyCodes,
|
|
112
|
-
resetForm,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState, useCallback, useEffect } from 'react';
|
|
4
|
+
import { useMutation } from '@tanstack/react-query';
|
|
5
|
+
import { toast } from 'sonner';
|
|
6
|
+
import { Toast } from '../../ui/feedback/Toast';
|
|
7
|
+
import { copyToClipboard } from '../../../utils/browser/clipboard';
|
|
8
|
+
import {
|
|
9
|
+
generateTwoFactorAction,
|
|
10
|
+
confirmTwoFactorAction,
|
|
11
|
+
} from '../../../modules/accounts/actions/account-management.action';
|
|
12
|
+
import { withAction } from '../../../utils/withAction';
|
|
13
|
+
|
|
14
|
+
type Step = 'code' | 'recovery';
|
|
15
|
+
|
|
16
|
+
interface UseTwoFactorAuthFormProps {
|
|
17
|
+
onSuccess?: () => void;
|
|
18
|
+
open?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default function useTwoFactorAuthForm({ onSuccess, open }: UseTwoFactorAuthFormProps = {}) {
|
|
22
|
+
const [code, setCode] = useState('');
|
|
23
|
+
const [step, setStep] = useState<Step>('code');
|
|
24
|
+
|
|
25
|
+
const generateMutation = useMutation({
|
|
26
|
+
mutationFn: withAction(generateTwoFactorAction),
|
|
27
|
+
onError: () => {
|
|
28
|
+
toast.custom((t) => (
|
|
29
|
+
<Toast variant="error" message="Erro ao gerar QR Code. Tente novamente." toastId={t} />
|
|
30
|
+
));
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const confirmMutation = useMutation({
|
|
35
|
+
mutationFn: withAction(confirmTwoFactorAction),
|
|
36
|
+
onSuccess: () => {
|
|
37
|
+
setStep('recovery');
|
|
38
|
+
},
|
|
39
|
+
onError: () => {
|
|
40
|
+
toast.custom((t) => (
|
|
41
|
+
<Toast variant="error" message="Erro ao confirmar código. Tente novamente." toastId={t} />
|
|
42
|
+
));
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const { mutate: triggerGenerate } = generateMutation;
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (open) {
|
|
50
|
+
triggerGenerate();
|
|
51
|
+
}
|
|
52
|
+
}, [open, triggerGenerate]);
|
|
53
|
+
|
|
54
|
+
const generateFailed = generateMutation.isError;
|
|
55
|
+
|
|
56
|
+
const isCodeComplete = code.length === 6;
|
|
57
|
+
|
|
58
|
+
const handleSubmit = () => {
|
|
59
|
+
if (!isCodeComplete) return;
|
|
60
|
+
confirmMutation.mutate(code);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const handleFinalize = () => {
|
|
64
|
+
onSuccess?.();
|
|
65
|
+
setTimeout(resetForm, 300);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const handleCopyCodes = useCallback(async () => {
|
|
69
|
+
const codes = generateMutation.data?.backupCodes ?? [];
|
|
70
|
+
const success = await copyToClipboard(codes.join('\n'));
|
|
71
|
+
if (success) {
|
|
72
|
+
toast.custom((t) => (
|
|
73
|
+
<Toast variant="success" message="Códigos copiados com sucesso" toastId={t} />
|
|
74
|
+
));
|
|
75
|
+
}
|
|
76
|
+
}, [generateMutation.data]);
|
|
77
|
+
|
|
78
|
+
const resetForm = () => {
|
|
79
|
+
setCode('');
|
|
80
|
+
setStep('code');
|
|
81
|
+
generateMutation.reset();
|
|
82
|
+
confirmMutation.reset();
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
code,
|
|
87
|
+
setCode,
|
|
88
|
+
isCodeComplete,
|
|
89
|
+
isGenerating: generateMutation.isPending,
|
|
90
|
+
isConfirming: confirmMutation.isPending,
|
|
91
|
+
generateFailed,
|
|
92
|
+
retryGenerate: triggerGenerate,
|
|
93
|
+
qrcode: generateMutation.data?.qrcode,
|
|
94
|
+
backupCodes: generateMutation.data?.backupCodes ?? [],
|
|
95
|
+
step,
|
|
96
|
+
handleSubmit,
|
|
97
|
+
handleFinalize,
|
|
98
|
+
handleCopyCodes,
|
|
99
|
+
resetForm,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
@@ -84,47 +84,50 @@ export function MyProfileSection({ onClose, storageUrl }: MyProfileSectionProps)
|
|
|
84
84
|
};
|
|
85
85
|
|
|
86
86
|
const handleDelete = async () => {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
87
|
+
try {
|
|
88
|
+
if (isOwner) {
|
|
89
|
+
await deleteAccount.mutateAsync();
|
|
90
|
+
} else {
|
|
91
|
+
await deleteAccountUser.mutateAsync(user!.id);
|
|
92
|
+
}
|
|
93
|
+
await logout();
|
|
94
|
+
} catch (error) {
|
|
92
95
|
toast.custom((t) => (
|
|
93
96
|
<Toast
|
|
94
97
|
variant="error"
|
|
95
|
-
message={
|
|
98
|
+
message={error instanceof Error ? error.message : 'Erro ao excluir. Tente novamente.'}
|
|
96
99
|
toastId={t}
|
|
97
100
|
/>
|
|
98
101
|
));
|
|
99
102
|
setDeleteConfirmOpen(false);
|
|
100
|
-
return;
|
|
101
103
|
}
|
|
102
|
-
|
|
103
|
-
await logout();
|
|
104
104
|
};
|
|
105
105
|
|
|
106
106
|
const onSubmit = async (data: MyProfileFormData) => {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
107
|
+
try {
|
|
108
|
+
await updateAccountUser.mutateAsync({
|
|
109
|
+
name: data.name,
|
|
110
|
+
gender: GENDER_OPTIONS.find((g) => g.value === data.gender)?.apiValue,
|
|
111
|
+
...(selectedPhoto ? { photo: selectedPhoto } : {}),
|
|
112
|
+
...(photoRemoved ? { photo: '' } : {}),
|
|
113
|
+
});
|
|
113
114
|
|
|
114
|
-
|
|
115
|
+
form.reset(data);
|
|
116
|
+
setSelectedPhoto(null);
|
|
117
|
+
setPhotoRemoved(false);
|
|
115
118
|
toast.custom((t) => (
|
|
116
|
-
<Toast variant="
|
|
119
|
+
<Toast variant="success" message="Perfil atualizado com sucesso" toastId={t} />
|
|
120
|
+
));
|
|
121
|
+
onClose();
|
|
122
|
+
} catch (error) {
|
|
123
|
+
toast.custom((t) => (
|
|
124
|
+
<Toast
|
|
125
|
+
variant="error"
|
|
126
|
+
message={error instanceof Error ? error.message : 'Erro ao atualizar perfil'}
|
|
127
|
+
toastId={t}
|
|
128
|
+
/>
|
|
117
129
|
));
|
|
118
|
-
return;
|
|
119
130
|
}
|
|
120
|
-
|
|
121
|
-
form.reset(data);
|
|
122
|
-
setSelectedPhoto(null);
|
|
123
|
-
setPhotoRemoved(false);
|
|
124
|
-
toast.custom((t) => (
|
|
125
|
-
<Toast variant="success" message="Perfil atualizado com sucesso" toastId={t} />
|
|
126
|
-
));
|
|
127
|
-
onClose();
|
|
128
131
|
};
|
|
129
132
|
|
|
130
133
|
return (
|
|
@@ -87,28 +87,14 @@ export function PreferencesSection({ onClose }: PreferencesSectionProps) {
|
|
|
87
87
|
setIsSaving(true);
|
|
88
88
|
try {
|
|
89
89
|
const apiLanguage = LANGUAGE_OPTIONS.find((o) => o.value === data.language)?.apiValue ?? data.language.toLowerCase();
|
|
90
|
-
|
|
90
|
+
await updateAccountUser.mutateAsync({
|
|
91
91
|
language: apiLanguage,
|
|
92
92
|
receive_sms: data.receive_sms,
|
|
93
93
|
receive_email: data.receive_email,
|
|
94
94
|
});
|
|
95
95
|
|
|
96
|
-
if (!userResult.success) {
|
|
97
|
-
toast.custom((t) => (
|
|
98
|
-
<Toast variant="error" message={userResult.error ?? 'Erro ao salvar preferências'} toastId={t} />
|
|
99
|
-
));
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
96
|
if (canEditPreferences && Object.keys(accountFields).length > 0) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (!accountResult.success) {
|
|
107
|
-
toast.custom((t) => (
|
|
108
|
-
<Toast variant="error" message={accountResult.error ?? 'Erro ao salvar preferências da conta'} toastId={t} />
|
|
109
|
-
));
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
97
|
+
await updateAccount.mutateAsync(accountFields);
|
|
112
98
|
}
|
|
113
99
|
|
|
114
100
|
reset(data);
|
|
@@ -116,6 +102,14 @@ export function PreferencesSection({ onClose }: PreferencesSectionProps) {
|
|
|
116
102
|
<Toast variant="success" message="Preferências salvas com sucesso" toastId={t} />
|
|
117
103
|
));
|
|
118
104
|
onClose();
|
|
105
|
+
} catch (error) {
|
|
106
|
+
toast.custom((t) => (
|
|
107
|
+
<Toast
|
|
108
|
+
variant="error"
|
|
109
|
+
message={error instanceof Error ? error.message : 'Erro ao salvar preferências'}
|
|
110
|
+
toastId={t}
|
|
111
|
+
/>
|
|
112
|
+
));
|
|
119
113
|
} finally {
|
|
120
114
|
setIsSaving(false);
|
|
121
115
|
}
|
package/src/index.ts
CHANGED
|
@@ -74,6 +74,7 @@ export type {
|
|
|
74
74
|
export { cn } from "./infra/utils/clsx";
|
|
75
75
|
export { buildQueryParams } from "./infra/utils/params";
|
|
76
76
|
export { parseSchema, parseResult } from "./infra/utils/parser";
|
|
77
|
+
export { withAction } from "./utils/withAction";
|
|
77
78
|
|
|
78
79
|
// Layout
|
|
79
80
|
export {
|