|
| 1 | +import { |
| 2 | + Button, |
| 3 | + Switch as HopeSwitch, |
| 4 | + FormControl, |
| 5 | + FormHelperText, |
| 6 | + FormLabel, |
| 7 | + Heading, |
| 8 | + Input, |
| 9 | + VStack, |
| 10 | +} from "@hope-ui/solid" |
| 11 | +import { MaybeLoading, FolderChooseInput } from "~/components" |
| 12 | +import { useFetch, useRouter, useT } from "~/hooks" |
| 13 | +import { handleResp, notify, r } from "~/utils" |
| 14 | +import { VirtualHost, PEmptyResp, PResp } from "~/types" |
| 15 | +import { createStore } from "solid-js/store" |
| 16 | + |
| 17 | +const AddOrEdit = () => { |
| 18 | + const t = useT() |
| 19 | + const { params, back } = useRouter() |
| 20 | + const { id } = params |
| 21 | + const [vhost, setVhost] = createStore<VirtualHost>({ |
| 22 | + id: 0, |
| 23 | + enabled: true, |
| 24 | + domain: "", |
| 25 | + path: "", |
| 26 | + web_hosting: false, |
| 27 | + }) |
| 28 | + const [vhostLoading, loadVhost] = useFetch( |
| 29 | + (): PResp<VirtualHost> => r.get(`/admin/vhost/get?id=${id}`), |
| 30 | + ) |
| 31 | + |
| 32 | + const initEdit = async () => { |
| 33 | + const resp = await loadVhost() |
| 34 | + handleResp<VirtualHost>(resp, setVhost) |
| 35 | + } |
| 36 | + if (id) { |
| 37 | + initEdit() |
| 38 | + } |
| 39 | + const [okLoading, ok] = useFetch((): PEmptyResp => { |
| 40 | + return r.post(`/admin/vhost/${id ? "update" : "create"}`, vhost) |
| 41 | + }) |
| 42 | + return ( |
| 43 | + <MaybeLoading loading={vhostLoading()}> |
| 44 | + <VStack w="$full" alignItems="start" spacing="$4"> |
| 45 | + <Heading>{t(`global.${id ? "edit" : "add"}`)}</Heading> |
| 46 | + |
| 47 | + {/* 启用开关 */} |
| 48 | + <FormControl |
| 49 | + w="$full" |
| 50 | + display="flex" |
| 51 | + flexDirection="row" |
| 52 | + alignItems="center" |
| 53 | + gap="$3" |
| 54 | + > |
| 55 | + <FormLabel for="enabled" mb="0"> |
| 56 | + {t("virtual_hosts.enabled")} |
| 57 | + </FormLabel> |
| 58 | + <HopeSwitch |
| 59 | + id="enabled" |
| 60 | + checked={vhost.enabled} |
| 61 | + onChange={(e: any) => setVhost("enabled", e.currentTarget.checked)} |
| 62 | + /> |
| 63 | + </FormControl> |
| 64 | + |
| 65 | + {/* 域名 */} |
| 66 | + <FormControl w="$full" display="flex" flexDirection="column" required> |
| 67 | + <FormLabel for="domain">{t("virtual_hosts.domain")}</FormLabel> |
| 68 | + <Input |
| 69 | + id="domain" |
| 70 | + placeholder="example.com" |
| 71 | + value={vhost.domain} |
| 72 | + onInput={(e) => setVhost("domain", e.currentTarget.value)} |
| 73 | + /> |
| 74 | + <FormHelperText>{t("virtual_hosts.domain_help")}</FormHelperText> |
| 75 | + </FormControl> |
| 76 | + |
| 77 | + {/* 路径 */} |
| 78 | + <FormControl w="$full" display="flex" flexDirection="column" required> |
| 79 | + <FormLabel for="path">{t("virtual_hosts.path")}</FormLabel> |
| 80 | + <FolderChooseInput |
| 81 | + id="path" |
| 82 | + value={vhost.path} |
| 83 | + onChange={(path) => setVhost("path", path)} |
| 84 | + /> |
| 85 | + <FormHelperText>{t("virtual_hosts.path_help")}</FormHelperText> |
| 86 | + </FormControl> |
| 87 | + |
| 88 | + {/* Web 托管开关 */} |
| 89 | + <FormControl w="$full" display="flex" flexDirection="column"> |
| 90 | + <FormControl |
| 91 | + display="flex" |
| 92 | + flexDirection="row" |
| 93 | + alignItems="center" |
| 94 | + gap="$3" |
| 95 | + > |
| 96 | + <FormLabel for="web_hosting" mb="0"> |
| 97 | + {t("virtual_hosts.web_hosting")} |
| 98 | + </FormLabel> |
| 99 | + <HopeSwitch |
| 100 | + id="web_hosting" |
| 101 | + checked={vhost.web_hosting} |
| 102 | + onChange={(e: any) => |
| 103 | + setVhost("web_hosting", e.currentTarget.checked) |
| 104 | + } |
| 105 | + /> |
| 106 | + </FormControl> |
| 107 | + <FormHelperText>{t("virtual_hosts.web_hosting_help")}</FormHelperText> |
| 108 | + </FormControl> |
| 109 | + |
| 110 | + <Button |
| 111 | + loading={okLoading()} |
| 112 | + onClick={async () => { |
| 113 | + const resp = await ok() |
| 114 | + handleResp(resp, () => { |
| 115 | + notify.success(t("global.save_success")) |
| 116 | + back() |
| 117 | + }) |
| 118 | + }} |
| 119 | + > |
| 120 | + {t(`global.${id ? "save" : "add"}`)} |
| 121 | + </Button> |
| 122 | + </VStack> |
| 123 | + </MaybeLoading> |
| 124 | + ) |
| 125 | +} |
| 126 | + |
| 127 | +export default AddOrEdit |
0 commit comments