Skip to content

Commit

Permalink
front: editor: fixes #6439
Browse files Browse the repository at this point in the history
Details:
- Adds the required attribute to the composition code input so that it
  is red when empty
- Adds a canSave optional function to rangeEdition/tool-factory, to
  allow disabling the save button when state is not correct
- Uses the new canSave function in SpeedEditionTool to disable the save
  button when there are empty composition codes
  • Loading branch information
jacomyal committed Jan 25, 2024
1 parent 8349da9 commit e5390a5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const SpeedSectionMetadataForm: FC = () => {
<div key={key} className="form-group field field-string">
<div className="d-flex flex-row align-items-center">
<input
required
className="form-control flex-grow-2 flex-shrink-1 mr-2 px-2"
placeholder=""
type="text"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ interface RangeEditionToolParams<T extends EditorRange> {
messagesComponent: ComponentType;
layersComponent: ComponentType<{ map: Map }>;
leftPanelComponent: ComponentType;
canSave?: (state: RangeEditionState<T>) => boolean;
}

function getRangeEditionTool<T extends EditorRange>({
Expand All @@ -57,6 +58,7 @@ function getRangeEditionTool<T extends EditorRange>({
messagesComponent,
layersComponent,
leftPanelComponent,
canSave,
}: RangeEditionToolParams<T>): Tool<RangeEditionState<T>> {
const layersEntity = getNewEntity();
function getInitialState(): RangeEditionState<T> {
Expand Down Expand Up @@ -91,8 +93,10 @@ function getRangeEditionTool<T extends EditorRange>({
id: `save-${objectTypeAction}`,
icon: AiFillSave,
labelTranslationKey: `Editor.tools.${objectTypeEdition}-edition.actions.save-${objectTypeAction}`,
isDisabled({ isLoading }) {
return isLoading || false;
isDisabled({ isLoading, state }) {
if (isLoading) return true;
if (canSave) return !canSave(state);
return false;
},
async onClick({ state, setState, dispatch, infraID }) {
const { initialEntity, entity } = state;
Expand Down
5 changes: 5 additions & 0 deletions front/src/applications/editor/tools/rangeEdition/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const SpeedEditionTool = getRangeEditionTool<SpeedSectionEntity | SpeedSe
messagesComponent: SpeedSectionMessages,
layersComponent: SpeedSectionEditionLayers,
leftPanelComponent: RangeEditionLeftPanel,
canSave(state) {
const records = state.entity.properties.speed_limit_by_tag || {};
const compositionCodes = Object.keys(records);
return compositionCodes.every((code) => !!code);
},
});

export const ElectrificationEditionTool = getRangeEditionTool<ElectrificationEntity>({
Expand Down

0 comments on commit e5390a5

Please sign in to comment.