Skip to content

Commit

Permalink
Add "Go to date" dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed Jan 23, 2020
1 parent 06a345e commit 2153628
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/main/i18n/translations/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const translationsDe: Partial<Translations> = {
// Menu (app-specific)
"export": "Exportieren",
"export-to-format": "Als {format} exportieren",
"go-to-date": "Zu Datum gehen",
"import": "Importieren",
"import-from-format": "Von {format} importieren",
"license": "Lizenz",
Expand Down Expand Up @@ -70,6 +71,7 @@ const translationsDe: Partial<Translations> = {
"allow-future-entries": "Einträge in der Zukunft erlauben",
"diary-entries": "Tagebucheinträge",
"no": "Nein",
"ok": "OK",
"reset-diary": "Tagebuch zurücksetzen",
"reset-diary-confirm": "Ja, ich bin mir sicher",
"reset-diary-msg": "Bist du sicher, dass du dein Tagebuch zurücksetzen möchtest? Diese Aktion wird all deine Einträge unwiderruflich löschen.",
Expand Down
2 changes: 2 additions & 0 deletions src/main/i18n/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const translationsEn: Translations = {
// Menu (app-specific)
"export": "Export",
"export-to-format": "Export to {format}",
"go-to-date": "Go to Date",
"import": "Import",
"import-from-format": "Import from {format}",
"license": "License",
Expand Down Expand Up @@ -70,6 +71,7 @@ const translationsEn: Translations = {
"allow-future-entries": "Allow entries in the future",
"diary-entries": "Diary entries",
"no": "No",
"ok": "OK",
"reset-diary": "Reset diary",
"reset-diary-confirm": "Yes, I am sure",
"reset-diary-msg": "Are you sure you want to reset your diary? This will delete all of your content. The data cannot be restored.",
Expand Down
8 changes: 2 additions & 6 deletions src/main/ipcMain/senders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ export const lock = (): void => {

// Overlays

export const showPrefOverlay = (): void => {
getWindow().webContents.send("showPrefOverlay");
};

export const showStatsOverlay = (): void => {
getWindow().webContents.send("showStatsOverlay");
export const openOverlay = (overlayType: OverlayType): void => {
getWindow().webContents.send("openOverlay", overlayType);
};
1 change: 1 addition & 0 deletions src/main/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const DISABLED_MENU_ITEMS = [
"exportMd",
"exportPdf",
"exportTxtDayOne",
"goToDate",
"importJsonDayOne",
"importJsonJrnl",
"importJsonMiniDiary",
Expand Down
4 changes: 2 additions & 2 deletions src/main/menu/menus/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
importJsonMiniDiary,
importTxtDayOne,
lock,
showStatsOverlay,
openOverlay,
} from "../../ipcMain/senders";

export default function getFileMenu(): MenuItemConstructorOptions {
Expand Down Expand Up @@ -99,7 +99,7 @@ export default function getFileMenu(): MenuItemConstructorOptions {
label: translate("statistics"),
id: "statistics",
click(): void {
showStatsOverlay();
openOverlay("statistics");
},
},
],
Expand Down
9 changes: 9 additions & 0 deletions src/main/menu/menus/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ import {
setDaySelectedPrevious,
setMonthSelectedNext,
setMonthSelectedPrevious,
openOverlay,
} from "../../ipcMain/senders";

export default function getViewMenu(): MenuItemConstructorOptions {
return {
label: translate("view"),
submenu: [
{
label: `${translate("go-to-date")}…`,
id: "goToDate",
click(): void {
openOverlay("go-to-date");
},
},
{ type: "separator" },
{
label: translate("previous-day"),
id: "previousDay",
Expand Down
4 changes: 2 additions & 2 deletions src/main/menu/preferencesItem.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { MenuItemConstructorOptions } from "electron";

import { translate } from "../i18n/i18n";
import { showPrefOverlay } from "../ipcMain/senders";
import { openOverlay } from "../ipcMain/senders";

export default function getPreferencesItem(): MenuItemConstructorOptions {
return {
label: `${translate("preferences")}…`,
id: "preferences",
accelerator: "CmdOrCtrl+,",
click(): void {
showPrefOverlay();
openOverlay("preferences");
},
};
}
3 changes: 3 additions & 0 deletions src/renderer/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { Component, ReactNode } from "react";

import { toggleWindowSize } from "../electron/window";
import { translations } from "../utils/i18n";
import GoToDateOverlayContainer from "./overlays/go-to-date-overlay/GoToDateOverlayContainer";
import ImportOverlayContainer from "./overlays/import-overlay/ImportOverlayContainer";
import PrefOverlayContainer from "./overlays/pref-overlay/PrefOverlayContainer";
import StatsOverlayContainer from "./overlays/stats-overlay/StatsOverlayContainer";
Expand Down Expand Up @@ -38,6 +39,8 @@ export default class App extends Component<Props, State> {
switch (overlay) {
case "none":
return null;
case "go-to-date":
return <GoToDateOverlayContainer />;
case "import":
return <ImportOverlayContainer />;
case "preferences":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import logger from "electron-log";
import React, { ChangeEvent, FormEvent, ReactElement, useState } from "react";

import { momentIndex, toIndexDate } from "../../../utils/dateFormat";
import { translations } from "../../../utils/i18n";
import OverlayContainer from "../overlay-hoc/OverlayContainer";

export interface StateProps {
allowFutureEntries: boolean;
dateSelected: Date;
}

export interface DispatchProps {
closeOverlay: () => void;
setDateSelected: (date: Date) => void;
}

type Props = StateProps & DispatchProps;

/**
* Dialog for quickly jumping to a certain date.
*/
export default function GoToDateOverlay(props: Props): ReactElement {
const { allowFutureEntries, closeOverlay, dateSelected, setDateSelected } = props;

const todayFormatted = toIndexDate(new Date());
const dateSelectedFormatted = toIndexDate(dateSelected);

// `date` can become `undefined` when the user's date input is incomplete (e.g. year not filled in
// yet
const [date, setDate] = useState<string | undefined>(dateSelectedFormatted);

const onChange = (event: ChangeEvent<HTMLInputElement>): void => setDate(event.target.value);

const onSubmit = (event: FormEvent<HTMLFormElement>): void => {
event.preventDefault();
if (!date) {
logger.error("Cannot go to date: Date is not defined");
} else {
setDateSelected(momentIndex(date).toDate());
closeOverlay();
}
};

const canSubmit =
date && // `date` must be defined (i.e. user must have provided a valid date input)
date !== dateSelectedFormatted && // Selected date cannot be the currently selected one
(allowFutureEntries || date <= todayFormatted); // Disallow future dates if option is set

return (
<OverlayContainer className="go-to-date-overlay">
<form onSubmit={onSubmit}>
<h1>{translations["go-to-date"]}</h1>
<label htmlFor="input-go-to-date">
Date:
<input
type="date"
id="input-go-to-date"
defaultValue={dateSelectedFormatted}
onChange={onChange}
/>
</label>
<button type="submit" className="button button-main" disabled={!canSubmit}>
{translations.ok}
</button>
</form>
</OverlayContainer>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { connect } from "react-redux";

import { closeOverlay } from "../../../store/app/actionCreators";
import { SetOverlayAction } from "../../../store/app/types";
import { setDateSelected } from "../../../store/diary/actionCreators";
import { SetDateSelectedAction } from "../../../store/diary/types";
import { RootState, ThunkDispatchT } from "../../../store/store";
import GoToDateOverlay, { DispatchProps, StateProps } from "./GoToDateOverlay";

const mapStateToProps = (state: RootState): StateProps => ({
allowFutureEntries: state.app.allowFutureEntries,
dateSelected: state.diary.dateSelected,
});

const mapDispatchToProps = (dispatch: ThunkDispatchT): DispatchProps => ({
closeOverlay: (): SetOverlayAction => dispatch(closeOverlay()),
setDateSelected: (date: Date): SetDateSelectedAction => dispatch(setDateSelected(date)),
});

export default connect(mapStateToProps, mapDispatchToProps)(GoToDateOverlay);
8 changes: 2 additions & 6 deletions src/renderer/electron/ipcRenderer/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,8 @@ function initIpcListeners(): void {

// Overlays

ipcRenderer.on("showPrefOverlay", (): void => {
dispatchThunk(openOverlay("preferences"));
});

ipcRenderer.on("showStatsOverlay", (): void => {
dispatchThunk(openOverlay("statistics"));
ipcRenderer.on("openOverlay", (_, overlayType: OverlayType): void => {
dispatchThunk(openOverlay(overlayType));
});

// Screen lock
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface Metadata {

// Overlay

type OverlayType = "none" | "import" | "preferences" | "statistics";
type OverlayType = "none" | "go-to-date" | "import" | "preferences" | "statistics";

// Theme

Expand Down
2 changes: 2 additions & 0 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface Translations {
// Menu (app-specific)
"export": string,
"export-to-format": string,
"go-to-date": string,
"import": string,
"import-from-format": string,
"license": string,
Expand Down Expand Up @@ -68,6 +69,7 @@ export interface Translations {
"allow-future-entries": string,
"diary-entries": string,
"no": string,
"ok": string,
"reset-diary": string,
"reset-diary-confirm": string,
"reset-diary-msg": string,
Expand Down

0 comments on commit 2153628

Please sign in to comment.