fix: copy logic and consistency changes

This commit is contained in:
Piyush Gupta
2024-11-13 13:06:03 +05:30
parent a1719c2258
commit c7981fc4d9
4 changed files with 29 additions and 63 deletions

View File

@@ -1,7 +1,6 @@
"use client";
import { createActionClassAction } from "@/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/actions";
import { isValidCssSelector } from "@/app/lib/actionClass/actionClass";
import { getFormattedErrorMessage } from "@/lib/utils/helper";
import { Code2Icon, MousePointerClickIcon, SparklesIcon } from "lucide-react";
import { useTranslations } from "next-intl";
@@ -22,17 +21,17 @@ interface ActivityTabProps {
environmentId: string;
environment: TEnvironment;
toCopyActionClasses: TActionClass[];
toCopyEnvironment: TEnvironment;
isViewer: boolean;
otherEnvironment: TEnvironment;
isReadOnly: boolean;
}
export const ActionActivityTab = ({
actionClass,
toCopyActionClasses,
toCopyEnvironment,
otherEnvironment,
environmentId,
environment,
isViewer,
isReadOnly,
}: ActivityTabProps) => {
const t = useTranslations();
const [activeSurveys, setActiveSurveys] = useState<string[] | undefined>();
@@ -77,7 +76,7 @@ export const ActionActivityTab = ({
const { type } = data;
let copyName = data.name + " (copy)";
try {
if (isViewer) {
if (isReadOnly) {
throw new Error(t("common.you_are_not_authorised_to_perform_this_action"));
}
@@ -91,45 +90,12 @@ export const ActionActivityTab = ({
throw new Error(t("environments.actions.action_with_key_already_exists", { key: data.key }));
}
if (
data.type === "noCode" &&
data.noCodeConfig?.type === "click" &&
data.noCodeConfig.elementSelector.cssSelector &&
!isValidCssSelector(data.noCodeConfig.elementSelector.cssSelector)
) {
throw new Error("Invalid CSS Selector");
}
let updatedAction = {
...data,
name: copyName.trim(),
environmentId: otherEnvironment.id,
};
let updatedAction = {};
if (type === "noCode") {
updatedAction = {
name: copyName.trim(),
description: data.description,
environmentId: toCopyEnvironment.id,
type: "noCode",
noCodeConfig: {
...data.noCodeConfig,
...(data.type === "noCode" &&
data.noCodeConfig?.type === "click" && {
elementSelector: {
cssSelector: data.noCodeConfig.elementSelector.cssSelector,
innerHtml: data.noCodeConfig.elementSelector.innerHtml,
},
}),
},
};
} else if (type === "code") {
updatedAction = {
name: copyName.trim(),
description: data.description,
environmentId: toCopyEnvironment.id,
type: "code",
key: data.key,
};
}
// const newActionClass: TActionClass =
const createActionClassResposne = await createActionClassAction({
action: updatedAction as TActionClassInput,
});

View File

@@ -11,7 +11,7 @@ interface ActionClassesTableProps {
environment: TEnvironment;
children: [JSX.Element, JSX.Element[]];
isReadOnly: boolean;
toCopyEnvironment: TEnvironment;
otherEnvironment: TEnvironment;
toCopyActionClasses: TActionClass[];
}
@@ -22,7 +22,7 @@ export const ActionClassesTable = ({
children: [TableHeading, actionRows],
isReadOnly,
toCopyActionClasses,
toCopyEnvironment,
otherEnvironment,
}: ActionClassesTableProps) => {
const [isActionDetailModalOpen, setActionDetailModalOpen] = useState(false);
@@ -62,7 +62,7 @@ export const ActionClassesTable = ({
actionClass={activeActionClass}
isReadOnly={isReadOnly}
toCopyActionClasses={toCopyActionClasses}
toCopyEnvironment={toCopyEnvironment}
otherEnvironment={otherEnvironment}
/>
)}
</>

View File

@@ -14,7 +14,7 @@ interface ActionDetailModalProps {
actionClass: TActionClass;
actionClasses: TActionClass[];
isReadOnly: boolean;
toCopyEnvironment: TEnvironment;
otherEnvironment: TEnvironment;
toCopyActionClasses: TActionClass[];
}
@@ -27,7 +27,7 @@ export const ActionDetailModal = ({
environment,
isReadOnly,
toCopyActionClasses,
toCopyEnvironment,
otherEnvironment,
}: ActionDetailModalProps) => {
const t = useTranslations();
const tabs = [
@@ -36,8 +36,8 @@ export const ActionDetailModal = ({
children: (
<ActionActivityTab
toCopyActionClasses={toCopyActionClasses}
toCopyEnvironment={toCopyEnvironment}
isViewer={isReadOnly}
otherEnvironment={otherEnvironment}
isReadOnly={isReadOnly}
environment={environment}
actionClass={actionClass}
environmentId={environmentId}

View File

@@ -10,7 +10,7 @@ import { getTranslations } from "next-intl/server";
import { redirect } from "next/navigation";
import { getActionClasses } from "@formbricks/lib/actionClass/service";
import { authOptions } from "@formbricks/lib/authOptions";
import { getEnvironment, getEnvironments } from "@formbricks/lib/environment/service";
import { getEnvironments } from "@formbricks/lib/environment/service";
import { getMembershipByUserIdOrganizationId } from "@formbricks/lib/membership/service";
import { getAccessFlags } from "@formbricks/lib/membership/utils";
import { getOrganizationByEnvironmentId } from "@formbricks/lib/organization/service";
@@ -41,20 +41,20 @@ const Page = async ({ params }) => {
throw new Error(t("common.organization_not_found"));
}
const environment = await getEnvironment(params.environmentId);
if (!environment?.productId) {
throw new Error(t("common.environment_not_found"));
}
const environments = await getEnvironments(environment.productId);
if (!product) {
throw new Error(t("common.product_not_found"));
}
const toCopyEnvironment = environments.filter((env) => env.id !== params.environmentId)[0];
const environments = await getEnvironments(product.id);
const currentEnvironment = environments.find((env) => env.id === params.environmentId);
const toCopyActionClasses = await getActionClasses(toCopyEnvironment.id);
if (!currentEnvironment) {
throw new Error(t("common.environment_not_found"));
}
const otherEnvironment = environments.filter((env) => env.id !== params.environmentId)[0];
const toCopyActionClasses = await getActionClasses(otherEnvironment.id);
const currentUserMembership = await getMembershipByUserIdOrganizationId(session?.user.id, organization.id);
const { isMember, isBilling } = getAccessFlags(currentUserMembership?.role);
@@ -81,8 +81,8 @@ const Page = async ({ params }) => {
<PageContentWrapper>
<PageHeader pageTitle={t("common.actions")} cta={!isReadOnly ? renderAddActionButton() : undefined} />
<ActionClassesTable
environment={environment}
toCopyEnvironment={toCopyEnvironment}
environment={currentEnvironment}
otherEnvironment={otherEnvironment}
toCopyActionClasses={toCopyActionClasses}
environmentId={params.environmentId}
actionClasses={actionClasses}