Skip to content

Commit

Permalink
Improve edit list functionality (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
synnestokkevaag authored Jan 16, 2025
1 parent 73768ec commit 7cc4430
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 42 deletions.
30 changes: 5 additions & 25 deletions plugins/ros/src/components/scenarioTable/ScenarioTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { useScenario } from '../../contexts/ScenarioContext';
import { RiSc, RiScWithMetadata } from '../../utils/types';
import { useFontStyles } from '../../utils/style';
import { useRiScs } from '../../contexts/RiScContext';
import { arrayNotEquals } from '../../utils/utilityfunctions';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { CheckCircle, Edit } from '@mui/icons-material';
Expand All @@ -33,7 +32,6 @@ export const ScenarioTable = ({ riSc }: ScenarioTableProps) => {
const { openNewScenarioWizard, openScenarioDrawer } = useScenario();
const [tempScenarios, setTempScenarios] = useState(riSc.scenarios);
const { updateRiSc, updateStatus } = useRiScs();
const [isOrderChanged, setIsOrderChanged] = useState(false);

useEffect(() => {
if (!updateStatus.isSuccess) {
Expand All @@ -59,24 +57,13 @@ export const ScenarioTable = ({ riSc }: ScenarioTableProps) => {
const [removed] = updatedScenarios.splice(dragIndex, 1);
updatedScenarios.splice(hoverIndex, 0, removed);
setTempScenarios(updatedScenarios);
const updatedOrder = updatedScenarios.map(scenario => scenario.ID);
setIsOrderChanged(
arrayNotEquals(
riSc.scenarios.map(scenario => scenario.ID),
updatedOrder,
),
);
};

const saveOrder = () => {
const updatedRiSc = {
...riSc,
scenarios: tempScenarios,
scenarios: updatedScenarios,
};

updateRiSc(updatedRiSc, () => {
setIsOrderChanged(false);
});
updateRiSc(updatedRiSc, () => {});
};

const [isEditing, setIsEditing] = useState<boolean>(false);
Expand Down Expand Up @@ -110,16 +97,9 @@ export const ScenarioTable = ({ riSc }: ScenarioTableProps) => {
startIcon={isEditing ? <CheckCircle /> : <Edit />}
variant="text"
color="primary"
onClick={() => {
if (isEditing && isOrderChanged) {
saveOrder();
setIsEditing(false);
} else if (isEditing && !isOrderChanged) {
setIsEditing(false);
} else {
setIsEditing(true);
}
}}
onClick={() =>
isEditing ? setIsEditing(false) : setIsEditing(true)
}
>
{isEditing
? t('scenarioTable.doneEditing')
Expand Down
21 changes: 7 additions & 14 deletions plugins/ros/src/components/scenarioTable/ScenarioTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,22 @@ export const ScenarioTableRow = ({
const [, drop] = useDrop({
accept: 'row',
hover(item: { index: number }, monitor) {
if (!ref.current) {
return;
}
if (!ref.current) return;

const dragIndex = item.index;
const hoverIndex = index;

if (dragIndex === hoverIndex) {
return;
}
if (dragIndex === hoverIndex) return;

const hoverBoundingRect = ref.current?.getBoundingClientRect();
const hoverMiddleY =
(hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
const clientOffset = monitor.getClientOffset();
const hoverClientY = clientOffset!.y - hoverBoundingRect.top;

if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
return;
}

if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
return;
}
const isMovingDown =
dragIndex < hoverIndex && hoverClientY < hoverMiddleY;
const isMovingUp = dragIndex > hoverIndex && hoverClientY > hoverMiddleY;
if (isMovingDown || isMovingUp) return;

moveRow(dragIndex, hoverIndex);
item.index = hoverIndex;
Expand Down
6 changes: 3 additions & 3 deletions plugins/ros/src/utils/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ export const pluginRiScTranslationRef = createTranslationRef({
probabilityChar: 'P',
completed: 'complete',
},
editButton: 'Edit scenario list',
doneEditing: 'Save editing',
editButton: 'Edit list',
doneEditing: 'Finish editing',
},
riskMatrix: {
title: 'Risk matrix', // Risk matrix or Risk overview
Expand Down Expand Up @@ -557,7 +557,7 @@ export const pluginRiScNorwegianTranslation = createTranslationResource({
'scenarioTable.columns.probabilityChar': 'S',
'scenarioTable.columns.completed': 'fullført',
'scenarioTable.editButton': 'Rediger liste',
'scenarioTable.doneEditing': 'Lagre endringer',
'scenarioTable.doneEditing': 'Avslutt redigering',

'riskMatrix.title': 'Risikomatrise',
'riskMatrix.estimatedRisk.title': 'Estimert risiko',
Expand Down

0 comments on commit 7cc4430

Please sign in to comment.