Working sensor modal

This commit is contained in:
Jan Zípek 2022-08-28 12:55:49 +02:00
parent 0e202c0850
commit 8758d7b64e
4 changed files with 25 additions and 12 deletions

View File

@ -253,6 +253,7 @@ section.content {
.settings-modal .actions { .settings-modal .actions {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: flex-end;
} }
.settings-modal .actions .remove { .settings-modal .actions .remove {

View File

@ -33,7 +33,7 @@ export const SensorsPage = () => {
<div className="actions"></div> <div className="actions"></div>
</div> </div>
{sensors.data?.map((i) => ( {sensors.data?.map((i) => (
<SensorItem key={i.id} sensor={i} /> <SensorItem key={i.id} sensor={i} onEdit={setEdited} />
))} ))}
</div> </div>
</div> </div>

View File

@ -1,7 +1,7 @@
import { createSensor, SensorInfo, updateSensor } from '@/api/sensors' import { createSensor, SensorInfo, updateSensor } from '@/api/sensors'
import { Modal } from '@/components/Modal' import { Modal } from '@/components/Modal'
import { useState } from 'preact/hooks' import { useState } from 'preact/hooks'
import { useMutation } from 'react-query' import { useMutation, useQueryClient } from 'react-query'
type Props = { type Props = {
open: boolean open: boolean
@ -10,24 +10,31 @@ type Props = {
} }
export const SensorFormModal = ({ open, onClose, sensor }: Props) => { export const SensorFormModal = ({ open, onClose, sensor }: Props) => {
const queryClient = useQueryClient()
const createMutation = useMutation(createSensor)
const updateMutation = useMutation(updateSensor)
const [formState, setFormState] = useState(() => ({ const [formState, setFormState] = useState(() => ({
name: sensor?.name ?? '', name: sensor?.name ?? '',
})) }))
const createMutation = useMutation(createSensor)
const updateMutation = useMutation(updateSensor)
const handleSave = async (e: Event) => { const handleSave = async (e: Event) => {
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
onClose() if (isLoading) {
return
}
if (sensor) { if (sensor) {
updateMutation.mutate({ id: sensor.id, name: formState.name }) await updateMutation.mutateAsync({ id: sensor.id, name: formState.name })
} else { } else {
createMutation.mutate(formState.name) await createMutation.mutateAsync(formState.name)
} }
queryClient.invalidateQueries(['/sensors'])
onClose()
} }
const handleChange = (e: Event) => { const handleChange = (e: Event) => {
@ -39,6 +46,8 @@ export const SensorFormModal = ({ open, onClose, sensor }: Props) => {
}) })
} }
const isLoading = createMutation.isLoading || updateMutation.isLoading
return ( return (
<Modal onClose={onClose} open={open}> <Modal onClose={onClose} open={open}>
<form onSubmit={handleSave}> <form onSubmit={handleSave}>
@ -50,14 +59,16 @@ export const SensorFormModal = ({ open, onClose, sensor }: Props) => {
minLength={1} minLength={1}
required required
onChange={handleChange} onChange={handleChange}
autoFocus
value={formState.name}
/> />
</div> </div>
<div className="actions"> <div className="actions">
<button className="cancel" type="button"> <button className="cancel" type="button" onClick={onClose}>
Cancel Cancel
</button> </button>
<button>Save</button> <button disabled={isLoading}>Save</button>
</div> </div>
</form> </form>
</Modal> </Modal>

View File

@ -4,9 +4,10 @@ import { useState } from 'preact/hooks'
type Props = { type Props = {
sensor: SensorInfo sensor: SensorInfo
onEdit: (sensor: SensorInfo) => void
} }
export const SensorItem = ({ sensor }: Props) => { export const SensorItem = ({ sensor, onEdit }: Props) => {
const [showPassword, setShowPassword] = useState(false) const [showPassword, setShowPassword] = useState(false)
return ( return (
@ -25,7 +26,7 @@ export const SensorItem = ({ sensor }: Props) => {
</button> </button>
</div> </div>
<div className="actions"> <div className="actions">
<button> <button onClick={() => onEdit(sensor)}>
<EditIcon /> Edit <EditIcon /> Edit
</button> </button>
<button> <button>