Skip to content

Commit fb93cd0

Browse files
authored
Merge pull request #1497 from AppQuality/UN-2207-filter-out-global-templates
UN-2207-filter-out-global-templates
2 parents 40be6ef + f664279 commit fb93cd0

File tree

7 files changed

+65
-30
lines changed

7 files changed

+65
-30
lines changed

src/pages/Dashboard/LaunchCampaignCards.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,28 @@ const LaunchCampaignCards = () => {
4646
[promoTemplates, workspaceNoStrapiTemplates, workspaceStrapiTemplates]
4747
);
4848

49+
// Remove duplicated template ids from allTemplates (remove from promoTemplates if already in workspace templates)
50+
const uniqueTemplateIds = new Set<number>();
51+
const uniqueAllTemplates = allTemplates.filter((template) => {
52+
if (uniqueTemplateIds.has(template.id)) {
53+
return false;
54+
}
55+
uniqueTemplateIds.add(template.id);
56+
return true;
57+
});
58+
4959
const handleCloseDrawer = () => {
5060
setIsDrawerOpen(false);
5161
};
5262

5363
const handleClick = useCallback(
5464
(tid: number) => {
55-
setSelectedTemplate(allTemplates.find((template) => template.id === tid));
65+
setSelectedTemplate(
66+
uniqueAllTemplates.find((template) => template.id === tid)
67+
);
5668
setIsDrawerOpen(true);
5769
},
58-
[allTemplates, setSelectedTemplate, setIsDrawerOpen]
70+
[uniqueAllTemplates, setSelectedTemplate, setIsDrawerOpen]
5971
);
6072

6173
if (!data) return null;
@@ -85,7 +97,7 @@ const LaunchCampaignCards = () => {
8597
</Paragraph>
8698
</Col>
8799
</Row>
88-
<ServiceTiles onClick={handleClick} promoTemplates={allTemplates} />
100+
<ServiceTiles onClick={handleClick} promoTemplates={uniqueAllTemplates} />
89101
{selectedTemplate && (
90102
<PlanCreationInterface
91103
isOpen={isDrawerOpen}

src/pages/Dashboard/PromoContext.tsx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ export const PromoContextProvider = ({ children }: { children: ReactNode }) => {
4848
const { data: workspaceTemplatesData } = useGetWorkspacesByWidTemplatesQuery(
4949
{
5050
wid: activeWorkspace?.id.toString() || '',
51-
filterBy: {
52-
isPromo: 0,
53-
},
5451
orderBy: 'order',
5552
order: 'asc',
5653
},
@@ -74,26 +71,30 @@ export const PromoContextProvider = ({ children }: { children: ReactNode }) => {
7471

7572
const workspaceStrapiTemplates = useMemo(() => {
7673
if (!workspaceTemplatesData) return [];
77-
return workspaceTemplatesData.items.reduce<
78-
PromoContextProps['promoTemplates']
79-
>((acc, template) => {
80-
if ('strapi' in template) {
81-
acc.push(template);
82-
}
83-
return acc;
84-
}, []);
74+
return workspaceTemplatesData.items
75+
.filter(
76+
(template) => template.workspace_id // Filter out global templates (those without a workspace_id)
77+
)
78+
.reduce<PromoContextProps['promoTemplates']>((acc, template) => {
79+
if ('strapi' in template) {
80+
acc.push(template);
81+
}
82+
return acc;
83+
}, []);
8584
}, [workspaceTemplatesData]);
8685

8786
const workspaceNoStrapiTemplates = useMemo(() => {
8887
if (!workspaceTemplatesData) return [];
89-
return workspaceTemplatesData.items.reduce<
90-
PromoContextProps['promoTemplates']
91-
>((acc, template) => {
92-
if (!('strapi' in template)) {
93-
acc.push(template);
94-
}
95-
return acc;
96-
}, []);
88+
return workspaceTemplatesData.items
89+
.filter(
90+
(template) => template.workspace_id // Filter out global templates (those without a workspace_id)
91+
)
92+
.reduce<PromoContextProps['promoTemplates']>((acc, template) => {
93+
if (!('strapi' in template)) {
94+
acc.push(template);
95+
}
96+
return acc;
97+
}, []);
9798
}, [workspaceTemplatesData]);
9899

99100
const PromoContextValue = useMemo(

src/pages/Dashboard/empty-state/projectEmptyState.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,28 @@ export const ProjectEmptyState = () => {
6060
[promoTemplates, workspaceNoStrapiTemplates, workspaceStrapiTemplates]
6161
);
6262

63+
// Remove duplicated template ids from allTemplates (remove from promoTemplates if already in workspace templates)
64+
const uniqueTemplateIds = new Set<number>();
65+
const uniqueAllTemplates = allTemplates.filter((template) => {
66+
if (uniqueTemplateIds.has(template.id)) {
67+
return false;
68+
}
69+
uniqueTemplateIds.add(template.id);
70+
return true;
71+
});
72+
6373
const handleCloseDrawer = useCallback(() => {
6474
setIsDrawerOpen(false);
6575
}, [setIsDrawerOpen]);
6676

6777
const handleClick = useCallback(
6878
(tid: number) => {
69-
setSelectedTemplate(allTemplates.find((template) => template.id === tid));
79+
setSelectedTemplate(
80+
uniqueAllTemplates.find((template) => template.id === tid)
81+
);
7082
setIsDrawerOpen(true);
7183
},
72-
[allTemplates, setSelectedTemplate, setIsDrawerOpen]
84+
[uniqueAllTemplates, setSelectedTemplate, setIsDrawerOpen]
7385
);
7486

7587
return (
@@ -107,7 +119,7 @@ export const ProjectEmptyState = () => {
107119
<div style={{ zIndex: 1, position: 'relative' }}>
108120
<ServiceTiles
109121
onClick={handleClick}
110-
promoTemplates={allTemplates}
122+
promoTemplates={uniqueAllTemplates}
111123
/>
112124
</div>
113125
<UGLogoMedium

src/pages/Templates/Context.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,15 @@ export const TemplatesContextProvider = ({
115115

116116
// Set promo templates when promoData changes
117117
useEffect(() => {
118-
setPromoTemplates(promoData?.items || []);
119-
}, [promoData]);
118+
const uniqueTailoredIds = new Set(
119+
data?.items.filter(isTailoredTemplate).map((template) => template.id)
120+
);
121+
setPromoTemplates(
122+
promoData?.items.filter(
123+
(template) => !uniqueTailoredIds.has(template.id) // Remove duplicated template ids from promo templates
124+
) || []
125+
);
126+
}, [promoData, data]);
120127

121128
// Build array of categories with templates for the frontend
122129
const templatesByCategory = useMemo(() => {

tests/e2e/dashboard_home.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ test.describe('Home page', () => {
3737
test('should show a list of suggested templates in promo', async () => {
3838
await expect(dashboard.elements().title()).toBeVisible();
3939
await expect(promoList.elements().promoList()).toBeVisible();
40-
await expect(promoList.elements().promoListItems()).toHaveCount(10);
40+
await expect(promoList.elements().promoListItems()).toHaveCount(5);
4141
});
4242

4343
test('should open the create plan interface when clicking on a promo item, a more info should go to the single template', async ({

tests/e2e/project.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test.describe('project page', () => {
3535
project.projectCampaigns.length
3636
);
3737
await expect(promoList.elements().promoList()).toBeVisible();
38-
await expect(promoList.elements().promoListItems()).toHaveCount(10);
38+
await expect(promoList.elements().promoListItems()).toHaveCount(5);
3939
});
4040

4141
test('the invite users button should be visible', async () => {
@@ -147,7 +147,7 @@ test.describe('project page empty state', () => {
147147
test('should display no campaigns, a list of suggested templates in promo', async () => {
148148
await expect(project.elements().projectsTable()).not.toBeVisible();
149149
await expect(promoList.elements().promoList()).toBeVisible();
150-
await expect(promoList.elements().promoListItems()).toHaveCount(10);
150+
await expect(promoList.elements().promoListItems()).toHaveCount(5);
151151
});
152152

153153
test('should open the create plan interface when clicking on a promo item, a more info should go to the single template', async ({

tests/fixtures/components/PromoList.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export class PromoList {
2323
}
2424

2525
async mockPromoTemplates() {
26+
// Mocks 2 requests:
27+
// - /workspaces/:wid/templates?orderBy=order&order=asc&filterBy[isPromo]=1
28+
// - /workspaces/:wid/templates?orderBy=order&order=asc
2629
await this.page.route('*/**/api/workspaces/1/templates*', async (route) => {
2730
await route.fulfill({
2831
path: 'tests/api/workspaces/wid/templates/_get/200_promo.json',

0 commit comments

Comments
 (0)