-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get recently used dicts from back-end
- Loading branch information
Showing
3 changed files
with
14 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
); |