2022-12-10 15:00:16 +01:00
|
|
|
import { Button, Flex, Grid, NumberInput } from '@mantine/core';
|
2022-12-08 22:17:33 +01:00
|
|
|
import { useForm } from '@mantine/form';
|
2022-12-10 15:00:16 +01:00
|
|
|
import { closeModal, ContextModalProps } from '@mantine/modals';
|
|
|
|
|
import { useConfigContext } from '../../../../config/provider';
|
|
|
|
|
import { useConfigStore } from '../../../../config/store';
|
|
|
|
|
import { ServiceType } from '../../../../types/service';
|
2022-12-08 22:17:33 +01:00
|
|
|
import { TileBaseType } from '../../../../types/tile';
|
|
|
|
|
|
|
|
|
|
export const ChangePositionModal = ({
|
|
|
|
|
context,
|
|
|
|
|
id,
|
|
|
|
|
innerProps,
|
|
|
|
|
}: ContextModalProps<{ type: 'service' | 'type'; tile: TileBaseType }>) => {
|
2022-12-10 15:00:16 +01:00
|
|
|
const updateConfig = useConfigStore((x) => x.updateConfig);
|
|
|
|
|
const { name: configName } = useConfigContext();
|
|
|
|
|
|
2022-12-08 22:17:33 +01:00
|
|
|
const form = useForm({
|
|
|
|
|
initialValues: {
|
2022-12-10 15:00:16 +01:00
|
|
|
tile: innerProps.tile,
|
2022-12-08 22:17:33 +01:00
|
|
|
},
|
2022-12-10 15:00:16 +01:00
|
|
|
validateInputOnChange: true,
|
|
|
|
|
validateInputOnBlur: true,
|
2022-12-08 22:17:33 +01:00
|
|
|
});
|
|
|
|
|
|
2022-12-10 15:00:16 +01:00
|
|
|
const onSubmit = () => {
|
|
|
|
|
if (!configName) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tileAsService = form.values.tile as ServiceType;
|
|
|
|
|
|
|
|
|
|
updateConfig(configName, (previous) => ({
|
|
|
|
|
...previous,
|
|
|
|
|
services: [...previous.services.filter((x) => x.id === tileAsService.id), tileAsService],
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-08 22:17:33 +01:00
|
|
|
return (
|
2022-12-10 15:00:16 +01:00
|
|
|
<form onSubmit={form.onSubmit(onSubmit)}>
|
2022-12-08 22:17:33 +01:00
|
|
|
<Grid>
|
|
|
|
|
<Grid.Col xs={12} md={6}>
|
2022-12-10 15:00:16 +01:00
|
|
|
<NumberInput
|
|
|
|
|
max={99}
|
|
|
|
|
min={0}
|
|
|
|
|
label="X Position"
|
|
|
|
|
description="0 or higher"
|
|
|
|
|
{...form.getInputProps('tile.shape.location.x')}
|
|
|
|
|
/>
|
2022-12-08 22:17:33 +01:00
|
|
|
</Grid.Col>
|
|
|
|
|
|
|
|
|
|
<Grid.Col xs={12} md={6}>
|
2022-12-10 15:00:16 +01:00
|
|
|
<NumberInput
|
|
|
|
|
max={99}
|
|
|
|
|
min={0}
|
|
|
|
|
label="Y Position"
|
|
|
|
|
description="0 or higher"
|
|
|
|
|
{...form.getInputProps('tile.shape.location.y')}
|
|
|
|
|
/>
|
2022-12-08 22:17:33 +01:00
|
|
|
</Grid.Col>
|
|
|
|
|
</Grid>
|
2022-12-10 15:00:16 +01:00
|
|
|
|
|
|
|
|
<Grid>
|
|
|
|
|
<Grid.Col xs={12} md={6}>
|
|
|
|
|
<NumberInput
|
|
|
|
|
max={24}
|
|
|
|
|
min={1}
|
|
|
|
|
label="Width"
|
|
|
|
|
description="Between 1 and 24"
|
|
|
|
|
{...form.getInputProps('tile.shape.size.width')}
|
|
|
|
|
/>
|
|
|
|
|
</Grid.Col>
|
|
|
|
|
|
|
|
|
|
<Grid.Col xs={12} md={6}>
|
|
|
|
|
<NumberInput
|
|
|
|
|
max={24}
|
|
|
|
|
min={1}
|
|
|
|
|
label="Height"
|
|
|
|
|
description="Between 1 and 24"
|
|
|
|
|
{...form.getInputProps('tile.shape.size.height')}
|
|
|
|
|
/>
|
|
|
|
|
</Grid.Col>
|
|
|
|
|
</Grid>
|
|
|
|
|
|
|
|
|
|
<Flex justify="end" gap="sm" mt="md">
|
|
|
|
|
<Button onClick={() => closeModal(id)} variant="light" color="gray">
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
<Button type="submit">Change Position</Button>
|
|
|
|
|
</Flex>
|
|
|
|
|
</form>
|
2022-12-08 22:17:33 +01:00
|
|
|
);
|
|
|
|
|
};
|