Skip to content

Commit

Permalink
tech(): refactor buildUiSchema Utils to not take EntityEditorOptions (#…
Browse files Browse the repository at this point in the history
…1312)

* refactor(): update getLocationOptions

* refactor(): getFeaturedImageUrl

* refactor(): getLocationExtent and getTagItems

* refactor(): getThumbnailUiSchemaElement

* refactor(): entityEditorOptions just hubEntity type

* refactor(): fix typing for location

* refactor(): feedback from pr
  • Loading branch information
jordantsanz authored Nov 6, 2023
1 parent 02934d7 commit f3cf4a5
Show file tree
Hide file tree
Showing 24 changed files with 673 additions and 123 deletions.
18 changes: 12 additions & 6 deletions packages/common/src/content/_internal/ContentUiSchemaEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getLocationExtent } from "../../core/schemas/internal/getLocationExtent
import { getLocationOptions } from "../../core/schemas/internal/getLocationOptions";
import { IUiSchema } from "../../core/schemas/types";
import { getThumbnailUiSchemaElement } from "../../core/schemas/internal/getThumbnailUiSchemaElement";
import { EntityEditorOptions } from "../../core/schemas/internal/EditorOptions";
import { IHubEditableContent } from "../../core/types";

/**
* @private
Expand All @@ -15,7 +15,7 @@ import { EntityEditorOptions } from "../../core/schemas/internal/EditorOptions";
*/
export const buildUiSchema = async (
i18nScope: string,
options: EntityEditorOptions,
options: Partial<IHubEditableContent>,
context: IArcGISContext
): Promise<IUiSchema> => {
return {
Expand Down Expand Up @@ -85,7 +85,11 @@ export const buildUiSchema = async (
},
},
},
getThumbnailUiSchemaElement(i18nScope, options),
getThumbnailUiSchemaElement(
i18nScope,
options.thumbnail,
options.thumbnailUrl
),
// tags
{
labelKey: `${i18nScope}.fields.tags.label`,
Expand All @@ -94,7 +98,7 @@ export const buildUiSchema = async (
options: {
control: "hub-field-input-combobox",
items: await getTagItems(
options,
options.tags,
context.portal.id,
context.hubRequestOptions
),
Expand Down Expand Up @@ -153,11 +157,13 @@ export const buildUiSchema = async (
options: {
control: "hub-field-input-location-picker",
extent: await getLocationExtent(
options,
options.location,
context.hubRequestOptions
),
options: await getLocationOptions(
options,
options.id,
options.type,
options.location,
context.portal.name,
context.hubRequestOptions
),
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/core/schemas/internal/EditorOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type EditorOptions = EntityEditorOptions | CardEditorOptions;
*
* However, it must always have "type" on the options, even if not the entire entity.
*/
export type EntityEditorOptions = HubEntity & Record<string, any>;
export type EntityEditorOptions = HubEntity;

/**
* Options to use when constructing a schema and uiSchema for
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { IArcGISContext } from "../../../ArcGISContext";
import { cacheBustUrl } from "../../../urls/cacheBustUrl";
import { EntityEditorOptions } from "./EditorOptions";

export function getFeaturedImageUrl(
options: EntityEditorOptions,
context: IArcGISContext
) {
export function getAuthedImageUrl(url: string, context: IArcGISContext) {
const queryParams = context.isAuthenticated
? `?token=${context.session.token}`
: "";
// TODO: Decide if the url should be passed in or plucked out of this deep path here
return (
options.view?.featuredImageUrl &&
cacheBustUrl(`${options.view.featuredImageUrl}${queryParams}`)
);
return url && cacheBustUrl(`${url}${queryParams}`);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { bBoxToExtent, orgExtent } from "../../../extent";
import { IHubRequestOptions } from "../../../types";
import { EntityEditorOptions } from "./EditorOptions";
import { IHubLocation } from "../../types";

/**
* Get the extent from the entity's location, if it has one.
* Otherwise, fall back to using the org extent.
*/
export async function getLocationExtent(
options: EntityEditorOptions,
location: IHubLocation,
hubRequestOptions: IHubRequestOptions
) {
return options.location?.extent?.length
? bBoxToExtent(options.location.extent)
return location?.extent?.length
? bBoxToExtent(location.extent)
: await orgExtent(hubRequestOptions);
}
12 changes: 6 additions & 6 deletions packages/common/src/core/schemas/internal/getLocationOptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { extentToBBox, orgExtent as orgExtent } from "../../../extent";
import { IHubRequestOptions } from "../../../types";
import { getTypeFromEntity } from "../../getTypeFromEntity";
import { EntityEditorOptions } from "./EditorOptions";
import { IHubLocation, IHubLocationOption } from "../../types/IHubLocation";
import { IExtent } from "@esri/arcgis-rest-types";

Expand All @@ -18,12 +17,13 @@ import { IExtent } from "@esri/arcgis-rest-types";
* location
*/
export async function getLocationOptions(
options: EntityEditorOptions,
id: string,
type: string,
location: IHubLocation,
portalName: string,
hubRequestOptions: IHubRequestOptions
): Promise<IHubLocationOption[]> {
const defaultExtent: IExtent = await orgExtent(hubRequestOptions);
const location: IHubLocation = options.location;

// Base options
const optionsArray = [
Expand All @@ -43,7 +43,7 @@ export async function getLocationOptions(
{
label: "{{shared.fields.location.custom:translate}}",
description: "{{shared.fields.location.customDescription:translate}}",
entityType: getTypeFromEntity(options),
entityType: getTypeFromEntity({ type }),
location: {
type: "custom",
spatialReference: defaultExtent.spatialReference,
Expand All @@ -60,9 +60,9 @@ export async function getLocationOptions(

return optionsArray.map((option) => {
// If this is a new entity, select the custom option by default
if (!options.id && option.location.type === "custom") {
if (!id && option.location.type === "custom") {
option.selected = true;
} else if (options.id && !location && option.location.type === "none") {
} else if (id && !location && option.location.type === "none") {
option.selected = true;
} else if (location?.type === option.location.type) {
option.location = location;
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/core/schemas/internal/getTagItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { EntityEditorOptions } from "./EditorOptions";
* be hoisted to hub.js
*/
export async function getTagItems(
options: EntityEditorOptions,
tags: string[],
orgId: string,
hubRequestOptions: IHubRequestOptions
): Promise<IUiSchemaComboboxItem[]> {
Expand Down Expand Up @@ -60,7 +60,7 @@ export async function getTagItems(
* and the tags fetched from the orgs, then remove duplicates, filter out
* any empty values and convert them to the IUiSchemaComboboxItem format
*/
const entityTags = options.tags || [];
const entityTags = tags || [];
return [...new Set([...tagsAgg.values.map((t) => t.value), ...entityTags])]
.filter((t) => t)
.map((t) => ({ value: t }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
IUiSchemaMessage,
UiSchemaMessageTypes,
} from "../types";
import { EntityEditorOptions } from "./EditorOptions";

/**
* Returns the UI schema element needed to render
Expand All @@ -15,14 +14,12 @@ import { EntityEditorOptions } from "./EditorOptions";
*/
export function getThumbnailUiSchemaElement(
i18nScope: string,
options: EntityEditorOptions
thumbnail: string,
thumbnailUrl: string
): IUiSchemaElement {
const messages: IUiSchemaMessage[] = [];
// Advise the user if the entity's thumbnail is either of the default values
if (
!options.thumbnail ||
options.thumbnail === "thumbnail/ago_downloaded.png"
) {
if (!thumbnail || thumbnail === "thumbnail/ago_downloaded.png") {
messages.push({
type: UiSchemaMessageTypes.custom,
display: "notice",
Expand All @@ -38,7 +35,7 @@ export function getThumbnailUiSchemaElement(
type: "Control",
options: {
control: "hub-field-input-image-picker",
imgSrc: options.thumbnailUrl,
imgSrc: thumbnailUrl,
maxWidth: 727,
maxHeight: 484,
aspectRatio: 1.5,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { IUiSchema } from "../../core/schemas/types";
import { IArcGISContext } from "../../ArcGISContext";
import { getFeaturedImageUrl } from "../../core/schemas/internal/getFeaturedImageUrl";
import { getAuthedImageUrl } from "../../core/schemas/internal/getAuthedImageUrl";
import { getTagItems } from "../../core/schemas/internal/getTagItems";
import { getCategoryItems } from "../../core/schemas/internal/getCategoryItems";
import { getLocationExtent } from "../../core/schemas/internal/getLocationExtent";
import { getLocationOptions } from "../../core/schemas/internal/getLocationOptions";
import { getThumbnailUiSchemaElement } from "../../core/schemas/internal/getThumbnailUiSchemaElement";
import { EntityEditorOptions } from "../../core/schemas/internal/EditorOptions";
import { IHubDiscussion } from "../../core/types";

/**
* @private
Expand All @@ -16,7 +16,7 @@ import { EntityEditorOptions } from "../../core/schemas/internal/EditorOptions";
*/
export const buildUiSchema = async (
i18nScope: string,
options: EntityEditorOptions,
options: Partial<IHubDiscussion>,
context: IArcGISContext
): Promise<IUiSchema> => {
return {
Expand Down Expand Up @@ -98,14 +98,21 @@ export const buildUiSchema = async (
},
},
},
getThumbnailUiSchemaElement(i18nScope, options),
getThumbnailUiSchemaElement(
i18nScope,
options.thumbnail,
options.thumbnailUrl
),
{
labelKey: `${i18nScope}.fields.featuredImage.label`,
scope: "/properties/view/properties/featuredImage",
type: "Control",
options: {
control: "hub-field-input-image-picker",
imgSrc: getFeaturedImageUrl(options, context),
imgSrc: getAuthedImageUrl(
options.view?.featuredImageUrl,
context
),
maxWidth: 727,
maxHeight: 484,
aspectRatio: 1.5,
Expand Down Expand Up @@ -136,7 +143,7 @@ export const buildUiSchema = async (
options: {
control: "hub-field-input-combobox",
items: await getTagItems(
options,
options.tags,
context.portal.id,
context.hubRequestOptions
),
Expand Down Expand Up @@ -172,11 +179,13 @@ export const buildUiSchema = async (
options: {
control: "hub-field-input-location-picker",
extent: await getLocationExtent(
options,
options.location,
context.hubRequestOptions
),
options: await getLocationOptions(
options,
options.id,
options.type,
options.location,
context.portal.name,
context.hubRequestOptions
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ export const buildUiSchema = async (
},
},
},
getThumbnailUiSchemaElement(i18nScope, options),
getThumbnailUiSchemaElement(
i18nScope,
options.thumbnail,
options.thumbnailUrl
),
],
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IUiSchema, UiSchemaRuleEffects } from "../../core/schemas/types";
import { getLocationExtent } from "../../core/schemas/internal/getLocationExtent";
import { getLocationOptions } from "../../core/schemas/internal/getLocationOptions";
import { getSharableGroupsComboBoxItems } from "../../core/schemas/internal/getSharableGroupsComboBoxItems";
import { EntityEditorOptions } from "../../core/schemas/internal/EditorOptions";
import { IHubInitiative } from "../../core/types";

/**
* @private
Expand All @@ -17,7 +17,7 @@ import { EntityEditorOptions } from "../../core/schemas/internal/EditorOptions";
*/
export const buildUiSchema = async (
i18nScope: string,
options: EntityEditorOptions,
options: Partial<IHubInitiative>,
context: IArcGISContext
): Promise<IUiSchema> => {
return {
Expand Down Expand Up @@ -107,11 +107,13 @@ export const buildUiSchema = async (
options: {
control: "hub-field-input-location-picker",
extent: await getLocationExtent(
options,
options.location,
context.hubRequestOptions
),
options: await getLocationOptions(
options,
options.id,
options.type,
options.location,
context.portal.name,
context.hubRequestOptions
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getLocationExtent } from "../../core/schemas/internal/getLocationExtent
import { getLocationOptions } from "../../core/schemas/internal/getLocationOptions";
import { getFeaturedContentCatalogs } from "../../core/schemas/internal/getFeaturedContentCatalogs";
import { getThumbnailUiSchemaElement } from "../../core/schemas/internal/getThumbnailUiSchemaElement";
import { EntityEditorOptions } from "../../core/schemas/internal/EditorOptions";
import { IHubInitiative } from "../../core";

/**
* @private
Expand All @@ -16,7 +16,7 @@ import { EntityEditorOptions } from "../../core/schemas/internal/EditorOptions";
*/
export const buildUiSchema = async (
i18nScope: string,
options: EntityEditorOptions,
options: Partial<IHubInitiative>,
context: IArcGISContext
): Promise<IUiSchema> => {
return {
Expand Down Expand Up @@ -80,15 +80,19 @@ export const buildUiSchema = async (
},
},
},
getThumbnailUiSchemaElement(i18nScope, options),
getThumbnailUiSchemaElement(
i18nScope,
options.thumbnail,
options.thumbnailUrl
),
{
labelKey: `${i18nScope}.fields.tags.label`,
scope: "/properties/tags",
type: "Control",
options: {
control: "hub-field-input-combobox",
items: await getTagItems(
options,
options.tags,
context.portal.id,
context.hubRequestOptions
),
Expand Down Expand Up @@ -133,11 +137,13 @@ export const buildUiSchema = async (
options: {
control: "hub-field-input-location-picker",
extent: await getLocationExtent(
options,
options.location,
context.hubRequestOptions
),
options: await getLocationOptions(
options,
options.id,
options.type,
options.location,
context.portal.name,
context.hubRequestOptions
),
Expand Down
Loading

0 comments on commit f3cf4a5

Please sign in to comment.