Commit 395d59c3 authored by Chunchi Che's avatar Chunchi Che

optimize pull deck progress

parent afdee64c
import { useConfig } from "@/config"; import { useConfig } from "@/config";
import { pfetch } from "@/infra";
import { handleHttps } from ".."; import { handleHttps } from "..";
import { MdproDeckLike, MdproResp } from "./schema"; import { MdproDeckLike, MdproResp } from "./schema";
...@@ -32,7 +31,6 @@ interface RespData { ...@@ -32,7 +31,6 @@ interface RespData {
export async function pullDecks( export async function pullDecks(
req: PullReq = defaultPullReq, req: PullReq = defaultPullReq,
progressCallback?: (progress: number) => void,
): Promise<MdproResp<RespData> | undefined> { ): Promise<MdproResp<RespData> | undefined> {
const myHeaders = mdproHeaders(); const myHeaders = mdproHeaders();
...@@ -46,13 +44,10 @@ export async function pullDecks( ...@@ -46,13 +44,10 @@ export async function pullDecks(
const url = new URL(`${mdproServer}/${API_PATH}`); const url = new URL(`${mdproServer}/${API_PATH}`);
url.search = params.toString(); url.search = params.toString();
const resp = await pfetch(url.toString(), { const resp = await fetch(url.toString(), {
init: { method: "GET",
method: "GET", headers: myHeaders,
headers: myHeaders, redirect: "follow",
redirect: "follow",
},
progressCallback,
}); });
return await handleHttps(resp, API_PATH); return await handleHttps(resp, API_PATH);
......
...@@ -25,7 +25,7 @@ interface Props { ...@@ -25,7 +25,7 @@ interface Props {
decks: MdproDeckLike[]; decks: MdproDeckLike[];
total: number; total: number;
onlyMine: boolean; onlyMine: boolean;
progress: number; loaded: boolean;
} }
// TODO: useConfig // TODO: useConfig
...@@ -38,7 +38,7 @@ const store = proxy<Props>({ ...@@ -38,7 +38,7 @@ const store = proxy<Props>({
decks: [], decks: [],
total: 0, total: 0,
onlyMine: false, onlyMine: false,
progress: 0.01, loaded: false,
}); });
export const DeckResults: React.FC = memo(() => { export const DeckResults: React.FC = memo(() => {
...@@ -46,7 +46,7 @@ export const DeckResults: React.FC = memo(() => { ...@@ -46,7 +46,7 @@ export const DeckResults: React.FC = memo(() => {
const { message } = App.useApp(); const { message } = App.useApp();
const { t: i18n } = useTranslation("DeckResults"); const { t: i18n } = useTranslation("DeckResults");
useEffect(() => { useEffect(() => {
resetProgress(); resetLoaded();
if (snap.onlyMine) { if (snap.onlyMine) {
// show only decks uploaded by myself // show only decks uploaded by myself
...@@ -56,29 +56,11 @@ export const DeckResults: React.FC = memo(() => { ...@@ -56,29 +56,11 @@ export const DeckResults: React.FC = memo(() => {
} }
}, [snap.query, snap.page, snap.onlyMine]); }, [snap.query, snap.page, snap.onlyMine]);
const onChangePage = async (page: number) => { const onChangePage = (page: number) => (store.page = page);
const resp = await pullDecks({
page,
size: PAGE_SIZE,
keyWord: store.query !== "" ? store.query : undefined,
sortLike: SORT_LIKE,
});
if (resp?.data) {
const { current, total, records } = resp.data;
store.page = current;
store.total = total;
store.decks = records;
} else if (resp?.code !== 0) {
message.error(resp?.message);
} else {
message.error("翻页失败,请检查您的网络状况。");
}
};
return ( return (
<> <>
{snap.progress === 1 ? ( {snap.loaded ? (
snap.decks.length ? ( snap.decks.length ? (
<div className={styles.container}> <div className={styles.container}>
<div className={styles["search-decks"]}> <div className={styles["search-decks"]}>
...@@ -109,7 +91,7 @@ export const DeckResults: React.FC = memo(() => { ...@@ -109,7 +91,7 @@ export const DeckResults: React.FC = memo(() => {
) )
) : ( ) : (
<div className={styles.empty}> <div className={styles.empty}>
<Loading progress={snap.progress * 100} /> <Loading />
</div> </div>
)} )}
</> </>
...@@ -174,16 +156,13 @@ const MdproDeckBlock: React.FC<{ ...@@ -174,16 +156,13 @@ const MdproDeckBlock: React.FC<{
); );
}; };
const updateMdproDeck = async () => { export const updateMdproDeck = async () => {
const resp = await pullDecks( const resp = await pullDecks({
{ page: store.page,
page: store.page, size: PAGE_SIZE,
size: PAGE_SIZE, keyWord: store.query !== "" ? store.query : undefined,
keyWord: store.query !== "" ? store.query : undefined, sortLike: SORT_LIKE,
sortLike: SORT_LIKE, });
},
updateProgress,
);
if (resp?.data) { if (resp?.data) {
const { total, records: newDecks } = resp.data; const { total, records: newDecks } = resp.data;
...@@ -193,19 +172,16 @@ const updateMdproDeck = async () => { ...@@ -193,19 +172,16 @@ const updateMdproDeck = async () => {
store.decks = []; store.decks = [];
} }
finishProgress(); finishLoaded();
}; };
const updatePersonalList = async (message: MessageInstance) => { const updatePersonalList = async (message: MessageInstance) => {
const user = accountStore.user; const user = accountStore.user;
if (user) { if (user) {
const resp = await getPersonalList( const resp = await getPersonalList({
{ userID: user.id,
userID: user.id, token: user.token,
token: user.token, });
},
updateProgress,
);
if (resp) { if (resp) {
if (resp.code !== 0 || resp.data === undefined) { if (resp.code !== 0 || resp.data === undefined) {
...@@ -249,12 +225,11 @@ const updatePersonalList = async (message: MessageInstance) => { ...@@ -249,12 +225,11 @@ const updatePersonalList = async (message: MessageInstance) => {
store.total = 0; store.total = 0;
} }
finishProgress(); finishLoaded();
}; };
const resetProgress = () => (store.progress = 0.01); const resetLoaded = () => (store.loaded = false);
const updateProgress = (progress: number) => (store.progress = progress * 0.9); const finishLoaded = () => (store.loaded = true);
const finishProgress = () => (store.progress = 1);
const copyMdproDeckToEditing = async (mdproDeck: MdproDeckLike) => { const copyMdproDeckToEditing = async (mdproDeck: MdproDeckLike) => {
// currently the content of the deck, which we named `Ydk`, // currently the content of the deck, which we named `Ydk`,
......
...@@ -28,6 +28,7 @@ import { ...@@ -28,6 +28,7 @@ import {
import { useConfig } from "@/config"; import { useConfig } from "@/config";
import { accountStore } from "@/stores"; import { accountStore } from "@/stores";
import { updateMdproDeck } from "../BuildDeck/DeckDatabase/DeckResults";
import { setCssProperties } from "../Duel/PlayMat/css"; import { setCssProperties } from "../Duel/PlayMat/css";
import { Setting } from "../Setting"; import { Setting } from "../Setting";
import styles from "./index.module.scss"; import styles from "./index.module.scss";
...@@ -52,6 +53,8 @@ export const loader: LoaderFunction = async () => { ...@@ -52,6 +53,8 @@ export const loader: LoaderFunction = async () => {
initForbidden(); initForbidden();
initI18N(); initI18N();
initSuper(); initSuper();
// TODO: should avoid reloading mdpro deck again
updateMdproDeck();
// set some styles // set some styles
setCssProperties(); setCssProperties();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment