File size: 1,552 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import { SiteGoal, SiteIntent } from './constants';
interface Flags {
isIntentCreateCourseGoalEnabled?: boolean;
}
export const goalsToIntent = ( goals: SiteGoal[], flags?: Flags ): SiteIntent => {
const { isIntentCreateCourseGoalEnabled } = flags ?? {};
// When DIFM and Import goals are selected together, DIFM Intent will have the priority and will be set.
if ( goals.includes( SiteGoal.DIFM ) ) {
return SiteIntent.DIFM;
}
if ( goals.includes( SiteGoal.Import ) ) {
return SiteIntent.Import;
}
if ( goals.includes( SiteGoal.Courses ) && isIntentCreateCourseGoalEnabled ) {
return SiteIntent.CreateCourseGoal;
}
// Newsletter flow
if ( goals.includes( SiteGoal.Newsletter ) ) {
return SiteIntent.NewsletterGoal;
}
// Prioritize Sell over Build and Write
if (
goals.some( ( goal ) =>
[ SiteGoal.Sell, SiteGoal.SellDigital, SiteGoal.SellPhysical ].includes( goal )
)
) {
return SiteIntent.Sell;
}
// Prioritize Build over Write
if ( goals.includes( SiteGoal.Promote ) ) {
return SiteIntent.Build;
}
if ( goals.includes( SiteGoal.Write ) ) {
return SiteIntent.Write;
}
return SiteIntent.Build;
};
export const serializeGoals = ( goals: SiteGoal[] ): string => {
// Serialize goals by first + alphabetical order.
const firstGoal = goals.find( ( goal ) =>
[ SiteGoal.Write, SiteGoal.Sell, SiteGoal.Promote, SiteGoal.DIFM, SiteGoal.Import ].includes(
goal
)
);
return ( firstGoal ? [ firstGoal ] : [] )
.concat( goals.filter( ( goal ) => goal !== firstGoal ).sort() )
.join( ',' );
};
|