import {
Button,
Checkbox,
Switch as HopeSwitch,
FormControl,
FormLabel,
Heading,
Input,
VStack,
Flex,
Textarea,
FormHelperText,
} from "@hope-ui/solid"
import { MaybeLoading, FolderChooseInput } from "~/components"
import { useFetch, useRouter, useT } from "~/hooks"
import { handleResp, notify, r } from "~/utils"
import { Meta, PEmptyResp, PResp } from "~/types"
import { createStore } from "solid-js/store"
import { For, Show } from "solid-js"
type ItemProps = {
name: string
sub?: boolean
onSub: (val: boolean) => void
help?: boolean
} & (
| { type: "string"; value: string; onChange: (val: string) => void }
| { type: "text"; value: string; onChange: (val: string) => void }
| { type: "bool"; value: boolean; onChange: (val: boolean) => void }
)
const Item = (props: ItemProps) => {
const t = useT()
return (
{t(`metas.${props.name}`)}
{props.type === "string" ? (
props.onChange(e.currentTarget.value)}
/>
) : props.type === "bool" ? (
props.onChange(e.currentTarget.checked)}
/>
) : (
{t(`metas.${props.name}_help`)}
)
}
const AddOrEdit = () => {
const t = useT()
const { params, back } = useRouter()
const { id } = params
const [meta, setMeta] = createStore({
id: 0,
path: "",
password: "",
p_sub: false,
write: false,
w_sub: false,
hide: "",
h_sub: false,
readme: "",
r_sub: false,
header: "",
header_sub: false,
})
const [metaLoading, loadMeta] = useFetch(
(): PResp => r.get(`/admin/meta/get?id=${id}`),
)
const initEdit = async () => {
const resp = await loadMeta()
handleResp(resp, setMeta)
}
if (id) {
initEdit()
}
const [okLoading, ok] = useFetch((): PEmptyResp => {
return r.post(`/admin/meta/${id ? "update" : "create"}`, meta)
})
return (
{t(`global.${id ? "edit" : "add"}`)}
{t(`metas.path`)}
setMeta("path", path)}
/>
{/*
{t(`metas.password`)}
setMeta("password", e.currentTarget.value)}
/>
*/}
{(item) => {
return (
// @ts-ignore
-
setMeta(item.name as keyof Meta, val)
}
sub={meta[item.sub] as boolean}
onSub={(val: boolean): void => setMeta(item.sub, val)}
help={item.help}
/>
)
}}
)
}
export default AddOrEdit