Skip to content

Commit 36c4502

Browse files
committed
Saving inputs to browser storage
1 parent 71d7f5c commit 36c4502

File tree

4 files changed

+91
-10
lines changed

4 files changed

+91
-10
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
# `Daily Time Recording`
1+
# Daily Time Recording
22

33
Simple utility to calculate how much time you have left to record today.
44

5+
The start time & duration are kept in browser storage. The recorded duration is also kept but cleared every day.
6+
7+
# Operation
8+
59
Inputs:
610

711
- Start Time
812
- Target Duration
913
- Break Duration
1014
- Break Taken Yes/No
1115
- Recorded Duration
16+
17+
Output:
18+
1219
- Remaining Duration
1320

1421
Calculation:
@@ -21,8 +28,4 @@ Remaining = Now - Start - Break (if taken) - Recorded
2128

2229
- `npm run build` - Builds for production, emitting to `dist/`
2330

24-
- `npm run preview` - Starts a server at http://localhost:4173/ to test production build locally
25-
26-
# Todo
27-
28-
- Save start, target & break duration in browser storage
31+
- `npm run preview` - Starts a server at http://localhost:4173/ to test production build locally

src/index.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import './style.css';
1010
import {durationRe} from "./duration.js";
1111
import Duration from "./Duration.jsx";
1212
import calculateRemaining from "./calc.js";
13+
import {getStoredRecordedTime, getStoredDefaults, setStoredRecordedTime, setStoredDefaults} from "./store.js";
1314

1415
const schema = yup.object({
1516
startTime: yup.string().matches(timeRe, {message: '12 or 24 hr time format required'}).required(),
@@ -30,7 +31,8 @@ export function App() {
3031
startTime: '09:00',
3132
target: '7.5h',
3233
break: '1h',
33-
recorded: ''
34+
recorded: getStoredRecordedTime(),
35+
...getStoredDefaults()
3436
},
3537
resolver: yupResolver(schema)
3638
});
@@ -49,6 +51,10 @@ export function App() {
4951
}
5052
setRemaining(calculateRemaining(currentData));
5153

54+
// save new values to storage
55+
setStoredDefaults({ startTime: currentData.startTime, target: currentData.target });
56+
setStoredRecordedTime(currentData.recorded);
57+
5258
const interval = setInterval(() => {
5359
setRemaining(calculateRemaining(currentData));
5460
}, 60000);
@@ -68,6 +74,10 @@ export function App() {
6874
Daily Time Tracking
6975
</div>
7076
<div class="title-bar-controls">
77+
<button type="button"
78+
aria-label="Help"
79+
title="Open in GitHub - https://github.com/sam159/daily-time-recording"
80+
onClick={() => window.open("https://github.com/sam159/daily-time-recording", "_blank")} />
7181
<button type="button" aria-label="Close" />
7282
</div>
7383
</div>

src/store.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import * as yup from "yup";
2+
3+
const DEFAULTS_KEY = 'defaults';
4+
const RECORDED_KEY = 'recorded';
5+
6+
const defaultsSchema = yup.object({
7+
startTime: yup.string().required(),
8+
target: yup.string().required(),
9+
}).required();
10+
11+
const recordedSchema = yup.object({
12+
recorded: yup.string().required(),
13+
updated: yup.number().required().nullable()
14+
}).required();
15+
16+
function getJsonValue(key, defaultValue = {}) {
17+
const item = localStorage.getItem(key);
18+
if (item != null) {
19+
try {
20+
const data = JSON.parse(item);
21+
if (typeof data === 'object') {
22+
return data;
23+
}
24+
} catch (e) {
25+
localStorage.removeItem(key);
26+
return defaultValue;
27+
}
28+
}
29+
return defaultValue;
30+
}
31+
32+
export function getStoredDefaults() {
33+
const value = getJsonValue(DEFAULTS_KEY);
34+
try {
35+
return defaultsSchema.validateSync(value);
36+
} catch (e) {
37+
return {};
38+
}
39+
}
40+
41+
export function setStoredDefaults(data) {
42+
localStorage.setItem(DEFAULTS_KEY, JSON.stringify(data));
43+
}
44+
45+
export function getStoredRecordedTime() {
46+
const info = getJsonValue(RECORDED_KEY, { recorded: '', updated: null });
47+
try {
48+
const data = recordedSchema.validateSync(info);
49+
if (data.updated != null) {
50+
const startOfDay = new Date();
51+
startOfDay.setHours(0, 0, 0, 0);
52+
53+
if (data.updated < startOfDay.valueOf()) {
54+
return '';
55+
}
56+
}
57+
return data.recorded;
58+
} catch (e) {
59+
return '';
60+
}
61+
}
62+
63+
export function setStoredRecordedTime(recorded) {
64+
localStorage.setItem(RECORDED_KEY, JSON.stringify({ recorded, updated: new Date().valueOf() }));
65+
}

src/time.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ export function minsToTime(totalMinutes) {
4040
let hours = Math.floor(absMinutes / 60);
4141
let minutes = absMinutes % 60;
4242
if (minutes > 0) {
43-
return `${prefix}${hours}h ${minutes}m`;
43+
if (hours > 0) {
44+
return `${prefix}${hours}h ${minutes}m`;
45+
}
46+
return `${prefix}${minutes}m`;
4447
}
4548
if (hours > 0) {
46-
return `${hours}h`;
49+
return `${prefix}${hours}h`;
4750
}
48-
return '0m';
51+
return `${prefix}0m`;
4952
}

0 commit comments

Comments
 (0)