From 4f433f68b97afe9e4a4faad0d4ea8a72f1fcc93a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20B=C3=A9gaudeau?= Date: Thu, 11 Mar 2021 17:12:20 +0100 Subject: [PATCH] [384] Make the child creation descriptions editing context aware MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: https://github.com/eclipse-sirius/sirius-components/issues/384 Signed-off-by: Stéphane Bégaudeau --- .../web/compat/diagrams/NoOpEditService.java | 8 +- .../sirius/web/emf/services/EditService.java | 53 ++- .../diagrams/NoOpEditService.java | 6 +- .../schema/EditingContextTypeProvider.java | 81 ++++ .../graphql/schema/ViewerTypeProvider.java | 69 +--- ...ChildCreationDescriptionsDataFetcher.java} | 25 +- .../EditingContextNamespacesDataFetcher.java} | 25 +- ...bjectCreationDescriptionsDataFetcher.java} | 30 +- .../MutationCreateChildDataFetcher.java | 4 +- .../MutationCreateRootObjectDataFetcher.java | 4 +- .../api/document/CreateRootObjectInput.java | 16 +- .../api/objects/CreateChildInput.java | 14 +- .../services/api/objects/IEditService.java | 8 +- .../handlers/NoOpEditService.java | 8 +- .../src/modals/new-object/NewObjectModal.tsx | 243 ++++++++---- .../modals/new-object/NewObjectModal.types.ts | 71 ++++ .../new-object/NewObjectModalMachine.ts | 193 ++++++++++ .../NewRootObjectModal.module.css | 17 - .../new-root-object/NewRootObjectModal.tsx | 364 ++++++++++++------ .../NewRootObjectModal.types.ts | 90 +++++ .../NewRootObjectModalMachine.ts | 261 +++++++++++++ frontend/src/tree/TreeItem.tsx | 4 +- 22 files changed, 1225 insertions(+), 369 deletions(-) rename backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/{user/UserChildCreationDescriptionsDataFetcher.java => editingcontext/EditingContextChildCreationDescriptionsDataFetcher.java} (59%) rename backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/{user/UserNamespacesDataFetcher.java => editingcontext/EditingContextNamespacesDataFetcher.java} (61%) rename backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/{user/UserRootObjectCreationDescriptionsDataFetcher.java => editingcontext/EditingContextRootObjectCreationDescriptionsDataFetcher.java} (51%) create mode 100644 frontend/src/modals/new-object/NewObjectModal.types.ts create mode 100644 frontend/src/modals/new-object/NewObjectModalMachine.ts delete mode 100644 frontend/src/modals/new-root-object/NewRootObjectModal.module.css create mode 100644 frontend/src/modals/new-root-object/NewRootObjectModal.types.ts create mode 100644 frontend/src/modals/new-root-object/NewRootObjectModalMachine.ts diff --git a/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/NoOpEditService.java b/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/NoOpEditService.java index d2db495caf0..a9e43ccacbe 100644 --- a/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/NoOpEditService.java +++ b/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/NoOpEditService.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2020 Obeo. + * Copyright (c) 2019, 2021 Obeo. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -35,7 +35,7 @@ public Optional findClass(String classId) { } @Override - public List getChildCreationDescriptions(String classId) { + public List getChildCreationDescriptions(UUID editingContextId, String classId) { return new ArrayList<>(); } @@ -50,12 +50,12 @@ public void delete(Object object) { } @Override - public List getNamespaces() { + public List getNamespaces(UUID editingContextId) { return new ArrayList<>(); } @Override - public List getRootCreationDescriptions(String namespaceId, boolean suggested) { + public List getRootCreationDescriptions(UUID editingContextId, String namespaceId, boolean suggested) { return new ArrayList<>(); } diff --git a/backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/services/EditService.java b/backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/services/EditService.java index 2e0dd046006..6a324562b86 100644 --- a/backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/services/EditService.java +++ b/backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/services/EditService.java @@ -31,6 +31,7 @@ import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EPackage; import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.impl.EPackageRegistryImpl; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; @@ -58,15 +59,19 @@ @Service public class EditService implements IEditService { + private final IEditingContextEPackageService editingContextEPackageService; + private final ComposedAdapterFactory composedAdapterFactory; - private final EPackage.Registry ePackageRegistry; + private final EPackage.Registry globalEPackageRegistry; private final ISuggestedRootObjectTypesProvider suggestedRootObjectTypesProvider; - public EditService(ComposedAdapterFactory composedAdapterFactory, EPackage.Registry ePackageRegistry, ISuggestedRootObjectTypesProvider suggestedRootObjectsProvider) { + public EditService(IEditingContextEPackageService editingContextEPackageService, ComposedAdapterFactory composedAdapterFactory, EPackage.Registry globalEPackageRegistry, + ISuggestedRootObjectTypesProvider suggestedRootObjectsProvider) { + this.editingContextEPackageService = Objects.requireNonNull(editingContextEPackageService); this.composedAdapterFactory = Objects.requireNonNull(composedAdapterFactory); - this.ePackageRegistry = Objects.requireNonNull(ePackageRegistry); + this.globalEPackageRegistry = Objects.requireNonNull(globalEPackageRegistry); this.suggestedRootObjectTypesProvider = Objects.requireNonNull(suggestedRootObjectsProvider); } @@ -77,7 +82,7 @@ public Optional findClass(String classId) { String eClassName = classIdService.getEClassName(classId); // @formatter:off - return classIdService.findEPackage(this.ePackageRegistry, ePackageName) + return classIdService.findEPackage(this.globalEPackageRegistry, ePackageName) .map(ePackage -> ePackage.getEClassifier(eClassName)) .filter(EClass.class::isInstance) .map(EClass.class::cast) @@ -87,11 +92,15 @@ public Optional findClass(String classId) { } @Override - public List getChildCreationDescriptions(String classId) { + public List getChildCreationDescriptions(UUID editingContextId, String classId) { List childCreationDescriptions = new ArrayList<>(); + EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl(this.globalEPackageRegistry); + List additionalEPackages = this.editingContextEPackageService.getEPackages(editingContextId); + additionalEPackages.forEach(ePackage -> ePackageRegistry.put(ePackage.getNsURI(), ePackage)); + ResourceSet resourceSet = new ResourceSetImpl(); - resourceSet.setPackageRegistry(this.ePackageRegistry); + resourceSet.setPackageRegistry(ePackageRegistry); AdapterFactoryEditingDomain editingDomain = new AdapterFactoryEditingDomain(this.composedAdapterFactory, new BasicCommandStack(), resourceSet); Resource resource = new JsonResourceImpl(URI.createURI("inmemory"), Map.of()); //$NON-NLS-1$ resourceSet.getResources().add(resource); @@ -202,23 +211,37 @@ public void delete(Object object) { } @Override - public List getNamespaces() { + public List getNamespaces(UUID editingContextId) { + List eNsURIs = new ArrayList<>(); + eNsURIs.addAll(this.globalEPackageRegistry.keySet()); + // @formatter:off - return this.ePackageRegistry.keySet().stream() + this.editingContextEPackageService.getEPackages(editingContextId).stream() + .map(EPackage::getNsURI) + .forEach(eNsURIs::add); + + return eNsURIs.stream() .map(nsUri -> new Namespace(nsUri, nsUri)) .collect(Collectors.toList()); // @formatter:on } @Override - public List getRootCreationDescriptions(String namespaceId, boolean suggested) { + public List getRootCreationDescriptions(UUID editingContextId, String namespaceId, boolean suggested) { List rootObjectCreationDescription = new ArrayList<>(); - EPackage ePackage = this.ePackageRegistry.getEPackage(namespaceId); + EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl(this.globalEPackageRegistry); + List additionalEPackages = this.editingContextEPackageService.getEPackages(editingContextId); + additionalEPackages.forEach(ePackage -> ePackageRegistry.put(ePackage.getNsURI(), ePackage)); + + EPackage ePackage = ePackageRegistry.getEPackage(namespaceId); if (ePackage != null) { List classes = new ArrayList<>(); if (suggested) { classes = this.suggestedRootObjectTypesProvider.getSuggestedRootObjectTypes(ePackage); + if (classes.isEmpty()) { + classes = this.getConcreteClasses(ePackage); + } } else { classes = this.getConcreteClasses(ePackage); } @@ -243,7 +266,7 @@ private List getConcreteClasses(EPackage ePackage) { public Optional createRootObject(IEditingContext editingContext, UUID documentId, String namespaceId, String rootObjectCreationDescriptionId) { Optional createdObjectOptional = Optional.empty(); - var optionalEClass = this.getMatchingEClass(namespaceId, rootObjectCreationDescriptionId); + var optionalEClass = this.getMatchingEClass(editingContext.getId(), namespaceId, rootObjectCreationDescriptionId); // @formatter:off var optionalEditingDomain = Optional.of(editingContext) @@ -275,9 +298,13 @@ public Optional createRootObject(IEditingContext editingContext, UUID do return createdObjectOptional; } - private Optional getMatchingEClass(String namespaceId, String rootObjectCreationDescriptionId) { + private Optional getMatchingEClass(UUID editingContextId, String namespaceId, String rootObjectCreationDescriptionId) { + EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl(this.globalEPackageRegistry); + List additionalEPackages = this.editingContextEPackageService.getEPackages(editingContextId); + additionalEPackages.forEach(ePackage -> ePackageRegistry.put(ePackage.getNsURI(), ePackage)); + // @formatter:off - return Optional.ofNullable(this.ePackageRegistry.getEPackage(namespaceId)) + return Optional.ofNullable(ePackageRegistry.getEPackage(namespaceId)) .map(ePackage -> ePackage.getEClassifier(rootObjectCreationDescriptionId)) .filter(EClass.class::isInstance) .map(EClass.class::cast) diff --git a/backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/compatibility/diagrams/NoOpEditService.java b/backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/compatibility/diagrams/NoOpEditService.java index 19d5defc37c..9d2f66fd9ed 100644 --- a/backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/compatibility/diagrams/NoOpEditService.java +++ b/backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/compatibility/diagrams/NoOpEditService.java @@ -35,7 +35,7 @@ public Optional findClass(String classId) { } @Override - public List getChildCreationDescriptions(String classId) { + public List getChildCreationDescriptions(UUID editingContextId, String classId) { return new ArrayList<>(); } @@ -50,12 +50,12 @@ public void delete(Object object) { } @Override - public List getNamespaces() { + public List getNamespaces(UUID editingContextId) { return new ArrayList<>(); } @Override - public List getRootCreationDescriptions(String namespaceId, boolean suggested) { + public List getRootCreationDescriptions(UUID editingContextId, String namespaceId, boolean suggested) { return new ArrayList<>(); } diff --git a/backend/sirius-web-graphql-schema/src/main/java/org/eclipse/sirius/web/graphql/schema/EditingContextTypeProvider.java b/backend/sirius-web-graphql-schema/src/main/java/org/eclipse/sirius/web/graphql/schema/EditingContextTypeProvider.java index 5619157d2b4..f6ce1036225 100644 --- a/backend/sirius-web-graphql-schema/src/main/java/org/eclipse/sirius/web/graphql/schema/EditingContextTypeProvider.java +++ b/backend/sirius-web-graphql-schema/src/main/java/org/eclipse/sirius/web/graphql/schema/EditingContextTypeProvider.java @@ -12,6 +12,8 @@ *******************************************************************************/ package org.eclipse.sirius.web.graphql.schema; +import static graphql.schema.GraphQLArgument.newArgument; +import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; import static graphql.schema.GraphQLList.list; import static graphql.schema.GraphQLNonNull.nonNull; import static graphql.schema.GraphQLTypeReference.typeRef; @@ -20,8 +22,12 @@ import java.util.Set; import org.eclipse.sirius.web.graphql.utils.schema.ITypeProvider; +import org.eclipse.sirius.web.services.api.objects.ChildCreationDescription; +import org.eclipse.sirius.web.services.api.objects.Namespace; import org.springframework.stereotype.Service; +import graphql.Scalars; +import graphql.schema.GraphQLArgument; import graphql.schema.GraphQLFieldDefinition; import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLType; @@ -36,6 +42,9 @@ * type EditingContext { * id: ID! * stereotypeDescriptions: [StereotypeDescription!]! + * childCreationDescriptions(classId: ID!): [ChildCreationDescription!]! + * rootObjectCreationDescriptions(namespaceId: ID!, suggested: Boolean!): [ChildCreationDescription!]! + * namespaces: [Namespace!]! * } * * @@ -48,6 +57,18 @@ public class EditingContextTypeProvider implements ITypeProvider { public static final String STEREOTYPE_DESCRIPTIONS_FIELD = "stereotypeDescriptions"; //$NON-NLS-1$ + public static final String CHILD_CREATION_DESCRIPTIONS_FIELD = "childCreationDescriptions"; //$NON-NLS-1$ + + public static final String CLASS_ID_ARGUMENT = "classId"; //$NON-NLS-1$ + + public static final String ROOT_OBJECT_CREATION_DESCRIPTIONS_FIELD = "rootObjectCreationDescriptions"; //$NON-NLS-1$ + + public static final String NAMESPACE_ID_ARGUMENT = "namespaceId"; //$NON-NLS-1$ + + public static final String SUGGESTED_ARGUMENT = "suggested"; //$NON-NLS-1$ + + public static final String NAMESPACES_FIELD = "namespaces"; //$NON-NLS-1$ + @Override public Set getTypes() { // @formatter:off @@ -55,6 +76,9 @@ public Set getTypes() { .name(TYPE) .field(new IdFieldProvider().getField()) .field(this.getStereotypeDescriptionsField()) + .field(this.getChildCreationDescriptionsField()) + .field(this.getRootObjectCreationDescriptionsField()) + .field(this.getNamespaceField()) .build(); // @formatter:on @@ -73,4 +97,61 @@ private GraphQLFieldDefinition getStereotypeDescriptionsField() { // @formatter:on } + private GraphQLFieldDefinition getChildCreationDescriptionsField() { + // @formatter:off + return newFieldDefinition() + .name(CHILD_CREATION_DESCRIPTIONS_FIELD) + .argument(this.getClassIdArgument()) + .type(nonNull(list(nonNull(typeRef(ChildCreationDescription.class.getSimpleName()))))) + .build(); + // @formatter:on + } + + private GraphQLArgument getClassIdArgument() { + // @formatter:off + return newArgument() + .name(CLASS_ID_ARGUMENT) + .type(nonNull(Scalars.GraphQLID)) + .build(); + // @formatter:on + } + + private GraphQLFieldDefinition getRootObjectCreationDescriptionsField() { + // @formatter:off + return newFieldDefinition() + .name(ROOT_OBJECT_CREATION_DESCRIPTIONS_FIELD) + .type(nonNull(list(nonNull(typeRef(ChildCreationDescription.class.getSimpleName()))))) + .argument(this.getNamespaceIdArgument()) + .argument(this.getSuggestedArgument()) + .build(); + // @formatter:on + } + + private GraphQLArgument getNamespaceIdArgument() { + // @formatter:off + return newArgument() + .name(NAMESPACE_ID_ARGUMENT) + .type(nonNull(Scalars.GraphQLID)) + .build(); + // @formatter:on + } + + private GraphQLArgument getSuggestedArgument() { + // @formatter:off + return newArgument() + .name(SUGGESTED_ARGUMENT) + .type(nonNull(Scalars.GraphQLBoolean)) + .build(); + // @formatter:on + } + + private GraphQLFieldDefinition getNamespaceField() { + // @formatter:off + return newFieldDefinition() + .name(NAMESPACES_FIELD) + .type(nonNull(list(nonNull(typeRef(Namespace.class.getSimpleName()))))) + .build(); + // @formatter:on + } + } diff --git a/backend/sirius-web-graphql-schema/src/main/java/org/eclipse/sirius/web/graphql/schema/ViewerTypeProvider.java b/backend/sirius-web-graphql-schema/src/main/java/org/eclipse/sirius/web/graphql/schema/ViewerTypeProvider.java index f3e0368d872..d37d39bc26b 100644 --- a/backend/sirius-web-graphql-schema/src/main/java/org/eclipse/sirius/web/graphql/schema/ViewerTypeProvider.java +++ b/backend/sirius-web-graphql-schema/src/main/java/org/eclipse/sirius/web/graphql/schema/ViewerTypeProvider.java @@ -26,8 +26,6 @@ import org.eclipse.sirius.web.graphql.utils.schema.ITypeProvider; import org.eclipse.sirius.web.services.api.accounts.Capabilities; -import org.eclipse.sirius.web.services.api.objects.ChildCreationDescription; -import org.eclipse.sirius.web.services.api.objects.Namespace; import org.springframework.stereotype.Service; import graphql.Scalars; @@ -52,8 +50,6 @@ * project(projectId: ID!): Project * editingContext(editingContextId: ID!): EditingContext * representationDescriptions(classId: ID): ViewerRepresentationDescriptionConnection! - * childCreationDescriptions(classId: ID!): [ChildCreationDescription!]! - * rootObjectCreationDescriptions(namespaceId: ID!, suggested: Boolean!): [ChildCreationDescription!]! * namespaces: [Namespace!]! * capabilities: Capabilities! * } @@ -64,8 +60,6 @@ * projects: [Project!]! * project(projectId: ID!): Project * representationDescriptions(classId: ID): ViewerRepresentationDescriptionConnection! - * childCreationDescriptions(classId: ID!): [ChildCreationDescription!]! - * rootObjectCreationDescriptions(namespaceId: ID!, suggested: Boolean!): [ChildCreationDescription!]! * namespaces: [Namespace!]! * capabilities: Capabilities! * } @@ -102,13 +96,7 @@ public class ViewerTypeProvider implements ITypeProvider { public static final String EDITING_CONTEXT_ID_ARGUMENT = "editingContextId"; //$NON-NLS-1$ - public static final String NAMESPACES_FIELD = "namespaces"; //$NON-NLS-1$ - - public static final String ROOT_OBJECT_CREATION_DESCRIPTIONS_FIELD = "rootObjectCreationDescriptions"; //$NON-NLS-1$ - - public static final String NAMESPACE_ID_ARGUMENT = "namespaceId"; //$NON-NLS-1$ - - public static final String SUGGESTED_ARGUMENT = "suggested"; //$NON-NLS-1$ + public static final String CLASS_ID_ARGUMENT = "classId"; //$NON-NLS-1$ public static final String REPRESENTATION_DESCRIPTIONS_FIELD = "representationDescriptions"; //$NON-NLS-1$ @@ -116,10 +104,6 @@ public class ViewerTypeProvider implements ITypeProvider { public static final String VIEWER_REPRESENTATION_DESCRIPTIONS_EDGE = TYPE + RepresentationDescriptionTypeProvider.TYPE + GraphQLConstants.EDGE; - public static final String CHILD_CREATION_DESCRIPTIONS_FIELD = "childCreationDescriptions"; //$NON-NLS-1$ - - public static final String CLASS_ID_ARGUMENT = "classId"; //$NON-NLS-1$ - public static final String CAPABILITIES_FIELD = "capabilities"; //$NON-NLS-1$ @Override @@ -160,10 +144,7 @@ protected List getViewerFieldDefinitions() { viewerFieldsDefinition.add(this.getProjectsField()); viewerFieldsDefinition.add(this.getProjectField()); viewerFieldsDefinition.add(this.getEditingContextField()); - viewerFieldsDefinition.add(this.getNamespaceField()); - viewerFieldsDefinition.add(this.getRootObjectCreationDescriptionsField()); viewerFieldsDefinition.add(this.getRepresentationDescriptionField()); - viewerFieldsDefinition.add(this.getChildCreationDescriptionsField()); viewerFieldsDefinition.add(this.getCapabilitiesField()); return viewerFieldsDefinition; } @@ -217,44 +198,6 @@ private GraphQLFieldDefinition getEditingContextField() { // @formatter:on } - private GraphQLFieldDefinition getNamespaceField() { - // @formatter:off - return newFieldDefinition() - .name(NAMESPACES_FIELD) - .type(nonNull(list(nonNull(typeRef(Namespace.class.getSimpleName()))))) - .build(); - // @formatter:on - } - - private GraphQLFieldDefinition getRootObjectCreationDescriptionsField() { - // @formatter:off - return newFieldDefinition() - .name(ROOT_OBJECT_CREATION_DESCRIPTIONS_FIELD) - .type(nonNull(list(nonNull(typeRef(ChildCreationDescription.class.getSimpleName()))))) - .argument(this.getNamespaceIdArgument()) - .argument(this.getSuggestedArgument()) - .build(); - // @formatter:on - } - - private GraphQLArgument getNamespaceIdArgument() { - // @formatter:off - return newArgument() - .name(NAMESPACE_ID_ARGUMENT) - .type(nonNull(Scalars.GraphQLID)) - .build(); - // @formatter:on - } - - private GraphQLArgument getSuggestedArgument() { - // @formatter:off - return newArgument() - .name(SUGGESTED_ARGUMENT) - .type(nonNull(Scalars.GraphQLBoolean)) - .build(); - // @formatter:on - } - private GraphQLFieldDefinition getRepresentationDescriptionField() { // @formatter:off return newFieldDefinition() @@ -265,16 +208,6 @@ private GraphQLFieldDefinition getRepresentationDescriptionField() { // @formatter:on } - private GraphQLFieldDefinition getChildCreationDescriptionsField() { - // @formatter:off - return newFieldDefinition() - .name(CHILD_CREATION_DESCRIPTIONS_FIELD) - .argument(this.getClassIdArgument()) - .type(nonNull(list(nonNull(typeRef(ChildCreationDescription.class.getSimpleName()))))) - .build(); - // @formatter:on - } - private GraphQLArgument getClassIdArgument() { // @formatter:off return newArgument() diff --git a/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/user/UserChildCreationDescriptionsDataFetcher.java b/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/editingcontext/EditingContextChildCreationDescriptionsDataFetcher.java similarity index 59% rename from backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/user/UserChildCreationDescriptionsDataFetcher.java rename to backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/editingcontext/EditingContextChildCreationDescriptionsDataFetcher.java index b2c4d8c3fb3..3075302be4c 100644 --- a/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/user/UserChildCreationDescriptionsDataFetcher.java +++ b/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/editingcontext/EditingContextChildCreationDescriptionsDataFetcher.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2020 Obeo. + * Copyright (c) 2021 Obeo. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -10,12 +10,14 @@ * Contributors: * Obeo - initial API and implementation *******************************************************************************/ -package org.eclipse.sirius.web.graphql.datafetchers.user; +package org.eclipse.sirius.web.graphql.datafetchers.editingcontext; import java.util.List; import java.util.Objects; +import java.util.UUID; import org.eclipse.sirius.web.annotations.spring.graphql.QueryDataFetcher; +import org.eclipse.sirius.web.graphql.schema.EditingContextTypeProvider; import org.eclipse.sirius.web.graphql.schema.ViewerTypeProvider; import org.eclipse.sirius.web.services.api.objects.ChildCreationDescription; import org.eclipse.sirius.web.services.api.objects.IEditService; @@ -24,32 +26,31 @@ import graphql.schema.DataFetchingEnvironment; /** - * Computes the child creation descriptors for a specific class. + * The data fetcher used to retrieve the child creation descriptions accessible in an editing context. *

* It will be used to fetch the data for the following GraphQL field: *

* *
- * type Viewer {
- *   childCreationDescriptions(classId: ID!): [ChildCreationDescription!]!
+ * type EditingContext {
+ *   rootObjectCreationDescriptions(namespaceId: ID!, suggested: Boolean!): [ChildCreationDescription!]!
  * }
  * 
* - * @author sbegaudeau + * @author hmarchadour */ -@QueryDataFetcher(type = ViewerTypeProvider.USER_TYPE, field = ViewerTypeProvider.CHILD_CREATION_DESCRIPTIONS_FIELD) -public class UserChildCreationDescriptionsDataFetcher implements IDataFetcherWithFieldCoordinates> { - +@QueryDataFetcher(type = EditingContextTypeProvider.TYPE, field = EditingContextTypeProvider.CHILD_CREATION_DESCRIPTIONS_FIELD) +public class EditingContextChildCreationDescriptionsDataFetcher implements IDataFetcherWithFieldCoordinates> { private final IEditService editService; - public UserChildCreationDescriptionsDataFetcher(IEditService editService) { + public EditingContextChildCreationDescriptionsDataFetcher(IEditService editService) { this.editService = Objects.requireNonNull(editService); } @Override public List get(DataFetchingEnvironment environment) throws Exception { + UUID editingContextId = environment.getSource(); String classIdArgument = environment.getArgument(ViewerTypeProvider.CLASS_ID_ARGUMENT); - return this.editService.getChildCreationDescriptions(classIdArgument); + return this.editService.getChildCreationDescriptions(editingContextId, classIdArgument); } - } diff --git a/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/user/UserNamespacesDataFetcher.java b/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/editingcontext/EditingContextNamespacesDataFetcher.java similarity index 61% rename from backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/user/UserNamespacesDataFetcher.java rename to backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/editingcontext/EditingContextNamespacesDataFetcher.java index d4b427daadb..46678284411 100644 --- a/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/user/UserNamespacesDataFetcher.java +++ b/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/editingcontext/EditingContextNamespacesDataFetcher.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2020 Obeo. + * Copyright (c) 2021 Obeo. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -10,13 +10,14 @@ * Contributors: * Obeo - initial API and implementation *******************************************************************************/ -package org.eclipse.sirius.web.graphql.datafetchers.user; +package org.eclipse.sirius.web.graphql.datafetchers.editingcontext; import java.util.List; import java.util.Objects; +import java.util.UUID; import org.eclipse.sirius.web.annotations.spring.graphql.QueryDataFetcher; -import org.eclipse.sirius.web.graphql.schema.ViewerTypeProvider; +import org.eclipse.sirius.web.graphql.schema.EditingContextTypeProvider; import org.eclipse.sirius.web.services.api.objects.IEditService; import org.eclipse.sirius.web.services.api.objects.Namespace; import org.eclipse.sirius.web.spring.graphql.api.IDataFetcherWithFieldCoordinates; @@ -24,30 +25,30 @@ import graphql.schema.DataFetchingEnvironment; /** - * The data fetcher used to retrieve a document with a given documentId. + * The data fetcher used to retrieve the namespaces accessible in an editing context. *

* It will be used to fetch the data for the following GraphQL field: *

* *
- * type Viewer {
- *   namespaces: Namespace
+ * type EditingContext {
+ *   namespaces: [Namespace!]!
  * }
  * 
* - * @author lfasani + * @author hmarchadour */ -@QueryDataFetcher(type = ViewerTypeProvider.USER_TYPE, field = ViewerTypeProvider.NAMESPACES_FIELD) -public class UserNamespacesDataFetcher implements IDataFetcherWithFieldCoordinates> { - +@QueryDataFetcher(type = EditingContextTypeProvider.TYPE, field = EditingContextTypeProvider.NAMESPACES_FIELD) +public class EditingContextNamespacesDataFetcher implements IDataFetcherWithFieldCoordinates> { private final IEditService editService; - public UserNamespacesDataFetcher(IEditService editService) { + public EditingContextNamespacesDataFetcher(IEditService editService) { this.editService = Objects.requireNonNull(editService); } @Override public List get(DataFetchingEnvironment environment) throws Exception { - return this.editService.getNamespaces(); + UUID editingContextId = environment.getSource(); + return this.editService.getNamespaces(editingContextId); } } diff --git a/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/user/UserRootObjectCreationDescriptionsDataFetcher.java b/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/editingcontext/EditingContextRootObjectCreationDescriptionsDataFetcher.java similarity index 51% rename from backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/user/UserRootObjectCreationDescriptionsDataFetcher.java rename to backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/editingcontext/EditingContextRootObjectCreationDescriptionsDataFetcher.java index c84cd8b23b7..75f75b34c7a 100644 --- a/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/user/UserRootObjectCreationDescriptionsDataFetcher.java +++ b/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/editingcontext/EditingContextRootObjectCreationDescriptionsDataFetcher.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2020 Obeo. + * Copyright (c) 2021 Obeo. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -10,13 +10,14 @@ * Contributors: * Obeo - initial API and implementation *******************************************************************************/ -package org.eclipse.sirius.web.graphql.datafetchers.user; +package org.eclipse.sirius.web.graphql.datafetchers.editingcontext; import java.util.List; import java.util.Objects; +import java.util.UUID; import org.eclipse.sirius.web.annotations.spring.graphql.QueryDataFetcher; -import org.eclipse.sirius.web.graphql.schema.ViewerTypeProvider; +import org.eclipse.sirius.web.graphql.schema.EditingContextTypeProvider; import org.eclipse.sirius.web.services.api.objects.ChildCreationDescription; import org.eclipse.sirius.web.services.api.objects.IEditService; import org.eclipse.sirius.web.spring.graphql.api.IDataFetcherWithFieldCoordinates; @@ -24,34 +25,33 @@ import graphql.schema.DataFetchingEnvironment; /** - * Computes the root object creation descriptions of a document. + * The data fetcher used to retrieve the root object creation descriptions accessible in an editing context. *

* It will be used to fetch the data for the following GraphQL field: *

* *
- * type Viewer {
- *   rootObjectCreationDescriptions(namespaceId: String!, suggested: boolean!): [ChildCreationDescription!]!
+ * type EditingContext {
+ *   childCreationDescriptions(classId: ID!): [ChildCreationDescription!]!
  * }
  * 
* - * @author lfasani + * @author hmarchadour */ -@QueryDataFetcher(type = ViewerTypeProvider.USER_TYPE, field = ViewerTypeProvider.ROOT_OBJECT_CREATION_DESCRIPTIONS_FIELD) -public class UserRootObjectCreationDescriptionsDataFetcher implements IDataFetcherWithFieldCoordinates> { - +@QueryDataFetcher(type = EditingContextTypeProvider.TYPE, field = EditingContextTypeProvider.ROOT_OBJECT_CREATION_DESCRIPTIONS_FIELD) +public class EditingContextRootObjectCreationDescriptionsDataFetcher implements IDataFetcherWithFieldCoordinates> { private final IEditService editService; - public UserRootObjectCreationDescriptionsDataFetcher(IEditService editService) { + public EditingContextRootObjectCreationDescriptionsDataFetcher(IEditService editService) { this.editService = Objects.requireNonNull(editService); } @Override public List get(DataFetchingEnvironment environment) throws Exception { - String namespaceId = environment.getArgument(ViewerTypeProvider.NAMESPACE_ID_ARGUMENT); - Boolean suggested = environment.getArgument(ViewerTypeProvider.SUGGESTED_ARGUMENT); + UUID editingContextId = environment.getSource(); + String namespaceId = environment.getArgument(EditingContextTypeProvider.NAMESPACE_ID_ARGUMENT); + Boolean suggested = environment.getArgument(EditingContextTypeProvider.SUGGESTED_ARGUMENT); - return this.editService.getRootCreationDescriptions(namespaceId, suggested); + return this.editService.getRootCreationDescriptions(editingContextId, namespaceId, suggested); } - } diff --git a/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/mutation/MutationCreateChildDataFetcher.java b/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/mutation/MutationCreateChildDataFetcher.java index c7816631ca9..f701b71e478 100644 --- a/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/mutation/MutationCreateChildDataFetcher.java +++ b/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/mutation/MutationCreateChildDataFetcher.java @@ -75,10 +75,10 @@ public IPayload get(DataFetchingEnvironment environment) throws Exception { IPayload payload = new ErrorPayload(input.getId(), this.messageService.unauthorized()); - boolean canEdit = this.dataFetchingEnvironmentService.canEdit(environment, input.getProjectId()); + boolean canEdit = this.dataFetchingEnvironmentService.canEdit(environment, input.getEditingContextId()); if (canEdit) { // @formatter:off - payload = this.editingContextEventProcessorRegistry.dispatchEvent(input.getProjectId(), input) + payload = this.editingContextEventProcessorRegistry.dispatchEvent(input.getEditingContextId(), input) .orElse(new ErrorPayload(input.getId(), this.messageService.unexpectedError())); // @formatter:on } diff --git a/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/mutation/MutationCreateRootObjectDataFetcher.java b/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/mutation/MutationCreateRootObjectDataFetcher.java index a2d0be050b3..392dd3ec391 100644 --- a/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/mutation/MutationCreateRootObjectDataFetcher.java +++ b/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/datafetchers/mutation/MutationCreateRootObjectDataFetcher.java @@ -74,10 +74,10 @@ public IPayload get(DataFetchingEnvironment environment) throws Exception { IPayload payload = new ErrorPayload(input.getId(), this.messageService.unauthorized()); - boolean canEdit = this.dataFetchingEnvironmentService.canEdit(environment, input.getProjectId()); + boolean canEdit = this.dataFetchingEnvironmentService.canEdit(environment, input.getEditingContextId()); if (canEdit) { // @formatter:off - payload = this.editingContextEventProcessorRegistry.dispatchEvent(input.getProjectId(), input) + payload = this.editingContextEventProcessorRegistry.dispatchEvent(input.getEditingContextId(), input) .orElse(new ErrorPayload(input.getId(), this.messageService.unexpectedError())); // @formatter:on } diff --git a/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/document/CreateRootObjectInput.java b/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/document/CreateRootObjectInput.java index d368abcd260..09407c254cc 100644 --- a/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/document/CreateRootObjectInput.java +++ b/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/document/CreateRootObjectInput.java @@ -23,7 +23,7 @@ import org.eclipse.sirius.web.core.api.IInput; /** - * The input object of the create root objec mutation. + * The input object of the create root object mutation. * * @author lfasani */ @@ -31,7 +31,7 @@ public final class CreateRootObjectInput implements IInput { private UUID id; - private UUID projectId; + private UUID editingContextId; private UUID documentId; @@ -43,9 +43,9 @@ public CreateRootObjectInput() { // Used by Jackson } - public CreateRootObjectInput(UUID id, UUID projectId, UUID documentId, String namespaceId, String rootObjectCreationDescriptionId) { + public CreateRootObjectInput(UUID id, UUID editingContextId, UUID documentId, String namespaceId, String rootObjectCreationDescriptionId) { this.id = Objects.requireNonNull(id); - this.projectId = Objects.requireNonNull(projectId); + this.editingContextId = Objects.requireNonNull(editingContextId); this.documentId = Objects.requireNonNull(documentId); this.namespaceId = Objects.requireNonNull(namespaceId); this.rootObjectCreationDescriptionId = Objects.requireNonNull(rootObjectCreationDescriptionId); @@ -62,8 +62,8 @@ public UUID getId() { @GraphQLID @GraphQLField @GraphQLNonNull - public UUID getProjectId() { - return this.projectId; + public UUID getEditingContextId() { + return this.editingContextId; } @GraphQLID @@ -89,7 +89,7 @@ public String getRootObjectCreationDescriptionId() { @Override public String toString() { - String pattern = "{0} '{'id: {1}, projectId: {2}, documentId: {3}, namespaceId: {4}, rootObjectCreationDescriptionId: {5}'}'"; //$NON-NLS-1$ - return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.projectId, this.documentId, this.namespaceId, this.rootObjectCreationDescriptionId); + String pattern = "{0} '{'id: {1}, editingContextId: {2}, documentId: {3}, namespaceId: {4}, rootObjectCreationDescriptionId: {5}'}'"; //$NON-NLS-1$ + return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.editingContextId, this.documentId, this.namespaceId, this.rootObjectCreationDescriptionId); } } diff --git a/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/objects/CreateChildInput.java b/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/objects/CreateChildInput.java index dd31cb57de3..d9f6a084769 100644 --- a/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/objects/CreateChildInput.java +++ b/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/objects/CreateChildInput.java @@ -31,7 +31,7 @@ public final class CreateChildInput implements IInput { private UUID id; - private UUID projectId; + private UUID editingContextId; private String objectId; @@ -41,9 +41,9 @@ public CreateChildInput() { // Used by Jackson } - public CreateChildInput(UUID id, UUID projectId, String objectId, String childCreationDescriptionId) { + public CreateChildInput(UUID id, UUID editingContextId, String objectId, String childCreationDescriptionId) { this.id = Objects.requireNonNull(id); - this.projectId = Objects.requireNonNull(projectId); + this.editingContextId = Objects.requireNonNull(editingContextId); this.objectId = Objects.requireNonNull(objectId); this.childCreationDescriptionId = Objects.requireNonNull(childCreationDescriptionId); } @@ -59,8 +59,8 @@ public UUID getId() { @GraphQLID @GraphQLField @GraphQLNonNull - public UUID getProjectId() { - return this.projectId; + public UUID getEditingContextId() { + return this.editingContextId; } @GraphQLID @@ -79,7 +79,7 @@ public String getChildCreationDescriptionId() { @Override public String toString() { - String pattern = "{0} '{'id: {1}, projectId: {2}, objectId: {3}, childCreationDescriptionId: {4}'}'"; //$NON-NLS-1$ - return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.projectId, this.objectId, this.childCreationDescriptionId); + String pattern = "{0} '{'id: {1}, editingContextId: {2}, objectId: {3}, childCreationDescriptionId: {4}'}'"; //$NON-NLS-1$ + return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.editingContextId, this.objectId, this.childCreationDescriptionId); } } diff --git a/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/objects/IEditService.java b/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/objects/IEditService.java index 8da392bece3..919e303c9ad 100644 --- a/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/objects/IEditService.java +++ b/backend/sirius-web-services-api/src/main/java/org/eclipse/sirius/web/services/api/objects/IEditService.java @@ -27,13 +27,13 @@ public interface IEditService { Optional findClass(String classId); - List getChildCreationDescriptions(String classId); + List getNamespaces(UUID editingContextId); - Optional createChild(IEditingContext editingContext, Object object, String childCreationDescriptionId); + List getRootCreationDescriptions(UUID editingContextId, String namespaceId, boolean suggested); - List getNamespaces(); + List getChildCreationDescriptions(UUID editingContextId, String classId); - List getRootCreationDescriptions(String namespaceId, boolean suggested); + Optional createChild(IEditingContext editingContext, Object object, String childCreationDescriptionId); Optional createRootObject(IEditingContext editingContext, UUID documentId, String namespaceId, String rootObjectCreationDescriptionId); diff --git a/backend/sirius-web-spring-collaborative/src/test/java/org/eclipse/sirius/web/spring/collaborative/handlers/NoOpEditService.java b/backend/sirius-web-spring-collaborative/src/test/java/org/eclipse/sirius/web/spring/collaborative/handlers/NoOpEditService.java index a3cdd759a65..2f8178af5b2 100644 --- a/backend/sirius-web-spring-collaborative/src/test/java/org/eclipse/sirius/web/spring/collaborative/handlers/NoOpEditService.java +++ b/backend/sirius-web-spring-collaborative/src/test/java/org/eclipse/sirius/web/spring/collaborative/handlers/NoOpEditService.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2020 Obeo. + * Copyright (c) 2019, 2021 Obeo. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -35,7 +35,7 @@ public Optional findClass(String classId) { } @Override - public List getChildCreationDescriptions(String classId) { + public List getChildCreationDescriptions(UUID editingContextId, String classId) { return new ArrayList<>(); } @@ -50,12 +50,12 @@ public void delete(Object object) { } @Override - public List getNamespaces() { + public List getNamespaces(UUID editingContextId) { return new ArrayList<>(); } @Override - public List getRootCreationDescriptions(String namespaceId, boolean suggested) { + public List getRootCreationDescriptions(UUID editingContextId, String namespaceId, boolean suggested) { return new ArrayList<>(); } diff --git a/frontend/src/modals/new-object/NewObjectModal.tsx b/frontend/src/modals/new-object/NewObjectModal.tsx index 06e79558d5a..7b9d9a741fd 100644 --- a/frontend/src/modals/new-object/NewObjectModal.tsx +++ b/frontend/src/modals/new-object/NewObjectModal.tsx @@ -11,14 +11,39 @@ * Obeo - initial API and implementation *******************************************************************************/ import { useMutation, useQuery } from '@apollo/client'; -import { ActionButton, Buttons } from 'core/button/Button'; -import { Form } from 'core/form/Form'; -import { Label } from 'core/label/Label'; -import { Select } from 'core/select/Select'; +import Button from '@material-ui/core/Button'; +import Dialog from '@material-ui/core/Dialog'; +import DialogActions from '@material-ui/core/DialogActions'; +import DialogContent from '@material-ui/core/DialogContent'; +import DialogTitle from '@material-ui/core/DialogTitle'; +import IconButton from '@material-ui/core/IconButton'; +import InputLabel from '@material-ui/core/InputLabel'; +import MenuItem from '@material-ui/core/MenuItem'; +import Select from '@material-ui/core/Select'; +import Snackbar from '@material-ui/core/Snackbar'; +import { makeStyles } from '@material-ui/core/styles'; +import CloseIcon from '@material-ui/icons/Close'; +import { useMachine } from '@xstate/react'; import gql from 'graphql-tag'; -import { Modal } from 'modals/Modal'; -import PropTypes from 'prop-types'; -import React, { useEffect, useState } from 'react'; +import { + GQLCreateChildMutationData, + GQLGetChildCreationDescriptionsQueryData, + GQLGetChildCreationDescriptionsQueryVariables, + NewObjectModalProps, +} from 'modals/new-object/NewObjectModal.types'; +import { + ChangeChildCreationDescriptionEvent, + CreateChildEvent, + FetchedChildCreationDescriptionsEvent, + HandleResponseEvent, + HideToastEvent, + NewObjectModalContext, + NewObjectModalEvent, + newObjectModalMachine, + SchemaValue, + ShowToastEvent, +} from 'modals/new-object/NewObjectModalMachine'; +import React, { useEffect } from 'react'; import { v4 as uuid } from 'uuid'; const createChildMutation = gql` @@ -40,99 +65,165 @@ const createChildMutation = gql` `; const getChildCreationDescriptionsQuery = gql` - query getChildCreationDescriptions($classId: ID!) { + query getChildCreationDescriptions($editingContextId: ID!, $classId: ID!) { viewer { - childCreationDescriptions(classId: $classId) { - id - label + editingContext(editingContextId: $editingContextId) { + childCreationDescriptions(classId: $classId) { + id + label + } } } } `; -const propTypes = { - projectId: PropTypes.string.isRequired, - classId: PropTypes.string.isRequired, - objectId: PropTypes.string.isRequired, - onObjectCreated: PropTypes.func.isRequired, - onClose: PropTypes.func.isRequired, -}; +const useNewObjectModalStyles = makeStyles((theme) => ({ + form: { + display: 'flex', + flexDirection: 'column', + '& > *': { + marginBottom: theme.spacing(1), + }, + }, +})); -export const NewObjectModal = ({ projectId, classId, objectId, onObjectCreated, onClose }) => { - const initialState = { - selectedChildCreationDescriptionId: undefined, - childCreationDescriptions: [], - }; - const [state, setState] = useState(initialState); - const { selectedChildCreationDescriptionId, childCreationDescriptions } = state; +export const NewObjectModal = ({ + editingContextId, + classId, + objectId, + onObjectCreated, + onClose, +}: NewObjectModalProps) => { + const classes = useNewObjectModalStyles(); + const [{ value, context }, dispatch] = useMachine(newObjectModalMachine); + const { newObjectModal, toast } = value as SchemaValue; + const { selectedChildCreationDescriptionId, childCreationDescriptions, objectToSelect, message } = context; const { - loading: descriptionsLoading, - data: descriptionsResult, - error: descriptionError, - } = useQuery(getChildCreationDescriptionsQuery, { variables: { classId } }); + loading: childCreationDescriptionsLoading, + data: childCreationDescriptionsData, + error: childCreationDescriptionsError, + } = useQuery( + getChildCreationDescriptionsQuery, + { variables: { editingContextId, classId } } + ); useEffect(() => { - if (!descriptionsLoading && !descriptionError && descriptionsResult?.viewer) { - setState((prevState) => { - const newState = { ...prevState }; - newState.childCreationDescriptions = descriptionsResult.viewer.childCreationDescriptions; - - if (newState.childCreationDescriptions.length > 0) { - newState.selectedChildCreationDescriptionId = newState.childCreationDescriptions[0].id; - } - - return newState; - }); + if (!childCreationDescriptionsLoading) { + if (childCreationDescriptionsError) { + const showToastEvent: ShowToastEvent = { + type: 'SHOW_TOAST', + message: 'An unexpected error has occurred, please refresh the page', + }; + dispatch(showToastEvent); + } + if (childCreationDescriptionsData) { + const fetchChildCreationDescriptionsEvent: FetchedChildCreationDescriptionsEvent = { + type: 'HANDLE_FETCHED_CHILD_CREATION_DESCRIPTIONS', + data: childCreationDescriptionsData, + }; + dispatch(fetchChildCreationDescriptionsEvent); + } } - }, [descriptionsLoading, descriptionsResult, descriptionError]); - - // Used to update the selected child creation description id - const setSelectedChildCreationDescriptionId = (event) => { - const selectedChildCreationDescriptionId = event.target.value; + }, [childCreationDescriptionsLoading, childCreationDescriptionsData, childCreationDescriptionsError, dispatch]); - setState((prevState) => { - const newState = { ...prevState }; - newState.selectedChildCreationDescriptionId = selectedChildCreationDescriptionId; - return newState; - }); + const onChildCreationDescriptionChange = (event) => { + const value = event.target.value; + const changeChildCreationDescriptionEvent: ChangeChildCreationDescriptionEvent = { + type: 'CHANGE_CHILD_CREATION_DESCRIPTION', + childCreationDescriptionId: value, + }; + dispatch(changeChildCreationDescriptionEvent); }; - // Create the new child - const [createChild, { loading, data, error }] = useMutation(createChildMutation); - const onCreateChild = (event) => { - event.preventDefault(); + const [createChild, { loading: createChildLoading, data: createChildData, error: createChildError }] = useMutation< + GQLCreateChildMutationData + >(createChildMutation); + useEffect(() => { + if (!createChildLoading) { + if (createChildError) { + const showToastEvent: ShowToastEvent = { + type: 'SHOW_TOAST', + message: 'An unexpected error has occurred, please refresh the page', + }; + dispatch(showToastEvent); + } + if (createChildData) { + const handleResponseEvent: HandleResponseEvent = { type: 'HANDLE_RESPONSE', data: createChildData }; + dispatch(handleResponseEvent); + } + } + }, [createChildLoading, createChildData, createChildError, dispatch]); + + const onCreateObject = () => { + dispatch({ type: 'CREATE_CHILD' } as CreateChildEvent); const input = { id: uuid(), - projectId, + editingContextId, objectId, childCreationDescriptionId: selectedChildCreationDescriptionId, }; createChild({ variables: { input } }); }; + useEffect(() => { - if (!loading && !error && data?.createChild?.object) { - onObjectCreated(data.createChild.object); + if (newObjectModal === 'success') { + onObjectCreated(objectToSelect); } - }, [loading, data, error, onObjectCreated]); + }, [newObjectModal, onObjectCreated, objectToSelect]); return ( - -
-