Skip to content

Commit

Permalink
feat: added undo && redu feature with shorcut keys while still inside…
Browse files Browse the repository at this point in the history
… on task list
  • Loading branch information
roldanjr committed Jul 11, 2020
1 parent 2e83d8b commit 2b626ca
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 269 deletions.
12 changes: 6 additions & 6 deletions app/electron/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ function createMainWindow() {
async () => {
try {
if (win) {
const data = await getFromStorage(win, "settings");
if (data.minimizeToTray) {
const data = await getFromStorage(win, "state");
if (data.settings.minimizeToTray) {
if (!isFullScreen) {
win?.hide();
if (tray === null && data.minimizeToTray) {
if (tray === null && data.settings.minimizeToTray) {
createSystemTray();
}
}
Expand All @@ -125,13 +125,13 @@ function createMainWindow() {
e.preventDefault();
try {
if (win) {
const data = await getFromStorage(win, "settings");
if (!data.closeToTray) {
const data = await getFromStorage(win, "state");
if (!data.settings.closeToTray) {
app.exit();
} else {
if (!isFullScreen) {
win?.hide();
if (tray === null && data.closeToTray) {
if (tray === null && data.settings.closeToTray) {
createSystemTray();
}
}
Expand Down
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"react-scripts": "^3.4.1",
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.8",
"redux-undo": "^1.0.1",
"styled-components": "^5.1.1",
"universal-analytics": "^0.4.23",
"uuid": "^8.2.0",
Expand Down
5 changes: 1 addition & 4 deletions app/src/routes/Tasks/TaskDetails/TaskDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import React, {
import { useSelector, useDispatch } from "react-redux";
import {
AppStateTypes,
TaskTypes,
editTaskCard,
editTaskCardText,
removeTaskCard,
Expand Down Expand Up @@ -46,9 +45,7 @@ const TaskDetails = React.forwardRef<HTMLDivElement, Props>(

const dispatch = useDispatch();

const tasks: TaskTypes[] = useSelector(
(state: AppStateTypes) => state.tasks
);
const tasks = useSelector((state: AppStateTypes) => state.tasks.present);

const { openExternalCallback } = useContext(ElectronContext);

Expand Down
32 changes: 28 additions & 4 deletions app/src/routes/Tasks/Tasks.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import React from "react";
import React, { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import { ActionCreators as UndoActionCreator } from "redux-undo";
import { DragDropContext, DropResult, Droppable } from "react-beautiful-dnd";
import {
StyledTaskContainer,
StyledTaskStickySection,
StyledTaskWrapper,
StyledTaskMain,
} from "styles";
import { AppStateTypes, addTaskList, TaskTypes, dragList } from "store";
import { AppStateTypes, addTaskList, dragList } from "store";

import TaskFormButton from "./TaskFormButton";
import TaskInnerList from "./TaskInnerList";

export const Tasks: React.FC = () => {
const tasks: TaskTypes[] = useSelector((state: AppStateTypes) => state.tasks);
const tasks = useSelector((state: AppStateTypes) => state.tasks);

const dispatch = useDispatch();

Expand All @@ -38,6 +39,29 @@ export const Tasks: React.FC = () => {
);
};

useEffect(() => {
function registerUndoRedoKeys(e: KeyboardEvent) {
const activeElement = document.activeElement?.tagName;

if (activeElement !== "INPUT" && activeElement !== "TEXTAREA") {
if (e.ctrlKey && e.key === "z") {
if (tasks.past.length > 0) {
dispatch(UndoActionCreator.undo());
}
}

if (e.ctrlKey && e.key === "Z") {
if (tasks.future.length > 0) {
dispatch(UndoActionCreator.redo());
}
}
}
}

document.addEventListener("keydown", registerUndoRedoKeys);
return () => document.removeEventListener("keydown", registerUndoRedoKeys);
}, [dispatch, tasks.past.length, tasks.future.length]);

return (
<DragDropContext onDragEnd={onDragEnd}>
<StyledTaskMain>
Expand All @@ -48,7 +72,7 @@ export const Tasks: React.FC = () => {
{...provided.droppableProps}
ref={provided.innerRef}
>
<TaskInnerList tasks={tasks} />
<TaskInnerList tasks={tasks.present} />

{provided.placeholder}

Expand Down
3 changes: 1 addition & 2 deletions app/src/routes/Timer/PriorityCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useRef } from "react";
import { useSelector, useDispatch } from "react-redux";
import {
AppStateTypes,
TaskTypes,
setTaskCardDone,
skipTaskCard,
removeTaskCard,
Expand All @@ -27,7 +26,7 @@ import { useTargetOutside } from "hooks";
import { isObjectEmpty } from "utils";

const PriorityCard: React.FC = () => {
const tasks: TaskTypes[] = useSelector((state: AppStateTypes) => state.tasks);
const tasks = useSelector((state: AppStateTypes) => state.tasks.present);

const dispatch = useDispatch();

Expand Down
83 changes: 19 additions & 64 deletions app/src/store/config/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getFromStorage } from "utils";
import {
ConfigTypes,
ConfigActionTypes,
Expand All @@ -11,7 +12,6 @@ import {
SET_THIRD_SPECIAL_BREAK,
SET_FOUTH_SPECIAL_BREAK,
} from "./types";
import { getFromStorage, saveToStorage } from "utils";

const defaultConfig: ConfigTypes = {
stayFocus: 25,
Expand Down Expand Up @@ -42,9 +42,8 @@ const defaultConfig: ConfigTypes = {
},
};

const config = getFromStorage("config")
? getFromStorage("config")
: defaultConfig;
const config =
(getFromStorage("state") && getFromStorage("state").config) || defaultConfig;

const initialState: ConfigTypes = config;

Expand All @@ -53,105 +52,61 @@ export const configReducer = (
action: ConfigActionTypes
) => {
switch (action.type) {
case SET_STAY_FOCUS: {
const newState = {
case SET_STAY_FOCUS:
return {
...state,
stayFocus: action.payload,
};

saveToStorage("config", newState);

return newState;
}
case SET_SHORT_BREAK: {
const newState = {
case SET_SHORT_BREAK:
return {
...state,
shortBreak: action.payload,
};

saveToStorage("config", newState);

return newState;
}
case SET_LONG_BREAK: {
const newState = {
case SET_LONG_BREAK:
return {
...state,
longBreak: action.payload,
};

saveToStorage("config", newState);

return newState;
}
case SET_SESSION_ROUNDS: {
const newState = {
case SET_SESSION_ROUNDS:
return {
...state,
sessionRounds: action.payload,
};

saveToStorage("config", newState);

return newState;
}
case SET_FIRST_SPECIAL_BREAK: {
const newState = {
case SET_FIRST_SPECIAL_BREAK:
return {
...state,
specialBreaks: {
...state.specialBreaks,
firstBreak: action.payload,
},
};

saveToStorage("config", newState);

return newState;
}
case SET_SECOND_SPECIAL_BREAK: {
const newState = {
case SET_SECOND_SPECIAL_BREAK:
return {
...state,
specialBreaks: {
...state.specialBreaks,
secondBreak: action.payload,
},
};

saveToStorage("config", newState);

return newState;
}
case SET_THIRD_SPECIAL_BREAK: {
const newState = {
case SET_THIRD_SPECIAL_BREAK:
return {
...state,
specialBreaks: {
...state.specialBreaks,
thirdBreak: action.payload,
},
};

saveToStorage("config", newState);

return newState;
}
case SET_FOUTH_SPECIAL_BREAK: {
const newState = {
case SET_FOUTH_SPECIAL_BREAK:
return {
...state,
specialBreaks: {
...state.specialBreaks,
fourthBreak: action.payload,
},
};

saveToStorage("config", newState);

return newState;
}
case RESTORE_DEFAULT_CONFIG:
saveToStorage("config", defaultConfig);

return defaultConfig;
default:
saveToStorage("config", state);

return state;
}
};
Loading

0 comments on commit 2b626ca

Please sign in to comment.