Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/lib/sensoricons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ function getIcon(iconName: string) {
return <Icon className={sensorIconClassName} />
}

function assignIcon(sensorType: string, sensorTitle: string) {
const normalizedSensorType = sensorType.toLowerCase()
const normalizedSensorTitle = sensorTitle.toLowerCase()
// args should accept null values for drafted / undefined sensors while editing
function assignIcon(sensorType?: string | null, sensorTitle?: string | null) {
const normalizedSensorType = sensorType?.toLowerCase() ?? ''
const normalizedSensorTitle = sensorTitle?.toLowerCase() ?? ''

if (
normalizedSensorTitle.includes('luftfeuchte') ||
Expand Down
32 changes: 25 additions & 7 deletions app/routes/device.$deviceId.edit.sensors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,19 @@ export async function action({ request, params }: Route.ActionArgs) {

const updatedSensorsDataJson = JSON.parse(updatedSensorsData)

for (const [index, sensor] of updatedSensorsDataJson.entries()) {
let persistedOrder = 0

for (const sensor of updatedSensorsDataJson) {
if (sensor?.new === true && sensor?.edited === true) {
await addNewSensor({
title: sensor.title,
unit: sensor.unit,
sensorType: sensor.sensorType,
icon: sensor.icon,
deviceId,
order: index,
order: persistedOrder,
})
persistedOrder++
} else if (sensor?.deleted === true) {
await deleteSensor(sensor.id)
} else if (!sensor?.new) {
Expand All @@ -95,8 +98,9 @@ export async function action({ request, params }: Route.ActionArgs) {
unit: sensor.unit,
sensorType: sensor.sensorType,
icon: sensor.icon,
order: index,
order: persistedOrder,
})
persistedOrder++
}
}

Expand Down Expand Up @@ -137,6 +141,16 @@ export default function EditBoxSensors() {
//* to view toast on edit-page
const [setToastOpen] = useOutletContext<[(_open: boolean) => void]>()

// Need to look up original sensor id in case a new sensor is prepended
// in edit mode and the operation is cancelled
const getOriginalSensor = React.useCallback(
(sensor: any, index: number) =>
sensor?.id
? data.find((item: any) => item.id === sensor.id)
: data[index],
[data],
)

React.useEffect(() => {
//* if sensors data were updated successfully
if (actionData && actionData?.isUpdated) {
Expand Down Expand Up @@ -209,7 +223,6 @@ export default function EditBoxSensors() {
variant="outline"
onClick={() => {
setSensorsData([
...sensorsData,
{
title: undefined,
unit: undefined,
Expand All @@ -218,6 +231,7 @@ export default function EditBoxSensors() {
new: true,
notValidInput: true,
},
...sensorsData,
])
}}
className="gap-2"
Expand Down Expand Up @@ -576,10 +590,14 @@ export default function EditBoxSensors() {
if (sensor?.new) {
sensorsData.splice(index, 1)
} else {
const originalSensor = getOriginalSensor(
sensor,
index,
)
sensor.editing = false
sensor.title = data[index].title
sensor.unit = data[index].unit
sensor.sensorType = data[index].sensorType
sensor.title = originalSensor?.title
sensor.unit = originalSensor?.unit
sensor.sensorType = originalSensor?.sensorType
}
}}
>
Expand Down
Loading