Skip to content

Commit

Permalink
feat: name in new entity picker
Browse files Browse the repository at this point in the history
  • Loading branch information
collinlokken committed Jan 10, 2024
1 parent 7b7d8c3 commit 65d41bd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
5 changes: 3 additions & 2 deletions e2e/tests/plugin-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test('Create blueprint', async ({ page }) => {
).toBeVisible()
})

test('Create entity', async ({ page }) => {
test('Create entity with name', async ({ page }) => {
await page
.getByRole('button', { name: 'package Playwright', exact: true })
.click({ button: 'right' })
Expand All @@ -52,11 +52,12 @@ test('Create entity', async ({ page }) => {
await dialog.getByRole('button', { name: 'plugins' }).click()
await dialog.getByRole('button', { name: 'Playwright' }).click()
await dialog.getByRole('button', { name: 'PlaywrightBlueprint' }).click()
await dialog.getByLabel('Name').fill('PlaywrightEntity')
await dialog.getByRole('button', { name: 'Create' }).click()
await expect(page.getByRole('alert')).toHaveText(['Entity is created'])
await expect(dialog).not.toBeVisible()
await expect(
page.getByRole('button', { name: 'file new_entity' })
page.getByRole('button', { name: 'file PlaywrightEntity' })
).toBeVisible()
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {
ApplicationContext,
BlueprintPicker,
Dialog,
ErrorResponse,
TAttribute,
TBlueprint,
TreeNode,
useDMSS,
} from '@development-framework/dm-core'
import { Button, Progress } from '@equinor/eds-core-react'
import { Button, Progress, TextField } from '@equinor/eds-core-react'
import { AxiosError } from 'axios'
import React, { useState } from 'react'
import React, { useContext, useEffect, useState } from 'react'
import { toast } from 'react-toastify'
import { EDialog } from '../../types'
import {
Expand All @@ -22,13 +26,33 @@ type TProps = {

const NewEntityDialog = (props: TProps) => {
const { setDialogId, node, setNodeOpen } = props
const [blueprint, setBlueprint] = useState<string>('')
const [blueprintName, setBlueprintName] = useState<string>('')
const [blueprint, setBlueprint] = useState<TBlueprint>()
const [newName, setNewName] = useState<string>('')
const [loading, setLoading] = useState<boolean>(false)
const dmssAPI = useDMSS()

const { name } = useContext(ApplicationContext)

useEffect(() => {
if (!blueprintName) return
dmssAPI
.blueprintGet({ typeRef: blueprintName, context: name })
.then((response: any) => {
setBlueprint(response.data.blueprint)
})
}, [blueprintName])

const nameField: TAttribute | undefined = blueprint?.attributes.find(
(attr: TAttribute) => attr.name === 'name'
)
const handleCreate = () => {
setLoading(true)
node
.appendEntity(`dmss://${blueprint}`, 'new_entity')
.appendEntity(
`dmss://${blueprintName}`,
newName.length > 0 ? newName : undefined
)
.then(() => {
setNodeOpen(true)
toast.success(`Entity is created`)
Expand Down Expand Up @@ -57,13 +81,27 @@ const NewEntityDialog = (props: TProps) => {
<Dialog.CustomContent>
<BlueprintPicker
label='Blueprint'
onChange={setBlueprint}
formData={blueprint}
onChange={setBlueprintName}
formData={blueprintName}
fieldType={'input-field'}
/>
{nameField && (
<TextField
id={'entity-name'}
onChange={(e: any) => setNewName(e.target.value)}
label={'Name'}
helperText={nameField.optional ? 'Optional' : 'Required'}
/>
)}
</Dialog.CustomContent>
<Dialog.Actions>
<Button disabled={blueprint === ''} onClick={handleCreate}>
<Button
disabled={
blueprintName === '' ||
(nameField && !nameField.optional && !newName)
}
onClick={handleCreate}
>
{loading ? <Progress.Dots /> : 'Create'}
</Button>
<Button variant='outlined' onClick={() => setDialogId(undefined)}>
Expand Down
2 changes: 1 addition & 1 deletion packages/dm-core/src/domain/Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export class TreeNode {

// Creates a new entity in DMSS of the given type and saves it to this target,
// returns the entity's UUID
async appendEntity(type: string, name: string): Promise<string> {
async appendEntity(type: string, name: string | undefined): Promise<string> {
const response = await this.tree.dmssApi.instantiateEntity({
// @ts-ignore
entity: { name: name, type: type },
Expand Down

0 comments on commit 65d41bd

Please sign in to comment.