import { runInAction } from "mobx"; import { getMillis } from "taehui-ts/date"; import { NoteAPINote, GetNoteAPI } from "src/wwwAPI"; import { wwwAXIOS } from "src/Www"; export default function setNoteStore() { let lastMillis = getMillis(); let lastWant = ""; return { totalCount: 0, highestCount: 0, wantAvatar: "", notes: [] as NoteAPINote[], noteVariety: 0, fit: 0, src: 0, input: "", want: "", lastPage: 0, pageUnit: 10, viewUnit: 20, isLoading: false, setNoteVariety(noteVariety: number) { this.noteVariety = noteVariety; }, setFit(fit: number) { this.fit = fit; }, setSrc(src: number) { this.src = src; }, setInput(input: string) { this.input = input; }, setWant(want: string) { this.want = want; }, async getNote(page: number, setPage: (page: number) => void) { this.wantAvatar = this.src === 1 ? this.want : ""; const isNewWant = this.want !== lastWant; lastWant = this.want; if (isNewWant && page > 1) { setPage(1); } else { const millis = getMillis(); lastMillis = millis; this.isLoading = true; const { status, data } = await wwwAXIOS.get<GetNoteAPI>("/note", { params: { noteVariety: this.noteVariety, fit: this.fit, src: this.src, want: this.want, page, viewUnit: this.viewUnit, }, }); if (millis >= lastMillis) { if (status === 200) { runInAction(() => { this.totalCount = data.totalCount; this.highestCount = data.highestCount; this.lastPage = Math.ceil(data.noteCount / this.viewUnit); this.notes = data.notes; this.isLoading = false; }); } } } }, }; }