Skip to content

Commit

Permalink
WIP: check if elements in list are references
Browse files Browse the repository at this point in the history
  • Loading branch information
soofstad committed Oct 13, 2023
1 parent 0cedd33 commit 4eed1fe
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"name": "task list",
"type": "CORE:BlueprintAttribute",
"attributeType": "./Task",
"dimensions": "*"
"dimensions": "*",
"contained": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Wash_the_car",
"type": "./blueprints/Task",
"description": "Car needs a thorough wash inside and outside.",
"assigned": "Mark Johnson",
"complete": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Mow_the_lawn",
"type": "./blueprints/Task",
"description": "",
"assigned": "",
"complete": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,5 @@
"_id": "taskList",
"name": "TaskList",
"type": "./blueprints/TaskList",
"task list": [
{
"name": "Wash the car",
"type": "./blueprints/Task",
"description": "Car needs a thorough wash inside and outside.",
"assigned": "Mark Johnson",
"complete": false
},
{
"name": "Mow the lawn",
"type": "./blueprints/Task",
"description": "",
"assigned": "",
"complete": false
},
{
"name": "Paint the living room",
"type": "./blueprints/Task",
"description": "",
"assigned": "",
"complete": false
}
]
"task list": []
}
8 changes: 6 additions & 2 deletions packages/dm-core-plugins/src/list/ListPlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, {useContext, useEffect, useMemo, useState} from 'react'
import {
ErrorResponse,
IUIPlugin,
Expand All @@ -14,8 +14,9 @@ import {
import { Button, Icon, Tooltip, Typography } from '@equinor/eds-core-react'
import { AxiosError, AxiosResponse } from 'axios'
import { AppendButton, ListItemButton, SaveButton } from './Components'
import { moveItem } from './utils'
import {IsListOfReferences, moveItem} from './utils'
import { add, minimize } from '@equinor/eds-icons'
import {ApplicationContext} from "@development-framework/dm-core"

type TListConfig = {
expanded?: boolean
Expand Down Expand Up @@ -72,6 +73,8 @@ export const ListPlugin = (props: IUIPlugin & { config?: TListConfig }) => {
const [unsavedChanges, setUnsavedChanges] = useState<boolean>(false)
const [isLoading, setIsLoading] = useState<boolean>(false)
const dmssAPI = useDMSS()
const { name: applicationContext } = useContext(ApplicationContext)
const listOfReferences = useMemo(()=>IsListOfReferences(dmssAPI, applicationContext, idReference), [])

const paginatedRows = items.slice(
paginationPage * paginationRowsPerPage,
Expand Down Expand Up @@ -110,6 +113,7 @@ export const ListPlugin = (props: IUIPlugin & { config?: TListConfig }) => {
}

function addItem() {

setUnsavedChanges(true)
dmssAPI
.instantiateEntity({
Expand Down
40 changes: 40 additions & 0 deletions packages/dm-core-plugins/src/list/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {DmssAPI, ErrorResponse, TAttribute, TBlueprint} from "@development-framework/dm-core";
import {AxiosError} from 'axios'

function arrayMove(arr: any[], fromIndex: number, toIndex: number) {
const arrayCopy = [...arr]
const element = arrayCopy[fromIndex]
Expand All @@ -12,3 +15,40 @@ export function moveItem(list: any[], key: string, direction: 'up' | 'down') {
const updatedList = arrayMove(list, itemIndex, toIndex)
return updatedList
}

function splitLastOccurrence(str: string, substring: string): [string, string | undefined] {
const lastIndex = str.lastIndexOf(substring);

const before = str.slice(0, lastIndex);

const after = str.slice(lastIndex + 1);

return [before, after];
}

function splitIdReferenceOnLastAttribute(idReference: string): [string, string | undefined] {
const lastIndex = Math.max(idReference.lastIndexOf('.'), idReference.lastIndexOf('['))
const root = idReference.slice(0, lastIndex)
const attr = idReference.slice(lastIndex + 1)
return [root, attr]
}

export async function IsListOfReferences(dmssAPI: DmssAPI, context: string, idReference: string): Promise<boolean> {
if (idReference.includes('.') || idReference.includes('[')) {
const [root, attribute] = splitIdReferenceOnLastAttribute(idReference)
return dmssAPI
.documentGet({
address: root,
})
.then((response: any) => {
const parentType: string = response.data.type
return dmssAPI.blueprintGet({typeRef: parentType, context: context})
.then((response: any) => {
const blueprint: TBlueprint = response.data.blueprint
return blueprint.attributes?.find((attr: TAttribute) => attr.name === attribute).contained
})
})
}

return true
}

0 comments on commit 4eed1fe

Please sign in to comment.