Skip to content

Commit

Permalink
Get recently used dicts from back-end
Browse files Browse the repository at this point in the history
  • Loading branch information
OuOu2021 committed Nov 16, 2023
1 parent 5b7af55 commit 6c97a07
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/components/Chart/Chart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ echarts.use([
const { t } = useI18n();
const progressNum = 3;
const progressNum = 5;
const historyStore = useHistoryStore();
const userStore = useUserStore();
const dictsToGenerateProgress: DictVo[] = historyStore.recentlyUsedDicts.slice(
Expand Down
3 changes: 2 additions & 1 deletion src/components/Dicts/Dicts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ onMounted(async () => {
try {
const { data: dictsData } = await axios.get("/api/dicts");
dicts.value = dictsData;
historyStore.updateRecentlyUsedDicts();
const promises = dicts.value.map(async (dict) => {
const { data: reviewNum } = await axios.get(
`/api/dicts/${dict.id}/review/num`,
Expand Down Expand Up @@ -179,7 +180,7 @@ function startNewTask(dict: DictVo, task: Task): void {
wordsPerRound.value = optionsStore.qwertyWordsPerRound;
break;
}
historyStore.unShiftRecentlyUsed(dict);
router.push({
name: Task[task],
query: {
Expand Down
26 changes: 11 additions & 15 deletions src/store/historyStore.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import { defineStore } from "pinia";
import { ref } from "vue";
import { DictVo, includesDict } from "../components/Dicts/common";
import axios from "axios";
import { useUserStore } from "./userStore";

export const useHistoryStore = defineStore(
"history",
() => {
const recentlyUsedDicts = ref(Array<DictVo>());
const recentlyUsedDicts = ref<DictVo[]>([]);

const isRecentlyUsed = (dict: DictVo): boolean => {
return includesDict(recentlyUsedDicts.value, dict);
};
// 将当前词典加入RecentlyUsed,如果已经在其中则将其排到最前面
const unShiftRecentlyUsed = (dict: DictVo): void => {
for(const [index, x] of recentlyUsedDicts.value.entries()){
if(dict.name == x.name &&
dict.id == x.id &&
dict.language == x.language){
recentlyUsedDicts.value.splice(index);
break;
}

const updateRecentlyUsedDicts = (): void => {
if(useUserStore().userVo){
axios.get("/api/dicts/recently-used").then((res)=>{
recentlyUsedDicts.value = res.data;
});
}
recentlyUsedDicts.value.unshift(dict);
};
return {
recentlyUsedDicts,
isRecentlyUsed,
unShiftRecentlyUsed,
updateRecentlyUsedDicts,
};
},
{
persist: true,
},
);

0 comments on commit 6c97a07

Please sign in to comment.