Skip to content

Commit

Permalink
[3758] Store property and form payload in a context
Browse files Browse the repository at this point in the history
Bug: #3758
Signed-off-by: Michaël Charfadi <[email protected]>
  • Loading branch information
mcharfadi committed Jul 25, 2024
1 parent 9c5941d commit ca8343f
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ As a result, the following maven modules have been deleted: `sirius-web-sample-a
=== New Features

- https://github.com/eclipse-sirius/sirius-web/issues/3748[#3748] [diagram] Store current diagram payload in `DiagramContext`

- https://github.com/eclipse-sirius/sirius-web/issues/3758[#3578] [form] Store property and form payload in a context

=== Improvements

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 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 React from 'react';
import { FormContextValue } from './FormContext.types';

const value: FormContextValue = {
formPayload: null,
};

export const FormContext = React.createContext<FormContextValue>(value);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright (c) 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 { GQLFormEventPayload } from '../form/FormEventFragments.types';

export interface FormContextValue {
formPayload: GQLFormEventPayload | null;
}
3 changes: 3 additions & 0 deletions packages/forms/frontend/sirius-components-forms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
export { FormContext } from './contexts/FormContext';
export type { FormContextValue } from './contexts/FormContext.types';
export * from './form/Form.types';
export * from './form/FormEventFragments';
export type * from './form/FormEventFragments.types';
Expand All @@ -28,6 +30,7 @@ export * from './propertysections/ListPropertySection';
export type { ListStyleProps } from './propertysections/ListPropertySection.types';
export type { MultiSelectStyleProps } from './propertysections/MultiSelectPropertySection.types';
export * from './propertysections/PropertySectionLabel';
export type { PropertySectionLabelDecoratorProps } from './propertysections/PropertySectionLabel.types';
export * from './propertysections/PropertySectionLabelExtensionPoints';
export type { RadioStyleProps } from './propertysections/RadioPropertySection.types';
export type { SelectStyleProps } from './propertysections/SelectPropertySection.types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import { RepresentationComponentProps, Toast } from '@eclipse-sirius/sirius-comp
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import { useMachine } from '@xstate/react';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import { FormContext } from '../contexts/FormContext';
import { Form } from '../form/Form';
import { formRefreshedEventPayloadFragment } from '../form/FormEventFragments';
import { GQLFormEventSubscription } from '../form/FormEventFragments.types';
import { Page } from '../pages/Page';
import { ToolbarAction } from '../toolbaraction/ToolbarAction';
import { FormRepresentationState } from './FormRepresentation.types';
import {
FormRepresentationContext,
FormRepresentationEvent,
Expand Down Expand Up @@ -89,6 +91,9 @@ export const FormRepresentation = ({ editingContextId, representationId, readOnl
);
const { toast, formRepresentation } = value as SchemaValue;
const { id, formId, form, message } = context;
const [state, setState] = useState<FormRepresentationState>({
payload: null,
});

/**
* Displays an other form if the selection indicates that we should display another properties view.
Expand All @@ -110,6 +115,10 @@ export const FormRepresentation = ({ editingContextId, representationId, readOnl
},
fetchPolicy: 'no-cache',
onData: ({ data }) => {
if (data.data) {
const { formEvent } = data.data;
setState((prevState) => ({ ...prevState, payload: formEvent }));
}
const handleDataEvent: HandleSubscriptionResultEvent = {
type: 'HANDLE_SUBSCRIPTION_RESULT',
result: data,
Expand Down Expand Up @@ -171,12 +180,17 @@ export const FormRepresentation = ({ editingContextId, representationId, readOnl

return (
<div data-representation-kind="form">
{content}
<Toast
message={message}
open={toast === 'visible'}
onClose={() => dispatch({ type: 'HIDE_TOAST' } as HideToastEvent)}
/>
<FormContext.Provider
value={{
formPayload: state.payload,
}}>
{content}
<Toast
message={message}
open={toast === 'visible'}
onClose={() => dispatch({ type: 'HIDE_TOAST' } as HideToastEvent)}
/>
</FormContext.Provider>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*******************************************************************************
* Copyright (c) 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 { GQLFormEventPayload } from '../form/FormEventFragments.types';

export interface FormRepresentationState {
payload: GQLFormEventPayload | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export { editProjectNavbarSubtitleExtensionPoint } from './views/edit-project/Ed
export { useCurrentProject } from './views/edit-project/useCurrentProject';
export type { UseCurrentProjectValue } from './views/edit-project/useCurrentProject.types';
export type { GQLProject } from './views/edit-project/useProjectAndRepresentationMetadata.types';
export { DetailsContext } from './views/edit-project/workbench-views/DetailContext';
export type { DetailsContextValue } from './views/edit-project/workbench-views/DetailsContext.types';
export { DetailsView } from './views/edit-project/workbench-views/DetailsView';
export type { GQLDetailsEventPayload } from './views/edit-project/workbench-views/useDetailsViewSubscription.types';
export { type CreateProjectAreaCardProps } from './views/project-browser/create-projects-area/CreateProjectArea.types';
export { createProjectAreaCardExtensionPoint } from './views/project-browser/create-projects-area/CreateProjectAreaExtensionPoints';
export { projectActionButtonMenuItemExtensionPoint } from './views/project-browser/list-projects-area/ProjectActionButtonExtensionPoints';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 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 React from 'react';
import { DetailsContextValue } from './DetailsContext.types';

const value: DetailsContextValue = {
detailsPayload: null,
};

export const DetailsContext = React.createContext<DetailsContextValue>(value);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright (c) 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 { GQLDetailsEventPayload } from './useDetailsViewSubscription.types';

export interface DetailsContextValue {
detailsPayload: GQLDetailsEventPayload | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { FormBasedView } from '@eclipse-sirius/sirius-components-forms';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import { useEffect, useState } from 'react';
import { DetailsContext } from './DetailContext';
import { DetailsViewState } from './DetailsView.types';
import { useDetailsViewSubscription } from './useDetailsViewSubscription';

Expand Down Expand Up @@ -52,7 +53,7 @@ export const DetailsView = ({ editingContextId, readOnly }: WorkbenchViewCompone

const objectIds: string[] = state.currentSelection.entries.map((entry) => entry.id);
const skip = objectIds.length === 0;
const { form } = useDetailsViewSubscription(editingContextId, objectIds, skip);
const { form, payload } = useDetailsViewSubscription(editingContextId, objectIds, skip);

const classes = useDetailsViewStyles();

Expand All @@ -63,5 +64,14 @@ export const DetailsView = ({ editingContextId, readOnly }: WorkbenchViewCompone
</div>
);
}
return <FormBasedView editingContextId={editingContextId} form={form} readOnly={readOnly} />;
return (
<div data-representation-kind="form-details">
<DetailsContext.Provider
value={{
detailsPayload: payload,
}}>
<FormBasedView editingContextId={editingContextId} form={form} readOnly={readOnly} />
</DetailsContext.Provider>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const useDetailsViewSubscription = (
id: crypto.randomUUID(),
form: null,
complete: false,
payload: null,
});

const input: GQLDetailsEventInput = {
Expand All @@ -63,6 +64,7 @@ export const useDetailsViewSubscription = (
const { data: gqlDetailsEventSubscription } = data;
if (gqlDetailsEventSubscription) {
const { detailsEvent: payload } = gqlDetailsEventSubscription;
setState((prevState) => ({ ...prevState, payload }));
if (isFormRefreshedEventPayload(payload)) {
const { form } = payload;
setState((prevState) => ({ ...prevState, form }));
Expand Down Expand Up @@ -93,6 +95,7 @@ export const useDetailsViewSubscription = (
return {
loading,
form: state.form,
payload: state.payload,
complete: state.complete,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import { GQLForm } from '@eclipse-sirius/sirius-components-forms';
export interface UseDetailsViewSubscriptionValue {
loading: boolean;
form: GQLForm | null;
payload: GQLDetailsEventPayload | null;
complete: boolean;
}

export interface UseDetailsViewSubscriptionState {
id: string;
form: GQLForm | null;
complete: boolean;
payload: GQLDetailsEventPayload | null;
}

export interface GQLDetailsEventInput {
Expand Down

0 comments on commit ca8343f

Please sign in to comment.