Skip to content

Commit

Permalink
fix: setup onsubmit secret
Browse files Browse the repository at this point in the history
  • Loading branch information
Zephyruso authored and Zephyruso committed Sep 4, 2023
1 parent 880a40f commit 30fd139
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/pages/Setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -51,28 +56,27 @@ export default () => {
}

const onSubmit = async ({ url, secret }: { url: string; secret: string }) => {
if (!(await checkEndpoint(url, secret))) {
return
}

const id = uuid()
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)
}

if (!point) {
// new host and secret
setEndpointList([{ id, url, secret }, ...list])
onSetupSuccess(id)

return
}

if (!(await checkEndpoint(url, secret))) {
return
}
// exist host we update secret and id no matter secret is equal or not
point.secret = secret
point.id = id

const id = uuid()
setEndpointList([{ id, url, secret }, ...list])
setEndpointList(list)
onSetupSuccess(id)
}

Expand All @@ -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)
Expand All @@ -94,11 +103,6 @@ export default () => {
}`,
secret: query.get('secret') ?? '',
})
} else {
void onSubmit({
url: 'http://127.0.0.1:9090',
secret: '',
})
}
})

Expand Down

0 comments on commit 30fd139

Please sign in to comment.