Add bilibili toy form submit

This commit is contained in:
奇趣保罗 2024-03-06 11:14:16 +08:00
parent 9277ea51af
commit 4161c5587c
3 changed files with 73 additions and 13 deletions

View File

@ -0,0 +1,37 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";
const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { token, siteUrl } = await chrome.storage.local.get(["token", "siteUrl"]);
if (req.body.action === "submitAddForm") {
const { values } = req.body;
let imageBlob: Blob | undefined;
if (values.imageUrl) {
imageBlob = await fetch(values.imageUrl).then((res) => res.blob());
}
const formData = new FormData();
Object.keys(values).forEach((item) => {
formData.append(item, String(values[item]));
});
if (imageBlob) {
formData.append("image", imageBlob);
}
const addReq = await fetch(`${siteUrl}/api/toy/add`, {
method: "POST",
body: formData,
headers: {
"paul-token-code": token,
},
}).then((res) => res.json());
res.send(addReq);
}
}
export default handler;

View File

@ -1,10 +1,12 @@
import { useEffect, useState, type FormEvent } from "react"; import { useEffect, useState } from "react";
import { sendToBackground } from "@plasmohq/messaging";
import Placeholder from "~components/ui/placeholder"; import Placeholder from "~components/ui/placeholder";
import Form from "~components/ui/form"; import Form from "~components/ui/form";
import { IconBack } from "~assets/icons"; import { IconBack } from "~assets/icons";
import styles from "./popup.module.less"; import styles from "./popup.module.less";
import stylesB from "./bili.module.less"; import stylesB from "./bili.module.less";
import useForm from "~hooks/useForm"; import useForm from "~hooks/useForm";
import { add } from "~components/ui/message/utils";
interface ReadProps { interface ReadProps {
onBack: () => void; onBack: () => void;
@ -39,15 +41,36 @@ function Toy({ onBack }: ReadProps) {
initialValues: { initialValues: {
type: 3, type: 3,
}, },
onSubmit: (values) => {
console.log(values);
},
}); });
const [imgs, setImgs] = useState([]); const [imgs, setImgs] = useState([]);
const [currentImg, setCurrentImg] = useState(0); const [currentImg, setCurrentImg] = useState(0);
const [failed, setFailed] = useState(false); const [failed, setFailed] = useState(false);
const submitForm = (body: FormValue) => {
sendToBackground({
name: "bili",
body: {
action: "submitAddForm",
values: {
...body,
imageUrl: imgs[0],
},
},
}).then((res) => {
if (res.status === "Success") {
add({
content: "提交成功",
});
}
else {
add({
content: res.msg,
});
}
});
}
useEffect(() => { useEffect(() => {
getInfo().then((res) => { getInfo().then((res) => {
if (!res) { if (!res) {
@ -77,34 +100,34 @@ function Toy({ onBack }: ReadProps) {
<img src={imgs[currentImg]} alt="" /> <img src={imgs[currentImg]} alt="" />
<div className={stylesB.selector}> <div className={stylesB.selector}>
{imgs.map((item, index) => ( {imgs.map((item, index) => (
<img src={item} alt="" onClick={() => setCurrentImg(index)} /> <img key={index} src={item} alt="" onClick={() => setCurrentImg(index)} />
))} ))}
</div> </div>
</div> </div>
<Form className={stylesB.form} onSubmit={onSubmit((data) => console.log(data))}> <Form className={stylesB.form} onSubmit={onSubmit(submitForm)}>
<div> <div>
<label htmlFor="name"></label> <label htmlFor="name"></label>
<input {...bindInput("name")} required placeholder="中文名" /> <input {...bindInput("name")} required placeholder="中文名" />
</div> </div>
<div> <div>
<label htmlFor="ename"></label> <label htmlFor="ename"></label>
<input {...bindInput("ename")} required placeholder="英文名" /> <input {...bindInput("ename")} placeholder="英文名" />
</div> </div>
<div> <div>
<label htmlFor="project"></label> <label htmlFor="project"></label>
<input {...bindInput("project")} required placeholder="作品" /> <input {...bindInput("project")} placeholder="作品" />
</div> </div>
<div> <div>
<label htmlFor="made"></label> <label htmlFor="made"></label>
<input {...bindInput("made")} required placeholder="制作" /> <input {...bindInput("made")} placeholder="制作" />
</div> </div>
<div> <div>
<label htmlFor="sale"></label> <label htmlFor="sale"></label>
<input {...bindInput("sale")} required placeholder="发售日期" /> <input {...bindInput("sale")} placeholder="发售日期" />
</div> </div>
<div> <div>
<label htmlFor="type"></label> <label htmlFor="type"></label>
<select {...bindInput("type")}> <select {...bindInput("type")} required>
<option value={1}></option> <option value={1}></option>
<option value={2}></option> <option value={2}></option>
<option value={3}></option> <option value={3}></option>
@ -112,7 +135,7 @@ function Toy({ onBack }: ReadProps) {
</div> </div>
<div> <div>
<label htmlFor="desc"></label> <label htmlFor="desc"></label>
<textarea {...bindInput("desc")} rows={5} required placeholder="描述"></textarea> <textarea {...bindInput("desc")} rows={5} placeholder="描述"></textarea>
</div> </div>
<button type="submit"></button> <button type="submit"></button>

View File

@ -1,5 +1,5 @@
import { sendToBackground } from "@plasmohq/messaging";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { sendToBackground } from "@plasmohq/messaging";
import Form from "~components/ui/form"; import Form from "~components/ui/form";
import { IconBack } from "~assets/icons"; import { IconBack } from "~assets/icons";
import styles from "./popup.module.less"; import styles from "./popup.module.less";