fix: setup onsubmit secret

This commit is contained in:
Zephyruso 2023-09-04 13:34:20 +08:00
parent 880a40f18e
commit 30fd13946b

View File

@ -8,7 +8,12 @@ import { For, onMount } from 'solid-js'
import { v4 as uuid } from 'uuid'
import { z } from 'zod'
import { Button } from '~/components'
import { endpointList, setEndpointList, setSelectedEndpoint } from '~/signals'
import {
endpointList,
selectedEndpoint,
setEndpointList,
setSelectedEndpoint,
} from '~/signals'
const schema = z.object({
url: z.string().url().nonempty(),
@ -51,29 +56,28 @@ export default () => {
}
const onSubmit = async ({ url, secret }: { url: string; secret: string }) => {
const list = endpointList().slice()
const point = list.find((history) => history.url === url)
if (point) {
const { id, secret: oldSecret } = point
if (secret !== oldSecret && !(await checkEndpoint(url, secret))) {
point.secret = secret
setEndpointList(list)
}
onSetupSuccess(id)
return
}
if (!(await checkEndpoint(url, secret))) {
return
}
const id = uuid()
const list = endpointList().slice()
const point = list.find((history) => history.url === url)
if (!point) {
// new host and secret
setEndpointList([{ id, url, secret }, ...list])
onSetupSuccess(id)
return
}
// exist host we update secret and id no matter secret is equal or not
point.secret = secret
point.id = id
setEndpointList(list)
onSetupSuccess(id)
}
const { form } = createForm<z.infer<typeof schema>>({
@ -81,8 +85,13 @@ export default () => {
onSubmit,
})
const onRemove = (id: string) =>
const onRemove = (id: string) => {
if (selectedEndpoint() === id) {
setSelectedEndpoint('')
}
setEndpointList(endpointList().filter((e) => e.id !== id))
}
onMount(() => {
const query = new URLSearchParams(window.location.search)
@ -94,11 +103,6 @@ export default () => {
}`,
secret: query.get('secret') ?? '',
})
} else {
void onSubmit({
url: 'http://127.0.0.1:9090',
secret: '',
})
}
})