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

View File

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

View File

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