support for marking entries older than a threshold

This commit is contained in:
Athou
2022-08-15 20:47:46 +02:00
parent 11f5b22cb4
commit a8db632c4a
6 changed files with 125 additions and 74 deletions

View File

@@ -3,6 +3,8 @@ import { client } from "app/client"
import { Constants } from "app/constants"
import { RootState } from "app/store"
import { Entries, Entry, MarkRequest } from "app/types"
// eslint-disable-next-line import/no-cycle
import { reloadTree } from "./tree"
export type EntrySourceType = "category" | "feed"
export type EntrySource = { type: EntrySourceType; id: string }
@@ -77,10 +79,14 @@ export const markEntry = createAsyncThunk(
condition: arg => arg.entry.read !== arg.read,
}
)
export const markAllEntries = createAsyncThunk("entries/entry/markAll", (arg: { sourceType: EntrySourceType; req: MarkRequest }) => {
const endpoint = arg.sourceType === "category" ? client.category.markEntries : client.feed.markEntries
endpoint(arg.req)
})
export const markAllEntries = createAsyncThunk<void, { sourceType: EntrySourceType; req: MarkRequest }, { state: RootState }>(
"entries/entry/markAll",
async (arg, thunkApi) => {
const endpoint = arg.sourceType === "category" ? client.category.markEntries : client.feed.markEntries
await endpoint(arg.req)
thunkApi.dispatch(reloadTree())
}
)
export const selectEntry = createAsyncThunk<void, Entry, { state: RootState }>("entries/entry/select", (arg, thunkApi) => {
const state = thunkApi.getState()
const entry = state.entries.entries.find(e => e.id === arg.id)
@@ -146,10 +152,12 @@ export const entriesSlice = createSlice({
e.read = action.meta.arg.read
})
})
builder.addCase(markAllEntries.pending, state => {
state.entries.forEach(e => {
e.read = true
})
builder.addCase(markAllEntries.pending, (state, action) => {
state.entries
.filter(e => (action.meta.arg.req.olderThan ? e.date < action.meta.arg.req.olderThan : true))
.forEach(e => {
e.read = true
})
})
builder.addCase(loadEntries.pending, (state, action) => {
state.source = action.meta.arg

View File

@@ -2,7 +2,8 @@ import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit"
import { client } from "app/client"
import { Category, CollapseRequest } from "app/types"
import { visitCategoryTree } from "app/utils"
import { markAllEntries, markEntry } from "./entries"
// eslint-disable-next-line import/no-cycle
import { markEntry } from "./entries"
import { redirectTo } from "./redirect"
interface TreeState {
@@ -47,26 +48,6 @@ export const treeSlice = createSlice({
})
)
})
builder.addCase(markAllEntries.pending, (state, action) => {
if (!state.rootCategory) return
const { sourceType } = action.meta.arg
const sourceId = action.meta.arg.req.id
visitCategoryTree(state.rootCategory, c => {
if (sourceType === "category" && c.id === sourceId) {
visitCategoryTree(c, c2 =>
c2.feeds.forEach(f => {
f.unread = 0
})
)
} else if (sourceType === "feed") {
c.feeds
.filter(f => f.id === +sourceId)
.forEach(f => {
f.unread = 0
})
}
})
})
builder.addCase(redirectTo, state => {
state.mobileMenuOpen = false
})