Commit 069dd0a3 authored by Chunchi Che's avatar Chunchi Che

Merge branch 'feat/dpu' into 'main'

add progress for loading mdpro deck

See merge request !386
parents 755b5d03 e3201846
Pipeline #27896 passed with stages
in 8 minutes and 51 seconds
import { useConfig } from "@/config"; import { useConfig } from "@/config";
import { pfetch } from "@/infra";
import { MdproDeck, MdproResp } from "./schema"; import { MdproDeck, MdproResp } from "./schema";
import { handleHttps, mdproHeaders } from "./util"; import { handleHttps, mdproHeaders } from "./util";
...@@ -15,14 +16,18 @@ export interface PersonalListReq { ...@@ -15,14 +16,18 @@ export interface PersonalListReq {
export async function getPersonalList( export async function getPersonalList(
req: PersonalListReq, req: PersonalListReq,
progressCallback?: (progress: number) => void,
): Promise<MdproResp<MdproDeck[]> | undefined> { ): Promise<MdproResp<MdproDeck[]> | undefined> {
const myHeaders = mdproHeaders(); const myHeaders = mdproHeaders();
myHeaders.append("token", req.token); myHeaders.append("token", req.token);
const resp = await fetch(`${mdproServer}/${API_PATH}/${req.userID}`, { const resp = await pfetch(`${mdproServer}/${API_PATH}/${req.userID}`, {
method: "GET", init: {
headers: myHeaders, method: "GET",
redirect: "follow", headers: myHeaders,
redirect: "follow",
},
progressCallback,
}); });
return await handleHttps(resp, API_PATH); return await handleHttps(resp, API_PATH);
......
import { useConfig } from "@/config"; import { useConfig } from "@/config";
import { pfetch } from "@/infra";
import { MdproDeckLike, MdproResp } from "./schema"; import { MdproDeckLike, MdproResp } from "./schema";
import { handleHttps, mdproHeaders } from "./util"; import { handleHttps, mdproHeaders } from "./util";
...@@ -30,6 +31,7 @@ interface RespData { ...@@ -30,6 +31,7 @@ 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();
...@@ -43,10 +45,13 @@ export async function pullDecks( ...@@ -43,10 +45,13 @@ 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 fetch(url.toString(), { const resp = await pfetch(url.toString(), {
method: "GET", init: {
headers: myHeaders, method: "GET",
redirect: "follow", headers: myHeaders,
redirect: "follow",
},
progressCallback,
}); });
return await handleHttps(resp, API_PATH); return await handleHttps(resp, API_PATH);
......
...@@ -10,7 +10,7 @@ import { deleteDeck, getPersonalList, mgetDeck, pullDecks } from "@/api"; ...@@ -10,7 +10,7 @@ import { deleteDeck, getPersonalList, mgetDeck, pullDecks } from "@/api";
import { MdproDeckLike } from "@/api/mdproDeck/schema"; import { MdproDeckLike } from "@/api/mdproDeck/schema";
import { useConfig } from "@/config"; import { useConfig } from "@/config";
import { accountStore } from "@/stores"; import { accountStore } from "@/stores";
import { IconFont } from "@/ui/Shared"; import { IconFont, Loading } from "@/ui/Shared";
import { setSelectedDeck } from "../.."; import { setSelectedDeck } from "../..";
import { editDeckStore } from "../../store"; import { editDeckStore } from "../../store";
...@@ -25,6 +25,7 @@ interface Props { ...@@ -25,6 +25,7 @@ interface Props {
decks: MdproDeckLike[]; decks: MdproDeckLike[];
total: number; total: number;
onlyMine: boolean; onlyMine: boolean;
progress: number;
} }
// TODO: useConfig // TODO: useConfig
...@@ -37,6 +38,7 @@ const store = proxy<Props>({ ...@@ -37,6 +38,7 @@ const store = proxy<Props>({
decks: [], decks: [],
total: 0, total: 0,
onlyMine: false, onlyMine: false,
progress: 0.01,
}); });
export const DeckResults: React.FC = memo(() => { export const DeckResults: React.FC = memo(() => {
...@@ -44,29 +46,13 @@ export const DeckResults: React.FC = memo(() => { ...@@ -44,29 +46,13 @@ 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();
if (snap.onlyMine) { if (snap.onlyMine) {
// show only decks uploaded by myself // show only decks uploaded by myself
updatePersonalList(message); updatePersonalList(message);
} else { } else {
const update = async () => { updateMdproDeck();
const resp = await pullDecks({
page: snap.page,
size: PAGE_SIZE,
keyWord: snap.query !== "" ? snap.query : undefined,
sortLike: SORT_LIKE,
});
if (resp?.data) {
const { total, records: newDecks } = resp.data;
store.total = total;
store.decks = newDecks;
} else {
store.decks = [];
}
};
update();
} }
}, [snap.query, snap.page, snap.onlyMine]); }, [snap.query, snap.page, snap.onlyMine]);
...@@ -92,32 +78,38 @@ export const DeckResults: React.FC = memo(() => { ...@@ -92,32 +78,38 @@ export const DeckResults: React.FC = memo(() => {
return ( return (
<> <>
{snap.decks.length ? ( {snap.progress === 1 ? (
<div className={styles.container}> snap.decks.length ? (
<div className={styles["search-decks"]}> <div className={styles.container}>
{snap.decks.map((deck) => ( <div className={styles["search-decks"]}>
<MdproDeckBlock {snap.decks.map((deck) => (
key={deck.deckId} <MdproDeckBlock
deck={deck} key={deck.deckId}
onlyMine={snap.onlyMine} deck={deck}
onlyMine={snap.onlyMine}
/>
))}
</div>
<div style={{ textAlign: "center", padding: "0.625rem 0 1.25rem" }}>
<Pagination
current={snap.page}
onChange={onChangePage}
total={snap.total}
pageSize={PAGE_SIZE}
showLessItems
hideOnSinglePage
/> />
))} </div>
</div> </div>
<div style={{ textAlign: "center", padding: "0.625rem 0 1.25rem" }}> ) : (
<Pagination <div className={styles.empty}>
current={snap.page} <IconFont type="icon-empty" size={40} />
onChange={onChangePage} <div>{i18n("NoDeckGroupFound")}</div>
total={snap.total}
pageSize={PAGE_SIZE}
showLessItems
hideOnSinglePage
/>
</div> </div>
</div> )
) : ( ) : (
<div className={styles.empty}> <div className={styles.empty}>
<IconFont type="icon-empty" size={40} /> <Loading progress={snap.progress * 100} />
<div>{i18n("NoDeckGroupFound")}</div>
</div> </div>
)} )}
</> </>
...@@ -182,13 +174,38 @@ const MdproDeckBlock: React.FC<{ ...@@ -182,13 +174,38 @@ const MdproDeckBlock: React.FC<{
); );
}; };
const updateMdproDeck = async () => {
const resp = await pullDecks(
{
page: store.page,
size: PAGE_SIZE,
keyWord: store.query !== "" ? store.query : undefined,
sortLike: SORT_LIKE,
},
updateProgress,
);
if (resp?.data) {
const { total, records: newDecks } = resp.data;
store.total = total;
store.decks = newDecks;
} else {
store.decks = [];
}
finishProgress();
};
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, {
token: user.token, userID: user.id,
}); token: user.token,
},
updateProgress,
);
if (resp) { if (resp) {
if (resp.code !== 0 || resp.data === undefined) { if (resp.code !== 0 || resp.data === undefined) {
...@@ -231,8 +248,14 @@ const updatePersonalList = async (message: MessageInstance) => { ...@@ -231,8 +248,14 @@ const updatePersonalList = async (message: MessageInstance) => {
store.decks = []; store.decks = [];
store.total = 0; store.total = 0;
} }
finishProgress();
}; };
const resetProgress = () => (store.progress = 0.01);
const updateProgress = (progress: number) => (store.progress = progress * 0.9);
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`,
// haven't been downloaded, so we need to fetch from server again by `mgetDeck` // haven't been downloaded, so we need to fetch from server again by `mgetDeck`
......
...@@ -198,5 +198,8 @@ ...@@ -198,5 +198,8 @@
"TurnOnSoundEffects": "Efeitos sonoros", "TurnOnSoundEffects": "Efeitos sonoros",
"SwitchMusicAccordingToTheEnvironment": "Trocar música conforme o ambiente", "SwitchMusicAccordingToTheEnvironment": "Trocar música conforme o ambiente",
"LanguageSettings": "Idioma" "LanguageSettings": "Idioma"
},
"DeckResults": {
"NoDeckGroupFound": "Não foi possível encontrar o baralho correspondente"
} }
} }
...@@ -200,5 +200,8 @@ ...@@ -200,5 +200,8 @@
"TurnOnSoundEffects": "开启音效", "TurnOnSoundEffects": "开启音效",
"SwitchMusicAccordingToTheEnvironment": "根据环境切换音乐", "SwitchMusicAccordingToTheEnvironment": "根据环境切换音乐",
"LanguageSettings": "语言" "LanguageSettings": "语言"
},
"DeckResults": {
"NoDeckGroupFound": "找不到相应卡组"
} }
} }
...@@ -200,5 +200,8 @@ ...@@ -200,5 +200,8 @@
"TurnOnSoundEffects": "Sound effects", "TurnOnSoundEffects": "Sound effects",
"SwitchMusicAccordingToTheEnvironment": "Switch music according to the environment", "SwitchMusicAccordingToTheEnvironment": "Switch music according to the environment",
"LanguageSettings": "Language" "LanguageSettings": "Language"
},
"DeckResults": {
"NoDeckGroupFound": "Cannot find the corresponding card deck"
} }
} }
...@@ -198,5 +198,8 @@ ...@@ -198,5 +198,8 @@
"TurnOnSoundEffects": "Effets sonores", "TurnOnSoundEffects": "Effets sonores",
"SwitchMusicAccordingToTheEnvironment": "Changer la musique en fonction de l'environnement", "SwitchMusicAccordingToTheEnvironment": "Changer la musique en fonction de l'environnement",
"LanguageSettings": "Langue" "LanguageSettings": "Langue"
},
"DeckResults": {
"NoDeckGroupFound": "Impossible de trouver le jeu de cartes correspondant"
} }
} }
...@@ -198,5 +198,8 @@ ...@@ -198,5 +198,8 @@
"TurnOnSoundEffects": "効果音をオンにする", "TurnOnSoundEffects": "効果音をオンにする",
"SwitchMusicAccordingToTheEnvironment": "環境に応じて音楽を切り替える", "SwitchMusicAccordingToTheEnvironment": "環境に応じて音楽を切り替える",
"LanguageSettings": "言語" "LanguageSettings": "言語"
},
"DeckResults": {
"NoDeckGroupFound": "対応するカードデッキが見つかりません"
} }
} }
...@@ -197,5 +197,8 @@ ...@@ -197,5 +197,8 @@
"TurnOnMusic": "Música", "TurnOnMusic": "Música",
"TurnOnSoundEffects": "Efeitos sonoros", "TurnOnSoundEffects": "Efeitos sonoros",
"SwitchMusicAccordingToTheEnvironment": "Trocar música conforme o ambiente" "SwitchMusicAccordingToTheEnvironment": "Trocar música conforme o ambiente"
},
"DeckResults": {
"NoDeckGroupFound": "Não foi possível encontrar o baralho correspondente"
} }
} }
...@@ -198,5 +198,8 @@ ...@@ -198,5 +198,8 @@
"TurnOnSoundEffects": "Efectos de sonido", "TurnOnSoundEffects": "Efectos de sonido",
"SwitchMusicAccordingToTheEnvironment": "Cambiar la música según el entorno", "SwitchMusicAccordingToTheEnvironment": "Cambiar la música según el entorno",
"LanguageSettings": "Idioma" "LanguageSettings": "Idioma"
},
"DeckResults": {
"NoDeckGroupFound": "No se puede encontrar el mazo de cartas correspondiente"
} }
} }
...@@ -32,6 +32,7 @@ const resources = { ...@@ -32,6 +32,7 @@ const resources = {
Menu: translationChinese.Menu, Menu: translationChinese.Menu,
SelectCardModal: translationChinese.SelectCardModal, SelectCardModal: translationChinese.SelectCardModal,
SystemSettings: translationChinese.SystemSettings, SystemSettings: translationChinese.SystemSettings,
DeckResults: translationChinese.DeckResults,
}, },
en: { en: {
Header: translationEnglish.Header, Header: translationEnglish.Header,
...@@ -51,6 +52,7 @@ const resources = { ...@@ -51,6 +52,7 @@ const resources = {
Menu: translationEnglish.Menu, Menu: translationEnglish.Menu,
SelectCardModal: translationEnglish.SelectCardModal, SelectCardModal: translationEnglish.SelectCardModal,
SystemSettings: translationEnglish.SystemSettings, SystemSettings: translationEnglish.SystemSettings,
DeckResults: translationEnglish.DeckResults,
}, },
es: { es: {
Header: translationSpanish.Header, Header: translationSpanish.Header,
...@@ -70,6 +72,7 @@ const resources = { ...@@ -70,6 +72,7 @@ const resources = {
Menu: translationSpanish.Menu, Menu: translationSpanish.Menu,
SelectCardModal: translationSpanish.SelectCardModal, SelectCardModal: translationSpanish.SelectCardModal,
SystemSettings: translationSpanish.SystemSettings, SystemSettings: translationSpanish.SystemSettings,
DeckResults: translationSpanish.DeckResults,
}, },
fr: { fr: {
Header: translationFrench.Header, Header: translationFrench.Header,
...@@ -89,6 +92,7 @@ const resources = { ...@@ -89,6 +92,7 @@ const resources = {
Menu: translationFrench.Menu, Menu: translationFrench.Menu,
SelectCardModal: translationFrench.SelectCardModal, SelectCardModal: translationFrench.SelectCardModal,
SystemSettings: translationFrench.SystemSettings, SystemSettings: translationFrench.SystemSettings,
DeckResults: translationFrench.DeckResults,
}, },
ja: { ja: {
Header: translationJapanese.Header, Header: translationJapanese.Header,
...@@ -108,6 +112,7 @@ const resources = { ...@@ -108,6 +112,7 @@ const resources = {
Menu: translationJapanese.Menu, Menu: translationJapanese.Menu,
SelectCardModal: translationJapanese.SelectCardModal, SelectCardModal: translationJapanese.SelectCardModal,
SystemSettings: translationJapanese.SystemSettings, SystemSettings: translationJapanese.SystemSettings,
DeckResults: translationJapanese.DeckResults,
}, },
br: { br: {
Header: translationBrazilian.Header, Header: translationBrazilian.Header,
...@@ -127,6 +132,7 @@ const resources = { ...@@ -127,6 +132,7 @@ const resources = {
Menu: translationBrazilian.Menu, Menu: translationBrazilian.Menu,
SelectCardModal: translationBrazilian.SelectCardModal, SelectCardModal: translationBrazilian.SelectCardModal,
SystemSettings: translationBrazilian.SystemSettings, SystemSettings: translationBrazilian.SystemSettings,
DeckResults: translationBrazilian.DeckResults,
}, },
pt: { pt: {
Header: translationPortuguese.Header, Header: translationPortuguese.Header,
...@@ -146,6 +152,7 @@ const resources = { ...@@ -146,6 +152,7 @@ const resources = {
Menu: translationPortuguese.Menu, Menu: translationPortuguese.Menu,
SelectCardModal: translationPortuguese.SelectCardModal, SelectCardModal: translationPortuguese.SelectCardModal,
SystemSettings: translationPortuguese.SystemSettings, SystemSettings: translationPortuguese.SystemSettings,
DeckResults: translationPortuguese.DeckResults,
}, },
}; };
......
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