feat(SW-3288): Added sort order on campaign pages to sort campaigns on hotel pages
Approved-by: Matilda Landström
This commit is contained in:
@@ -8,7 +8,6 @@ query GetCampaignPagesByHotelUid(
|
|||||||
$today: DateTime!
|
$today: DateTime!
|
||||||
) {
|
) {
|
||||||
all_campaign_page(
|
all_campaign_page(
|
||||||
locale: $locale
|
|
||||||
where: {
|
where: {
|
||||||
OR: [
|
OR: [
|
||||||
{ included_hotels: { list_1: { hotel_page: { uid: $hotelPageUid } } } }
|
{ included_hotels: { list_1: { hotel_page: { uid: $hotelPageUid } } } }
|
||||||
@@ -19,10 +18,13 @@ query GetCampaignPagesByHotelUid(
|
|||||||
{ OR: [{ enddate: null }, { enddate_gte: $today }] }
|
{ OR: [{ enddate: null }, { enddate_gte: $today }] }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
locale: $locale
|
||||||
|
limit: 100
|
||||||
) {
|
) {
|
||||||
items {
|
items {
|
||||||
heading
|
heading
|
||||||
url
|
url
|
||||||
|
sort_order
|
||||||
card_content {
|
card_content {
|
||||||
heading
|
heading
|
||||||
image
|
image
|
||||||
@@ -42,7 +44,6 @@ query GetCampaignPagesByHotelUidRefs(
|
|||||||
$today: DateTime!
|
$today: DateTime!
|
||||||
) {
|
) {
|
||||||
all_campaign_page(
|
all_campaign_page(
|
||||||
locale: $locale
|
|
||||||
where: {
|
where: {
|
||||||
OR: [
|
OR: [
|
||||||
{ included_hotels: { list_1: { hotel_page: { uid: $hotelPageUid } } } }
|
{ included_hotels: { list_1: { hotel_page: { uid: $hotelPageUid } } } }
|
||||||
@@ -53,6 +54,8 @@ query GetCampaignPagesByHotelUidRefs(
|
|||||||
{ OR: [{ enddate: null }, { enddate_gte: $today }] }
|
{ OR: [{ enddate: null }, { enddate_gte: $today }] }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
locale: $locale
|
||||||
|
limit: 100
|
||||||
) {
|
) {
|
||||||
items {
|
items {
|
||||||
...CampaignPageRef
|
...CampaignPageRef
|
||||||
|
|||||||
@@ -185,6 +185,7 @@ export const campaignPagesByHotelUidSchema = z
|
|||||||
z.object({
|
z.object({
|
||||||
heading: z.string(),
|
heading: z.string(),
|
||||||
url: z.string(),
|
url: z.string(),
|
||||||
|
sort_order: z.number().nullish(),
|
||||||
card_content: z
|
card_content: z
|
||||||
.object({
|
.object({
|
||||||
image: tempImageVaultAssetSchema,
|
image: tempImageVaultAssetSchema,
|
||||||
@@ -198,7 +199,8 @@ export const campaignPagesByHotelUidSchema = z
|
|||||||
)
|
)
|
||||||
.transform((data) => {
|
.transform((data) => {
|
||||||
const mappedCampaigns = data.map((campaign) => {
|
const mappedCampaigns = data.map((campaign) => {
|
||||||
const { card_content, hero, system, heading, url } = campaign
|
const { card_content, hero, system, heading, sort_order, url } =
|
||||||
|
campaign
|
||||||
const hasImage = !!(card_content?.image || hero.image)
|
const hasImage = !!(card_content?.image || hero.image)
|
||||||
const cardContentImage = card_content?.image || hero.image
|
const cardContentImage = card_content?.image || hero.image
|
||||||
const heroImage = hero.image || card_content?.image
|
const heroImage = hero.image || card_content?.image
|
||||||
@@ -207,6 +209,7 @@ export const campaignPagesByHotelUidSchema = z
|
|||||||
return {
|
return {
|
||||||
id: system.uid,
|
id: system.uid,
|
||||||
url: removeMultipleSlashes(`/${system.locale}/${url}`),
|
url: removeMultipleSlashes(`/${system.locale}/${url}`),
|
||||||
|
sort_order,
|
||||||
card_content: {
|
card_content: {
|
||||||
...card_content,
|
...card_content,
|
||||||
heading: card_content?.heading || heading,
|
heading: card_content?.heading || heading,
|
||||||
|
|||||||
@@ -103,11 +103,37 @@ export function getSortedCampaigns(
|
|||||||
prioritizedCampaignUids: string[],
|
prioritizedCampaignUids: string[],
|
||||||
campaigns: Campaigns
|
campaigns: Campaigns
|
||||||
) {
|
) {
|
||||||
|
// First we'll sort on the list of prioritizedCampaignUids provided by the
|
||||||
|
// hotel page. If the campaign is not in the prioritized list, it will be
|
||||||
|
// added to the end of the sorted list using the default order from Contentstack.
|
||||||
const prioritizedSet = new Set(prioritizedCampaignUids)
|
const prioritizedSet = new Set(prioritizedCampaignUids)
|
||||||
const prioritized = prioritizedCampaignUids.flatMap((id) => {
|
const prioritized = prioritizedCampaignUids.flatMap((id) => {
|
||||||
const found = campaigns.find((c) => c.id === id)
|
const found = campaigns.find((c) => c.id === id)
|
||||||
return found ? [found] : []
|
return found ? [found] : []
|
||||||
})
|
})
|
||||||
const others = campaigns.filter((c) => !prioritizedSet.has(c.id))
|
const others = campaigns.filter((c) => !prioritizedSet.has(c.id))
|
||||||
return [...prioritized, ...others]
|
const sortedCampaigns = [...prioritized, ...others]
|
||||||
|
|
||||||
|
// Finally, we'll sort the campaigns by their sort_order, which gets higher priority
|
||||||
|
// than the order by prioritizedCampaignUids. If sort_order is not provided,
|
||||||
|
// we'll keep the original order. The higher the sort_order value, the higher the priority.
|
||||||
|
return sortedCampaigns.sort((a, b) => {
|
||||||
|
const aSortOrder = a.sort_order
|
||||||
|
const bSortOrder = b.sort_order
|
||||||
|
|
||||||
|
const aHasSortOrder = typeof aSortOrder === "number"
|
||||||
|
const bHasSortOrder = typeof bSortOrder === "number"
|
||||||
|
|
||||||
|
if (aHasSortOrder && bHasSortOrder) {
|
||||||
|
// Descending order: higher sort_order comes first
|
||||||
|
return bSortOrder - aSortOrder
|
||||||
|
}
|
||||||
|
if (aHasSortOrder && !bHasSortOrder) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
if (!aHasSortOrder && bHasSortOrder) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user