Skip to content

Commit

Permalink
revised getConversionCount() and corrected checkState() if source is …
Browse files Browse the repository at this point in the history
…meter
  • Loading branch information
danielshid committed Dec 7, 2024
1 parent 42d8616 commit 8708754
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,41 +63,55 @@ export default function EditConversionModalComponent(props: EditConversionModalC
};
/* End State */

/**
* Calculates the number of conversions that use a given unit as a source or destination (not both).
* @param unit The unit that is used for calculating the number of conversions.
* @param conversions An array of conversion data objects, each representing a conversion
* @returns The sum of conversions that use the provided unit.
*/
const getConversionCount = (unit: UnitData, conversions: ConversionData[]) => {
let count = 0;
if (unit === unitDataById[state.sourceId]) {
for (const conversion of Object.values(conversions)) {
if (conversion.sourceId === unit.id) {
const unitId = unit.id;
// If the given unit is a source only count conversions that share the same source.
if (unitId === state.sourceId) {
for (const conversion of conversions) {
if (conversion.sourceId === unitId) {
count++;
}
}
} else if (unit === unitDataById[state.destinationId]) {
for (const conversion of Object.values(conversions)) {
if (conversion.destinationId === unit.id) {
// If the given unit is a destination only count conversions that share the same destination.
} else if (unitId === state.destinationId) {
for (const conversion of conversions) {
if (conversion.destinationId === unitId) {
count++;
}
}
}
return count;
};

// Performs checks to warn the admin of the impact deleting a conversion will have on meter units and possible graphing units.
const checkState = () => {
const source = unitDataById[state.sourceId];
const dest = unitDataById[state.destinationId];
let msg = '';
let cancel = false;
if (source.typeOfUnit === UnitType.meter) {
const srcCount = getConversionCount(source, conversionDetails);
const relatedMeters = Object.values(meterDataById).filter(meter => meter.unitId === source.id);
if (srcCount === 1) {
msg += `${translate('conversion.delete.meter.orphan')} "${unitDataById[state.destinationId].name}".\n`;
msg += `${translate('conversion.delete.meter.orphan')} "${dest.name}".\n`;
msg += `${translate('conversion.delete.meter.related')} "${source.name}":\n`;
relatedMeters.forEach(meter => {
msg += `"${meter.name}"\n`;
});
cancel = true;
} else {
msg += `${translate('conversion.delete.meter.ungraphable')}\n`;
for (const meterId of Object.values(meterDataById)) {
if (meterId.unitId === source.id) {
msg += `"${meterId.name}"\n`;
cancel = true;
}
}
msg += `${translate('conversion.delete.meter.related')} "${source.name}":\n`;
relatedMeters.forEach(meter => {
msg += `"${meter.name}"\n`;
});
msg += `${translate('conversion.delete.meter.ungraphable')} "${source.name}".\n`;
}
} else if (source.typeOfUnit === UnitType.suffix) {
const srcCount = getConversionCount(source, conversionDetails);
Expand All @@ -107,16 +121,18 @@ export default function EditConversionModalComponent(props: EditConversionModalC
} else if (source.typeOfUnit === UnitType.unit && dest.typeOfUnit === UnitType.unit) {
const destCount = getConversionCount(dest, conversionDetails);
if (destCount === 1) {
msg += `${translate('conversion.delete.unit.orphan')} "${unitDataById[state.destinationId].name}".\n`;
msg += `${translate('conversion.delete.unit.orphan')} "${dest.name}".\n`;
}
if (state.bidirectional) {
const srcCount = getConversionCount(source, conversionDetails);
if (srcCount === 1) {
msg += `${translate('conversion.delete.unit.orphan')} "${unitDataById[state.destinationId].name}".\n`;
msg += `${translate('conversion.delete.unit.orphan')} "${dest.name}".\n`;
}
}
if (msg === '') {
msg += `${translate('conversion.delete.unit')}\n`;
// TODO: Check after deleting the conversion to see if a change happens.
// Notify the admin of any consequences caused by deleting the conversion.
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/app/styles/modal.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ select option[value="true"] {

.confirmation-message {
white-space: pre-wrap;
}
}
9 changes: 6 additions & 3 deletions src/client/app/translations/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ const LocaleTranslationData = {
"conversion.create.source.destination.same": "The source and destination cannot be the same for a conversion",
"conversion.delete.conversion": "Delete Conversion",
"conversion.delete.meter.orphan": "Deleting this meter conversion will orphan meter",
"conversion.delete.meter.ungraphable": "Deleting this meter conversion will make the following meter(s) ungraphable:",
"conversion.delete.meter.related": "The following meters use source unit",
"conversion.delete.meter.ungraphable": "Deleting this meter conversion will reduce the number of graphable units for meter",
"conversion.delete.restricted": "This conversion cannot be deleted until the significant consequences of doing this are addressed.",
"conversion.delete.suffix.disable": "Deleting this suffix conversion will disable use of suffix",
"conversion.delete.unit" : "Deleting this conversion between two units of type unit may reduce the possible graphing units for some meter(s) but cannot be known until after the deletion happens.",
Expand Down Expand Up @@ -592,7 +593,8 @@ const LocaleTranslationData = {
"conversion.create.source.destination.same": "The source and destination cannot be the same for a conversion\u{26A1}",
"conversion.delete.conversion": "Delete Conversion\u{26A1}",
"conversion.delete.meter.orphan": "Deleting this meter conversion will orphan meter\u{26A1}",
"conversion.delete.meter.ungraphable": "Deleting this meter conversion will make the following meter(s) ungraphable:\u{26A1}",
"conversion.delete.meter.related": "The following meters use source unit\u{26A1}",
"conversion.delete.meter.ungraphable": "Deleting this meter conversion will reduce the number of graphable units for meter\u{26A1}",
"conversion.delete.restricted": "This conversion cannot be deleted until the significant consequences of doing this are addressed.\u{26A1}",
"conversion.delete.suffix.disable": "Deleting this suffix conversion will disable use of suffix\u{26A1}",
"conversion.delete.unit" : "Deleting this conversion between two units of type unit may reduce the possible graphing units for some meter(s) but cannot be known until after the deletion happens.\u{26A1}",
Expand Down Expand Up @@ -1120,7 +1122,8 @@ const LocaleTranslationData = {
"conversion.create.source.destination.same": "La fuente y destinación no pueden ser la misma para una conversión",
"conversion.delete.conversion": "Eliminar conversión",
"conversion.delete.meter.orphan": "Deleting this meter conversion will orphan meter\u{26A1}",
"conversion.delete.meter.ungraphable": "Deleting this meter conversion will make the following meter(s) ungraphable:\u{26A1}",
"conversion.delete.meter.related": "The following meters use source unit\u{26A1}",
"conversion.delete.meter.ungraphable": "Deleting this meter conversion will reduce the number of graphable units for meter\u{26A1}",
"conversion.delete.restricted": "This conversion cannot be deleted until the significant consequences of doing this are addressed.\u{26A1}",
"conversion.delete.suffix.disable": "Deleting this suffix conversion will disable use of suffix\u{26A1}",
"conversion.delete.unit" : "Deleting this conversion between two units of type unit may reduce the possible graphing units for some meter(s) but cannot be known until after the deletion happens.\u{26A1}",
Expand Down

0 comments on commit 8708754

Please sign in to comment.