Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update hub search result to view model signature #1124

Merged
merged 10 commits into from
Jul 19, 2023
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 6 additions & 13 deletions packages/common/src/core/behaviors/IWithCardBehavior.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { ICardActionLink, IHubCardViewModel } from "../types/IHubCardViewModel";
import {
IConvertToCardModelOpts,
IHubCardViewModel,
} from "../types/IHubCardViewModel";

export interface IWithCardBehavior {
/**
* Convert an entity into a card view model that can
* be consumed by the suite of hub gallery components
*
* @param target card link contextual target
* @param actionLinks card action links
* @param locale internationalization locale
* @param opts view model options
*/
convertToCardViewModel(
target: "ago" | "view" | "workspace",
actionLinks: ICardActionLink[],
/**
* TODO: move transform logic to FE so we don't need to pass
* locale down (follow https://devtopia.esri.com/dc/hub/issues/7255)
*/
locale: string
): IHubCardViewModel;
convertToCardModel(opts?: IConvertToCardModelOpts): IHubCardViewModel;
}
40 changes: 40 additions & 0 deletions packages/common/src/core/types/IHubCardViewModel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IArcGISContext } from "../../ArcGISContext";
import { IHubSearchResult } from "../../search/types/IHubSearchResult";
import { AccessLevel } from "./types";

// structure the arcgis-hub-card expects to receive
Expand Down Expand Up @@ -38,6 +40,44 @@ export interface ICardActionLink {
href?: string;
i18nKey?: string;
label?: string;
showLabel?: boolean;
icon?: string;
buttonStyle?: "outline" | "outline-fill" | "solid" | "transparent";
}

export type CardModelTarget =
// link to the entity's canonical "self" - for most entities,
// this will be AGO; however, there are exceptions - for sites,
// "self" will be the site url itself
| "self"
// link to the entity's "view" url relative to the site
| "siteRelative"
// link to the entity's workspace url relative to the site
| "workspaceRelative"
// no link
| "none"
// only emit an event when the link is clicked - allows
// consumers to apply a custom redirect
| "event";

export type EntityToCardModelFn<T> = (
entity: T,
context: IArcGISContext,
opts?: IConvertToCardModelOpts
) => IHubCardViewModel;

export type ResultToCardModelFn = (
result: IHubSearchResult,
opts?: IConvertToCardModelOpts
) => IHubCardViewModel;

export interface IConvertToCardModelOpts {
actionLinks?: ICardActionLink[];
baseUrl?: string;
/**
* TODO: move transform logic to FE so we don't need to pass
* locale down (follow https://devtopia.esri.com/dc/hub/issues/7255)
*/
locale?: string;
target?: CardModelTarget;
}
1 change: 1 addition & 0 deletions packages/common/src/core/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * from "./DynamicValues";
export * from "./IReference";
export * from "./Metrics";
export * from "./MaybeTranslate";
export * from "./IHubCardViewModel";
3 changes: 3 additions & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export * from "./metrics";
// Unclear _why_ this needs to be here vs in core/index.ts
// but if it's not here, the function is not exported
export * from "./core/updateHubEntity";
// Unclear _why_ this needs to be here vs. in urls/index.ts
// but if it's exported there, random tests start failing
export * from "./urls/getCardModelUrl";

import OperationStack from "./OperationStack";
import OperationError from "./OperationError";
Expand Down
32 changes: 9 additions & 23 deletions packages/common/src/initiatives/HubInitiative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
UiSchemaElementOptions,
IEditorConfig,
IResolvedMetric,
IWithCardBehavior,
} from "../core";
import { getEntityEditorSchemas } from "../core/schemas/getEntityEditorSchemas";
import {
Expand All @@ -25,10 +26,10 @@ import { IWithMetricsBehavior } from "../core/behaviors/IWithMetricsBehavior";
import { getEntityMetrics } from "../metrics/getEntityMetrics";
import { resolveMetric } from "../metrics/resolveMetric";
import {
ICardActionLink,
IConvertToCardModelOpts,
IHubCardViewModel,
} from "../core/types/IHubCardViewModel";
import { convertInitiativeEntityToCardViewModel } from "./view";
import { initiativeToCardModel } from "./view";

/**
* Hub Initiative Class
Expand All @@ -39,7 +40,8 @@ export class HubInitiative
IWithStoreBehavior<IHubInitiative>,
IWithCatalogBehavior,
IWithMetricsBehavior,
IWithSharingBehavior
IWithSharingBehavior,
IWithCardBehavior
{
private _catalog: Catalog;

Expand Down Expand Up @@ -239,27 +241,11 @@ export class HubInitiative

/**
* Convert the initiative entity into a card view model that
* can be consumed by the suit of hub gallery components
* can be consumed by the suite of hub gallery components
*
* @param target card link contextual target
* @param actionLinks card action links
* @param locale internationalization locale
* @param opts view model options
*/
convertToCardViewModel(
target: "ago" | "view" | "workspace",
actionLinks: ICardActionLink[],
/**
* TODO: move transform logic to FE so we don't need to pass
* locale down (follow https://devtopia.esri.com/dc/hub/issues/7255)
*/
locale: string
): IHubCardViewModel {
return convertInitiativeEntityToCardViewModel(
this.entity,
this.context,
target,
actionLinks,
locale
);
convertToCardModel(opts?: IConvertToCardModelOpts): IHubCardViewModel {
return initiativeToCardModel(this.entity, this.context, opts);
}
}
81 changes: 39 additions & 42 deletions packages/common/src/initiatives/view.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import { IArcGISContext, IHubSearchResult, getFamily } from "..";
import {
getHubRelativeUrl,
getShortenedCategories,
} from "../content/_internal/internalContentUtils";
import { getShortenedCategories } from "../content/_internal/internalContentUtils";
import { IHubInitiative } from "../core";
import {
ICardActionLink,
getCardModelUrlFromEntity,
getCardModelUrlFromResult,
} from "../urls/getCardModelUrl";
import {
EntityToCardModelFn,
ResultToCardModelFn,
IConvertToCardModelOpts,
IHubCardViewModel,
} from "../core/types/IHubCardViewModel";
import { getItemHomeUrl } from "../urls/get-item-home-url";
import { getRelativeWorkspaceUrl } from "../core/getRelativeWorkspaceUrl";

/**
* Convert an initiative entity in a card view model
* that can be consumed by the suite of hub gallery components
*
* @param initiative initiative entity
* @param context auth & portal information
* @param target card link contextual target
* @param actionLinks card action links
* @param locale internationalization locale
* @param opts view model options
*/
export const convertInitiativeEntityToCardViewModel = (
export const initiativeToCardModel: EntityToCardModelFn<IHubInitiative> = (
initiative: IHubInitiative,
context: IArcGISContext,
target: "ago" | "view" | "workspace" = "ago",
actionLinks: ICardActionLink[] = [],
/**
* TODO: move transform logic to FE so we don't need to pass
* locale down (follow https://devtopia.esri.com/dc/hub/issues/7255)
*/
locale: string = "en-US"
opts?: IConvertToCardModelOpts
): IHubCardViewModel => {
const titleUrl = {
ago: getItemHomeUrl(initiative.id, context.hubRequestOptions),
view: getHubRelativeUrl(initiative.type, initiative.id),
workspace: getRelativeWorkspaceUrl(initiative.type, initiative.id),
}[target];
const {
actionLinks = [],
baseUrl = "",
locale = "en-US",
target = "self",
} = opts || {};

const titleUrl = getCardModelUrlFromEntity(
initiative,
context,
target,
baseUrl
);

return {
...getSharedInitiativeCardViewModel(initiative, locale),
...getSharedInitiativeCardModel(initiative, locale),
actionLinks,
titleUrl,
...(initiative.thumbnailUrl && { thumbnailUrl: initiative.thumbnailUrl }),
Expand All @@ -51,29 +52,25 @@ export const convertInitiativeEntityToCardViewModel = (
* that can be consumed by the suite of hub gallery components
*
* @param searchResult hub initiative search result
* @param target card link contextual target
* @param actionLinks card action links
* @param locale internationalization locale
* @param opts view model options
*/
export const convertInitiativeSearchResultToCardViewModel = (
export const initiativeResultToCardModel: ResultToCardModelFn = (
searchResult: IHubSearchResult,
target: "ago" | "view" | "workspace" = "ago",
actionLinks: ICardActionLink[] = [],
/**
* TODO: move transform logic to FE so we don't need to pass
* locale down (follow https://devtopia.esri.com/dc/hub/issues/7255)
*/
locale: string = "en-US"
opts?: IConvertToCardModelOpts
): IHubCardViewModel => {
const titleUrl = {
ago: searchResult.links.self,
view: searchResult.links.siteRelative,
workspace: searchResult.links.workspaceRelative,
}[target];
const {
actionLinks = [],
baseUrl = "",
locale = "en-US",
target = "self",
} = opts || {};

const titleUrl = getCardModelUrlFromResult(searchResult, target, baseUrl);

return {
...getSharedInitiativeCardViewModel(searchResult, locale),
...getSharedInitiativeCardModel(searchResult, locale),
actionLinks,
...(searchResult.index && { index: searchResult.index }),
titleUrl,
...(searchResult.links.thumbnail && {
thumbnailUrl: searchResult.links.thumbnail,
Expand All @@ -88,7 +85,7 @@ export const convertInitiativeSearchResultToCardViewModel = (
* @param entityOrSearchResult intiative entity or hub search result
* @param locale internationalization locale
*/
const getSharedInitiativeCardViewModel = (
const getSharedInitiativeCardModel = (
entityOrSearchResult: IHubInitiative | IHubSearchResult,
locale: string
): IHubCardViewModel => {
Expand Down
27 changes: 5 additions & 22 deletions packages/common/src/projects/HubProject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DEFAULT_PROJECT } from "./defaults";

import {
IHubProject,
IWithCatalogBehavior,
Expand All @@ -22,10 +21,10 @@ import { IWithMetricsBehavior } from "../core/behaviors/IWithMetricsBehavior";
import { getEntityMetrics } from "../metrics/getEntityMetrics";
import { resolveMetric } from "../metrics/resolveMetric";
import {
ICardActionLink,
IConvertToCardModelOpts,
IHubCardViewModel,
} from "../core/types/IHubCardViewModel";
import { convertProjectEntityToCardViewModel } from "./view";
import { projectToCardModel } from "./view";

/**
* Hub Project Class
Expand Down Expand Up @@ -157,26 +156,10 @@ export class HubProject
* Convert the project entity into a card view model that can
* be consumed by the suite of hub gallery components
*
* @param target card link contextual target
* @param actionLinks card action links
* @param locale internationalization locale
* @param opts view model options
*/
convertToCardViewModel(
target: "ago" | "view" | "workspace",
actionLinks: ICardActionLink[],
/**
* TODO: move transform logic to FE so we don't need to pass
* locale down (follow https://devtopia.esri.com/dc/hub/issues/7255)
*/
locale: string
): IHubCardViewModel {
return convertProjectEntityToCardViewModel(
this.entity,
this.context,
target,
actionLinks,
locale
);
convertToCardModel(opts?: IConvertToCardModelOpts): IHubCardViewModel {
return projectToCardModel(this.entity, this.context, opts);
}

/**
Expand Down
Loading