-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[4348] Move quick tools declaration on the backend
Bug: #4348 Signed-off-by: Michaël Charfadi <michael.charfadi@obeosoft.com>
- Loading branch information
Showing
16 changed files
with
782 additions
and
463 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...ms/frontend/sirius-components-diagrams/src/renderer/collapse/useCollapseExpandElement.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
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); | ||
} | ||
} | ||
}, | ||
[editingContextId, diagramId, collapseExpandMutation] | ||
); | ||
|
||
useEffect(() => { | ||
if (collpaseNodeError) { | ||
addErrorMessage('An unexpected error has occurred, please refresh the page'); | ||
} | ||
}, [collpaseNodeError]); | ||
|
||
return { collapseExpandElement }; | ||
}; |
47 changes: 47 additions & 0 deletions
47
...ontend/sirius-components-diagrams/src/renderer/collapse/useCollapseExpandElement.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...rams/frontend/sirius-components-diagrams/src/renderer/delete/useDiagramDeleteMutation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => { | ||
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] | ||
); | ||
|
||
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 }; | ||
}; |
Oops, something went wrong.