Skip to content

Commit

Permalink
Add notify attribute dragStart handler. Standardize fieldRequiredResu…
Browse files Browse the repository at this point in the history
…lts.
  • Loading branch information
tealefristoe committed Sep 26, 2024
1 parent e022546 commit e7c2206
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { V2CaseTable } from "../../data-interactive/data-interactive-component-types"
import { CreateOrShowTileFn, DIComponentHandler } from "../../data-interactive/handlers/component-handler"
import { errorResult } from "../../data-interactive/handlers/di-results"
import { errorResult, fieldRequiredResult } from "../../data-interactive/handlers/di-results"
import { appState } from "../../models/app-state"
import {
getDataSetByNameOrId, getSharedCaseMetadataFromDataset, getSharedDataSetFromDataSetId
Expand All @@ -16,7 +16,7 @@ export const caseTableCardComponentHandler: DIComponentHandler = {
create({ type, values }) {
const { document } = appState
const { dataContext, horizontalScrollOffset } = values as V2CaseTable
const dataContextNotFound = errorResult(t("V3.DI.Error.fieldRequired", { vars: ["Create", type, "dataContext"] }))
const dataContextNotFound = fieldRequiredResult("Create", type, "dataContext")
if (!dataContext) return dataContextNotFound
const dataSet = getDataSetByNameOrId(document, dataContext)
if (!dataSet) return dataContextNotFound
Expand Down
7 changes: 5 additions & 2 deletions v3/src/data-interactive/data-interactive-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export interface DIAllCases {
}
}
export type DIAttribute = Partial<ICodapV2Attribute>
export interface DINotifyAttribute {
request?: string
}
export interface DIAttributeLocationValues {
collection?: string | number
position?: number
Expand Down Expand Up @@ -178,8 +181,8 @@ export interface DIResources {
}

// types for values accepted as inputs by the API
export type DISingleValues = DIAttribute | DIAttributeLocationValues | DICase | DIDataContext | DINotifyDataContext |
DIGlobal | DIInteractiveFrame | DIItemValues | DICreateCollection | DINewCase | DIUpdateCase |
export type DISingleValues = DIAttribute | DINotifyAttribute | DIAttributeLocationValues | DICase | DIDataContext |
DINotifyDataContext | DIGlobal | DIInteractiveFrame | DIItemValues | DICreateCollection | DINewCase | DIUpdateCase |
DINotification | DIItemSearchNotify | DILogMessage | V2SpecificComponent
export type DIValues = DISingleValues | DISingleValues[] | number | string[]

Expand Down
32 changes: 24 additions & 8 deletions v3/src/data-interactive/handlers/attribute-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { getSharedCaseMetadataFromDataset } from "../../models/shared/shared-dat
import { t } from "../../utilities/translation/translate"
import { registerDIHandler } from "../data-interactive-handler"
import { convertAttributeToV2, convertAttributeToV2FromResources } from "../data-interactive-type-utils"
import { DIAttribute, DIHandler, DIResources, DIValues } from "../data-interactive-types"
import { DIAttribute, DIHandler, DINotifyAttribute, DIResources, DIValues } from "../data-interactive-types"
import { createAttribute, updateAttribute } from "./di-handler-utils"
import { attributeNotFoundResult, collectionNotFoundResult, dataContextNotFoundResult } from "./di-results"
import {
attributeNotFoundResult, collectionNotFoundResult, dataContextNotFoundResult, errorResult, fieldRequiredResult
} from "./di-results"

export const diAttributeHandler: DIHandler = {
create(resources: DIResources, _values?: DIValues) {
Expand All @@ -19,12 +21,7 @@ export const diAttributeHandler: DIHandler = {
// Wrap single attribute in array and bail if any new attributes are missing names
const attributeValues = Array.isArray(values) ? values : [values]
const attributeErrors = attributeValues.map(singleValue => {
if (!singleValue?.name) {
return {
success: false,
values: { error: t("V3.DI.Error.fieldRequired", { vars: ["Create", "attribute", "name"] }) }
} as const
}
if (!singleValue?.name) return fieldRequiredResult("Create", "attribute", "name")
return { success: true }
}).filter(error => !error.success)
if (attributeErrors.length > 0) return attributeErrors[0]
Expand Down Expand Up @@ -75,6 +72,25 @@ export const diAttributeHandler: DIHandler = {
}
},

notify(resources: DIResources, values?: DIValues) {
const { attribute, dataContext } = resources
if (!dataContext) return dataContextNotFoundResult
if (!attribute) return attributeNotFoundResult

const { request } = (values ?? {}) as DINotifyAttribute
if (!request) return fieldRequiredResult("Notify", "attribute", "request")

if (request === "dragStart") {
// Emit an event that will be captured by a custom dnd-kit sensor to start a drag
document.dispatchEvent(new CustomEvent("attributeDragStart", {
detail: { attribute, dataContext }
}))
return { success: true }
}

return errorResult(t("V3.DI.Error.unknownRequest", { vars: [request] }))
},

update(resources: DIResources, _values?: DIValues) {
const { attribute, dataContext } = resources
if (!attribute || Array.isArray(_values)) return attributeNotFoundResult
Expand Down
7 changes: 3 additions & 4 deletions v3/src/data-interactive/handlers/data-context-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import { basicDataSetInfo, convertDataSetToV2 } from "../data-interactive-type-u
import { getAttribute } from "../data-interactive-utils"
import { findTileFromNameOrId } from "../resource-parser-utils"
import { createCollection } from "./di-handler-utils"
import { dataContextNotFoundResult, errorResult } from "./di-results"
import { dataContextNotFoundResult, errorResult, fieldRequiredResult } from "./di-results"
import { toV3CaseId } from "../../utilities/codap-utils"

const requestRequiedResult =
errorResult(t("V3.DI.Error.fieldRequired", { vars: ["Notify", "dataContext", "request"] }))
const requestRequiedResult = fieldRequiredResult("Notify", "dataContext", "request")

export const diDataContextHandler: DIHandler = {
create(_resources: DIResources, _values?: DIValues) {
Expand Down Expand Up @@ -85,7 +84,7 @@ export const diDataContextHandler: DIHandler = {

const successResult = { success: true as const, values: {} }
if (request === "setAside") {
if (!caseIDs) return errorResult(t("V3.DI.Error.fieldRequired", { vars: ["Notify", "dataContext", "caseIDs"] }))
if (!caseIDs) return fieldRequiredResult("Notify", "dataContext", "caseIDs")
dataContext.hideCasesOrItems(caseIDs.map(caseId => toV3CaseId(caseId)))
return successResult
} else if (request === "restoreSetasides") {
Expand Down
4 changes: 4 additions & 0 deletions v3/src/data-interactive/handlers/di-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export const couldNotParseQueryResult = errorResult(t("V3.DI.Error.couldNotParse
export const dataContextNotFoundResult = errorResult(t("V3.DI.Error.dataContextNotFound"))
export const itemNotFoundResult = errorResult(t("V3.DI.Error.itemNotFound"))
export const valuesRequiredResult = errorResult(t("V3.DI.Error.valuesRequired"))

export function fieldRequiredResult(action: string, resource: string, field: string) {
return errorResult(t("V3.DI.Error.fieldRequired", { vars: [action, resource, field] }))
}
7 changes: 2 additions & 5 deletions v3/src/data-interactive/handlers/handler-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "../data-interactive-types"
import { getV2ItemResult, getCaseRequestResultValues } from "../data-interactive-type-utils"
import { attrNamesToIds } from "../data-interactive-utils"
import { caseNotFoundResult, dataContextNotFoundResult, itemNotFoundResult } from "./di-results"
import { caseNotFoundResult, dataContextNotFoundResult, fieldRequiredResult, itemNotFoundResult } from "./di-results"

export function deleteCaseBy(resources: DIResources, aCase?: ICase) {
const { dataContext } = resources
Expand Down Expand Up @@ -78,10 +78,7 @@ export function updateCaseBy(

const { itemReturnStyle, nestedValues, resourceName } = options ?? {}

const missingFieldResult = (field: string) => ({
success: false as const,
values: { error: t("V3.DI.Error.fieldRequired", { vars: ["update", resourceName ?? "case", field] }) }
})
const missingFieldResult = (field: string) => fieldRequiredResult("update", resourceName ?? "case", field)
if (!values) return missingFieldResult("values")

let _values = values as DICaseValues
Expand Down
5 changes: 2 additions & 3 deletions v3/src/data-interactive/handlers/item-search-handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { toV2Id } from "../../utilities/codap-utils"
import { t } from "../../utilities/translation/translate"
import { registerDIHandler } from "../data-interactive-handler"
import { getV2ItemResult } from "../data-interactive-type-utils"
import { ICase } from "../../models/data/data-set-types"
import { DIHandler, DIItemSearchNotify, DIResources, DIValues } from "../data-interactive-types"
import { couldNotParseQueryResult, dataContextNotFoundResult, errorResult, valuesRequiredResult } from "./di-results"
import { couldNotParseQueryResult, dataContextNotFoundResult, fieldRequiredResult, valuesRequiredResult } from "./di-results"

export const diItemSearchHandler: DIHandler = {
delete(resources: DIResources) {
Expand Down Expand Up @@ -36,7 +35,7 @@ export const diItemSearchHandler: DIHandler = {

if (!values) return valuesRequiredResult
const { itemOrder } = values as DIItemSearchNotify
if (!itemOrder) return errorResult(t("V3.DI.Error.fieldRequired", { vars: ["Notify", "itemSearch", "itemOrder"] }))
if (!itemOrder) return fieldRequiredResult("Notify", "itemSearch", "itemOrder")

dataContext.applyModelChange(() => {
const itemIds = itemSearch.map(({ __id__ }) => __id__)
Expand Down

0 comments on commit e7c2206

Please sign in to comment.