Skip to content

Commit

Permalink
New widget fixes (#312)
Browse files Browse the repository at this point in the history
* Added cssId

* Domain verification logic changed, Other fixes

---------

Co-authored-by: Rajat Saxena <[email protected]>
  • Loading branch information
rajat1saxena and Rajat Saxena authored Jan 31, 2024
1 parent 4504035 commit 4662d8c
Show file tree
Hide file tree
Showing 47 changed files with 860 additions and 359 deletions.
49 changes: 24 additions & 25 deletions apps/web/middlewares/verify-domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,25 @@ const getDomainBasedOnCustomDomain = async (
return await DomainModel.findOne({ customDomain, deleted: false });
};

const getDomain = async ({
hostName,
domainName,
}: {
hostName: string;
domainName: string;
}): Promise<Domain | null> => {
const domainBoundToLiveWebsite = new RegExp(`${process.env.DOMAIN}$`);

if (
process.env.NODE_ENV === "production" &&
!domainBoundToLiveWebsite.test(hostName)
) {
return await getDomainBasedOnCustomDomain(hostName);
} else {
return await getDomainBasedOnSubdomain(domainName);
const getDomain = async (hostName: string): Promise<Domain | null> => {
const isProduction = process.env.NODE_ENV === "production";
const isSubdomain = hostName.endsWith(`.${process.env.DOMAIN}`);
console.log(
"getDomain:1",
isProduction,
isSubdomain,
hostName,
process.env.DOMAIN,
);

if (isProduction && (hostName === process.env.DOMAIN || !isSubdomain)) {
console.log("getDomain:2", "getDomainBasedOnCustomDomain");
return getDomainBasedOnCustomDomain(hostName);
}

console.log("getDomain:2", "getDomainBasedOnSubdomain");
const [subdomain] = hostName?.split(".");
return getDomainBasedOnSubdomain(subdomain);
};

const hasValidSubscription = async (email: string): Promise<boolean> => {
Expand All @@ -62,30 +64,27 @@ export default async function verifyDomain(
let domain: Domain | null;

if (process.env.MULTITENANT === "true") {
const domainName = req.headers.host?.split(".")[0];
const { host } = req.headers;

if (!domainName) {
if (!host) {
throw new Error(responses.domain_missing);
}

domain = await getDomain({
hostName: req.headers.host || "",
domainName,
});
domain = await getDomain(host);

if (!domain) {
return res.status(404).json({
message: `${responses.domain_doesnt_exist}: ${domainName}`,
message: `${responses.domain_doesnt_exist}: ${host?.split(
".",
)[0]}`,
});
//throw new Error(`${responses.domain_doesnt_exist}: ${domainName}`);
}

const validSubscription = await hasValidSubscription(domain.email);
if (!validSubscription) {
return res
.status(404)
.json({ message: responses.not_valid_subscription });
//throw new Error(responses.not_valid_subscription);
}
} else {
domain = await DomainModel.findOne({
Expand Down
138 changes: 82 additions & 56 deletions packages/common-widgets/src/content/admin-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import {
TextEditor,
Form,
FormField,
ContentPaddingSelector,
CssIdField,
} from "@courselit/components-library";
import {
verticalPadding as defaultVerticalPadding,
horizontalPadding as defaultHorizontalPadding,
} from "./defaults";

interface AdminWidgetProps {
settings: Settings;
Expand All @@ -33,6 +39,13 @@ export default function AdminWidget({ settings, onChange }: AdminWidgetProps) {
const [badgeForegroundColor, setBadgeForegroundColor] = useState(
settings.badgeForegroundColor,
);
const [horizontalPadding, setHorizontalPadding] = useState<number>(
settings.horizontalPadding || defaultHorizontalPadding,
);
const [verticalPadding, setVerticalPadding] = useState<number>(
settings.verticalPadding || defaultVerticalPadding,
);
const [cssId, setCssId] = useState(settings.cssId);

useEffect(() => {
onChange({
Expand All @@ -43,6 +56,9 @@ export default function AdminWidget({ settings, onChange }: AdminWidgetProps) {
foregroundColor,
badgeBackgroundColor,
badgeForegroundColor,
horizontalPadding,
verticalPadding,
cssId,
});
}, [
title,
Expand All @@ -52,68 +68,78 @@ export default function AdminWidget({ settings, onChange }: AdminWidgetProps) {
foregroundColor,
badgeBackgroundColor,
badgeForegroundColor,
horizontalPadding,
verticalPadding,
cssId,
]);

return (
<div className="flex flex-col">
<div className="mb-4">
<div className="flex flex-col gap-4 mb-4">
<AdminWidgetPanel title="Header">
<Form>
<AdminWidgetPanel title="Header">
<FormField
label="Title"
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<div>
<p className="mb-1 font-medium">Description</p>
<TextEditor
initialContent={description}
onChange={(state: any) => setDescription(state)}
showToolbar={false}
/>
</div>
<Select
title="Header alignment"
value={headerAlignment}
options={[
{ label: "Left", value: "left" },
{ label: "Center", value: "center" },
]}
onChange={(value: Alignment) =>
setHeaderAlignment(value)
}
/>
</AdminWidgetPanel>
</Form>
</div>
<div className="mb-4">
<AdminWidgetPanel title="Design">
<ColorSelector
title="Background color"
value={backgroundColor || "inherit"}
onChange={(value?: string) => setBackgroundColor(value)}
<FormField
label="Title"
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<ColorSelector
title="Text color"
value={foregroundColor || "inherit"}
onChange={(value?: string) => setForegroundColor(value)}
/>
<ColorSelector
title="Badge color"
value={badgeBackgroundColor || "inherit"}
onChange={(value?: string) =>
setBadgeBackgroundColor(value)
}
/>
<ColorSelector
title="Badge text color"
value={badgeForegroundColor || "inherit"}
onChange={(value?: string) =>
setBadgeForegroundColor(value)
}
</Form>
<div>
<p className="mb-1 font-medium">Description</p>
<TextEditor
initialContent={description}
onChange={(state: any) => setDescription(state)}
showToolbar={false}
/>
</AdminWidgetPanel>
</div>
</div>
<Select
title="Header alignment"
value={headerAlignment}
options={[
{ label: "Left", value: "left" },
{ label: "Center", value: "center" },
]}
onChange={(value: Alignment) => setHeaderAlignment(value)}
/>
</AdminWidgetPanel>
<AdminWidgetPanel title="Design">
<ColorSelector
title="Background color"
value={backgroundColor || "inherit"}
onChange={(value?: string) => setBackgroundColor(value)}
/>
<ColorSelector
title="Text color"
value={foregroundColor || "inherit"}
onChange={(value?: string) => setForegroundColor(value)}
/>
<ColorSelector
title="Badge color"
value={badgeBackgroundColor || "inherit"}
onChange={(value?: string) =>
setBadgeBackgroundColor(value)
}
/>
<ColorSelector
title="Badge text color"
value={badgeForegroundColor || "inherit"}
onChange={(value?: string) =>
setBadgeForegroundColor(value)
}
/>
<ContentPaddingSelector
value={horizontalPadding}
min={50}
onChange={setHorizontalPadding}
/>
<ContentPaddingSelector
variant="vertical"
value={verticalPadding}
onChange={setVerticalPadding}
/>
</AdminWidgetPanel>
<AdminWidgetPanel title="Advanced">
<CssIdField value={cssId} onChange={setCssId} />
</AdminWidgetPanel>
</div>
);
}
2 changes: 2 additions & 0 deletions packages/common-widgets/src/content/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const horizontalPadding = 100;
export const verticalPadding = 16;
3 changes: 3 additions & 0 deletions packages/common-widgets/src/content/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ export default interface Settings extends WidgetDefaultSettings {
foregroundColor?: string;
badgeBackgroundColor?: string;
badgeForegroundColor?: string;
horizontalPadding: number;
verticalPadding: number;
cssId?: string;
}
Loading

0 comments on commit 4662d8c

Please sign in to comment.