-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[4348] Move quick tools declaration on the backend #4349
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023, 2024 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 | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
import { gql, useMutation } from '@apollo/client'; | ||
import { useMultiToast } from '@eclipse-sirius/sirius-components-core'; | ||
import { useCallback, useContext, useEffect } from 'react'; | ||
import { DiagramContext } from '../../contexts/DiagramContext'; | ||
import { DiagramContextValue } from '../../contexts/DiagramContext.types'; | ||
import { | ||
GQLCollapsingState, | ||
GQLErrorPayload, | ||
GQLUpdateCollapsingStateData, | ||
GQLUpdateCollapsingStateInput, | ||
GQLUpdateCollapsingStatePayload, | ||
GQLUpdateCollapsingStateVariables, | ||
UseCollapseExpandElement, | ||
} from './useCollapseExpandElement.types'; | ||
|
||
const updateCollapsingStateMutation = gql` | ||
mutation updateCollapsingState($input: UpdateCollapsingStateInput!) { | ||
updateCollapsingState(input: $input) { | ||
__typename | ||
... on SuccessPayload { | ||
id | ||
} | ||
... on ErrorPayload { | ||
message | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const isErrorPayload = (payload: GQLUpdateCollapsingStatePayload): payload is GQLErrorPayload => | ||
payload.__typename === 'ErrorPayload'; | ||
|
||
export const useCollapseExpandElement = (): UseCollapseExpandElement => { | ||
const { addMessages, addErrorMessage } = useMultiToast(); | ||
const { diagramId, editingContextId } = useContext<DiagramContextValue>(DiagramContext); | ||
|
||
const [collapseExpandMutation, { error: collpaseNodeError }] = useMutation< | ||
GQLUpdateCollapsingStateData, | ||
GQLUpdateCollapsingStateVariables | ||
>(updateCollapsingStateMutation); | ||
|
||
const collapseExpandElement = useCallback( | ||
async (nodeId: string, collapsingState: GQLCollapsingState) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use async not await There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it was already used so I did not change it but if there was no reason to use async I can remove it. |
||
const input: GQLUpdateCollapsingStateInput = { | ||
id: crypto.randomUUID(), | ||
editingContextId, | ||
representationId: diagramId, | ||
diagramElementId: nodeId, | ||
collapsingState, | ||
}; | ||
const { data: collapseNodeData } = await collapseExpandMutation({ variables: { input } }); | ||
if (collapseNodeData) { | ||
const { collapseExpandDiagramElement } = collapseNodeData; | ||
if (isErrorPayload(collapseExpandDiagramElement)) { | ||
addMessages(collapseExpandDiagramElement.messages); | ||
} | ||
} | ||
Comment on lines
+63
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just write this kind of code as we always do |
||
}, | ||
[editingContextId, diagramId, collapseExpandMutation] | ||
); | ||
|
||
useEffect(() => { | ||
if (collpaseNodeError) { | ||
addErrorMessage('An unexpected error has occurred, please refresh the page'); | ||
} | ||
}, [collpaseNodeError]); | ||
|
||
return { collapseExpandElement }; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023, 2024 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 | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
import { GQLMessage } from '../Tool.types'; | ||
|
||
export interface UseCollapseExpandElement { | ||
collapseExpandElement: (nodeId: string, collapsingState: GQLCollapsingState) => void; | ||
} | ||
|
||
export interface GQLUpdateCollapsingStateData { | ||
collapseExpandDiagramElement: GQLUpdateCollapsingStatePayload; | ||
} | ||
|
||
export interface GQLUpdateCollapsingStatePayload { | ||
__typename: string; | ||
} | ||
|
||
export interface GQLUpdateCollapsingStateVariables { | ||
input: GQLUpdateCollapsingStateInput; | ||
} | ||
|
||
export interface GQLUpdateCollapsingStateInput { | ||
id: string; | ||
editingContextId: string; | ||
representationId: string; | ||
diagramElementId: string; | ||
collapsingState: GQLCollapsingState; | ||
} | ||
|
||
export enum GQLCollapsingState { | ||
EXPANDED = 'EXPANDED', | ||
COLLAPSED = 'COLLAPSED', | ||
} | ||
|
||
export interface GQLErrorPayload extends GQLUpdateCollapsingStatePayload { | ||
message: string; | ||
messages: GQLMessage[]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023, 2024 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 | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
import { gql, useMutation } from '@apollo/client'; | ||
import { useMultiToast } from '@eclipse-sirius/sirius-components-core'; | ||
import { useCallback, useContext, useEffect } from 'react'; | ||
import { DiagramContext } from '../../contexts/DiagramContext'; | ||
import { DiagramContextValue } from '../../contexts/DiagramContext.types'; | ||
import { | ||
GQLDeleteFromDiagramData, | ||
GQLDeleteFromDiagramInput, | ||
GQLDeleteFromDiagramPayload, | ||
GQLDeleteFromDiagramSuccessPayload, | ||
GQLDeleteFromDiagramVariables, | ||
GQLDeletionPolicy, | ||
GQLErrorPayload, | ||
UseDiagramDeleteMutation, | ||
} from './useDiagramDeleteMutation.types'; | ||
|
||
export const deleteFromDiagramMutation = gql` | ||
mutation deleteFromDiagram($input: DeleteFromDiagramInput!) { | ||
deleteFromDiagram(input: $input) { | ||
__typename | ||
... on ErrorPayload { | ||
messages { | ||
body | ||
level | ||
} | ||
} | ||
... on DeleteFromDiagramSuccessPayload { | ||
messages { | ||
body | ||
level | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const isErrorPayload = (payload: GQLDeleteFromDiagramPayload): payload is GQLErrorPayload => | ||
payload.__typename === 'ErrorPayload'; | ||
|
||
const isSuccessPayload = (payload: GQLDeleteFromDiagramPayload): payload is GQLDeleteFromDiagramSuccessPayload => | ||
payload.__typename === 'DeleteFromDiagramSuccessPayload'; | ||
|
||
export const useDiagramDeleteMutation = (): UseDiagramDeleteMutation => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This hook should probably be named |
||
const { addMessages, addErrorMessage } = useMultiToast(); | ||
const { diagramId, editingContextId } = useContext<DiagramContextValue>(DiagramContext); | ||
|
||
const [deleteElementsMutation, { data: deleteElementsData, error: deleteElementsError }] = useMutation< | ||
GQLDeleteFromDiagramData, | ||
GQLDeleteFromDiagramVariables | ||
>(deleteFromDiagramMutation); | ||
|
||
const deleteElements = useCallback( | ||
async (nodeIds: string[], edgesId: string[], deletePocily: GQLDeletionPolicy) => { | ||
const input: GQLDeleteFromDiagramInput = { | ||
id: crypto.randomUUID(), | ||
editingContextId, | ||
representationId: diagramId, | ||
nodeIds: nodeIds, | ||
edgeIds: edgesId, | ||
deletionPolicy: deletePocily, | ||
}; | ||
|
||
const { data } = await deleteElementsMutation({ variables: { input } }); | ||
if (data) { | ||
const { deleteFromDiagram } = data; | ||
if (isErrorPayload(deleteFromDiagram) || isSuccessPayload(deleteFromDiagram)) { | ||
addMessages(deleteFromDiagram.messages); | ||
} | ||
} | ||
}, | ||
[editingContextId, diagramId, deleteElementsMutation] | ||
); | ||
Comment on lines
+64
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use async / await, we have tons of hooks using mutations, just do the same thing |
||
|
||
useEffect(() => { | ||
if (deleteElementsError) { | ||
addErrorMessage('An unexpected error has occurred, please refresh the page'); | ||
} | ||
if (deleteElementsData) { | ||
const { deleteFromDiagram } = deleteElementsData; | ||
if (isErrorPayload(deleteFromDiagram) || isSuccessPayload(deleteFromDiagram)) { | ||
addMessages(deleteFromDiagram.messages); | ||
} | ||
} | ||
}, [deleteElementsData, deleteElementsError]); | ||
|
||
return { deleteElements }; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this image coming from? Do not add coupling between unrelated modules. Is that coming from the diagram view DSL? If so,
sirius-components-collaborative-diagrams
can be reused with the view DSL so it cannot be referenced this way. Since I don't see any reference to this constant anywhere insirius-components-collaborative-diagrams
, you can remove it entirely