import { useContext, useState } from "react";
import { Text, TouchableHighlight, View } from "react-native";
import MyButton from "../../../../../Components/Button";
import { useTranslation } from "react-i18next";
import {
AppContext,
AppSelectedUserDevice,
Constants,
GetDevice,
ModalContainer,
NewAction,
} from "../../../../../utils";
function Layer({ number, selected, onPress }) {
return (
{number}
);
}
export default function LayerSelectionModalContent({ navigation, route }) {
const appContext = useContext(AppContext);
const { t } = useTranslation();
const { actionId, selectedLayers } = route.params;
// The selected layers will be in the params if the user is changing the layers of an existing action
const [selectedLayer, setSelectedLayer] = useState(
selectedLayers === undefined ? [] : selectedLayers
);
const handleSelectLayerClick = (layerNumber) => {
if (selectedLayer.includes(layerNumber)) {
setSelectedLayer((sLayer) =>
sLayer.filter((item) => item != layerNumber)
);
} else {
setSelectedLayer((sLayer) => [...sLayer, layerNumber]);
}
};
return (
{Array.from({ length: 4 }).map((_, i) => (
handleSelectLayerClick(i + 1)}
/>
))}
{
// The layers will be sorted in ascending order -> 1, 2, 3, 4
const sortedSelectedLayers = selectedLayer.sort((a, b) => a - b);
// New action will be created
if (actionId === undefined) {
const newAction = NewAction(
GetDevice(appContext.devices).selectedScene,
Constants.actionType.layers,
{ layers: sortedSelectedLayers }
);
appContext.setDeviceSceneActions((arr) => [...arr, newAction]);
route.params["action"] = newAction;
navigation.navigate(AppSelectedUserDevice.current.routeName);
navigation.navigate("modalLightsEditAction", {
...route.params,
actionType: Constants.actionType.layers,
});
} else {
// user is editing the layers of an existing action
appContext.setDeviceSceneActions((arr) => {
const newArr = [...arr];
const actionIndex = newArr.findIndex(
(item) => item.actionId === actionId
);
if (actionIndex !== -1) {
newArr[actionIndex].modeAdjustments.layers =
sortedSelectedLayers;
}
return newArr;
});
navigation.goBack();
}
}}
/>
);
}