show new words this week instead of this month

This commit is contained in:
adueck 2024-03-06 14:03:37 -05:00
parent e09f3b7d14
commit 1a3d89f63f
3 changed files with 29 additions and 18 deletions

View File

@ -203,7 +203,7 @@ class App extends Component<RouteComponentProps, State> {
}
if (this.props.location.pathname === "/new-entries") {
this.setState({
results: dictionary.getNewWordsThisMonth(),
results: dictionary.getNewWords("week"),
page: 1,
});
}
@ -335,7 +335,7 @@ class App extends Component<RouteComponentProps, State> {
}
if (this.props.location.pathname === "/new-entries") {
this.setState({
results: dictionary.getNewWordsThisMonth(),
results: dictionary.getNewWords("week"),
page: 1,
});
}
@ -647,7 +647,7 @@ class App extends Component<RouteComponentProps, State> {
to="/new-entries"
className="plain-link font-weight-light"
>
<div className="my-4">New words this month</div>
<div className="my-4">New words this week</div>
</Link>
<div className="my-4 pt-3">
<Link

View File

@ -83,20 +83,27 @@ function getExpForInflections(input: string, index: "p" | "f"): RegExp {
return new RegExp(`^${base}[و|ې|ه]?`);
}
function tsOneMonthBack(): number {
// https://stackoverflow.com/a/24049314/8620945
const d = new Date();
const m = d.getMonth();
d.setMonth(d.getMonth() - 1);
function tsBack(period: "month" | "week"): number {
if (period === "month") {
// https://stackoverflow.com/a/24049314/8620945
const d = new Date();
const m = d.getMonth();
d.setMonth(d.getMonth() - 1);
// If still in same month, set date to last day of
// previous month
if (d.getMonth() === m) d.setDate(0);
d.setHours(0, 0, 0);
d.setMilliseconds(0);
// If still in same month, set date to last day of
// previous month
if (d.getMonth() === m) d.setDate(0);
d.setHours(0, 0, 0);
d.setMilliseconds(0);
// Get the time value in milliseconds and convert to seconds
return d.getTime();
// Get the time value in milliseconds and convert to seconds
return d.getTime();
}
const currentDate = new Date();
const lastWeekDate = new Date(
currentDate.getTime() - 7 * 24 * 60 * 60 * 1000
);
return lastWeekDate.getTime();
}
function alphabeticalLookup({
@ -575,10 +582,12 @@ export const dictionary: DictionaryAPI = {
});
},
exactPashtoSearch: pashtoExactLookup,
getNewWordsThisMonth: function (): T.DictionaryEntry[] {
getNewWords: function (period: "week" | "month"): T.DictionaryEntry[] {
return dictDb.collection
.chain()
.find({ ts: { $gt: tsOneMonthBack() } })
.find({
ts: { $gt: tsBack(period) },
})
.simplesort("ts")
.data()
.reverse();

View File

@ -40,7 +40,9 @@ export type DictionaryAPI = {
exactPashtoSearch: (
search: string
) => import("@lingdocs/ps-react").Types.DictionaryEntry[];
getNewWordsThisMonth: () => import("@lingdocs/ps-react").Types.DictionaryEntry[];
getNewWords: (
period: "month" | "week"
) => import("@lingdocs/ps-react").Types.DictionaryEntry[];
findOneByTs: (
ts: number
) => import("@lingdocs/ps-react").Types.DictionaryEntry | undefined;