mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-01-25 02:08:11 +00:00
feat(roles): RBAC functionality (#6201)
This commit is contained in:
@@ -53,6 +53,7 @@ export const sendInvite = async (formData: FormData) => {
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
|
||||
const email = formData.get("email");
|
||||
const role = formData.get("role");
|
||||
const url = new URL(`${keyServer}/tenants/invitations`);
|
||||
|
||||
const body = JSON.stringify({
|
||||
@@ -61,7 +62,18 @@ export const sendInvite = async (formData: FormData) => {
|
||||
attributes: {
|
||||
email,
|
||||
},
|
||||
relationships: {},
|
||||
relationships: {
|
||||
roles: {
|
||||
data: role
|
||||
? [
|
||||
{
|
||||
id: role,
|
||||
type: "roles",
|
||||
},
|
||||
]
|
||||
: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -91,10 +103,40 @@ export const updateInvite = async (formData: FormData) => {
|
||||
|
||||
const invitationId = formData.get("invitationId");
|
||||
const invitationEmail = formData.get("invitationEmail");
|
||||
const expiresAt = formData.get("expires_at");
|
||||
const roleId = formData.get("role");
|
||||
const expiresAt =
|
||||
formData.get("expires_at") ||
|
||||
new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString();
|
||||
|
||||
const url = new URL(`${keyServer}/tenants/invitations/${invitationId}`);
|
||||
|
||||
const body: any = {
|
||||
data: {
|
||||
type: "invitations",
|
||||
id: invitationId,
|
||||
attributes: {},
|
||||
relationships: {},
|
||||
},
|
||||
};
|
||||
|
||||
// Only add attributes that exist in the formData
|
||||
if (invitationEmail) {
|
||||
body.data.attributes.email = invitationEmail;
|
||||
}
|
||||
if (expiresAt) {
|
||||
body.data.attributes.expires_at = expiresAt;
|
||||
}
|
||||
if (roleId) {
|
||||
body.data.relationships.roles = {
|
||||
data: [
|
||||
{
|
||||
id: roleId,
|
||||
type: "roles",
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "PATCH",
|
||||
@@ -103,23 +145,18 @@ export const updateInvite = async (formData: FormData) => {
|
||||
Accept: "application/vnd.api+json",
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
data: {
|
||||
type: "invitations",
|
||||
id: invitationId,
|
||||
attributes: {
|
||||
email: invitationEmail,
|
||||
...(expiresAt && { expires_at: expiresAt }),
|
||||
},
|
||||
},
|
||||
}),
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
return { error };
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
revalidatePath("/invitations");
|
||||
return parseStringify(data);
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("Error updating invitation:", error);
|
||||
return {
|
||||
error: getErrorMessage(error),
|
||||
};
|
||||
|
||||
1
ui/actions/manage-groups/index.ts
Normal file
1
ui/actions/manage-groups/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./manage-groups";
|
||||
238
ui/actions/manage-groups/manage-groups.ts
Normal file
238
ui/actions/manage-groups/manage-groups.ts
Normal file
@@ -0,0 +1,238 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { auth } from "@/auth.config";
|
||||
import { getErrorMessage, parseStringify, wait } from "@/lib";
|
||||
import { ManageGroupPayload, ProviderGroupsResponse } from "@/types/components";
|
||||
|
||||
export const getProviderGroups = async ({
|
||||
page = 1,
|
||||
query = "",
|
||||
sort = "",
|
||||
filters = {},
|
||||
}: {
|
||||
page?: number;
|
||||
query?: string;
|
||||
sort?: string;
|
||||
filters?: Record<string, string | number>;
|
||||
}): Promise<ProviderGroupsResponse | undefined> => {
|
||||
const session = await auth();
|
||||
|
||||
if (isNaN(Number(page)) || page < 1) redirect("/manage-groups");
|
||||
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
const url = new URL(`${keyServer}/provider-groups`);
|
||||
|
||||
if (page) url.searchParams.append("page[number]", page.toString());
|
||||
if (query) url.searchParams.append("filter[search]", query);
|
||||
if (sort) url.searchParams.append("sort", sort);
|
||||
|
||||
// Handle multiple filters
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
if (key !== "filter[search]") {
|
||||
url.searchParams.append(key, String(value));
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetch(url.toString(), {
|
||||
headers: {
|
||||
Accept: "application/vnd.api+json",
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Error fetching provider groups: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data: ProviderGroupsResponse = await response.json();
|
||||
const parsedData = parseStringify(data);
|
||||
revalidatePath("/manage-groups");
|
||||
return parsedData;
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("Error fetching provider groups:", error);
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
export const getProviderGroupInfoById = async (providerGroupId: string) => {
|
||||
const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
const url = new URL(`${keyServer}/provider-groups/${providerGroupId}`);
|
||||
|
||||
try {
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "application/vnd.api+json",
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Failed to fetch provider group info: ${response.statusText}`,
|
||||
);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return parseStringify(data);
|
||||
} catch (error) {
|
||||
return {
|
||||
error: getErrorMessage(error),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const createProviderGroup = async (formData: FormData) => {
|
||||
const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
|
||||
const name = formData.get("name") as string;
|
||||
const providersJson = formData.get("providers") as string;
|
||||
const rolesJson = formData.get("roles") as string;
|
||||
|
||||
// Parse JSON strings and handle empty cases
|
||||
const providers = providersJson ? JSON.parse(providersJson) : [];
|
||||
const roles = rolesJson ? JSON.parse(rolesJson) : [];
|
||||
|
||||
// Prepare base payload
|
||||
const payload: any = {
|
||||
data: {
|
||||
type: "provider-groups",
|
||||
attributes: {
|
||||
name,
|
||||
},
|
||||
relationships: {},
|
||||
},
|
||||
};
|
||||
|
||||
// Add relationships only if there are items
|
||||
if (providers.length > 0) {
|
||||
payload.data.relationships.providers = {
|
||||
data: providers,
|
||||
};
|
||||
}
|
||||
|
||||
if (roles.length > 0) {
|
||||
payload.data.relationships.roles = {
|
||||
data: roles,
|
||||
};
|
||||
}
|
||||
|
||||
const body = JSON.stringify(payload);
|
||||
|
||||
try {
|
||||
const url = new URL(`${keyServer}/provider-groups`);
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/vnd.api+json",
|
||||
Accept: "application/vnd.api+json",
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
body,
|
||||
});
|
||||
const data = await response.json();
|
||||
revalidatePath("/manage-groups");
|
||||
return parseStringify(data);
|
||||
} catch (error) {
|
||||
return {
|
||||
error: getErrorMessage(error),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const updateProviderGroup = async (
|
||||
providerGroupId: string,
|
||||
formData: FormData,
|
||||
) => {
|
||||
const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
|
||||
const name = formData.get("name") as string;
|
||||
const providersJson = formData.get("providers") as string;
|
||||
const rolesJson = formData.get("roles") as string;
|
||||
|
||||
const providers = providersJson ? JSON.parse(providersJson) : [];
|
||||
const roles = rolesJson ? JSON.parse(rolesJson) : [];
|
||||
|
||||
const payload: ManageGroupPayload = {
|
||||
data: {
|
||||
type: "provider-groups",
|
||||
id: providerGroupId,
|
||||
attributes: name ? { name } : undefined,
|
||||
relationships: {},
|
||||
},
|
||||
};
|
||||
|
||||
// Add relationships only if there are items
|
||||
if (providers.length > 0) {
|
||||
payload.data.relationships!.providers = { data: providers };
|
||||
}
|
||||
|
||||
if (roles.length > 0) {
|
||||
payload.data.relationships!.roles = { data: roles };
|
||||
}
|
||||
|
||||
try {
|
||||
const url = `${keyServer}/provider-groups/${providerGroupId}`;
|
||||
const response = await fetch(url, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/vnd.api+json",
|
||||
Accept: "application/vnd.api+json",
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorResponse = await response.json();
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("Error response:", errorResponse);
|
||||
throw new Error(
|
||||
`Failed to update provider group: ${response.status} ${response.statusText}`,
|
||||
);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
revalidatePath("/manage-groups");
|
||||
return parseStringify(data);
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("Unexpected error:", error);
|
||||
return {
|
||||
error: getErrorMessage(error),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteProviderGroup = async (formData: FormData) => {
|
||||
const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
|
||||
const providerGroupId = formData.get("id");
|
||||
const url = new URL(`${keyServer}/provider-groups/${providerGroupId}`);
|
||||
|
||||
try {
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
});
|
||||
const data = await response.json();
|
||||
await wait(2000);
|
||||
revalidatePath("/manage-groups");
|
||||
return parseStringify(data);
|
||||
} catch (error) {
|
||||
return {
|
||||
error: getErrorMessage(error),
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -17,7 +17,7 @@ export const getProviders = async ({
|
||||
if (isNaN(Number(page)) || page < 1) redirect("/providers");
|
||||
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
const url = new URL(`${keyServer}/providers`);
|
||||
const url = new URL(`${keyServer}/providers?include=provider_groups`);
|
||||
|
||||
if (page) url.searchParams.append("page[number]", page.toString());
|
||||
if (query) url.searchParams.append("filter[search]", query);
|
||||
|
||||
1
ui/actions/roles/index.ts
Normal file
1
ui/actions/roles/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./roles";
|
||||
223
ui/actions/roles/roles.ts
Normal file
223
ui/actions/roles/roles.ts
Normal file
@@ -0,0 +1,223 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { auth } from "@/auth.config";
|
||||
import { getErrorMessage, parseStringify } from "@/lib";
|
||||
|
||||
export const getRoles = async ({
|
||||
page = 1,
|
||||
query = "",
|
||||
sort = "",
|
||||
filters = {},
|
||||
}) => {
|
||||
const session = await auth();
|
||||
|
||||
if (isNaN(Number(page)) || page < 1) redirect("/roles");
|
||||
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
const url = new URL(`${keyServer}/roles`);
|
||||
|
||||
if (page) url.searchParams.append("page[number]", page.toString());
|
||||
if (query) url.searchParams.append("filter[search]", query);
|
||||
if (sort) url.searchParams.append("sort", sort);
|
||||
|
||||
// Handle multiple filters
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
if (key !== "filter[search]") {
|
||||
url.searchParams.append(key, String(value));
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const invitations = await fetch(url.toString(), {
|
||||
headers: {
|
||||
Accept: "application/vnd.api+json",
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
});
|
||||
const data = await invitations.json();
|
||||
const parsedData = parseStringify(data);
|
||||
revalidatePath("/roles");
|
||||
return parsedData;
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("Error fetching roles:", error);
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
export const getRoleInfoById = async (roleId: string) => {
|
||||
const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
const url = new URL(`${keyServer}/roles/${roleId}`);
|
||||
|
||||
try {
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "application/vnd.api+json",
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch role info: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return parseStringify(data);
|
||||
} catch (error) {
|
||||
return {
|
||||
error: getErrorMessage(error),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const addRole = async (formData: FormData) => {
|
||||
const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
|
||||
const name = formData.get("name") as string;
|
||||
const groups = formData.getAll("groups[]") as string[];
|
||||
// Prepare base payload
|
||||
const payload: any = {
|
||||
data: {
|
||||
type: "roles",
|
||||
attributes: {
|
||||
name,
|
||||
manage_users: formData.get("manage_users") === "true",
|
||||
manage_account: formData.get("manage_account") === "true",
|
||||
manage_billing: formData.get("manage_billing") === "true",
|
||||
manage_providers: formData.get("manage_providers") === "true",
|
||||
manage_integrations: formData.get("manage_integrations") === "true",
|
||||
manage_scans: formData.get("manage_scans") === "true",
|
||||
unlimited_visibility: formData.get("unlimited_visibility") === "true",
|
||||
},
|
||||
relationships: {},
|
||||
},
|
||||
};
|
||||
|
||||
// Add relationships only if there are items
|
||||
if (groups.length > 0) {
|
||||
payload.data.relationships.provider_groups = {
|
||||
data: groups.map((groupId: string) => ({
|
||||
type: "provider-groups",
|
||||
id: groupId,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
const body = JSON.stringify(payload);
|
||||
|
||||
try {
|
||||
const url = new URL(`${keyServer}/roles`);
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/vnd.api+json",
|
||||
Accept: "application/vnd.api+json",
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
body,
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
revalidatePath("/roles");
|
||||
return data;
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("Error during API call:", error);
|
||||
return {
|
||||
error: getErrorMessage(error),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const updateRole = async (formData: FormData, roleId: string) => {
|
||||
const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
|
||||
const name = formData.get("name") as string;
|
||||
const groups = formData.getAll("groups[]") as string[];
|
||||
|
||||
const payload: any = {
|
||||
data: {
|
||||
type: "roles",
|
||||
id: roleId,
|
||||
attributes: {
|
||||
...(name && { name }),
|
||||
manage_users: formData.get("manage_users") === "true",
|
||||
manage_account: formData.get("manage_account") === "true",
|
||||
manage_billing: formData.get("manage_billing") === "true",
|
||||
manage_providers: formData.get("manage_providers") === "true",
|
||||
manage_integrations: formData.get("manage_integrations") === "true",
|
||||
manage_scans: formData.get("manage_scans") === "true",
|
||||
unlimited_visibility: formData.get("unlimited_visibility") === "true",
|
||||
},
|
||||
relationships: {},
|
||||
},
|
||||
};
|
||||
|
||||
if (groups.length > 0) {
|
||||
payload.data.relationships.provider_groups = {
|
||||
data: groups.map((groupId: string) => ({
|
||||
type: "provider-groups",
|
||||
id: groupId,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
const body = JSON.stringify(payload);
|
||||
|
||||
try {
|
||||
const url = new URL(`${keyServer}/roles/${roleId}`);
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/vnd.api+json",
|
||||
Accept: "application/vnd.api+json",
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
body,
|
||||
});
|
||||
const data = await response.json();
|
||||
revalidatePath("/roles");
|
||||
return data;
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("Error during API call:", error);
|
||||
return {
|
||||
error: getErrorMessage(error),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteRole = async (roleId: string) => {
|
||||
const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
|
||||
const url = new URL(`${keyServer}/roles/${roleId}`);
|
||||
try {
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData?.message || "Failed to delete the role");
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
revalidatePath("/roles");
|
||||
return data;
|
||||
} catch (error) {
|
||||
return {
|
||||
error: getErrorMessage(error),
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -14,10 +14,10 @@ export const getUsers = async ({
|
||||
}) => {
|
||||
const session = await auth();
|
||||
|
||||
if (isNaN(Number(page)) || page < 1) redirect("/users");
|
||||
if (isNaN(Number(page)) || page < 1) redirect("/users?include=roles");
|
||||
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
const url = new URL(`${keyServer}/users`);
|
||||
const url = new URL(`${keyServer}/users?include=roles`);
|
||||
|
||||
if (page) url.searchParams.append("page[number]", page.toString());
|
||||
if (query) url.searchParams.append("filter[search]", query);
|
||||
@@ -94,6 +94,58 @@ export const updateUser = async (formData: FormData) => {
|
||||
revalidatePath("/users");
|
||||
return parseStringify(data);
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(error);
|
||||
return {
|
||||
error: getErrorMessage(error),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const updateUserRole = async (formData: FormData) => {
|
||||
const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
|
||||
const userId = formData.get("userId") as string;
|
||||
const roleId = formData.get("roleId") as string;
|
||||
|
||||
// Validate required fields
|
||||
if (!userId || !roleId) {
|
||||
return { error: "userId and roleId are required" };
|
||||
}
|
||||
|
||||
const url = new URL(`${keyServer}/users/${userId}/relationships/roles`);
|
||||
|
||||
const requestBody = {
|
||||
data: [
|
||||
{
|
||||
type: "roles",
|
||||
id: roleId,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/vnd.api+json",
|
||||
Accept: "application/vnd.api+json",
|
||||
Authorization: `Bearer ${session?.accessToken}`,
|
||||
},
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
return { error: data.errors || "An error occurred" };
|
||||
}
|
||||
|
||||
revalidatePath("/users"); // Update the path as needed
|
||||
return parseStringify(data);
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(error);
|
||||
return {
|
||||
error: getErrorMessage(error),
|
||||
|
||||
@@ -1,7 +1,31 @@
|
||||
import React from "react";
|
||||
import { Suspense } from "react";
|
||||
|
||||
import { getRoles } from "@/actions/roles";
|
||||
import { SkeletonInvitationInfo } from "@/components/invitations/workflow";
|
||||
import { SendInvitationForm } from "@/components/invitations/workflow/forms/send-invitation-form";
|
||||
|
||||
export default function SendInvitationPage() {
|
||||
return <SendInvitationForm />;
|
||||
export default async function SendInvitationPage() {
|
||||
const rolesData = await getRoles({});
|
||||
|
||||
return (
|
||||
<Suspense fallback={<SkeletonInvitationInfo />}>
|
||||
<SSRSendInvitation rolesData={rolesData?.data || []} />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
|
||||
const SSRSendInvitation = ({ rolesData }: { rolesData: Array<any> }) => {
|
||||
const hasRoles = rolesData && rolesData.length > 0;
|
||||
|
||||
return (
|
||||
<SendInvitationForm
|
||||
roles={rolesData.map((role) => ({
|
||||
id: role.id,
|
||||
name: role.attributes.name,
|
||||
}))}
|
||||
defaultRole={!hasRoles ? "admin" : undefined}
|
||||
isSelectorDisabled={!hasRoles}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Spacer } from "@nextui-org/react";
|
||||
import { Suspense } from "react";
|
||||
import React, { Suspense } from "react";
|
||||
|
||||
import { getInvitations } from "@/actions/invitations/invitation";
|
||||
import { getRoles } from "@/actions/roles";
|
||||
import { FilterControls } from "@/components/filters";
|
||||
import { filterInvitations } from "@/components/filters/data-filters";
|
||||
import { SendInvitationButton } from "@/components/invitations";
|
||||
@@ -11,7 +12,7 @@ import {
|
||||
} from "@/components/invitations/table";
|
||||
import { Header } from "@/components/ui";
|
||||
import { DataTable, DataTableFilterCustom } from "@/components/ui/table";
|
||||
import { SearchParamsProps } from "@/types";
|
||||
import { InvitationProps, Role, SearchParamsProps } from "@/types";
|
||||
|
||||
export default async function Invitations({
|
||||
searchParams,
|
||||
@@ -54,12 +55,58 @@ const SSRDataTable = async ({
|
||||
// Extract query from filters
|
||||
const query = (filters["filter[search]"] as string) || "";
|
||||
|
||||
// Fetch invitations and roles
|
||||
const invitationsData = await getInvitations({ query, page, sort, filters });
|
||||
const rolesData = await getRoles({});
|
||||
|
||||
// Create a dictionary for roles by invitation ID
|
||||
const roleDict = (rolesData?.data || []).reduce(
|
||||
(acc: Record<string, Role>, role: Role) => {
|
||||
role.relationships.invitations.data.forEach((invitation: any) => {
|
||||
acc[invitation.id] = role;
|
||||
});
|
||||
return acc;
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
// Generate the array of roles with all the roles available
|
||||
const roles = Array.from(
|
||||
new Map(
|
||||
(rolesData?.data || []).map((role: Role) => [
|
||||
role.id,
|
||||
{ id: role.id, name: role.attributes?.name || "Unnamed Role" },
|
||||
]),
|
||||
).values(),
|
||||
);
|
||||
|
||||
// Expand the invitations
|
||||
const expandedInvitations = invitationsData?.data?.map(
|
||||
(invitation: InvitationProps) => {
|
||||
const role = roleDict[invitation.id];
|
||||
|
||||
return {
|
||||
...invitation,
|
||||
relationships: {
|
||||
...invitation.relationships,
|
||||
role,
|
||||
},
|
||||
roles, // Include all roles here for each invitation
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
// Create the expanded response
|
||||
const expandedResponse = {
|
||||
...invitationsData,
|
||||
data: expandedInvitations,
|
||||
roles,
|
||||
};
|
||||
|
||||
return (
|
||||
<DataTable
|
||||
columns={ColumnsInvitation}
|
||||
data={invitationsData?.data || []}
|
||||
data={expandedResponse?.data || []}
|
||||
metadata={invitationsData?.meta}
|
||||
/>
|
||||
);
|
||||
|
||||
24
ui/app/(prowler)/manage-groups/layout.tsx
Normal file
24
ui/app/(prowler)/manage-groups/layout.tsx
Normal file
@@ -0,0 +1,24 @@
|
||||
import "@/styles/globals.css";
|
||||
|
||||
import { Spacer } from "@nextui-org/react";
|
||||
import React from "react";
|
||||
|
||||
import { NavigationHeader } from "@/components/ui";
|
||||
|
||||
interface ProviderLayoutProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function ProviderLayout({ children }: ProviderLayoutProps) {
|
||||
return (
|
||||
<>
|
||||
<NavigationHeader
|
||||
title="Manage providers groups"
|
||||
icon="icon-park-outline:close-small"
|
||||
href="/providers"
|
||||
/>
|
||||
<Spacer y={16} />
|
||||
{children}
|
||||
</>
|
||||
);
|
||||
}
|
||||
173
ui/app/(prowler)/manage-groups/page.tsx
Normal file
173
ui/app/(prowler)/manage-groups/page.tsx
Normal file
@@ -0,0 +1,173 @@
|
||||
import { Divider, Spacer } from "@nextui-org/react";
|
||||
import { redirect } from "next/navigation";
|
||||
import React, { Suspense } from "react";
|
||||
|
||||
import {
|
||||
getProviderGroupInfoById,
|
||||
getProviderGroups,
|
||||
} from "@/actions/manage-groups/manage-groups";
|
||||
import { getProviders } from "@/actions/providers";
|
||||
import { getRoles } from "@/actions/roles";
|
||||
import { FilterControls } from "@/components/filters/filter-controls";
|
||||
import { AddGroupForm, EditGroupForm } from "@/components/manage-groups/forms";
|
||||
import { SkeletonManageGroups } from "@/components/manage-groups/skeleton-manage-groups";
|
||||
import { ColumnGroups } from "@/components/manage-groups/table";
|
||||
import { DataTable } from "@/components/ui/table";
|
||||
import { ProviderProps, Role, SearchParamsProps } from "@/types";
|
||||
|
||||
export default function ManageGroupsPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: SearchParamsProps;
|
||||
}) {
|
||||
const searchParamsKey = JSON.stringify(searchParams);
|
||||
const providerGroupId = searchParams.groupId;
|
||||
|
||||
return (
|
||||
<div className="grid min-h-[70vh] grid-cols-1 items-center justify-center gap-4 md:grid-cols-12">
|
||||
<div className="col-span-1 flex justify-end md:col-span-4">
|
||||
<Suspense key={searchParamsKey} fallback={<SkeletonManageGroups />}>
|
||||
{providerGroupId ? (
|
||||
<SSRDataEditGroup searchParams={searchParams} />
|
||||
) : (
|
||||
<SSRAddGroupForm />
|
||||
)}
|
||||
</Suspense>
|
||||
</div>
|
||||
|
||||
<Divider orientation="vertical" className="mx-auto h-full" />
|
||||
|
||||
<div className="col-span-1 flex-col justify-start md:col-span-6">
|
||||
<FilterControls />
|
||||
<Spacer y={8} />
|
||||
<h3 className="mb-4 text-sm font-bold uppercase">Provider Groups</h3>
|
||||
<Suspense key={searchParamsKey} fallback={<SkeletonManageGroups />}>
|
||||
<SSRDataTable searchParams={searchParams} />
|
||||
</Suspense>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const SSRAddGroupForm = async () => {
|
||||
const providersResponse = await getProviders({});
|
||||
const rolesResponse = await getRoles({});
|
||||
|
||||
const providersData =
|
||||
providersResponse?.data?.map((provider: ProviderProps) => ({
|
||||
id: provider.id,
|
||||
name: provider.attributes.alias,
|
||||
})) || [];
|
||||
|
||||
const rolesData =
|
||||
rolesResponse?.data?.map((role: Role) => ({
|
||||
id: role.id,
|
||||
name: role.attributes.name,
|
||||
})) || [];
|
||||
|
||||
return <AddGroupForm providers={providersData} roles={rolesData} />;
|
||||
};
|
||||
|
||||
const SSRDataEditGroup = async ({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: SearchParamsProps;
|
||||
}) => {
|
||||
const providerGroupId = searchParams.groupId;
|
||||
|
||||
// Redirect if no group ID is provided or if the parameter is invalid
|
||||
if (!providerGroupId || Array.isArray(providerGroupId)) {
|
||||
redirect("/manage-groups");
|
||||
}
|
||||
|
||||
// Fetch the provider group details
|
||||
const providerGroupData = await getProviderGroupInfoById(providerGroupId);
|
||||
|
||||
if (!providerGroupData || providerGroupData.error) {
|
||||
return <div>Provider group not found</div>;
|
||||
}
|
||||
|
||||
const providersResponse = await getProviders({});
|
||||
const rolesResponse = await getRoles({});
|
||||
|
||||
const providersList =
|
||||
providersResponse?.data?.map((provider: ProviderProps) => ({
|
||||
id: provider.id,
|
||||
name: provider.attributes.alias,
|
||||
})) || [];
|
||||
|
||||
const rolesList =
|
||||
rolesResponse?.data?.map((role: Role) => ({
|
||||
id: role.id,
|
||||
name: role.attributes.name,
|
||||
})) || [];
|
||||
|
||||
const { attributes, relationships } = providerGroupData.data;
|
||||
|
||||
const associatedProviders = relationships.providers?.data.map(
|
||||
(provider: ProviderProps) => {
|
||||
const matchingProvider = providersList.find(
|
||||
(p: ProviderProps) => p.id === provider.id,
|
||||
);
|
||||
return {
|
||||
id: provider.id,
|
||||
name: matchingProvider?.name || "Unavailable for your role",
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
const associatedRoles = relationships.roles?.data.map((role: Role) => {
|
||||
const matchingRole = rolesList.find((r: Role) => r.id === role.id);
|
||||
return {
|
||||
id: role.id,
|
||||
name: matchingRole?.name || "Unavailable for your role",
|
||||
};
|
||||
});
|
||||
|
||||
const formData = {
|
||||
name: attributes.name,
|
||||
providers: associatedProviders,
|
||||
roles: associatedRoles,
|
||||
};
|
||||
|
||||
return (
|
||||
<EditGroupForm
|
||||
providerGroupId={providerGroupId}
|
||||
providerGroupData={formData}
|
||||
allProviders={providersList}
|
||||
allRoles={rolesList}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
const SSRDataTable = async ({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: SearchParamsProps;
|
||||
}) => {
|
||||
const page = parseInt(searchParams.page?.toString() || "1", 10);
|
||||
const sort = searchParams.sort?.toString();
|
||||
|
||||
// Convert filters to the correct type
|
||||
const filters: Record<string, string> = {};
|
||||
Object.entries(searchParams)
|
||||
.filter(([key]) => key.startsWith("filter["))
|
||||
.forEach(([key, value]) => {
|
||||
filters[key] = value?.toString() || "";
|
||||
});
|
||||
|
||||
const query = (filters["filter[search]"] as string) || "";
|
||||
const providerGroupsData = await getProviderGroups({
|
||||
query,
|
||||
page,
|
||||
sort,
|
||||
filters,
|
||||
});
|
||||
return (
|
||||
<DataTable
|
||||
columns={ColumnGroups}
|
||||
data={providerGroupsData?.data || []}
|
||||
metadata={providerGroupsData?.meta}
|
||||
/>
|
||||
);
|
||||
};
|
||||
@@ -3,14 +3,15 @@ import { Suspense } from "react";
|
||||
|
||||
import { getProviders } from "@/actions/providers";
|
||||
import { FilterControls, filterProviders } from "@/components/filters";
|
||||
import { AddProvider } from "@/components/providers";
|
||||
import { ManageGroupsButton } from "@/components/manage-groups";
|
||||
import { AddProviderButton } from "@/components/providers";
|
||||
import {
|
||||
ColumnProviders,
|
||||
SkeletonTableProviders,
|
||||
} from "@/components/providers/table";
|
||||
import { Header } from "@/components/ui";
|
||||
import { DataTable, DataTableFilterCustom } from "@/components/ui/table";
|
||||
import { SearchParamsProps } from "@/types";
|
||||
import { ProviderProps, SearchParamsProps } from "@/types";
|
||||
|
||||
export default async function Providers({
|
||||
searchParams,
|
||||
@@ -26,7 +27,10 @@ export default async function Providers({
|
||||
<Spacer y={4} />
|
||||
<FilterControls search providers />
|
||||
<Spacer y={8} />
|
||||
<AddProvider />
|
||||
<div className="flex items-center gap-4 md:justify-end">
|
||||
<ManageGroupsButton />
|
||||
<AddProviderButton />
|
||||
</div>
|
||||
<Spacer y={4} />
|
||||
<DataTableFilterCustom filters={filterProviders || []} />
|
||||
<Spacer y={8} />
|
||||
@@ -59,10 +63,29 @@ const SSRDataTable = async ({
|
||||
const query = (filters["filter[search]"] as string) || "";
|
||||
|
||||
const providersData = await getProviders({ query, page, sort, filters });
|
||||
|
||||
const providerGroupDict =
|
||||
providersData?.included
|
||||
?.filter((item: any) => item.type === "provider-groups")
|
||||
.reduce((acc: Record<string, string>, group: any) => {
|
||||
acc[group.id] = group.attributes.name;
|
||||
return acc;
|
||||
}, {}) || {};
|
||||
|
||||
const enrichedProviders = providersData?.data.map(
|
||||
(provider: ProviderProps) => {
|
||||
const groupNames = provider.relationships.provider_groups.data.map(
|
||||
(group: { id: string }) =>
|
||||
providerGroupDict[group.id] || "Unknown Group",
|
||||
);
|
||||
return { ...provider, groupNames };
|
||||
},
|
||||
);
|
||||
|
||||
return (
|
||||
<DataTable
|
||||
columns={ColumnProviders}
|
||||
data={providersData?.data || []}
|
||||
data={enrichedProviders || []}
|
||||
metadata={providersData?.meta}
|
||||
/>
|
||||
);
|
||||
|
||||
51
ui/app/(prowler)/roles/(add-role)/edit/page.tsx
Normal file
51
ui/app/(prowler)/roles/(add-role)/edit/page.tsx
Normal file
@@ -0,0 +1,51 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { Suspense } from "react";
|
||||
|
||||
import { getProviderGroups } from "@/actions/manage-groups/manage-groups";
|
||||
import { getRoleInfoById } from "@/actions/roles/roles";
|
||||
import { SkeletonRoleForm } from "@/components/roles/workflow";
|
||||
import { EditRoleForm } from "@/components/roles/workflow/forms/edit-role-form";
|
||||
import { SearchParamsProps } from "@/types";
|
||||
|
||||
export default async function EditRolePage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: SearchParamsProps;
|
||||
}) {
|
||||
const searchParamsKey = JSON.stringify(searchParams || {});
|
||||
|
||||
return (
|
||||
<Suspense key={searchParamsKey} fallback={<SkeletonRoleForm />}>
|
||||
<SSRDataRole searchParams={searchParams} />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
|
||||
const SSRDataRole = async ({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: SearchParamsProps;
|
||||
}) => {
|
||||
const roleId = searchParams.roleId;
|
||||
|
||||
if (!roleId || Array.isArray(roleId)) {
|
||||
redirect("/roles");
|
||||
}
|
||||
|
||||
const roleData = await getRoleInfoById(roleId as string);
|
||||
|
||||
if (!roleData || roleData.error) {
|
||||
return <div>Role not found</div>;
|
||||
}
|
||||
|
||||
const groupsResponse = await getProviderGroups({});
|
||||
const groups =
|
||||
groupsResponse?.data?.map(
|
||||
(group: { id: string; attributes: { name: string } }) => ({
|
||||
id: group.id,
|
||||
name: group.attributes.name,
|
||||
}),
|
||||
) || [];
|
||||
|
||||
return <EditRoleForm roleId={roleId} roleData={roleData} groups={groups} />;
|
||||
};
|
||||
32
ui/app/(prowler)/roles/(add-role)/layout.tsx
Normal file
32
ui/app/(prowler)/roles/(add-role)/layout.tsx
Normal file
@@ -0,0 +1,32 @@
|
||||
import "@/styles/globals.css";
|
||||
|
||||
import { Spacer } from "@nextui-org/react";
|
||||
import React from "react";
|
||||
|
||||
import { WorkflowAddEditRole } from "@/components/roles/workflow";
|
||||
import { NavigationHeader } from "@/components/ui";
|
||||
|
||||
interface RoleLayoutProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function RoleLayout({ children }: RoleLayoutProps) {
|
||||
return (
|
||||
<>
|
||||
<NavigationHeader
|
||||
title="Role Management"
|
||||
icon="icon-park-outline:close-small"
|
||||
href="/roles"
|
||||
/>
|
||||
<Spacer y={16} />
|
||||
<div className="grid grid-cols-1 gap-8 lg:grid-cols-12">
|
||||
<div className="order-1 my-auto hidden h-full lg:col-span-4 lg:col-start-2 lg:block">
|
||||
<WorkflowAddEditRole />
|
||||
</div>
|
||||
<div className="order-2 my-auto lg:col-span-5 lg:col-start-6">
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
17
ui/app/(prowler)/roles/(add-role)/new/page.tsx
Normal file
17
ui/app/(prowler)/roles/(add-role)/new/page.tsx
Normal file
@@ -0,0 +1,17 @@
|
||||
import React from "react";
|
||||
|
||||
import { getProviderGroups } from "@/actions/manage-groups/manage-groups";
|
||||
import { AddRoleForm } from "@/components/roles/workflow/forms/add-role-form";
|
||||
import { ProviderGroup } from "@/types";
|
||||
|
||||
export default async function AddRolePage() {
|
||||
const groupsResponse = await getProviderGroups({});
|
||||
|
||||
const groupsData =
|
||||
groupsResponse?.data?.map((group: ProviderGroup) => ({
|
||||
id: group.id,
|
||||
name: group.attributes.name,
|
||||
})) || [];
|
||||
|
||||
return <AddRoleForm groups={groupsData} />;
|
||||
}
|
||||
64
ui/app/(prowler)/roles/page.tsx
Normal file
64
ui/app/(prowler)/roles/page.tsx
Normal file
@@ -0,0 +1,64 @@
|
||||
import { Spacer } from "@nextui-org/react";
|
||||
import { Suspense } from "react";
|
||||
|
||||
import { getRoles } from "@/actions/roles";
|
||||
import { FilterControls } from "@/components/filters";
|
||||
import { filterRoles } from "@/components/filters/data-filters";
|
||||
import { AddRoleButton } from "@/components/roles";
|
||||
import { ColumnsRoles } from "@/components/roles/table";
|
||||
import { SkeletonTableRoles } from "@/components/roles/table";
|
||||
import { Header } from "@/components/ui";
|
||||
import { DataTable, DataTableFilterCustom } from "@/components/ui/table";
|
||||
import { SearchParamsProps } from "@/types";
|
||||
|
||||
export default async function Roles({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: SearchParamsProps;
|
||||
}) {
|
||||
const searchParamsKey = JSON.stringify(searchParams || {});
|
||||
|
||||
return (
|
||||
<>
|
||||
<Header title="Roles" icon="mdi:account-key-outline" />
|
||||
<Spacer y={4} />
|
||||
<FilterControls search />
|
||||
<Spacer y={8} />
|
||||
<AddRoleButton />
|
||||
<Spacer y={4} />
|
||||
<DataTableFilterCustom filters={filterRoles || []} />
|
||||
<Spacer y={8} />
|
||||
|
||||
<Suspense key={searchParamsKey} fallback={<SkeletonTableRoles />}>
|
||||
<SSRDataTable searchParams={searchParams} />
|
||||
</Suspense>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
const SSRDataTable = async ({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: SearchParamsProps;
|
||||
}) => {
|
||||
const page = parseInt(searchParams.page?.toString() || "1", 10);
|
||||
const sort = searchParams.sort?.toString();
|
||||
|
||||
// Extract all filter parameters
|
||||
const filters = Object.fromEntries(
|
||||
Object.entries(searchParams).filter(([key]) => key.startsWith("filter[")),
|
||||
);
|
||||
|
||||
// Extract query from filters
|
||||
const query = (filters["filter[search]"] as string) || "";
|
||||
|
||||
const rolesData = await getRoles({ query, page, sort, filters });
|
||||
|
||||
return (
|
||||
<DataTable
|
||||
columns={ColumnsRoles}
|
||||
data={rolesData?.data || []}
|
||||
metadata={rolesData?.meta}
|
||||
/>
|
||||
);
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Spacer } from "@nextui-org/react";
|
||||
import { Suspense } from "react";
|
||||
|
||||
import { getRoles } from "@/actions/roles";
|
||||
import { getUsers } from "@/actions/users/users";
|
||||
import { FilterControls } from "@/components/filters";
|
||||
import { filterUsers } from "@/components/filters/data-filters";
|
||||
@@ -8,7 +9,7 @@ import { Header } from "@/components/ui";
|
||||
import { DataTable, DataTableFilterCustom } from "@/components/ui/table";
|
||||
import { AddUserButton } from "@/components/users";
|
||||
import { ColumnsUser, SkeletonTableUser } from "@/components/users/table";
|
||||
import { SearchParamsProps } from "@/types";
|
||||
import { Role, SearchParamsProps, UserProps } from "@/types";
|
||||
|
||||
export default async function Users({
|
||||
searchParams,
|
||||
@@ -52,11 +53,49 @@ const SSRDataTable = async ({
|
||||
const query = (filters["filter[search]"] as string) || "";
|
||||
|
||||
const usersData = await getUsers({ query, page, sort, filters });
|
||||
const rolesData = await getRoles({});
|
||||
|
||||
// Create a dictionary for roles by user ID
|
||||
const roleDict = (usersData?.included || []).reduce(
|
||||
(acc: Record<string, any>, item: Role) => {
|
||||
if (item.type === "roles") {
|
||||
acc[item.id] = item.attributes;
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, Role>,
|
||||
);
|
||||
|
||||
// Generate the array of roles with all the roles available
|
||||
const roles = Array.from(
|
||||
new Map(
|
||||
(rolesData?.data || []).map((role: Role) => [
|
||||
role.id,
|
||||
{ id: role.id, name: role.attributes?.name || "Unnamed Role" },
|
||||
]),
|
||||
).values(),
|
||||
);
|
||||
|
||||
// Expand the users with their roles
|
||||
const expandedUsers = (usersData?.data || []).map((user: UserProps) => {
|
||||
// Check if the user has a role
|
||||
const roleId = user?.relationships?.roles?.data?.[0]?.id;
|
||||
const role = roleDict?.[roleId] || null;
|
||||
|
||||
return {
|
||||
...user,
|
||||
attributes: {
|
||||
...(user?.attributes || {}),
|
||||
role,
|
||||
},
|
||||
roles,
|
||||
};
|
||||
});
|
||||
|
||||
return (
|
||||
<DataTable
|
||||
columns={ColumnsUser}
|
||||
data={usersData?.data || []}
|
||||
data={expandedUsers || []}
|
||||
metadata={usersData?.meta}
|
||||
/>
|
||||
);
|
||||
|
||||
@@ -72,3 +72,11 @@ export const filterInvitations = [
|
||||
values: ["pending", "accepted", "expired", "revoked"],
|
||||
},
|
||||
];
|
||||
|
||||
export const filterRoles = [
|
||||
{
|
||||
key: "permission_state",
|
||||
labelCheckboxGroup: "Permissions",
|
||||
values: ["unlimited", "limited", "none"],
|
||||
},
|
||||
];
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Select, SelectItem } from "@nextui-org/react";
|
||||
import { MailIcon, ShieldIcon } from "lucide-react";
|
||||
import { Dispatch, SetStateAction } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import * as z from "zod";
|
||||
|
||||
import { updateInvite } from "@/actions/invitations/invitation";
|
||||
@@ -15,10 +15,14 @@ import { editInviteFormSchema } from "@/types";
|
||||
export const EditForm = ({
|
||||
invitationId,
|
||||
invitationEmail,
|
||||
roles = [],
|
||||
currentRole = "",
|
||||
setIsOpen,
|
||||
}: {
|
||||
invitationId: string;
|
||||
invitationEmail?: string;
|
||||
roles: Array<{ id: string; name: string }>;
|
||||
currentRole?: string;
|
||||
setIsOpen: Dispatch<SetStateAction<boolean>>;
|
||||
}) => {
|
||||
const formSchema = editInviteFormSchema;
|
||||
@@ -27,7 +31,8 @@ export const EditForm = ({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
invitationId,
|
||||
invitationEmail: invitationEmail,
|
||||
invitationEmail: invitationEmail || "",
|
||||
role: roles.find((role) => role.name === currentRole)?.id || "",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -36,21 +41,43 @@ export const EditForm = ({
|
||||
const isLoading = form.formState.isSubmitting;
|
||||
|
||||
const onSubmitClient = async (values: z.infer<typeof formSchema>) => {
|
||||
const formData = new FormData();
|
||||
console.log(values);
|
||||
const changedFields: { [key: string]: any } = {};
|
||||
|
||||
Object.entries(values).forEach(
|
||||
([key, value]) => value !== undefined && formData.append(key, value),
|
||||
);
|
||||
// Check if the email changed
|
||||
if (values.invitationEmail && values.invitationEmail !== invitationEmail) {
|
||||
changedFields.invitationEmail = values.invitationEmail;
|
||||
}
|
||||
|
||||
// Check if the role changed
|
||||
const currentRoleId =
|
||||
roles.find((role) => role.name === currentRole)?.id || "";
|
||||
if (values.role && values.role !== currentRoleId) {
|
||||
changedFields.role = values.role;
|
||||
}
|
||||
|
||||
// If there are no changes, avoid the request
|
||||
if (Object.keys(changedFields).length === 0) {
|
||||
toast({
|
||||
title: "No changes detected",
|
||||
description: "Please modify at least one field before saving.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
changedFields.invitationId = invitationId; // Always include the ID
|
||||
|
||||
const formData = new FormData();
|
||||
Object.entries(changedFields).forEach(([key, value]) => {
|
||||
formData.append(key, value);
|
||||
});
|
||||
|
||||
const data = await updateInvite(formData);
|
||||
|
||||
if (data?.error) {
|
||||
const errorMessage = `${data.error}`;
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Oops! Something went wrong",
|
||||
description: errorMessage,
|
||||
description: `${data.error}`,
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
@@ -67,9 +94,23 @@ export const EditForm = ({
|
||||
onSubmit={form.handleSubmit(onSubmitClient)}
|
||||
className="flex flex-col space-y-4"
|
||||
>
|
||||
<div className="text-md">
|
||||
Current email: <span className="font-bold">{invitationEmail}</span>
|
||||
<div className="flex flex-row justify-center space-x-4 rounded-lg bg-gray-50 p-3">
|
||||
<div className="flex items-center text-small text-gray-600">
|
||||
<MailIcon className="mr-2 h-4 w-4" />
|
||||
<span className="text-gray-500">Email:</span>
|
||||
<span className="ml-2 font-semibold text-gray-900">
|
||||
{invitationEmail}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center text-small text-gray-600">
|
||||
<ShieldIcon className="mr-2 h-4 w-4" />
|
||||
<span className="text-gray-500">Role:</span>
|
||||
<span className="ml-2 font-semibold text-gray-900">
|
||||
{currentRole}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<CustomInput
|
||||
control={form.control}
|
||||
@@ -83,6 +124,34 @@ export const EditForm = ({
|
||||
isInvalid={!!form.formState.errors.invitationEmail}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Controller
|
||||
name="role"
|
||||
control={form.control}
|
||||
render={({ field }) => (
|
||||
<Select
|
||||
{...field}
|
||||
label="Role"
|
||||
placeholder="Select a role"
|
||||
variant="bordered"
|
||||
selectedKeys={[field.value || ""]}
|
||||
onSelectionChange={(selected) =>
|
||||
field.onChange(selected?.currentKey || "")
|
||||
}
|
||||
>
|
||||
{roles.map((role) => (
|
||||
<SelectItem key={role.id}>{role.name}</SelectItem>
|
||||
))}
|
||||
</Select>
|
||||
)}
|
||||
/>
|
||||
|
||||
{form.formState.errors.role && (
|
||||
<p className="mt-2 text-sm text-red-600">
|
||||
{form.formState.errors.role.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<input type="hidden" name="invitationId" value={invitationId} />
|
||||
|
||||
<div className="flex w-full justify-center sm:space-x-6">
|
||||
|
||||
@@ -31,6 +31,15 @@ export const ColumnsInvitation: ColumnDef<InvitationProps>[] = [
|
||||
return <p className="font-semibold">{state}</p>;
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "role",
|
||||
header: () => <div className="text-left">Role</div>,
|
||||
cell: ({ row }) => {
|
||||
const roleName =
|
||||
row.original.relationships?.role?.attributes?.name || "No Role";
|
||||
return <p className="font-semibold">{roleName}</p>;
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "inserted_at",
|
||||
header: ({ column }) => (
|
||||
@@ -60,13 +69,13 @@ export const ColumnsInvitation: ColumnDef<InvitationProps>[] = [
|
||||
return <DateWithTime dateTime={expires_at} showTime={false} />;
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
accessorKey: "actions",
|
||||
header: () => <div className="text-right">Actions</div>,
|
||||
id: "actions",
|
||||
cell: ({ row }) => {
|
||||
return <DataTableRowActions row={row} />;
|
||||
const roles = row.original.roles;
|
||||
return <DataTableRowActions row={row} roles={roles} />;
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
@@ -24,28 +24,34 @@ import { DeleteForm, EditForm } from "../forms";
|
||||
|
||||
interface DataTableRowActionsProps<InvitationProps> {
|
||||
row: Row<InvitationProps>;
|
||||
roles?: { id: string; name: string }[];
|
||||
}
|
||||
const iconClasses =
|
||||
"text-2xl text-default-500 pointer-events-none flex-shrink-0";
|
||||
|
||||
export function DataTableRowActions<InvitationProps>({
|
||||
row,
|
||||
roles,
|
||||
}: DataTableRowActionsProps<InvitationProps>) {
|
||||
const [isEditOpen, setIsEditOpen] = useState(false);
|
||||
const [isDeleteOpen, setIsDeleteOpen] = useState(false);
|
||||
const invitationId = (row.original as { id: string }).id;
|
||||
const invitationEmail = (row.original as any).attributes?.email;
|
||||
const invitationRole = (row.original as any).relationships?.role?.attributes
|
||||
?.name;
|
||||
|
||||
return (
|
||||
<>
|
||||
<CustomAlertModal
|
||||
isOpen={isEditOpen}
|
||||
onOpenChange={setIsEditOpen}
|
||||
title="Edit Invitation"
|
||||
description={"Edit the invitation details"}
|
||||
title="Edit invitation details"
|
||||
>
|
||||
<EditForm
|
||||
invitationId={invitationId}
|
||||
invitationEmail={invitationEmail}
|
||||
currentRole={invitationRole}
|
||||
roles={roles || []}
|
||||
setIsOpen={setIsEditOpen}
|
||||
/>
|
||||
</CustomAlertModal>
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
"use client";
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Select, SelectItem } from "@nextui-org/react";
|
||||
import { SaveIcon } from "lucide-react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import * as z from "zod";
|
||||
|
||||
import { sendInvite } from "@/actions/invitations/invitation";
|
||||
@@ -14,11 +15,20 @@ import { ApiError } from "@/types";
|
||||
|
||||
const sendInvitationFormSchema = z.object({
|
||||
email: z.string().email("Please enter a valid email"),
|
||||
roleId: z.string().nonempty("Role is required"),
|
||||
});
|
||||
|
||||
export type FormValues = z.infer<typeof sendInvitationFormSchema>;
|
||||
|
||||
export const SendInvitationForm = () => {
|
||||
export const SendInvitationForm = ({
|
||||
roles = [],
|
||||
defaultRole = "admin",
|
||||
isSelectorDisabled = false,
|
||||
}: {
|
||||
roles: Array<{ id: string; name: string }>;
|
||||
defaultRole?: string;
|
||||
isSelectorDisabled: boolean;
|
||||
}) => {
|
||||
const { toast } = useToast();
|
||||
const router = useRouter();
|
||||
|
||||
@@ -26,6 +36,7 @@ export const SendInvitationForm = () => {
|
||||
resolver: zodResolver(sendInvitationFormSchema),
|
||||
defaultValues: {
|
||||
email: "",
|
||||
roleId: isSelectorDisabled ? defaultRole : "",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -34,6 +45,7 @@ export const SendInvitationForm = () => {
|
||||
const onSubmitClient = async (values: FormValues) => {
|
||||
const formData = new FormData();
|
||||
formData.append("email", values.email);
|
||||
formData.append("role", values.roleId);
|
||||
|
||||
try {
|
||||
const data = await sendInvite(formData);
|
||||
@@ -48,6 +60,12 @@ export const SendInvitationForm = () => {
|
||||
message: errorMessage,
|
||||
});
|
||||
break;
|
||||
case "/data/relationships/roles":
|
||||
form.setError("roleId", {
|
||||
type: "server",
|
||||
message: errorMessage,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
toast({
|
||||
variant: "destructive",
|
||||
@@ -75,6 +93,7 @@ export const SendInvitationForm = () => {
|
||||
onSubmit={form.handleSubmit(onSubmitClient)}
|
||||
className="flex flex-col space-y-4"
|
||||
>
|
||||
{/* Email Field */}
|
||||
<CustomInput
|
||||
control={form.control}
|
||||
name="email"
|
||||
@@ -87,6 +106,40 @@ export const SendInvitationForm = () => {
|
||||
isInvalid={!!form.formState.errors.email}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
name="roleId"
|
||||
control={form.control}
|
||||
render={({ field }) => (
|
||||
<>
|
||||
<Select
|
||||
{...field}
|
||||
label="Role"
|
||||
placeholder="Select a role"
|
||||
variant="bordered"
|
||||
isDisabled={isSelectorDisabled}
|
||||
selectedKeys={[field.value]}
|
||||
onSelectionChange={(selected) =>
|
||||
field.onChange(selected?.currentKey || "")
|
||||
}
|
||||
>
|
||||
{isSelectorDisabled ? (
|
||||
<SelectItem key={defaultRole}>{defaultRole}</SelectItem>
|
||||
) : (
|
||||
roles.map((role) => (
|
||||
<SelectItem key={role.id}>{role.name}</SelectItem>
|
||||
))
|
||||
)}
|
||||
</Select>
|
||||
{form.formState.errors.roleId && (
|
||||
<p className="mt-2 text-sm text-red-600">
|
||||
{form.formState.errors.roleId.message}
|
||||
</p>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Submit Button */}
|
||||
<div className="flex w-full justify-end sm:space-x-6">
|
||||
<CustomButton
|
||||
type="submit"
|
||||
|
||||
198
ui/components/manage-groups/forms/add-group-form.tsx
Normal file
198
ui/components/manage-groups/forms/add-group-form.tsx
Normal file
@@ -0,0 +1,198 @@
|
||||
"use client";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Divider } from "@nextui-org/react";
|
||||
import { SaveIcon } from "lucide-react";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import * as z from "zod";
|
||||
|
||||
import { createProviderGroup } from "@/actions/manage-groups";
|
||||
import { useToast } from "@/components/ui";
|
||||
import {
|
||||
CustomButton,
|
||||
CustomDropdownSelection,
|
||||
CustomInput,
|
||||
} from "@/components/ui/custom";
|
||||
import { Form } from "@/components/ui/form";
|
||||
import { ApiError } from "@/types";
|
||||
|
||||
const addGroupSchema = z.object({
|
||||
name: z.string().nonempty("Provider group name is required"),
|
||||
providers: z.array(z.string()).optional(),
|
||||
roles: z.array(z.string()).optional(),
|
||||
});
|
||||
|
||||
type FormValues = z.infer<typeof addGroupSchema>;
|
||||
|
||||
export const AddGroupForm = ({
|
||||
roles = [],
|
||||
providers = [],
|
||||
}: {
|
||||
roles: Array<{ id: string; name: string }>;
|
||||
providers: Array<{ id: string; name: string }>;
|
||||
}) => {
|
||||
const { toast } = useToast();
|
||||
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(addGroupSchema),
|
||||
defaultValues: {
|
||||
name: "",
|
||||
providers: [],
|
||||
roles: [],
|
||||
},
|
||||
});
|
||||
|
||||
const isLoading = form.formState.isSubmitting;
|
||||
|
||||
const onSubmitClient = async (values: FormValues) => {
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append("name", values.name);
|
||||
|
||||
if (values.providers?.length) {
|
||||
const providersData = values.providers.map((id) => ({
|
||||
id,
|
||||
type: "providers",
|
||||
}));
|
||||
formData.append("providers", JSON.stringify(providersData));
|
||||
}
|
||||
|
||||
if (values.roles?.length) {
|
||||
const rolesData = values.roles.map((id) => ({
|
||||
id,
|
||||
type: "roles",
|
||||
}));
|
||||
formData.append("roles", JSON.stringify(rolesData));
|
||||
}
|
||||
|
||||
const data = await createProviderGroup(formData);
|
||||
|
||||
if (data?.errors && data.errors.length > 0) {
|
||||
data.errors.forEach((error: ApiError) => {
|
||||
const errorMessage = error.detail;
|
||||
switch (error.source.pointer) {
|
||||
case "/data/attributes/name":
|
||||
form.setError("name", {
|
||||
type: "server",
|
||||
message: errorMessage,
|
||||
});
|
||||
break;
|
||||
case "/data/relationships/roles":
|
||||
form.setError("roles", {
|
||||
type: "server",
|
||||
message: errorMessage,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Oops! Something went wrong",
|
||||
description: errorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
form.reset();
|
||||
toast({
|
||||
title: "Success!",
|
||||
description: "The group was created successfully.",
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Error",
|
||||
description: "An unexpected error occurred. Please try again.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit(onSubmitClient)}
|
||||
className="flex flex-col space-y-4"
|
||||
>
|
||||
{/* Name Field */}
|
||||
<p className="text-small font-medium text-default-700">
|
||||
Please provide a name for the group. You can also select providers and
|
||||
roles to associate with the group, this step is optional and can be
|
||||
done later if needed.
|
||||
</p>
|
||||
<Divider orientation="horizontal" className="mb-2" />
|
||||
<div className="flex flex-col gap-2">
|
||||
<CustomInput
|
||||
control={form.control}
|
||||
name="name"
|
||||
type="text"
|
||||
label="Provider group name"
|
||||
labelPlacement="outside"
|
||||
placeholder="Enter the provider group name"
|
||||
variant="bordered"
|
||||
isRequired
|
||||
isInvalid={!!form.formState.errors.name}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Providers Field */}
|
||||
<Controller
|
||||
name="providers"
|
||||
control={form.control}
|
||||
render={({ field }) => (
|
||||
<CustomDropdownSelection
|
||||
label="Select Providers"
|
||||
name="providers"
|
||||
values={providers}
|
||||
selectedKeys={field.value || []}
|
||||
onChange={(name, selectedValues) =>
|
||||
field.onChange(selectedValues)
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
{form.formState.errors.providers && (
|
||||
<p className="mt-2 text-sm text-red-600">
|
||||
{form.formState.errors.providers.message}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{/* Roles Field */}
|
||||
<Controller
|
||||
name="roles"
|
||||
control={form.control}
|
||||
render={({ field }) => (
|
||||
<CustomDropdownSelection
|
||||
label="Select Roles"
|
||||
name="roles"
|
||||
values={roles}
|
||||
selectedKeys={field.value || []}
|
||||
onChange={(name, selectedValues) =>
|
||||
field.onChange(selectedValues)
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
{form.formState.errors.roles && (
|
||||
<p className="mt-2 text-sm text-red-600">
|
||||
{form.formState.errors.roles.message}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{/* Submit Button */}
|
||||
<div className="flex w-full justify-end sm:space-x-6">
|
||||
<CustomButton
|
||||
type="submit"
|
||||
ariaLabel="Create Group"
|
||||
className="w-1/2"
|
||||
variant="solid"
|
||||
color="action"
|
||||
size="md"
|
||||
isLoading={isLoading}
|
||||
startContent={!isLoading && <SaveIcon size={24} />}
|
||||
>
|
||||
{isLoading ? <>Loading</> : <span>Create Group</span>}
|
||||
</CustomButton>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
87
ui/components/manage-groups/forms/delete-group-form.tsx
Normal file
87
ui/components/manage-groups/forms/delete-group-form.tsx
Normal file
@@ -0,0 +1,87 @@
|
||||
"use client";
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import React, { Dispatch, SetStateAction } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import * as z from "zod";
|
||||
|
||||
import { deleteProviderGroup } from "@/actions/manage-groups/manage-groups";
|
||||
import { DeleteIcon } from "@/components/icons";
|
||||
import { useToast } from "@/components/ui";
|
||||
import { CustomButton } from "@/components/ui/custom";
|
||||
import { Form } from "@/components/ui/form";
|
||||
|
||||
const formSchema = z.object({
|
||||
groupId: z.string(),
|
||||
});
|
||||
|
||||
export const DeleteGroupForm = ({
|
||||
groupId,
|
||||
setIsOpen,
|
||||
}: {
|
||||
groupId: string;
|
||||
setIsOpen: Dispatch<SetStateAction<boolean>>;
|
||||
}) => {
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
});
|
||||
const { toast } = useToast();
|
||||
const isLoading = form.formState.isSubmitting;
|
||||
|
||||
async function onSubmitClient(formData: FormData) {
|
||||
// client-side validation
|
||||
const data = await deleteProviderGroup(formData);
|
||||
|
||||
if (data?.errors && data.errors.length > 0) {
|
||||
const error = data.errors[0];
|
||||
const errorMessage = `${error.detail}`;
|
||||
// show error
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Oops! Something went wrong",
|
||||
description: errorMessage,
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
title: "Success!",
|
||||
description: "The provider group was removed successfully.",
|
||||
});
|
||||
}
|
||||
setIsOpen(false); // Close the modal on success
|
||||
}
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form action={onSubmitClient}>
|
||||
<input type="hidden" name="id" value={groupId} />
|
||||
<div className="flex w-full justify-center sm:space-x-6">
|
||||
<CustomButton
|
||||
type="button"
|
||||
ariaLabel="Cancel"
|
||||
className="w-full bg-transparent"
|
||||
variant="faded"
|
||||
size="lg"
|
||||
radius="lg"
|
||||
onPress={() => setIsOpen(false)}
|
||||
isDisabled={isLoading}
|
||||
>
|
||||
<span>Cancel</span>
|
||||
</CustomButton>
|
||||
|
||||
<CustomButton
|
||||
type="submit"
|
||||
ariaLabel="Delete"
|
||||
className="w-full"
|
||||
variant="solid"
|
||||
color="danger"
|
||||
size="lg"
|
||||
isLoading={isLoading}
|
||||
startContent={!isLoading && <DeleteIcon size={24} />}
|
||||
>
|
||||
{isLoading ? <>Loading</> : <span>Delete</span>}
|
||||
</CustomButton>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
269
ui/components/manage-groups/forms/edit-group-form.tsx
Normal file
269
ui/components/manage-groups/forms/edit-group-form.tsx
Normal file
@@ -0,0 +1,269 @@
|
||||
"use client";
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Divider } from "@nextui-org/react";
|
||||
import { SaveIcon } from "lucide-react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import * as z from "zod";
|
||||
|
||||
import { updateProviderGroup } from "@/actions/manage-groups/manage-groups";
|
||||
import { useToast } from "@/components/ui";
|
||||
import {
|
||||
CustomButton,
|
||||
CustomDropdownSelection,
|
||||
CustomInput,
|
||||
} from "@/components/ui/custom";
|
||||
import { Form } from "@/components/ui/form";
|
||||
import { ApiError } from "@/types";
|
||||
|
||||
const editGroupSchema = z.object({
|
||||
name: z.string().nonempty("Provider group name is required"),
|
||||
providers: z.array(z.object({ id: z.string(), name: z.string() })).optional(),
|
||||
roles: z.array(z.object({ id: z.string(), name: z.string() })).optional(),
|
||||
});
|
||||
|
||||
export type FormValues = z.infer<typeof editGroupSchema>;
|
||||
|
||||
export const EditGroupForm = ({
|
||||
providerGroupId,
|
||||
providerGroupData,
|
||||
allProviders,
|
||||
allRoles,
|
||||
}: {
|
||||
providerGroupId: string;
|
||||
providerGroupData: FormValues;
|
||||
allProviders: { id: string; name: string }[];
|
||||
allRoles: { id: string; name: string }[];
|
||||
}) => {
|
||||
const { toast } = useToast();
|
||||
const router = useRouter();
|
||||
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(editGroupSchema),
|
||||
defaultValues: providerGroupData,
|
||||
});
|
||||
|
||||
const isLoading = form.formState.isSubmitting;
|
||||
|
||||
const onSubmitClient = async (values: FormValues) => {
|
||||
try {
|
||||
const updatedFields: Partial<FormValues> = {};
|
||||
|
||||
if (values.name !== providerGroupData.name) {
|
||||
updatedFields.name = values.name;
|
||||
}
|
||||
|
||||
if (
|
||||
JSON.stringify(values.providers) !==
|
||||
JSON.stringify(providerGroupData.providers)
|
||||
) {
|
||||
updatedFields.providers = values.providers;
|
||||
}
|
||||
|
||||
if (
|
||||
JSON.stringify(values.roles) !== JSON.stringify(providerGroupData.roles)
|
||||
) {
|
||||
updatedFields.roles = values.roles;
|
||||
}
|
||||
|
||||
if (Object.keys(updatedFields).length === 0) {
|
||||
toast({
|
||||
title: "No changes detected",
|
||||
description: "No updates were made to the provider group.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
if (updatedFields.name) {
|
||||
formData.append("name", updatedFields.name);
|
||||
}
|
||||
if (updatedFields.providers) {
|
||||
const providersData = updatedFields.providers.map((provider) => ({
|
||||
id: provider.id,
|
||||
type: "providers",
|
||||
}));
|
||||
formData.append("providers", JSON.stringify(providersData));
|
||||
}
|
||||
if (updatedFields.roles) {
|
||||
const rolesData = updatedFields.roles.map((role) => ({
|
||||
id: role.id,
|
||||
type: "roles",
|
||||
}));
|
||||
formData.append("roles", JSON.stringify(rolesData));
|
||||
}
|
||||
|
||||
const data = await updateProviderGroup(providerGroupId, formData);
|
||||
|
||||
if (data?.errors && data.errors.length > 0) {
|
||||
data.errors.forEach((error: ApiError) => {
|
||||
const errorMessage = error.detail;
|
||||
switch (error.source.pointer) {
|
||||
case "/data/attributes/name":
|
||||
form.setError("name", {
|
||||
type: "server",
|
||||
message: errorMessage,
|
||||
});
|
||||
break;
|
||||
case "/data/relationships/roles":
|
||||
form.setError("roles", {
|
||||
type: "server",
|
||||
message: errorMessage,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Oops! Something went wrong",
|
||||
description: errorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
title: "Success!",
|
||||
description: "The group was updated successfully.",
|
||||
});
|
||||
router.push("/manage-groups");
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Error",
|
||||
description: "An unexpected error occurred. Please try again.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit(onSubmitClient)}
|
||||
className="flex flex-col space-y-4"
|
||||
>
|
||||
<p className="text-small font-medium text-default-700">
|
||||
Edit the group information, including name, associated providers, and
|
||||
roles.
|
||||
</p>
|
||||
<Divider orientation="horizontal" className="mb-2" />
|
||||
|
||||
{/* Field for the name */}
|
||||
<div className="flex flex-col gap-2">
|
||||
<CustomInput
|
||||
control={form.control}
|
||||
name="name"
|
||||
type="text"
|
||||
label="Provider group name"
|
||||
labelPlacement="outside"
|
||||
placeholder="Enter the provider group name"
|
||||
variant="bordered"
|
||||
isRequired
|
||||
isInvalid={!!form.formState.errors.name}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Providers selection */}
|
||||
<Controller
|
||||
name="providers"
|
||||
control={form.control}
|
||||
render={({ field }) => {
|
||||
const combinedProviders = [
|
||||
...(providerGroupData.providers || []),
|
||||
...allProviders.filter(
|
||||
(p) =>
|
||||
!(providerGroupData.providers || []).some(
|
||||
(sp) => sp.id === p.id,
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
return (
|
||||
<CustomDropdownSelection
|
||||
label="Select Providers"
|
||||
name="providers"
|
||||
values={combinedProviders}
|
||||
selectedKeys={field.value?.map((p) => p.id) || []}
|
||||
onChange={(name, selectedValues) => {
|
||||
const selectedProviders = combinedProviders.filter(
|
||||
(provider) => selectedValues.includes(provider.id),
|
||||
);
|
||||
field.onChange(selectedProviders);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
{form.formState.errors.providers && (
|
||||
<p className="mt-2 text-sm text-red-600">
|
||||
{form.formState.errors.providers.message}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{/* Roles selection */}
|
||||
<Controller
|
||||
name="roles"
|
||||
control={form.control}
|
||||
render={({ field }) => {
|
||||
const combinedRoles = [
|
||||
...(providerGroupData.roles || []),
|
||||
...allRoles.filter(
|
||||
(r) =>
|
||||
!(providerGroupData.roles || []).some((sr) => sr.id === r.id),
|
||||
),
|
||||
];
|
||||
|
||||
return (
|
||||
<CustomDropdownSelection
|
||||
label="Select Roles"
|
||||
name="roles"
|
||||
values={combinedRoles}
|
||||
selectedKeys={field.value?.map((r) => r.id) || []}
|
||||
onChange={(name, selectedValues) => {
|
||||
const selectedRoles = combinedRoles.filter((role) =>
|
||||
selectedValues.includes(role.id),
|
||||
);
|
||||
field.onChange(selectedRoles);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
{form.formState.errors.roles && (
|
||||
<p className="mt-2 text-sm text-red-600">
|
||||
{form.formState.errors.roles.message}
|
||||
</p>
|
||||
)}
|
||||
<Divider orientation="horizontal" className="mb-2" />
|
||||
|
||||
<div className="flex w-full justify-end sm:space-x-6">
|
||||
<CustomButton
|
||||
type="button"
|
||||
ariaLabel="Cancel"
|
||||
className="w-fit bg-transparent"
|
||||
variant="faded"
|
||||
size="md"
|
||||
onPress={() => {
|
||||
router.push("/manage-groups");
|
||||
}}
|
||||
isDisabled={isLoading}
|
||||
>
|
||||
<span>Cancel</span>
|
||||
</CustomButton>
|
||||
<CustomButton
|
||||
type="submit"
|
||||
ariaLabel="Update Group"
|
||||
className="w-1/2"
|
||||
variant="solid"
|
||||
color="action"
|
||||
size="md"
|
||||
isLoading={isLoading}
|
||||
startContent={!isLoading && <SaveIcon size={24} />}
|
||||
>
|
||||
{isLoading ? <>Loading</> : <span>Update Group</span>}
|
||||
</CustomButton>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
3
ui/components/manage-groups/forms/index.ts
Normal file
3
ui/components/manage-groups/forms/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./add-group-form";
|
||||
export * from "./delete-group-form";
|
||||
export * from "./edit-group-form";
|
||||
2
ui/components/manage-groups/index.ts
Normal file
2
ui/components/manage-groups/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./manage-groups-button";
|
||||
export * from "./skeleton-manage-groups";
|
||||
20
ui/components/manage-groups/manage-groups-button.tsx
Normal file
20
ui/components/manage-groups/manage-groups-button.tsx
Normal file
@@ -0,0 +1,20 @@
|
||||
"use client";
|
||||
|
||||
import { SettingsIcon } from "lucide-react";
|
||||
|
||||
import { CustomButton } from "../ui/custom";
|
||||
|
||||
export const ManageGroupsButton = () => {
|
||||
return (
|
||||
<CustomButton
|
||||
asLink="/manage-groups"
|
||||
ariaLabel="Manage Groups"
|
||||
variant="dashed"
|
||||
color="warning"
|
||||
size="md"
|
||||
startContent={<SettingsIcon size={20} />}
|
||||
>
|
||||
Manage Groups
|
||||
</CustomButton>
|
||||
);
|
||||
};
|
||||
65
ui/components/manage-groups/skeleton-manage-groups.tsx
Normal file
65
ui/components/manage-groups/skeleton-manage-groups.tsx
Normal file
@@ -0,0 +1,65 @@
|
||||
import { Card, Skeleton } from "@nextui-org/react";
|
||||
import React from "react";
|
||||
|
||||
export const SkeletonManageGroups = () => {
|
||||
return (
|
||||
<Card className="h-full w-full space-y-5 p-4" radius="sm">
|
||||
{/* Table headers */}
|
||||
<div className="hidden justify-between md:flex">
|
||||
<Skeleton className="w-1/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-1/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-1/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
</div>
|
||||
|
||||
{/* Table body */}
|
||||
<div className="space-y-3">
|
||||
{[...Array(3)].map((_, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="flex flex-col items-center justify-between space-x-0 md:flex-row md:space-x-4"
|
||||
>
|
||||
<Skeleton className="mb-2 w-full rounded-lg md:mb-0 md:w-1/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 w-full rounded-lg md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-1/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-1/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
93
ui/components/manage-groups/table/column-groups.tsx
Normal file
93
ui/components/manage-groups/table/column-groups.tsx
Normal file
@@ -0,0 +1,93 @@
|
||||
"use client";
|
||||
|
||||
import { ColumnDef } from "@tanstack/react-table";
|
||||
|
||||
import { DateWithTime } from "@/components/ui/entities";
|
||||
import { DataTableColumnHeader } from "@/components/ui/table";
|
||||
import { ProviderGroup } from "@/types";
|
||||
|
||||
import { DataTableRowActions } from "./data-table-row-actions";
|
||||
|
||||
const getProviderData = (row: { original: ProviderGroup }) => {
|
||||
return row.original;
|
||||
};
|
||||
|
||||
export const ColumnGroups: ColumnDef<ProviderGroup>[] = [
|
||||
{
|
||||
accessorKey: "name",
|
||||
header: ({ column }) => (
|
||||
<DataTableColumnHeader column={column} title={"Name"} param="name" />
|
||||
),
|
||||
cell: ({ row }) => {
|
||||
const {
|
||||
attributes: { name },
|
||||
} = getProviderData(row);
|
||||
return (
|
||||
<p className="text-small font-medium">
|
||||
{name.charAt(0).toUpperCase() + name.slice(1).toLowerCase()}
|
||||
</p>
|
||||
);
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
accessorKey: "providers_count",
|
||||
header: ({ column }) => (
|
||||
<DataTableColumnHeader column={column} title="Providers" param="name" />
|
||||
),
|
||||
cell: ({ row }) => {
|
||||
const {
|
||||
relationships: { providers },
|
||||
} = getProviderData(row);
|
||||
return (
|
||||
<div className="flex h-8 w-8 items-center justify-center rounded-full bg-gray-100 dark:bg-gray-800">
|
||||
<span className="text-sm font-bold text-gray-900 dark:text-gray-100">
|
||||
{providers.meta.count}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
accessorKey: "roles_count",
|
||||
header: ({ column }) => (
|
||||
<DataTableColumnHeader column={column} title="Roles" param="roles" />
|
||||
),
|
||||
cell: ({ row }) => {
|
||||
const {
|
||||
relationships: { roles },
|
||||
} = getProviderData(row);
|
||||
return (
|
||||
<div className="flex h-8 w-8 items-center justify-center rounded-full bg-gray-100 dark:bg-gray-800">
|
||||
<span className="text-sm font-bold text-gray-900 dark:text-gray-100">
|
||||
{roles.meta.count}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
accessorKey: "added",
|
||||
header: ({ column }) => (
|
||||
<DataTableColumnHeader
|
||||
column={column}
|
||||
title={"Added"}
|
||||
param="inserted_at"
|
||||
/>
|
||||
),
|
||||
cell: ({ row }) => {
|
||||
const {
|
||||
attributes: { inserted_at },
|
||||
} = getProviderData(row);
|
||||
return <DateWithTime dateTime={inserted_at} showTime={false} />;
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "actions",
|
||||
cell: ({ row }) => {
|
||||
return <DataTableRowActions row={row} />;
|
||||
},
|
||||
},
|
||||
];
|
||||
99
ui/components/manage-groups/table/data-table-row-actions.tsx
Normal file
99
ui/components/manage-groups/table/data-table-row-actions.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
Button,
|
||||
Dropdown,
|
||||
DropdownItem,
|
||||
DropdownMenu,
|
||||
DropdownSection,
|
||||
DropdownTrigger,
|
||||
} from "@nextui-org/react";
|
||||
import {
|
||||
DeleteDocumentBulkIcon,
|
||||
EditDocumentBulkIcon,
|
||||
} from "@nextui-org/shared-icons";
|
||||
import { Row } from "@tanstack/react-table";
|
||||
import clsx from "clsx";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
|
||||
import { VerticalDotsIcon } from "@/components/icons";
|
||||
import { CustomAlertModal } from "@/components/ui/custom";
|
||||
|
||||
import { DeleteGroupForm } from "../forms";
|
||||
|
||||
interface DataTableRowActionsProps<ProviderProps> {
|
||||
row: Row<ProviderProps>;
|
||||
}
|
||||
const iconClasses =
|
||||
"text-2xl text-default-500 pointer-events-none flex-shrink-0";
|
||||
|
||||
export function DataTableRowActions<ProviderProps>({
|
||||
row,
|
||||
}: DataTableRowActionsProps<ProviderProps>) {
|
||||
const [isDeleteOpen, setIsDeleteOpen] = useState(false);
|
||||
const groupId = (row.original as { id: string }).id;
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
return (
|
||||
<>
|
||||
<CustomAlertModal
|
||||
isOpen={isDeleteOpen}
|
||||
onOpenChange={setIsDeleteOpen}
|
||||
title="Are you absolutely sure?"
|
||||
description="This action cannot be undone. This will permanently delete your provider account and remove your data from the server."
|
||||
>
|
||||
<DeleteGroupForm groupId={groupId} setIsOpen={setIsDeleteOpen} />
|
||||
</CustomAlertModal>
|
||||
|
||||
<div className="relative flex items-center justify-end gap-2">
|
||||
<Dropdown
|
||||
className="shadow-xl dark:bg-prowler-blue-800"
|
||||
placement="bottom"
|
||||
>
|
||||
<DropdownTrigger>
|
||||
<Button isIconOnly radius="full" size="sm" variant="light">
|
||||
<VerticalDotsIcon className="text-default-400" />
|
||||
</Button>
|
||||
</DropdownTrigger>
|
||||
<DropdownMenu
|
||||
closeOnSelect
|
||||
aria-label="Actions"
|
||||
color="default"
|
||||
variant="flat"
|
||||
>
|
||||
<DropdownSection title="Actions">
|
||||
<DropdownItem
|
||||
key="edit"
|
||||
description="Allows you to edit the provider group"
|
||||
textValue="Edit Provider Group"
|
||||
startContent={<EditDocumentBulkIcon className={iconClasses} />}
|
||||
onClick={() => router.push(`/manage-groups?groupId=${groupId}`)}
|
||||
>
|
||||
Edit Provider Group
|
||||
</DropdownItem>
|
||||
</DropdownSection>
|
||||
<DropdownSection title="Danger zone">
|
||||
<DropdownItem
|
||||
key="delete"
|
||||
className="text-danger"
|
||||
color="danger"
|
||||
description="Delete the provider group permanently"
|
||||
textValue="Delete Provider Group"
|
||||
startContent={
|
||||
<DeleteDocumentBulkIcon
|
||||
className={clsx(iconClasses, "!text-danger")}
|
||||
/>
|
||||
}
|
||||
onClick={() => setIsDeleteOpen(true)}
|
||||
>
|
||||
Delete Provider Group
|
||||
</DropdownItem>
|
||||
</DropdownSection>
|
||||
</DropdownMenu>
|
||||
</Dropdown>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
3
ui/components/manage-groups/table/index.ts
Normal file
3
ui/components/manage-groups/table/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./column-groups";
|
||||
export * from "./data-table-row-actions";
|
||||
export * from "./skeleton-table-groups";
|
||||
65
ui/components/manage-groups/table/skeleton-table-groups.tsx
Normal file
65
ui/components/manage-groups/table/skeleton-table-groups.tsx
Normal file
@@ -0,0 +1,65 @@
|
||||
import { Card, Skeleton } from "@nextui-org/react";
|
||||
import React from "react";
|
||||
|
||||
export const SkeletonTableGroups = () => {
|
||||
return (
|
||||
<Card className="h-full w-full space-y-5 p-4" radius="sm">
|
||||
{/* Table headers */}
|
||||
<div className="hidden justify-between md:flex">
|
||||
<Skeleton className="w-1/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-1/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-1/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
</div>
|
||||
|
||||
{/* Table body */}
|
||||
<div className="space-y-3">
|
||||
{[...Array(3)].map((_, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="flex flex-col items-center justify-between space-x-0 md:flex-row md:space-x-4"
|
||||
>
|
||||
<Skeleton className="mb-2 w-full rounded-lg md:mb-0 md:w-1/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 w-full rounded-lg md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-1/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-1/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
19
ui/components/providers/add-provider-button.tsx
Normal file
19
ui/components/providers/add-provider-button.tsx
Normal file
@@ -0,0 +1,19 @@
|
||||
"use client";
|
||||
|
||||
import { AddIcon } from "../icons";
|
||||
import { CustomButton } from "../ui/custom";
|
||||
|
||||
export const AddProviderButton = () => {
|
||||
return (
|
||||
<CustomButton
|
||||
asLink="/providers/connect-account"
|
||||
ariaLabel="Add Account"
|
||||
variant="solid"
|
||||
color="action"
|
||||
size="md"
|
||||
endContent={<AddIcon size={20} />}
|
||||
>
|
||||
Add Account
|
||||
</CustomButton>
|
||||
);
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
export * from "./add-provider";
|
||||
export * from "./add-provider-button";
|
||||
export * from "./forms/delete-form";
|
||||
export * from "./link-to-scans";
|
||||
export * from "./provider-info";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { Chip } from "@nextui-org/react";
|
||||
import { ColumnDef } from "@tanstack/react-table";
|
||||
|
||||
import { DateWithTime, SnippetId } from "@/components/ui/entities";
|
||||
@@ -10,8 +11,16 @@ import { LinkToScans } from "../link-to-scans";
|
||||
import { ProviderInfo } from "../provider-info";
|
||||
import { DataTableRowActions } from "./data-table-row-actions";
|
||||
|
||||
interface GroupNameChipsProps {
|
||||
groupNames?: string[];
|
||||
}
|
||||
|
||||
const getProviderData = (row: { original: ProviderProps }) => {
|
||||
return row.original;
|
||||
const provider = row.original;
|
||||
return {
|
||||
attributes: provider.attributes,
|
||||
groupNames: provider.groupNames,
|
||||
};
|
||||
};
|
||||
|
||||
export const ColumnProviders: ColumnDef<ProviderProps>[] = [
|
||||
@@ -44,6 +53,14 @@ export const ColumnProviders: ColumnDef<ProviderProps>[] = [
|
||||
return <LinkToScans providerUid={uid} />;
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "groupNames",
|
||||
header: "Groups",
|
||||
cell: ({ row }) => {
|
||||
const { groupNames } = getProviderData(row);
|
||||
return <GroupNameChips groupNames={groupNames || []} />;
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "uid",
|
||||
header: ({ column }) => (
|
||||
@@ -79,3 +96,24 @@ export const ColumnProviders: ColumnDef<ProviderProps>[] = [
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
export const GroupNameChips: React.FC<GroupNameChipsProps> = ({
|
||||
groupNames,
|
||||
}) => {
|
||||
return (
|
||||
<div className="flex max-w-[300px] flex-wrap gap-1">
|
||||
{groupNames?.map((name, index) => (
|
||||
<Chip
|
||||
key={index}
|
||||
size="sm"
|
||||
variant="flat"
|
||||
classNames={{
|
||||
base: "bg-default-100",
|
||||
}}
|
||||
>
|
||||
{name}
|
||||
</Chip>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -3,18 +3,18 @@
|
||||
import { AddIcon } from "../icons";
|
||||
import { CustomButton } from "../ui/custom";
|
||||
|
||||
export const AddProvider = () => {
|
||||
export const AddRoleButton = () => {
|
||||
return (
|
||||
<div className="flex w-full items-center justify-end">
|
||||
<CustomButton
|
||||
asLink="/providers/connect-account"
|
||||
ariaLabel="Add Account"
|
||||
asLink="/roles/new"
|
||||
ariaLabel="Add Role"
|
||||
variant="solid"
|
||||
color="action"
|
||||
size="md"
|
||||
endContent={<AddIcon size={20} />}
|
||||
>
|
||||
Add Account
|
||||
Add Role
|
||||
</CustomButton>
|
||||
</div>
|
||||
);
|
||||
1
ui/components/roles/index.ts
Normal file
1
ui/components/roles/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./add-role-button";
|
||||
118
ui/components/roles/table/column-roles.tsx
Normal file
118
ui/components/roles/table/column-roles.tsx
Normal file
@@ -0,0 +1,118 @@
|
||||
"use client";
|
||||
|
||||
import { ColumnDef } from "@tanstack/react-table";
|
||||
|
||||
import { DateWithTime } from "@/components/ui/entities";
|
||||
import { DataTableColumnHeader } from "@/components/ui/table";
|
||||
import { RolesProps } from "@/types";
|
||||
|
||||
import { DataTableRowActions } from "./data-table-row-actions";
|
||||
|
||||
const getRoleAttributes = (row: { original: RolesProps["data"][number] }) => {
|
||||
return row.original.attributes;
|
||||
};
|
||||
|
||||
const getRoleRelationships = (row: {
|
||||
original: RolesProps["data"][number];
|
||||
}) => {
|
||||
return row.original.relationships;
|
||||
};
|
||||
|
||||
export const ColumnsRoles: ColumnDef<RolesProps["data"][number]>[] = [
|
||||
{
|
||||
accessorKey: "role",
|
||||
header: ({ column }) => (
|
||||
<DataTableColumnHeader column={column} title={"Role"} param="name" />
|
||||
),
|
||||
cell: ({ row }) => {
|
||||
const data = getRoleAttributes(row);
|
||||
return (
|
||||
<p className="font-semibold">
|
||||
{data.name[0].toUpperCase() + data.name.slice(1).toLowerCase()}
|
||||
</p>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "users",
|
||||
header: ({ column }) => (
|
||||
<DataTableColumnHeader column={column} title={"Users"} param="users" />
|
||||
),
|
||||
cell: ({ row }) => {
|
||||
const relationships = getRoleRelationships(row);
|
||||
const count = relationships.users.meta.count;
|
||||
return (
|
||||
<p className="text-xs font-semibold">
|
||||
{count === 0
|
||||
? "No Users"
|
||||
: `${count} ${count === 1 ? "User" : "Users"}`}
|
||||
</p>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "invitations",
|
||||
header: ({ column }) => (
|
||||
<DataTableColumnHeader
|
||||
column={column}
|
||||
title={"Invitations"}
|
||||
param="invitations"
|
||||
/>
|
||||
),
|
||||
cell: ({ row }) => {
|
||||
const relationships = getRoleRelationships(row);
|
||||
return (
|
||||
<p className="text-xs font-semibold">
|
||||
{relationships.invitations.meta.count === 0
|
||||
? "No Invitations"
|
||||
: `${relationships.invitations.meta.count} ${
|
||||
relationships.invitations.meta.count === 1
|
||||
? "Invitation"
|
||||
: "Invitations"
|
||||
}`}
|
||||
</p>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "permission_state",
|
||||
header: ({ column }) => (
|
||||
<DataTableColumnHeader
|
||||
column={column}
|
||||
title={"Permissions"}
|
||||
param="permission_state"
|
||||
/>
|
||||
),
|
||||
cell: ({ row }) => {
|
||||
const { permission_state } = getRoleAttributes(row);
|
||||
return (
|
||||
<p className="text-xs font-semibold">
|
||||
{permission_state[0].toUpperCase() +
|
||||
permission_state.slice(1).toLowerCase()}
|
||||
</p>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "inserted_at",
|
||||
header: ({ column }) => (
|
||||
<DataTableColumnHeader
|
||||
column={column}
|
||||
title={"Added"}
|
||||
param="inserted_at"
|
||||
/>
|
||||
),
|
||||
cell: ({ row }) => {
|
||||
const { inserted_at } = getRoleAttributes(row);
|
||||
return <DateWithTime dateTime={inserted_at} showTime={false} />;
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "actions",
|
||||
header: () => <div className="text-right">Actions</div>,
|
||||
id: "actions",
|
||||
cell: ({ row }) => {
|
||||
return <DataTableRowActions row={row} />;
|
||||
},
|
||||
},
|
||||
];
|
||||
93
ui/components/roles/table/data-table-row-actions.tsx
Normal file
93
ui/components/roles/table/data-table-row-actions.tsx
Normal file
@@ -0,0 +1,93 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
Button,
|
||||
Dropdown,
|
||||
DropdownItem,
|
||||
DropdownMenu,
|
||||
DropdownSection,
|
||||
DropdownTrigger,
|
||||
} from "@nextui-org/react";
|
||||
import {
|
||||
DeleteDocumentBulkIcon,
|
||||
EditDocumentBulkIcon,
|
||||
} from "@nextui-org/shared-icons";
|
||||
import { Row } from "@tanstack/react-table";
|
||||
import clsx from "clsx";
|
||||
import { useState } from "react";
|
||||
|
||||
import { VerticalDotsIcon } from "@/components/icons";
|
||||
import { CustomAlertModal } from "@/components/ui/custom/custom-alert-modal";
|
||||
|
||||
import { DeleteRoleForm } from "../workflow/forms";
|
||||
interface DataTableRowActionsProps<RoleProps> {
|
||||
row: Row<RoleProps>;
|
||||
}
|
||||
const iconClasses =
|
||||
"text-2xl text-default-500 pointer-events-none flex-shrink-0";
|
||||
|
||||
export function DataTableRowActions<RoleProps>({
|
||||
row,
|
||||
}: DataTableRowActionsProps<RoleProps>) {
|
||||
const [isDeleteOpen, setIsDeleteOpen] = useState(false);
|
||||
const roleId = (row.original as { id: string }).id;
|
||||
return (
|
||||
<>
|
||||
<CustomAlertModal
|
||||
isOpen={isDeleteOpen}
|
||||
onOpenChange={setIsDeleteOpen}
|
||||
title="Are you absolutely sure?"
|
||||
description="This action cannot be undone. This will permanently delete your role and remove your data from the server."
|
||||
>
|
||||
<DeleteRoleForm roleId={roleId} setIsOpen={setIsDeleteOpen} />
|
||||
</CustomAlertModal>
|
||||
<div className="relative flex items-center justify-end gap-2">
|
||||
<Dropdown
|
||||
className="shadow-xl dark:bg-prowler-blue-800"
|
||||
placement="bottom"
|
||||
>
|
||||
<DropdownTrigger>
|
||||
<Button isIconOnly radius="full" size="sm" variant="light">
|
||||
<VerticalDotsIcon className="text-default-400" />
|
||||
</Button>
|
||||
</DropdownTrigger>
|
||||
<DropdownMenu
|
||||
closeOnSelect
|
||||
aria-label="Actions"
|
||||
color="default"
|
||||
variant="flat"
|
||||
>
|
||||
<DropdownSection title="Actions">
|
||||
<DropdownItem
|
||||
href={`/roles/edit?roleId=${roleId}`}
|
||||
key="check-details"
|
||||
description="Edit the role details"
|
||||
textValue="Edit Role"
|
||||
startContent={<EditDocumentBulkIcon className={iconClasses} />}
|
||||
>
|
||||
Edit Role
|
||||
</DropdownItem>
|
||||
</DropdownSection>
|
||||
<DropdownSection title="Danger zone">
|
||||
<DropdownItem
|
||||
key="delete"
|
||||
className="text-danger"
|
||||
color="danger"
|
||||
description="Delete the role permanently"
|
||||
textValue="Delete Role"
|
||||
startContent={
|
||||
<DeleteDocumentBulkIcon
|
||||
className={clsx(iconClasses, "!text-danger")}
|
||||
/>
|
||||
}
|
||||
onClick={() => setIsDeleteOpen(true)}
|
||||
>
|
||||
Delete Role
|
||||
</DropdownItem>
|
||||
</DropdownSection>
|
||||
</DropdownMenu>
|
||||
</Dropdown>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
3
ui/components/roles/table/index.ts
Normal file
3
ui/components/roles/table/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./column-roles";
|
||||
export * from "./data-table-row-actions";
|
||||
export * from "./skeleton-table-roles";
|
||||
59
ui/components/roles/table/skeleton-table-roles.tsx
Normal file
59
ui/components/roles/table/skeleton-table-roles.tsx
Normal file
@@ -0,0 +1,59 @@
|
||||
import { Card, Skeleton } from "@nextui-org/react";
|
||||
import React from "react";
|
||||
|
||||
export const SkeletonTableRoles = () => {
|
||||
return (
|
||||
<Card className="h-full w-full space-y-5 p-4" radius="sm">
|
||||
{/* Table headers */}
|
||||
<div className="hidden justify-between md:flex">
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-1/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
</div>
|
||||
|
||||
{/* Table body */}
|
||||
<div className="space-y-3">
|
||||
{[...Array(10)].map((_, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="flex flex-col items-center justify-between space-x-0 md:flex-row md:space-x-4"
|
||||
>
|
||||
<Skeleton className="mb-2 w-full rounded-lg md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 w-full rounded-lg md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-1/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
250
ui/components/roles/workflow/forms/add-role-form.tsx
Normal file
250
ui/components/roles/workflow/forms/add-role-form.tsx
Normal file
@@ -0,0 +1,250 @@
|
||||
"use client";
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Checkbox, Divider } from "@nextui-org/react";
|
||||
import { SaveIcon } from "lucide-react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
|
||||
import { addRole } from "@/actions/roles/roles";
|
||||
import { useToast } from "@/components/ui";
|
||||
import {
|
||||
CustomButton,
|
||||
CustomDropdownSelection,
|
||||
CustomInput,
|
||||
} from "@/components/ui/custom";
|
||||
import { Form } from "@/components/ui/form";
|
||||
import { addRoleFormSchema, ApiError } from "@/types";
|
||||
|
||||
type FormValues = z.infer<typeof addRoleFormSchema>;
|
||||
|
||||
export const AddRoleForm = ({
|
||||
groups,
|
||||
}: {
|
||||
groups: { id: string; name: string }[];
|
||||
}) => {
|
||||
const { toast } = useToast();
|
||||
const router = useRouter();
|
||||
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(addRoleFormSchema),
|
||||
defaultValues: {
|
||||
name: "",
|
||||
manage_users: false,
|
||||
manage_account: false,
|
||||
manage_billing: false,
|
||||
manage_providers: false,
|
||||
manage_integrations: false,
|
||||
manage_scans: false,
|
||||
unlimited_visibility: false,
|
||||
groups: [],
|
||||
},
|
||||
});
|
||||
|
||||
const manageProviders = form.watch("manage_providers");
|
||||
const unlimitedVisibility = form.watch("unlimited_visibility");
|
||||
|
||||
useEffect(() => {
|
||||
if (manageProviders) {
|
||||
form.setValue("unlimited_visibility", true, {
|
||||
shouldValidate: true,
|
||||
shouldDirty: true,
|
||||
shouldTouch: true,
|
||||
});
|
||||
}
|
||||
}, [manageProviders, form]);
|
||||
|
||||
const isLoading = form.formState.isSubmitting;
|
||||
|
||||
const onSelectAllChange = (checked: boolean) => {
|
||||
const permissions = [
|
||||
"manage_users",
|
||||
"manage_account",
|
||||
"manage_billing",
|
||||
"manage_providers",
|
||||
"manage_integrations",
|
||||
"manage_scans",
|
||||
"unlimited_visibility",
|
||||
];
|
||||
permissions.forEach((permission) => {
|
||||
form.setValue(permission as keyof FormValues, checked, {
|
||||
shouldValidate: true,
|
||||
shouldDirty: true,
|
||||
shouldTouch: true,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const onSubmitClient = async (values: FormValues) => {
|
||||
const formData = new FormData();
|
||||
formData.append("name", values.name);
|
||||
formData.append("manage_users", String(values.manage_users));
|
||||
formData.append("manage_account", String(values.manage_account));
|
||||
formData.append("manage_billing", String(values.manage_billing));
|
||||
formData.append("manage_providers", String(values.manage_providers));
|
||||
formData.append("manage_integrations", String(values.manage_integrations));
|
||||
formData.append("manage_scans", String(values.manage_scans));
|
||||
formData.append(
|
||||
"unlimited_visibility",
|
||||
String(values.unlimited_visibility),
|
||||
);
|
||||
|
||||
if (values.groups && values.groups.length > 0) {
|
||||
values.groups.forEach((group) => {
|
||||
formData.append("groups[]", group);
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await addRole(formData);
|
||||
|
||||
if (data?.errors && data.errors.length > 0) {
|
||||
data.errors.forEach((error: ApiError) => {
|
||||
const errorMessage = error.detail;
|
||||
switch (error.source.pointer) {
|
||||
case "/data/attributes/name":
|
||||
form.setError("name", {
|
||||
type: "server",
|
||||
message: errorMessage,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Error",
|
||||
description: errorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
title: "Role Added",
|
||||
description: "The role was added successfully.",
|
||||
});
|
||||
router.push("/roles");
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Error",
|
||||
description: "An unexpected error occurred. Please try again.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const permissions = [
|
||||
{ field: "manage_users", label: "Invite and Manage Users" },
|
||||
{ field: "manage_account", label: "Manage Account" },
|
||||
{ field: "manage_billing", label: "Manage Billing" },
|
||||
{ field: "manage_providers", label: "Manage Cloud Providers" },
|
||||
{ field: "manage_integrations", label: "Manage Integrations" },
|
||||
{ field: "manage_scans", label: "Manage Scans" },
|
||||
{ field: "unlimited_visibility", label: "Unlimited Visibility" },
|
||||
];
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit(onSubmitClient)}
|
||||
className="flex flex-col space-y-6"
|
||||
>
|
||||
<CustomInput
|
||||
control={form.control}
|
||||
name="name"
|
||||
type="text"
|
||||
label="Role Name"
|
||||
labelPlacement="inside"
|
||||
placeholder="Enter role name"
|
||||
variant="bordered"
|
||||
isRequired
|
||||
isInvalid={!!form.formState.errors.name}
|
||||
/>
|
||||
|
||||
<div className="flex flex-col space-y-4">
|
||||
<span className="text-lg font-semibold">Admin Permissions</span>
|
||||
|
||||
{/* Select All Checkbox */}
|
||||
<Checkbox
|
||||
isSelected={permissions.every((perm) =>
|
||||
form.watch(perm.field as keyof FormValues),
|
||||
)}
|
||||
onChange={(e) => onSelectAllChange(e.target.checked)}
|
||||
classNames={{
|
||||
label: "text-small",
|
||||
}}
|
||||
>
|
||||
Grant all admin permissions
|
||||
</Checkbox>
|
||||
|
||||
{/* Permissions Grid */}
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
{permissions.map(({ field, label }) => (
|
||||
<Checkbox
|
||||
key={field}
|
||||
{...form.register(field as keyof FormValues)}
|
||||
isSelected={!!form.watch(field as keyof FormValues)}
|
||||
classNames={{
|
||||
label: "text-small",
|
||||
}}
|
||||
>
|
||||
{label}
|
||||
</Checkbox>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<Divider className="my-4" />
|
||||
|
||||
{!unlimitedVisibility && (
|
||||
<div className="flex flex-col space-y-4">
|
||||
<span className="text-lg font-semibold">
|
||||
Groups and Account Visibility
|
||||
</span>
|
||||
|
||||
<p className="text-small font-medium text-default-700">
|
||||
Select the groups this role will have access to. If no groups are
|
||||
selected and unlimited visibility is not enabled, the role will
|
||||
not have access to any accounts.
|
||||
</p>
|
||||
|
||||
<Controller
|
||||
name="groups"
|
||||
control={form.control}
|
||||
render={({ field }) => (
|
||||
<CustomDropdownSelection
|
||||
label="Select Groups"
|
||||
name="groups"
|
||||
values={groups}
|
||||
selectedKeys={field.value || []}
|
||||
onChange={(name, selectedValues) =>
|
||||
field.onChange(selectedValues)
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
{form.formState.errors.groups && (
|
||||
<p className="mt-2 text-sm text-red-600">
|
||||
{form.formState.errors.groups.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<div className="flex w-full justify-end sm:space-x-6">
|
||||
<CustomButton
|
||||
type="submit"
|
||||
ariaLabel="Add Role"
|
||||
className="w-1/2"
|
||||
variant="solid"
|
||||
color="action"
|
||||
size="lg"
|
||||
isLoading={isLoading}
|
||||
startContent={!isLoading && <SaveIcon size={24} />}
|
||||
>
|
||||
{isLoading ? <>Loading</> : <span>Add Role</span>}
|
||||
</CustomButton>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
87
ui/components/roles/workflow/forms/delete-role-form.tsx
Normal file
87
ui/components/roles/workflow/forms/delete-role-form.tsx
Normal file
@@ -0,0 +1,87 @@
|
||||
"use client";
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import React, { Dispatch, SetStateAction } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import * as z from "zod";
|
||||
|
||||
import { deleteRole } from "@/actions/roles";
|
||||
import { DeleteIcon } from "@/components/icons";
|
||||
import { useToast } from "@/components/ui";
|
||||
import { CustomButton } from "@/components/ui/custom";
|
||||
import { Form } from "@/components/ui/form";
|
||||
|
||||
const formSchema = z.object({
|
||||
roleId: z.string(),
|
||||
});
|
||||
|
||||
export const DeleteRoleForm = ({
|
||||
roleId,
|
||||
setIsOpen,
|
||||
}: {
|
||||
roleId: string;
|
||||
setIsOpen: Dispatch<SetStateAction<boolean>>;
|
||||
}) => {
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
});
|
||||
const { toast } = useToast();
|
||||
const isLoading = form.formState.isSubmitting;
|
||||
|
||||
async function onSubmitClient(formData: FormData) {
|
||||
const roleId = formData.get("id") as string;
|
||||
const data = await deleteRole(roleId);
|
||||
|
||||
if (data?.errors && data.errors.length > 0) {
|
||||
const error = data.errors[0];
|
||||
const errorMessage = `${error.detail}`;
|
||||
// show error
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Oops! Something went wrong",
|
||||
description: errorMessage,
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
title: "Success!",
|
||||
description: "The role was removed successfully.",
|
||||
});
|
||||
}
|
||||
setIsOpen(false); // Close the modal on success
|
||||
}
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form action={onSubmitClient}>
|
||||
<input type="hidden" name="id" value={roleId} />
|
||||
<div className="flex w-full justify-center sm:space-x-6">
|
||||
<CustomButton
|
||||
type="button"
|
||||
ariaLabel="Cancel"
|
||||
className="w-full bg-transparent"
|
||||
variant="faded"
|
||||
size="lg"
|
||||
radius="lg"
|
||||
onPress={() => setIsOpen(false)}
|
||||
isDisabled={isLoading}
|
||||
>
|
||||
<span>Cancel</span>
|
||||
</CustomButton>
|
||||
|
||||
<CustomButton
|
||||
type="submit"
|
||||
ariaLabel="Delete"
|
||||
className="w-full"
|
||||
variant="solid"
|
||||
color="danger"
|
||||
size="lg"
|
||||
isLoading={isLoading}
|
||||
startContent={!isLoading && <DeleteIcon size={24} />}
|
||||
>
|
||||
{isLoading ? <>Loading</> : <span>Delete</span>}
|
||||
</CustomButton>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
272
ui/components/roles/workflow/forms/edit-role-form.tsx
Normal file
272
ui/components/roles/workflow/forms/edit-role-form.tsx
Normal file
@@ -0,0 +1,272 @@
|
||||
"use client";
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Checkbox, Divider } from "@nextui-org/react";
|
||||
import { SaveIcon } from "lucide-react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
|
||||
import { updateRole } from "@/actions/roles/roles";
|
||||
import { useToast } from "@/components/ui";
|
||||
import {
|
||||
CustomButton,
|
||||
CustomDropdownSelection,
|
||||
CustomInput,
|
||||
} from "@/components/ui/custom";
|
||||
import { Form } from "@/components/ui/form";
|
||||
import { ApiError, editRoleFormSchema } from "@/types";
|
||||
|
||||
type FormValues = z.infer<typeof editRoleFormSchema>;
|
||||
|
||||
export const EditRoleForm = ({
|
||||
roleId,
|
||||
roleData,
|
||||
groups,
|
||||
}: {
|
||||
roleId: string;
|
||||
roleData: {
|
||||
data: {
|
||||
attributes: FormValues;
|
||||
relationships?: {
|
||||
provider_groups?: {
|
||||
data: Array<{ id: string; type: string }>;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
groups: { id: string; name: string }[];
|
||||
}) => {
|
||||
const { toast } = useToast();
|
||||
const router = useRouter();
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(editRoleFormSchema),
|
||||
defaultValues: {
|
||||
...roleData.data.attributes,
|
||||
groups:
|
||||
roleData.data.relationships?.provider_groups?.data.map((g) => g.id) ||
|
||||
[],
|
||||
},
|
||||
});
|
||||
|
||||
const { watch, setValue } = form;
|
||||
|
||||
const manageProviders = watch("manage_providers");
|
||||
const unlimitedVisibility = watch("unlimited_visibility");
|
||||
|
||||
useEffect(() => {
|
||||
if (manageProviders && !unlimitedVisibility) {
|
||||
setValue("unlimited_visibility", true, {
|
||||
shouldValidate: true,
|
||||
shouldDirty: true,
|
||||
shouldTouch: true,
|
||||
});
|
||||
}
|
||||
}, [manageProviders, unlimitedVisibility, setValue]);
|
||||
|
||||
const isLoading = form.formState.isSubmitting;
|
||||
|
||||
const onSelectAllChange = (checked: boolean) => {
|
||||
const permissions = [
|
||||
"manage_users",
|
||||
"manage_account",
|
||||
"manage_billing",
|
||||
"manage_providers",
|
||||
"manage_integrations",
|
||||
"manage_scans",
|
||||
"unlimited_visibility",
|
||||
];
|
||||
permissions.forEach((permission) => {
|
||||
form.setValue(permission as keyof FormValues, checked, {
|
||||
shouldValidate: true,
|
||||
shouldDirty: true,
|
||||
shouldTouch: true,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const onSubmitClient = async (values: FormValues) => {
|
||||
try {
|
||||
const updatedFields: Partial<FormValues> = {};
|
||||
|
||||
if (values.name !== roleData.data.attributes.name) {
|
||||
updatedFields.name = values.name;
|
||||
}
|
||||
|
||||
updatedFields.manage_users = values.manage_users;
|
||||
updatedFields.manage_account = values.manage_account;
|
||||
updatedFields.manage_billing = values.manage_billing;
|
||||
updatedFields.manage_providers = values.manage_providers;
|
||||
updatedFields.manage_integrations = values.manage_integrations;
|
||||
updatedFields.manage_scans = values.manage_scans;
|
||||
updatedFields.unlimited_visibility = values.unlimited_visibility;
|
||||
|
||||
if (
|
||||
JSON.stringify(values.groups) !==
|
||||
JSON.stringify(roleData.data.relationships?.provider_groups?.data)
|
||||
) {
|
||||
updatedFields.groups = values.groups;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
|
||||
Object.entries(updatedFields).forEach(([key, value]) => {
|
||||
if (key === "groups" && Array.isArray(value)) {
|
||||
value.forEach((group) => {
|
||||
formData.append("groups[]", group);
|
||||
});
|
||||
} else {
|
||||
formData.append(key, String(value));
|
||||
}
|
||||
});
|
||||
|
||||
const data = await updateRole(formData, roleId);
|
||||
|
||||
if (data?.errors && data.errors.length > 0) {
|
||||
data.errors.forEach((error: ApiError) => {
|
||||
const errorMessage = error.detail;
|
||||
switch (error.source.pointer) {
|
||||
case "/data/attributes/name":
|
||||
form.setError("name", {
|
||||
type: "server",
|
||||
message: errorMessage,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Error",
|
||||
description: errorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
title: "Role Updated",
|
||||
description: "The role was updated successfully.",
|
||||
});
|
||||
router.push("/roles");
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Error",
|
||||
description: "An unexpected error occurred. Please try again.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const permissions = [
|
||||
{ field: "manage_users", label: "Invite and Manage Users" },
|
||||
{ field: "manage_account", label: "Manage Account" },
|
||||
{ field: "manage_billing", label: "Manage Billing" },
|
||||
{ field: "manage_providers", label: "Manage Cloud Providers" },
|
||||
{ field: "manage_integrations", label: "Manage Integrations" },
|
||||
{ field: "manage_scans", label: "Manage Scans" },
|
||||
{ field: "unlimited_visibility", label: "Unlimited Visibility" },
|
||||
];
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit(onSubmitClient)}
|
||||
className="flex flex-col space-y-6"
|
||||
>
|
||||
<CustomInput
|
||||
control={form.control}
|
||||
name="name"
|
||||
type="text"
|
||||
label="Role Name"
|
||||
labelPlacement="inside"
|
||||
placeholder="Enter role name"
|
||||
variant="bordered"
|
||||
isRequired
|
||||
isInvalid={!!form.formState.errors.name}
|
||||
/>
|
||||
|
||||
<div className="flex flex-col space-y-4">
|
||||
<span className="text-lg font-semibold">Admin Permissions</span>
|
||||
|
||||
{/* Select All Checkbox */}
|
||||
<Checkbox
|
||||
isSelected={permissions.every((perm) =>
|
||||
form.watch(perm.field as keyof FormValues),
|
||||
)}
|
||||
onChange={(e) => onSelectAllChange(e.target.checked)}
|
||||
classNames={{
|
||||
label: "text-small",
|
||||
}}
|
||||
>
|
||||
Grant all admin permissions
|
||||
</Checkbox>
|
||||
|
||||
{/* Permissions Grid */}
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
{permissions.map(({ field, label }) => (
|
||||
<Checkbox
|
||||
key={field}
|
||||
{...form.register(field as keyof FormValues)}
|
||||
isSelected={!!form.watch(field as keyof FormValues)}
|
||||
classNames={{
|
||||
label: "text-small",
|
||||
}}
|
||||
>
|
||||
{label}
|
||||
</Checkbox>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<Divider className="my-4" />
|
||||
|
||||
{!unlimitedVisibility && (
|
||||
<div className="flex flex-col space-y-4">
|
||||
<span className="text-lg font-semibold">Groups visibility</span>
|
||||
|
||||
<p className="text-small font-medium text-default-700">
|
||||
Select the groups this role will have access to. If no groups are
|
||||
selected and unlimited visibility is not enabled, the role will
|
||||
not have access to any accounts.
|
||||
</p>
|
||||
|
||||
<Controller
|
||||
name="groups"
|
||||
control={form.control}
|
||||
render={({ field }) => (
|
||||
<CustomDropdownSelection
|
||||
label="Select Groups"
|
||||
name="groups"
|
||||
values={groups}
|
||||
selectedKeys={field.value}
|
||||
onChange={(name, selectedValues) => {
|
||||
field.onChange(selectedValues);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
|
||||
{form.formState.errors.groups && (
|
||||
<p className="mt-2 text-sm text-red-600">
|
||||
{form.formState.errors.groups.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<div className="flex w-full justify-end sm:space-x-6">
|
||||
<CustomButton
|
||||
type="submit"
|
||||
ariaLabel="Update Role"
|
||||
className="w-1/2"
|
||||
variant="solid"
|
||||
color="action"
|
||||
size="lg"
|
||||
isLoading={isLoading}
|
||||
startContent={!isLoading && <SaveIcon size={24} />}
|
||||
>
|
||||
{isLoading ? <>Loading</> : <span>Update Role</span>}
|
||||
</CustomButton>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
3
ui/components/roles/workflow/forms/index.ts
Normal file
3
ui/components/roles/workflow/forms/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./add-role-form";
|
||||
export * from "./delete-role-form";
|
||||
export * from "./edit-role-form";
|
||||
3
ui/components/roles/workflow/index.ts
Normal file
3
ui/components/roles/workflow/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./skeleton-role-form";
|
||||
export * from "./vertical-steps";
|
||||
export * from "./workflow-add-edit-role";
|
||||
65
ui/components/roles/workflow/skeleton-role-form.tsx
Normal file
65
ui/components/roles/workflow/skeleton-role-form.tsx
Normal file
@@ -0,0 +1,65 @@
|
||||
import { Card, Skeleton } from "@nextui-org/react";
|
||||
import React from "react";
|
||||
|
||||
export const SkeletonRoleForm = () => {
|
||||
return (
|
||||
<Card className="h-full w-full space-y-5 p-4" radius="sm">
|
||||
{/* Table headers */}
|
||||
<div className="hidden justify-between md:flex">
|
||||
<Skeleton className="w-1/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-2/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-1/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="w-1/12 rounded-lg">
|
||||
<div className="h-8 bg-default-200"></div>
|
||||
</Skeleton>
|
||||
</div>
|
||||
|
||||
{/* Table body */}
|
||||
<div className="space-y-3">
|
||||
{[...Array(3)].map((_, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="flex flex-col items-center justify-between space-x-0 md:flex-row md:space-x-4"
|
||||
>
|
||||
<Skeleton className="mb-2 w-full rounded-lg md:mb-0 md:w-1/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 w-full rounded-lg md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-2/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-1/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
<Skeleton className="mb-2 hidden rounded-lg sm:flex md:mb-0 md:w-1/12">
|
||||
<div className="h-12 bg-default-300"></div>
|
||||
</Skeleton>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
291
ui/components/roles/workflow/vertical-steps.tsx
Normal file
291
ui/components/roles/workflow/vertical-steps.tsx
Normal file
@@ -0,0 +1,291 @@
|
||||
"use client";
|
||||
|
||||
import type { ButtonProps } from "@nextui-org/react";
|
||||
import { cn } from "@nextui-org/react";
|
||||
import { useControlledState } from "@react-stately/utils";
|
||||
import { domAnimation, LazyMotion, m } from "framer-motion";
|
||||
import type { ComponentProps } from "react";
|
||||
import React from "react";
|
||||
|
||||
export type VerticalStepProps = {
|
||||
className?: string;
|
||||
description?: React.ReactNode;
|
||||
title?: React.ReactNode;
|
||||
};
|
||||
|
||||
export interface VerticalStepsProps
|
||||
extends React.HTMLAttributes<HTMLButtonElement> {
|
||||
/**
|
||||
* An array of steps.
|
||||
*
|
||||
* @default []
|
||||
*/
|
||||
steps?: VerticalStepProps[];
|
||||
/**
|
||||
* The color of the steps.
|
||||
*
|
||||
* @default "primary"
|
||||
*/
|
||||
color?: ButtonProps["color"];
|
||||
/**
|
||||
* The current step index.
|
||||
*/
|
||||
currentStep?: number;
|
||||
/**
|
||||
* The default step index.
|
||||
*
|
||||
* @default 0
|
||||
*/
|
||||
defaultStep?: number;
|
||||
/**
|
||||
* Whether to hide the progress bars.
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
hideProgressBars?: boolean;
|
||||
/**
|
||||
* The custom class for the steps wrapper.
|
||||
*/
|
||||
className?: string;
|
||||
/**
|
||||
* The custom class for the step.
|
||||
*/
|
||||
stepClassName?: string;
|
||||
/**
|
||||
* Callback function when the step index changes.
|
||||
*/
|
||||
onStepChange?: (stepIndex: number) => void;
|
||||
}
|
||||
|
||||
function CheckIcon(props: ComponentProps<"svg">) {
|
||||
return (
|
||||
<svg
|
||||
{...props}
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth={2}
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<m.path
|
||||
animate={{ pathLength: 1 }}
|
||||
d="M5 13l4 4L19 7"
|
||||
initial={{ pathLength: 0 }}
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
transition={{
|
||||
delay: 0.2,
|
||||
type: "tween",
|
||||
ease: "easeOut",
|
||||
duration: 0.3,
|
||||
}}
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
|
||||
export const VerticalSteps = React.forwardRef<
|
||||
HTMLButtonElement,
|
||||
VerticalStepsProps
|
||||
>(
|
||||
(
|
||||
{
|
||||
color = "primary",
|
||||
steps = [],
|
||||
defaultStep = 0,
|
||||
onStepChange,
|
||||
currentStep: currentStepProp,
|
||||
hideProgressBars = false,
|
||||
stepClassName,
|
||||
className,
|
||||
...props
|
||||
},
|
||||
ref,
|
||||
) => {
|
||||
const [currentStep, setCurrentStep] = useControlledState(
|
||||
currentStepProp,
|
||||
defaultStep,
|
||||
onStepChange,
|
||||
);
|
||||
|
||||
const colors = React.useMemo(() => {
|
||||
let userColor;
|
||||
let fgColor;
|
||||
|
||||
const colorsVars = [
|
||||
"[--active-fg-color:var(--step-fg-color)]",
|
||||
"[--active-border-color:var(--step-color)]",
|
||||
"[--active-color:var(--step-color)]",
|
||||
"[--complete-background-color:var(--step-color)]",
|
||||
"[--complete-border-color:var(--step-color)]",
|
||||
"[--inactive-border-color:hsl(var(--nextui-default-300))]",
|
||||
"[--inactive-color:hsl(var(--nextui-default-300))]",
|
||||
];
|
||||
|
||||
switch (color) {
|
||||
case "primary":
|
||||
userColor = "[--step-color:hsl(var(--nextui-primary))]";
|
||||
fgColor = "[--step-fg-color:hsl(var(--nextui-primary-foreground))]";
|
||||
break;
|
||||
case "secondary":
|
||||
userColor = "[--step-color:hsl(var(--nextui-secondary))]";
|
||||
fgColor = "[--step-fg-color:hsl(var(--nextui-secondary-foreground))]";
|
||||
break;
|
||||
case "success":
|
||||
userColor = "[--step-color:hsl(var(--nextui-success))]";
|
||||
fgColor = "[--step-fg-color:hsl(var(--nextui-success-foreground))]";
|
||||
break;
|
||||
case "warning":
|
||||
userColor = "[--step-color:hsl(var(--nextui-warning))]";
|
||||
fgColor = "[--step-fg-color:hsl(var(--nextui-warning-foreground))]";
|
||||
break;
|
||||
case "danger":
|
||||
userColor = "[--step-color:hsl(var(--nextui-error))]";
|
||||
fgColor = "[--step-fg-color:hsl(var(--nextui-error-foreground))]";
|
||||
break;
|
||||
case "default":
|
||||
userColor = "[--step-color:hsl(var(--nextui-default))]";
|
||||
fgColor = "[--step-fg-color:hsl(var(--nextui-default-foreground))]";
|
||||
break;
|
||||
default:
|
||||
userColor = "[--step-color:hsl(var(--nextui-primary))]";
|
||||
fgColor = "[--step-fg-color:hsl(var(--nextui-primary-foreground))]";
|
||||
break;
|
||||
}
|
||||
|
||||
if (!className?.includes("--step-fg-color")) colorsVars.unshift(fgColor);
|
||||
if (!className?.includes("--step-color")) colorsVars.unshift(userColor);
|
||||
if (!className?.includes("--inactive-bar-color"))
|
||||
colorsVars.push(
|
||||
"[--inactive-bar-color:hsl(var(--nextui-default-300))]",
|
||||
);
|
||||
|
||||
return colorsVars;
|
||||
}, [color, className]);
|
||||
|
||||
return (
|
||||
<nav aria-label="Progress" className="max-w-fit">
|
||||
<ol className={cn("flex flex-col gap-y-3", colors, className)}>
|
||||
{steps?.map((step, stepIdx) => {
|
||||
const status =
|
||||
currentStep === stepIdx
|
||||
? "active"
|
||||
: currentStep < stepIdx
|
||||
? "inactive"
|
||||
: "complete";
|
||||
|
||||
return (
|
||||
<li key={stepIdx} className="relative">
|
||||
<div className="flex w-full max-w-full items-center">
|
||||
<button
|
||||
key={stepIdx}
|
||||
ref={ref}
|
||||
aria-current={status === "active" ? "step" : undefined}
|
||||
className={cn(
|
||||
"group flex w-full cursor-pointer items-center justify-center gap-4 rounded-large px-3 py-2.5",
|
||||
stepClassName,
|
||||
)}
|
||||
onClick={() => setCurrentStep(stepIdx)}
|
||||
{...props}
|
||||
>
|
||||
<div className="flex h-full items-center">
|
||||
<LazyMotion features={domAnimation}>
|
||||
<div className="relative">
|
||||
<m.div
|
||||
animate={status}
|
||||
className={cn(
|
||||
"relative flex h-[34px] w-[34px] items-center justify-center rounded-full border-medium text-large font-semibold text-default-foreground",
|
||||
{
|
||||
"shadow-lg": status === "complete",
|
||||
},
|
||||
)}
|
||||
data-status={status}
|
||||
initial={false}
|
||||
transition={{ duration: 0.25 }}
|
||||
variants={{
|
||||
inactive: {
|
||||
backgroundColor: "transparent",
|
||||
borderColor: "var(--inactive-border-color)",
|
||||
color: "var(--inactive-color)",
|
||||
},
|
||||
active: {
|
||||
backgroundColor: "transparent",
|
||||
borderColor: "var(--active-border-color)",
|
||||
color: "var(--active-color)",
|
||||
},
|
||||
complete: {
|
||||
backgroundColor:
|
||||
"var(--complete-background-color)",
|
||||
borderColor: "var(--complete-border-color)",
|
||||
},
|
||||
}}
|
||||
>
|
||||
<div className="flex items-center justify-center">
|
||||
{status === "complete" ? (
|
||||
<CheckIcon className="h-6 w-6 text-[var(--active-fg-color)]" />
|
||||
) : (
|
||||
<span>{stepIdx + 1}</span>
|
||||
)}
|
||||
</div>
|
||||
</m.div>
|
||||
</div>
|
||||
</LazyMotion>
|
||||
</div>
|
||||
<div className="flex-1 text-left">
|
||||
<div>
|
||||
<div
|
||||
className={cn(
|
||||
"text-medium font-medium text-default-foreground transition-[color,opacity] duration-300 group-active:opacity-70",
|
||||
{
|
||||
"text-default-500": status === "inactive",
|
||||
},
|
||||
)}
|
||||
>
|
||||
{step.title}
|
||||
</div>
|
||||
<div
|
||||
className={cn(
|
||||
"text-tiny text-default-600 transition-[color,opacity] duration-300 group-active:opacity-70 lg:text-small",
|
||||
{
|
||||
"text-default-500": status === "inactive",
|
||||
},
|
||||
)}
|
||||
>
|
||||
{step.description}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
{stepIdx < steps.length - 1 && !hideProgressBars && (
|
||||
<div
|
||||
aria-hidden="true"
|
||||
className={cn(
|
||||
"pointer-events-none absolute left-3 top-[calc(64px_*_var(--idx)_+_1)] flex h-1/2 -translate-y-1/3 items-center px-4",
|
||||
)}
|
||||
style={{
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
"--idx": stepIdx,
|
||||
}}
|
||||
>
|
||||
<div
|
||||
className={cn(
|
||||
"relative h-full w-0.5 bg-[var(--inactive-bar-color)] transition-colors duration-300",
|
||||
"after:absolute after:block after:h-0 after:w-full after:bg-[var(--active-border-color)] after:transition-[height] after:duration-300 after:content-['']",
|
||||
{
|
||||
"after:h-full": stepIdx < currentStep,
|
||||
},
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ol>
|
||||
</nav>
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
VerticalSteps.displayName = "VerticalSteps";
|
||||
64
ui/components/roles/workflow/workflow-add-edit-role.tsx
Normal file
64
ui/components/roles/workflow/workflow-add-edit-role.tsx
Normal file
@@ -0,0 +1,64 @@
|
||||
"use client";
|
||||
|
||||
import { Progress, Spacer } from "@nextui-org/react";
|
||||
import { usePathname } from "next/navigation";
|
||||
import React from "react";
|
||||
|
||||
import { VerticalSteps } from "./vertical-steps";
|
||||
|
||||
const steps = [
|
||||
{
|
||||
title: "Create a new role",
|
||||
description: "Enter the name of the role you want to add.",
|
||||
href: "/roles/new",
|
||||
},
|
||||
{
|
||||
title: "Edit a existing role",
|
||||
description:
|
||||
"Update the role's details, including its name and permissions.",
|
||||
href: "/roles/edit",
|
||||
},
|
||||
];
|
||||
|
||||
export const WorkflowAddEditRole = () => {
|
||||
const pathname = usePathname();
|
||||
|
||||
// Calculate current step based on pathname
|
||||
const currentStepIndex = steps.findIndex((step) =>
|
||||
pathname.endsWith(step.href),
|
||||
);
|
||||
const currentStep = currentStepIndex === -1 ? 0 : currentStepIndex;
|
||||
|
||||
return (
|
||||
<section className="max-w-sm">
|
||||
<h1 className="mb-2 text-xl font-medium" id="getting-started">
|
||||
Manage Role Permissions
|
||||
</h1>
|
||||
<p className="mb-5 text-small text-default-500">
|
||||
Define a new role with customized permissions or modify an existing one
|
||||
to meet your needs.
|
||||
</p>
|
||||
<Progress
|
||||
classNames={{
|
||||
base: "px-0.5 mb-5",
|
||||
label: "text-small",
|
||||
value: "text-small text-default-400",
|
||||
}}
|
||||
label="Steps"
|
||||
maxValue={steps.length - 1}
|
||||
minValue={0}
|
||||
showValueLabel={true}
|
||||
size="md"
|
||||
value={currentStep}
|
||||
valueLabel={`${currentStep + 1} of ${steps.length}`}
|
||||
/>
|
||||
<VerticalSteps
|
||||
hideProgressBars
|
||||
currentStep={currentStep}
|
||||
stepClassName="border border-default-200 dark:border-default-50 aria-[current]:bg-default-100 dark:aria-[current]:bg-prowler-blue-800 cursor-default"
|
||||
steps={steps}
|
||||
/>
|
||||
<Spacer y={4} />
|
||||
</section>
|
||||
);
|
||||
};
|
||||
@@ -127,7 +127,7 @@ export const ColumnGetScans: ColumnDef<ScanProps>[] = [
|
||||
header: ({ column }) => (
|
||||
<DataTableColumnHeader
|
||||
column={column}
|
||||
title={"Next execution"}
|
||||
title={"Next scan"}
|
||||
param="next_scan_at"
|
||||
/>
|
||||
),
|
||||
|
||||
156
ui/components/ui/custom/custom-dropdown-selection.tsx
Normal file
156
ui/components/ui/custom/custom-dropdown-selection.tsx
Normal file
@@ -0,0 +1,156 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
Button,
|
||||
Checkbox,
|
||||
CheckboxGroup,
|
||||
Divider,
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
ScrollShadow,
|
||||
} from "@nextui-org/react";
|
||||
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
||||
|
||||
import { PlusCircleIcon } from "@/components/icons";
|
||||
|
||||
interface CustomDropdownSelectionProps {
|
||||
label: string;
|
||||
name: string;
|
||||
values: { id: string; name: string }[];
|
||||
onChange: (name: string, selectedValues: string[]) => void;
|
||||
selectedKeys?: string[];
|
||||
}
|
||||
|
||||
const selectedTagClass =
|
||||
"inline-flex items-center border py-1 text-xs transition-colors border-transparent bg-default-500 text-secondary-foreground hover:bg-default-500/80 rounded-md px-2 font-normal";
|
||||
|
||||
export const CustomDropdownSelection: React.FC<
|
||||
CustomDropdownSelectionProps
|
||||
> = ({ label, name, values, onChange, selectedKeys = [] }) => {
|
||||
const [selectedValues, setSelectedValues] = useState<Set<string>>(
|
||||
new Set(selectedKeys),
|
||||
);
|
||||
const allValues = values.map((item) => item.id);
|
||||
|
||||
const memoizedValues = useMemo(() => values, [values]);
|
||||
|
||||
// Update the internal state when selectedKeys changes from props
|
||||
useEffect(() => {
|
||||
const newSelection = new Set(selectedKeys);
|
||||
if (
|
||||
JSON.stringify(Array.from(selectedValues)) !==
|
||||
JSON.stringify(Array.from(newSelection))
|
||||
) {
|
||||
if (selectedKeys.length === allValues.length) {
|
||||
newSelection.add("all");
|
||||
}
|
||||
setSelectedValues(newSelection);
|
||||
}
|
||||
}, [selectedKeys]);
|
||||
|
||||
const onSelectionChange = useCallback(
|
||||
(keys: string[]) => {
|
||||
setSelectedValues((prevSelected) => {
|
||||
const newSelection = new Set(keys);
|
||||
|
||||
// If all values are selected and "all" is not included,
|
||||
// add "all" automatically
|
||||
if (
|
||||
newSelection.size === allValues.length &&
|
||||
!newSelection.has("all")
|
||||
) {
|
||||
return new Set(["all", ...allValues]);
|
||||
} else if (prevSelected.has("all")) {
|
||||
// If "all" was previously selected, remove it
|
||||
newSelection.delete("all");
|
||||
return new Set(allValues.filter((key) => newSelection.has(key)));
|
||||
}
|
||||
return newSelection;
|
||||
});
|
||||
|
||||
// Notify the change without including "all"
|
||||
const selectedValues = keys.filter((key) => key !== "all");
|
||||
onChange(name, selectedValues);
|
||||
},
|
||||
[allValues, name, onChange],
|
||||
);
|
||||
|
||||
const handleSelectAllClick = useCallback(() => {
|
||||
setSelectedValues((prevSelected: Set<string>) => {
|
||||
const newSelection: Set<string> = prevSelected.has("all")
|
||||
? new Set()
|
||||
: new Set(["all", ...allValues]);
|
||||
|
||||
// Notify the change without including "all"
|
||||
const selectedValues = Array.from(newSelection).filter(
|
||||
(key) => key !== "all",
|
||||
);
|
||||
onChange(name, selectedValues);
|
||||
|
||||
return newSelection;
|
||||
});
|
||||
}, [allValues, name, onChange]);
|
||||
|
||||
return (
|
||||
<div className="relative flex w-full flex-col gap-2">
|
||||
<Popover backdrop="transparent" placement="bottom-start">
|
||||
<PopoverTrigger>
|
||||
<Button
|
||||
className="border-input hover:bg-accent hover:text-accent-foreground inline-flex h-10 items-center justify-center whitespace-nowrap rounded-md border border-dashed bg-background px-3 text-xs font-medium shadow-sm transition-colors focus-visible:outline-none disabled:opacity-50 dark:bg-prowler-blue-800"
|
||||
startContent={<PlusCircleIcon size={16} />}
|
||||
size="md"
|
||||
>
|
||||
<h3 className="text-small">{label}</h3>
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-80 dark:bg-prowler-blue-800">
|
||||
<div className="flex w-full flex-col gap-6 p-2">
|
||||
<CheckboxGroup
|
||||
color="default"
|
||||
label={label}
|
||||
value={Array.from(selectedValues)}
|
||||
onValueChange={onSelectionChange}
|
||||
className="font-bold"
|
||||
>
|
||||
<Checkbox
|
||||
className="font-normal"
|
||||
value="all"
|
||||
onClick={handleSelectAllClick}
|
||||
>
|
||||
Select All
|
||||
</Checkbox>
|
||||
<Divider orientation="horizontal" className="mt-2" />
|
||||
<ScrollShadow
|
||||
hideScrollBar
|
||||
className="flex max-h-96 max-w-56 flex-col gap-y-2 py-2"
|
||||
>
|
||||
{memoizedValues.map(({ id, name }) => (
|
||||
<Checkbox className="font-normal" key={id} value={id}>
|
||||
{name}
|
||||
</Checkbox>
|
||||
))}
|
||||
</ScrollShadow>
|
||||
</CheckboxGroup>
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
|
||||
{/* Selected Values Display */}
|
||||
{selectedValues.size > 0 && (
|
||||
<div className="mt-2 flex flex-wrap gap-2">
|
||||
{Array.from(selectedValues)
|
||||
.filter((value) => value !== "all")
|
||||
.map((value) => {
|
||||
const selectedItem = values.find((item) => item.id === value);
|
||||
return (
|
||||
<span key={value} className={selectedTagClass}>
|
||||
{selectedItem?.name || value}
|
||||
</span>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -2,6 +2,7 @@ export * from "./custom-alert-modal";
|
||||
export * from "./custom-box";
|
||||
export * from "./custom-button";
|
||||
export * from "./custom-dropdown-filter";
|
||||
export * from "./custom-dropdown-selection";
|
||||
export * from "./custom-input";
|
||||
export * from "./custom-loader";
|
||||
export * from "./custom-radio";
|
||||
|
||||
@@ -211,6 +211,18 @@ export const sectionItems: SidebarItem[] = [
|
||||
icon: "lucide:scan-search",
|
||||
title: "Scan Jobs",
|
||||
},
|
||||
{
|
||||
key: "roles",
|
||||
href: "/roles",
|
||||
icon: "mdi:account-key-outline",
|
||||
title: "Roles",
|
||||
},
|
||||
{
|
||||
key: "manage-groups",
|
||||
href: "/manage-groups",
|
||||
icon: "solar:settings-outline",
|
||||
title: "Manage Groups",
|
||||
},
|
||||
// {
|
||||
// key: "integrations",
|
||||
// href: "/integrations",
|
||||
|
||||
@@ -80,20 +80,10 @@ export const DataTableColumnHeader = <TData, TValue>({
|
||||
|
||||
return (
|
||||
<Button
|
||||
className="h-10 w-fit max-w-[110px] whitespace-nowrap bg-transparent px-0 text-left align-middle text-tiny font-semibold text-foreground-500 outline-none dark:text-slate-400"
|
||||
className="flex h-10 w-full items-center justify-between whitespace-nowrap bg-transparent px-0 text-left align-middle text-tiny font-semibold text-foreground-500 outline-none dark:text-slate-400"
|
||||
onClick={getToggleSortingHandler}
|
||||
>
|
||||
<span
|
||||
className="block whitespace-normal break-normal"
|
||||
style={{
|
||||
display: "-webkit-box",
|
||||
WebkitBoxOrient: "vertical",
|
||||
WebkitLineClamp: 2,
|
||||
width: "90px",
|
||||
}}
|
||||
>
|
||||
{title}
|
||||
</span>
|
||||
<span className="block whitespace-normal break-normal">{title}</span>
|
||||
{renderSortIcon()}
|
||||
</Button>
|
||||
);
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
"use client";
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Select, SelectItem } from "@nextui-org/react";
|
||||
import { ShieldIcon, UserIcon } from "lucide-react";
|
||||
import { Dispatch, SetStateAction } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import * as z from "zod";
|
||||
|
||||
import { updateUser } from "@/actions/users/users";
|
||||
import { updateUser, updateUserRole } from "@/actions/users/users";
|
||||
import { SaveIcon } from "@/components/icons";
|
||||
import { useToast } from "@/components/ui";
|
||||
import { CustomButton, CustomInput } from "@/components/ui/custom";
|
||||
@@ -17,12 +19,16 @@ export const EditForm = ({
|
||||
userName,
|
||||
userEmail,
|
||||
userCompanyName,
|
||||
roles = [],
|
||||
currentRole = "",
|
||||
setIsOpen,
|
||||
}: {
|
||||
userId: string;
|
||||
userName?: string;
|
||||
userEmail?: string;
|
||||
userCompanyName?: string;
|
||||
roles: Array<{ id: string; name: string }>;
|
||||
currentRole?: string;
|
||||
setIsOpen: Dispatch<SetStateAction<boolean>>;
|
||||
}) => {
|
||||
const formSchema = editUserFormSchema();
|
||||
@@ -34,6 +40,7 @@ export const EditForm = ({
|
||||
name: userName,
|
||||
email: userEmail,
|
||||
company_name: userCompanyName,
|
||||
role: roles.find((role) => role.name === currentRole)?.id || "",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -44,7 +51,7 @@ export const EditForm = ({
|
||||
const onSubmitClient = async (values: z.infer<typeof formSchema>) => {
|
||||
const formData = new FormData();
|
||||
|
||||
// Check if the value is not undefined before appending to FormData
|
||||
// Update basic user data
|
||||
if (values.name !== undefined) {
|
||||
formData.append("name", values.name);
|
||||
}
|
||||
@@ -58,6 +65,26 @@ export const EditForm = ({
|
||||
// Always include userId
|
||||
formData.append("userId", userId);
|
||||
|
||||
// Handle role updates separately
|
||||
if (values.role !== roles.find((role) => role.name === currentRole)?.id) {
|
||||
const roleFormData = new FormData();
|
||||
roleFormData.append("userId", userId);
|
||||
roleFormData.append("roleId", values.role || "");
|
||||
|
||||
const roleUpdateResponse = await updateUserRole(roleFormData);
|
||||
|
||||
if (roleUpdateResponse?.errors && roleUpdateResponse.errors.length > 0) {
|
||||
const error = roleUpdateResponse.errors[0];
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Role Update Failed",
|
||||
description: `${error.detail}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Update other user attributes
|
||||
const data = await updateUser(formData);
|
||||
|
||||
if (data?.errors && data.errors.length > 0) {
|
||||
@@ -84,22 +111,49 @@ export const EditForm = ({
|
||||
onSubmit={form.handleSubmit(onSubmitClient)}
|
||||
className="flex flex-col space-y-4"
|
||||
>
|
||||
<div className="text-md">
|
||||
Current name: <span className="font-bold">{userName}</span>
|
||||
<div className="flex flex-row justify-center space-x-4 rounded-lg bg-gray-50 p-3">
|
||||
<div className="flex items-center text-small text-gray-600">
|
||||
<UserIcon className="mr-2 h-4 w-4" />
|
||||
<span className="text-gray-500">Name:</span>
|
||||
<span className="ml-2 font-semibold text-gray-900">{userName}</span>
|
||||
</div>
|
||||
<div className="flex items-center text-small text-gray-600">
|
||||
<ShieldIcon className="mr-2 h-4 w-4" />
|
||||
<span className="text-gray-500">Role:</span>
|
||||
<span className="ml-2 font-semibold text-gray-900">
|
||||
{currentRole}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<CustomInput
|
||||
control={form.control}
|
||||
name="name"
|
||||
type="text"
|
||||
label="Name"
|
||||
labelPlacement="outside"
|
||||
placeholder={userName}
|
||||
variant="bordered"
|
||||
isRequired={false}
|
||||
isInvalid={!!form.formState.errors.name}
|
||||
/>
|
||||
<div className="flex flex-row gap-4">
|
||||
<div className="w-1/2">
|
||||
<CustomInput
|
||||
control={form.control}
|
||||
name="name"
|
||||
type="text"
|
||||
label="Name"
|
||||
labelPlacement="outside"
|
||||
placeholder={userName}
|
||||
variant="bordered"
|
||||
isRequired={false}
|
||||
isInvalid={!!form.formState.errors.name}
|
||||
/>
|
||||
</div>
|
||||
<div className="w-1/2">
|
||||
<CustomInput
|
||||
control={form.control}
|
||||
name="company_name"
|
||||
type="text"
|
||||
label="Company Name"
|
||||
labelPlacement="outside"
|
||||
placeholder={userCompanyName}
|
||||
variant="bordered"
|
||||
isRequired={false}
|
||||
isInvalid={!!form.formState.errors.company_name}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<CustomInput
|
||||
control={form.control}
|
||||
@@ -113,18 +167,36 @@ export const EditForm = ({
|
||||
isInvalid={!!form.formState.errors.email}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<CustomInput
|
||||
<Controller
|
||||
name="role"
|
||||
control={form.control}
|
||||
name="company_name"
|
||||
type="text"
|
||||
label="Company Name"
|
||||
labelPlacement="outside"
|
||||
placeholder={userCompanyName}
|
||||
variant="bordered"
|
||||
isRequired={false}
|
||||
isInvalid={!!form.formState.errors.company_name}
|
||||
render={({ field }) => (
|
||||
<Select
|
||||
{...field}
|
||||
label="Role"
|
||||
labelPlacement="outside"
|
||||
placeholder="Select a role"
|
||||
variant="bordered"
|
||||
selectedKeys={[field.value || ""]}
|
||||
onSelectionChange={(selected) => {
|
||||
const selectedKey = Array.from(selected).pop();
|
||||
field.onChange(selectedKey || "");
|
||||
}}
|
||||
>
|
||||
{roles.map((role: { id: string; name: string }) => (
|
||||
<SelectItem key={role.id}>{role.name}</SelectItem>
|
||||
))}
|
||||
</Select>
|
||||
)}
|
||||
/>
|
||||
|
||||
{form.formState.errors.role && (
|
||||
<p className="mt-2 text-sm text-red-600">
|
||||
{form.formState.errors.role.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<input type="hidden" name="userId" value={userId} />
|
||||
|
||||
|
||||
@@ -33,6 +33,14 @@ export const ColumnsUser: ColumnDef<UserProps>[] = [
|
||||
return <p className="font-semibold">{email}</p>;
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "role",
|
||||
header: () => <div className="text-left">Role</div>,
|
||||
cell: ({ row }) => {
|
||||
const { role } = getUserData(row);
|
||||
return <p className="font-semibold">{role?.name || "No Role"}</p>;
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "company_name",
|
||||
header: ({ column }) => (
|
||||
@@ -47,7 +55,6 @@ export const ColumnsUser: ColumnDef<UserProps>[] = [
|
||||
return <p className="font-semibold">{company_name}</p>;
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
accessorKey: "date_joined",
|
||||
header: ({ column }) => (
|
||||
@@ -68,7 +75,8 @@ export const ColumnsUser: ColumnDef<UserProps>[] = [
|
||||
header: () => <div className="text-right">Actions</div>,
|
||||
id: "actions",
|
||||
cell: ({ row }) => {
|
||||
return <DataTableRowActions row={row} />;
|
||||
const roles = row.original.roles;
|
||||
return <DataTableRowActions row={row} roles={roles} />;
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
@@ -21,34 +21,39 @@ import { CustomAlertModal } from "@/components/ui/custom";
|
||||
|
||||
import { DeleteForm, EditForm } from "../forms";
|
||||
|
||||
interface DataTableRowActionsProps<ProviderProps> {
|
||||
row: Row<ProviderProps>;
|
||||
interface DataTableRowActionsProps<UserProps> {
|
||||
row: Row<UserProps>;
|
||||
roles?: { id: string; name: string }[];
|
||||
}
|
||||
const iconClasses =
|
||||
"text-2xl text-default-500 pointer-events-none flex-shrink-0";
|
||||
|
||||
export function DataTableRowActions<ProviderProps>({
|
||||
export function DataTableRowActions<UserProps>({
|
||||
row,
|
||||
}: DataTableRowActionsProps<ProviderProps>) {
|
||||
roles,
|
||||
}: DataTableRowActionsProps<UserProps>) {
|
||||
const [isEditOpen, setIsEditOpen] = useState(false);
|
||||
const [isDeleteOpen, setIsDeleteOpen] = useState(false);
|
||||
const userId = (row.original as { id: string }).id;
|
||||
const userName = (row.original as any).attributes?.name;
|
||||
const userEmail = (row.original as any).attributes?.email;
|
||||
const userCompanyName = (row.original as any).attributes?.company_name;
|
||||
const userRole = (row.original as any).attributes?.role?.name;
|
||||
|
||||
return (
|
||||
<>
|
||||
<CustomAlertModal
|
||||
isOpen={isEditOpen}
|
||||
onOpenChange={setIsEditOpen}
|
||||
title="Edit user"
|
||||
description={"Edit the user details"}
|
||||
title="Edit user details"
|
||||
>
|
||||
<EditForm
|
||||
userId={userId}
|
||||
userName={userName}
|
||||
userEmail={userEmail}
|
||||
userCompanyName={userCompanyName}
|
||||
currentRole={userRole}
|
||||
roles={roles || []}
|
||||
setIsOpen={setIsEditOpen}
|
||||
/>
|
||||
</CustomAlertModal>
|
||||
|
||||
@@ -43,6 +43,69 @@ export interface FindingsByStatusData {
|
||||
version: string;
|
||||
};
|
||||
}
|
||||
export interface ManageGroupPayload {
|
||||
data: {
|
||||
type: "provider-groups";
|
||||
id: string;
|
||||
attributes?: {
|
||||
name: string;
|
||||
};
|
||||
relationships?: {
|
||||
providers?: { data: Array<{ id: string; type: string }> };
|
||||
roles?: { data: Array<{ id: string; type: string }> };
|
||||
};
|
||||
};
|
||||
}
|
||||
export interface ProviderGroup {
|
||||
type: "provider-groups";
|
||||
id: string;
|
||||
attributes: {
|
||||
name: string;
|
||||
inserted_at: string;
|
||||
updated_at: string;
|
||||
};
|
||||
relationships: {
|
||||
providers: {
|
||||
meta: {
|
||||
count: number;
|
||||
};
|
||||
data: {
|
||||
type: string;
|
||||
id: string;
|
||||
}[];
|
||||
};
|
||||
roles: {
|
||||
meta: {
|
||||
count: number;
|
||||
};
|
||||
data: {
|
||||
type: string;
|
||||
id: string;
|
||||
}[];
|
||||
};
|
||||
};
|
||||
links: {
|
||||
self: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ProviderGroupsResponse {
|
||||
links: {
|
||||
first: string;
|
||||
last: string;
|
||||
next: string | null;
|
||||
prev: string | null;
|
||||
};
|
||||
data: ProviderGroup[];
|
||||
meta: {
|
||||
pagination: {
|
||||
page: number;
|
||||
pages: number;
|
||||
count: number;
|
||||
};
|
||||
version: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface FindingsSeverityOverview {
|
||||
data: {
|
||||
@@ -222,11 +285,100 @@ export interface InvitationProps {
|
||||
id: string;
|
||||
};
|
||||
};
|
||||
role?: {
|
||||
data: {
|
||||
type: "roles";
|
||||
id: string;
|
||||
};
|
||||
attributes?: {
|
||||
name: string;
|
||||
manage_users?: boolean;
|
||||
manage_account?: boolean;
|
||||
manage_billing?: boolean;
|
||||
manage_providers?: boolean;
|
||||
manage_integrations?: boolean;
|
||||
manage_scans?: boolean;
|
||||
permission_state?: "unlimited" | "limited" | "none";
|
||||
};
|
||||
};
|
||||
};
|
||||
links: {
|
||||
self: string;
|
||||
};
|
||||
roles?: {
|
||||
id: string;
|
||||
name: string;
|
||||
}[];
|
||||
}
|
||||
|
||||
export interface Role {
|
||||
type: "roles";
|
||||
id: string;
|
||||
attributes: {
|
||||
name: string;
|
||||
manage_users: boolean;
|
||||
manage_account: boolean;
|
||||
manage_billing: boolean;
|
||||
manage_providers: boolean;
|
||||
manage_integrations: boolean;
|
||||
manage_scans: boolean;
|
||||
unlimited_visibility: boolean;
|
||||
permission_state: "unlimited" | "limited" | "none";
|
||||
inserted_at: string;
|
||||
updated_at: string;
|
||||
};
|
||||
relationships: {
|
||||
provider_groups: {
|
||||
meta: {
|
||||
count: number;
|
||||
};
|
||||
data: {
|
||||
type: string;
|
||||
id: string;
|
||||
}[];
|
||||
};
|
||||
users: {
|
||||
meta: {
|
||||
count: number;
|
||||
};
|
||||
data: {
|
||||
type: string;
|
||||
id: string;
|
||||
}[];
|
||||
};
|
||||
invitations: {
|
||||
meta: {
|
||||
count: number;
|
||||
};
|
||||
data: {
|
||||
type: string;
|
||||
id: string;
|
||||
}[];
|
||||
};
|
||||
};
|
||||
links: {
|
||||
self: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface RolesProps {
|
||||
links: {
|
||||
first: string;
|
||||
last: string;
|
||||
next: string | null;
|
||||
prev: string | null;
|
||||
};
|
||||
data: Role[];
|
||||
meta: {
|
||||
pagination: {
|
||||
page: number;
|
||||
pages: number;
|
||||
count: number;
|
||||
};
|
||||
version: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface UserProfileProps {
|
||||
data: {
|
||||
type: "users";
|
||||
@@ -236,6 +388,9 @@ export interface UserProfileProps {
|
||||
email: string;
|
||||
company_name: string;
|
||||
date_joined: string;
|
||||
role: {
|
||||
name: string;
|
||||
};
|
||||
};
|
||||
relationships: {
|
||||
memberships: {
|
||||
@@ -262,6 +417,9 @@ export interface UserProps {
|
||||
email: string;
|
||||
company_name: string;
|
||||
date_joined: string;
|
||||
role: {
|
||||
name: string;
|
||||
};
|
||||
};
|
||||
relationships: {
|
||||
memberships: {
|
||||
@@ -273,7 +431,20 @@ export interface UserProps {
|
||||
id: string;
|
||||
}>;
|
||||
};
|
||||
roles: {
|
||||
meta: {
|
||||
count: number;
|
||||
};
|
||||
data: Array<{
|
||||
type: "roles";
|
||||
id: string;
|
||||
}>;
|
||||
};
|
||||
};
|
||||
roles: {
|
||||
id: string;
|
||||
name: string;
|
||||
}[];
|
||||
}
|
||||
|
||||
export interface ProviderProps {
|
||||
@@ -301,6 +472,24 @@ export interface ProviderProps {
|
||||
id: string;
|
||||
};
|
||||
};
|
||||
relationships: {
|
||||
secret: {
|
||||
data: {
|
||||
type: string;
|
||||
id: string;
|
||||
} | null;
|
||||
};
|
||||
provider_groups: {
|
||||
meta: {
|
||||
count: number;
|
||||
};
|
||||
data: Array<{
|
||||
type: string;
|
||||
id: string;
|
||||
}>;
|
||||
};
|
||||
};
|
||||
groupNames?: string[];
|
||||
}
|
||||
|
||||
export interface ScanProps {
|
||||
|
||||
@@ -1,5 +1,29 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const addRoleFormSchema = z.object({
|
||||
name: z.string().min(1, "Name is required"),
|
||||
manage_users: z.boolean().default(false),
|
||||
manage_account: z.boolean().default(false),
|
||||
manage_billing: z.boolean().default(false),
|
||||
manage_providers: z.boolean().default(false),
|
||||
manage_integrations: z.boolean().default(false),
|
||||
manage_scans: z.boolean().default(false),
|
||||
unlimited_visibility: z.boolean().default(false),
|
||||
groups: z.array(z.string()).optional(),
|
||||
});
|
||||
|
||||
export const editRoleFormSchema = z.object({
|
||||
name: z.string().min(1, "Name is required"),
|
||||
manage_users: z.boolean().default(false),
|
||||
manage_account: z.boolean().default(false),
|
||||
manage_billing: z.boolean().default(false),
|
||||
manage_providers: z.boolean().default(false),
|
||||
manage_integrations: z.boolean().default(false),
|
||||
manage_scans: z.boolean().default(false),
|
||||
unlimited_visibility: z.boolean().default(false),
|
||||
groups: z.array(z.string()).optional(),
|
||||
});
|
||||
|
||||
export const editScanFormSchema = (currentName: string) =>
|
||||
z.object({
|
||||
scanName: z
|
||||
@@ -158,6 +182,7 @@ export const editInviteFormSchema = z.object({
|
||||
invitationId: z.string().uuid(),
|
||||
invitationEmail: z.string().email(),
|
||||
expires_at: z.string().optional(),
|
||||
role: z.string().optional(),
|
||||
});
|
||||
|
||||
export const editUserFormSchema = () =>
|
||||
@@ -177,4 +202,5 @@ export const editUserFormSchema = () =>
|
||||
.optional(),
|
||||
company_name: z.string().optional(),
|
||||
userId: z.string(),
|
||||
role: z.string().optional(),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user