-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathuseInsertTextualSysMLv2.ts
More file actions
85 lines (76 loc) · 2.54 KB
/
useInsertTextualSysMLv2.ts
File metadata and controls
85 lines (76 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*******************************************************************************
* Copyright (c) 2025, 2026 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 { GQLMessage, useMultiToast } from '@eclipse-sirius/sirius-components-core';
import { useEffect } from 'react';
import {
GQLErrorPayload,
GQLInsertTextualSysMLv2MutationData,
GQLInsertTextualSysMLv2Payload,
UseInsertTextualSysMLv2Value,
} from './useInsertTextualSysMLv2.types';
const insertTextualSysMLv2Mutation = gql`
mutation insertTextualSysMLv2($input: InsertTextualSysMLv2Input!) {
insertTextualSysMLv2(input: $input) {
__typename
... on ErrorPayload {
messages {
body
level
}
}
... on SuccessPayload {
messages {
body
level
}
}
}
}
`;
const isErrorPayload = (payload: GQLInsertTextualSysMLv2Payload): payload is GQLErrorPayload =>
payload.__typename === 'ErrorPayload';
export const useInsertTextualSysMLv2 = (): UseInsertTextualSysMLv2Value => {
const { addErrorMessage, addMessages } = useMultiToast();
const [performInsertTextualSysMLv2, { loading, data, error }] =
useMutation<GQLInsertTextualSysMLv2MutationData>(insertTextualSysMLv2Mutation);
useEffect(() => {
if (error) {
addErrorMessage('An unexpected error has occurred, please refresh the page');
}
if (data) {
const { insertTextualSysMLv2 } = data;
if (isErrorPayload(insertTextualSysMLv2)) {
const { messages } = insertTextualSysMLv2;
addMessages(messages);
}
}
}, [data, error]);
const insertTextualSysMLv2 = (editingContextId: string, objectId: string, textualContent: string) => {
const input = {
id: crypto.randomUUID(),
editingContextId,
objectId,
textualContent,
};
performInsertTextualSysMLv2({ variables: { input } });
};
const textualSysMLv2Inserted: boolean = data?.insertTextualSysMLv2.__typename === 'SuccessPayload';
const messages: GQLMessage[] = data?.insertTextualSysMLv2.messages || [];
return {
insertTextualSysMLv2,
loading,
textualSysMLv2Inserted,
messages,
};
};