@greatapps/greatauth-ui 0.3.7 → 0.3.9

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.
@@ -0,0 +1,82 @@
1
+ "use client";
2
+
3
+ import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
4
+ import type { GauthUser, GauthUserHookConfig, ApiResponse } from "../types/users";
5
+
6
+ function buildUrl(config: GauthUserHookConfig, path: string): string {
7
+ const lang = config.language || "pt-br";
8
+ const idWl = config.idWl || "1";
9
+ return `${config.gauthApiUrl}/v1/${lang}/${idWl}/accounts/${config.accountId}/${path}`;
10
+ }
11
+
12
+ async function request<T>(url: string, token: string, options?: RequestInit): Promise<ApiResponse<T>> {
13
+ const res = await fetch(url, {
14
+ headers: {
15
+ "Content-Type": "application/json",
16
+ Authorization: `Bearer ${token}`,
17
+ },
18
+ ...options,
19
+ });
20
+ if (!res.ok) {
21
+ let message = `HTTP ${res.status}`;
22
+ try { const b = await res.json(); message = b.message || message; } catch {}
23
+ throw new Error(message);
24
+ }
25
+ const json = await res.json();
26
+ if (json.status === 0) throw new Error(json.message || "Operação falhou");
27
+ return json;
28
+ }
29
+
30
+ export function useUsers(config: GauthUserHookConfig, params?: Record<string, string>) {
31
+ return useQuery({
32
+ queryKey: ["greatauth", "users", config.accountId, params],
33
+ queryFn: () => {
34
+ const qs = params ? "?" + new URLSearchParams(params).toString() : "";
35
+ return request<GauthUser[]>(buildUrl(config, `users${qs}`), config.token!);
36
+ },
37
+ enabled: !!config.token && !!config.accountId,
38
+ select: (res) => ({ data: res.data || [], total: res.total || 0 }),
39
+ });
40
+ }
41
+
42
+ export function useCreateUser(config: GauthUserHookConfig) {
43
+ const qc = useQueryClient();
44
+ return useMutation({
45
+ mutationFn: (data: { name: string; last_name?: string; email: string; password?: string; profile?: string; photo?: string | null }) =>
46
+ request<GauthUser>(buildUrl(config, "users"), config.token!, {
47
+ method: "POST",
48
+ body: JSON.stringify(data),
49
+ }),
50
+ onSuccess: () => qc.invalidateQueries({ queryKey: ["greatauth", "users"] }),
51
+ });
52
+ }
53
+
54
+ export function useUpdateUser(config: GauthUserHookConfig) {
55
+ const qc = useQueryClient();
56
+ return useMutation({
57
+ mutationFn: ({ id, body }: { id: number; body: Record<string, unknown> }) =>
58
+ request<GauthUser>(buildUrl(config, `users/${id}`), config.token!, {
59
+ method: "PUT",
60
+ body: JSON.stringify(body),
61
+ }),
62
+ onSuccess: () => qc.invalidateQueries({ queryKey: ["greatauth", "users"] }),
63
+ });
64
+ }
65
+
66
+ export function useDeleteUser(config: GauthUserHookConfig) {
67
+ const qc = useQueryClient();
68
+ return useMutation({
69
+ mutationFn: (id: number) =>
70
+ request<void>(buildUrl(config, `users/${id}`), config.token!, { method: "DELETE" }),
71
+ onSuccess: () => qc.invalidateQueries({ queryKey: ["greatauth", "users"] }),
72
+ });
73
+ }
74
+
75
+ export function useResetPassword(config: GauthUserHookConfig) {
76
+ const qc = useQueryClient();
77
+ return useMutation({
78
+ mutationFn: (id: number) =>
79
+ request<{ message: string }>(buildUrl(config, `users/${id}/reset-password`), config.token!, { method: "POST" }),
80
+ onSuccess: () => qc.invalidateQueries({ queryKey: ["greatauth", "users"] }),
81
+ });
82
+ }
package/src/index.ts CHANGED
@@ -24,5 +24,21 @@ export type { ImageCropUploadProps } from "./components";
24
24
  export { SidebarProvider, SidebarInset, SidebarTrigger, useSidebar } from "./components/ui/sidebar";
25
25
  export { TooltipProvider } from "./components/ui/tooltip";
26
26
 
27
+ // User Types
28
+ export type { GauthUser, UserProfile, UserPhone, PhoneLabel, GauthUserHookConfig } from "./types/users";
29
+
30
+ // User Hooks
31
+ export { useUsers, useCreateUser, useUpdateUser, useDeleteUser, useResetPassword } from "./hooks/use-users";
32
+
33
+ // User Components
34
+ export { UsersPage } from "./components/users/users-page";
35
+ export type { UsersPageProps } from "./components/users/users-page";
36
+ export { UserFormDialog } from "./components/users/user-form-dialog";
37
+ export type { UserFormDialogProps } from "./components/users/user-form-dialog";
38
+ export { UserProfileBadge } from "./components/users/user-profile-badge";
39
+ export type { UserProfileBadgeProps } from "./components/users/user-profile-badge";
40
+ export { DataTable } from "./components/users/data-table";
41
+ export type { DataTableProps } from "./components/users/data-table";
42
+
27
43
  // Utils
28
44
  export { cn } from "./lib/utils";
@@ -0,0 +1,46 @@
1
+ export type UserProfile = "viewer" | "collaborator" | "admin" | "owner" | "attendant";
2
+
3
+ export type PhoneLabel = "celular" | "comercial" | "residencial" | "outro";
4
+
5
+ export interface GauthUser {
6
+ id: number;
7
+ id_account: number;
8
+ name: string;
9
+ last_name: string;
10
+ email: string;
11
+ profile: UserProfile;
12
+ photo: string | null;
13
+ last_login: string | null;
14
+ deleted: number;
15
+ datetime_add: string;
16
+ datetime_alt: string | null;
17
+ datetime_del: string | null;
18
+ }
19
+
20
+ export interface UserPhone {
21
+ id: number;
22
+ id_account: number;
23
+ id_user: number;
24
+ phone_number: string;
25
+ label: PhoneLabel;
26
+ is_primary: number;
27
+ deleted: number;
28
+ datetime_add: string;
29
+ datetime_alt: string | null;
30
+ datetime_del: string | null;
31
+ }
32
+
33
+ export interface GauthUserHookConfig {
34
+ token: string | null;
35
+ accountId: number | string | null;
36
+ gauthApiUrl: string;
37
+ language?: string;
38
+ idWl?: string | number;
39
+ }
40
+
41
+ export interface ApiResponse<T = unknown> {
42
+ status: 0 | 1;
43
+ message: string;
44
+ data: T;
45
+ total?: number;
46
+ }