Skip to content
This repository has been archived by the owner on Jan 2, 2022. It is now read-only.

Commit

Permalink
fix(Empty state): Invalid date
Browse files Browse the repository at this point in the history
WDY-272
  • Loading branch information
jcmnunes committed Feb 26, 2020
1 parent a5ebdaa commit a7ca5c3
Show file tree
Hide file tree
Showing 8 changed files with 716 additions and 672 deletions.
1,319 changes: 661 additions & 658 deletions backend/package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"url": "git://github.com/jcmnunes/widy.git"
},
"scripts": {
"start": "node build/index.js",
"dev": "nodemon src/index.ts",
"start": "nodemon src/index.ts",
"start:prod": "node build/index.js",
"build": "tsc -p .",
"lint": "eslint ./src/**/*.{ts,js}",
"sample": "node ./src/data/load-sample-data.js",
"delete": "node ./src/data/load-sample-data.js --delete"
"sample": "ts-node --files src/data/load-sample-data.ts",
"delete": "ts-node --files src/data/load-sample-data.ts --delete"
},
"author": "Jose Nunes",
"license": "MIT",
Expand Down
1 change: 0 additions & 1 deletion backend/src/data/days.json
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,6 @@
"title": "Amet Venenatis Risus Cras",
"notes": "{\"object\":\"value\",\"document\":{\"object\":\"document\",\"data\":{},\"nodes\":[{\"object\":\"block\",\"type\":\"heading-one\",\"data\":{},\"nodes\":[{\"object\":\"text\",\"leaves\":[{\"object\":\"leaf\",\"text\":\"This is a note\",\"marks\":[]}]}]},{\"object\":\"block\",\"type\":\"paragraph\",\"data\":{},\"nodes\":[{\"object\":\"text\",\"leaves\":[{\"object\":\"leaf\",\"text\":\"Perf problems\",\"marks\":[]}]}]},{\"object\":\"block\",\"type\":\"numbered-list\",\"data\":{},\"nodes\":[{\"object\":\"block\",\"type\":\"list-item\",\"data\":{},\"nodes\":[{\"object\":\"text\",\"leaves\":[{\"object\":\"leaf\",\"text\":\"This is a list\",\"marks\":[]}]}]},{\"object\":\"block\",\"type\":\"list-item\",\"data\":{},\"nodes\":[{\"object\":\"text\",\"leaves\":[{\"object\":\"leaf\",\"text\":\"This is another list item\",\"marks\":[]}]}]},{\"object\":\"block\",\"type\":\"list-item\",\"data\":{},\"nodes\":[{\"object\":\"text\",\"leaves\":[{\"object\":\"leaf\",\"text\":\"And yet another item\",\"marks\":[]}]}]},{\"object\":\"block\",\"type\":\"list-item\",\"data\":{},\"nodes\":[{\"object\":\"text\",\"leaves\":[{\"object\":\"leaf\",\"text\":\"One more item\",\"marks\":[]}]}]}]},{\"object\":\"block\",\"type\":\"heading-one\",\"data\":{},\"nodes\":[{\"object\":\"text\",\"leaves\":[{\"object\":\"leaf\",\"text\":\"\",\"marks\":[]}]}]}]}}",
"time": 3000
"time": 0
},
{
"start": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ const mongooseOptions = {
useFindAndModify: false,
};
mongoose.connect(process.env.MONGO_URI, mongooseOptions).then(() => console.log('DB connected'));
mongoose.connection.on('error', err => {
mongoose.connection.on('error', (err: { message: string}) => {
console.log(`DB connection error: ${err.message}`);
});

// import all of our models - they need to be imported only once
const { Day } = require('../models/Day');
const { User } = require('../models/User');
const { DayModel } = require('../models/Day');
const { UserModel } = require('../models/User');

const days = JSON.parse(fs.readFileSync(__dirname + '/days.json', 'utf-8'));
const users = JSON.parse(fs.readFileSync(__dirname + '/users.json', 'utf-8'));

async function deleteData() {
console.log('😢😢 Goodbye Data...');
await Day.remove();
await User.remove();
await DayModel.remove({});
await UserModel.remove({});
console.log('Data Deleted. To load sample data, run\n\n\t npm run sample\n\n');
process.exit();
}

async function loadData() {
try {
await Day.insertMany(days);
await User.insertMany(users);
await DayModel.insertMany(days);
await UserModel.insertMany(users);
console.log('👍👍👍👍👍👍👍👍 Done!');
process.exit();
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ const ActiveTaskPopup = ({
) : null;
};

ActiveTaskPopup.defaultProps = {
selectedDayId: null,
};

ActiveTaskPopup.propTypes = {
activeTask: PropTypes.shape({
taskId: PropTypes.string.isRequired,
Expand All @@ -96,7 +100,7 @@ ActiveTaskPopup.propTypes = {
time: PropTypes.number.isRequired,
start: PropTypes.string,
}).isRequired,
selectedDayId: PropTypes.string.isRequired,
selectedDayId: PropTypes.string,
storeSelectedDay: PropTypes.func.isRequired,
getDay: PropTypes.func.isRequired,
stopTask: PropTypes.func.isRequired,
Expand Down
32 changes: 32 additions & 0 deletions frontend/src/helpers/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,35 @@ export const saveItem = (key, value) => {
// Ignore errors
}
};

/**
* Removes item from localstorage
*/
export const removeItem = key => {
try {
localStorage.removeItem(key);
} catch (err) {
// Ignore errors
}
};

/**
* Checks if the saved day id in localStorage is valid (if it can
* be selected)
*
* @param validDayIds {string[]} - List of day ids
*/
export const validateSelectedDayId = validDayIds => {
// If the saved selected day id is not in the days
// list ➜ remove it from localStorage
const savedSelectedDayId = loadItem('selectedDayId');
if (!savedSelectedDayId) return true;

const isDayValid = validDayIds.includes(savedSelectedDayId);

if (!isDayValid) {
removeItem('selectedDayId');
}

return isDayValid;
};
3 changes: 3 additions & 0 deletions frontend/src/sagas/days/createDay.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { call, put, takeLatest } from 'redux-saga/effects';
import moment from 'moment';
import { createDay } from '../../api/days';
import * as types from '../../actions/days/types';
import { saveItem } from '../../helpers/localStorage';

export function* createDaySaga() {
try {
Expand All @@ -11,6 +12,8 @@ export function* createDaySaga() {

yield put({ type: types.CREATE_DAY_SUCCESS, day });
yield put({ type: types.GET_DAY_REQUEST, payload: day.id });

saveItem('selectedDayId', day.id);
} catch (error) {
yield put({ type: types.CREATE_DAY_FAILURE, error });
}
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/sagas/days/getDays.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { call, put, takeLatest } from 'redux-saga/effects';
import { getDays } from '../../api/days';
import * as types from '../../actions/days/types';
import * as activeTaskTypes from '../../actions/activeTask/types';
import { loadItem } from '../../helpers/localStorage';
import { loadItem, validateSelectedDayId } from '../../helpers/localStorage';

const normalize = data => {
const normalized = {
Expand All @@ -22,7 +22,10 @@ export function* getDaysSaga() {
const { data } = yield call(getDays);
const { byId, order } = normalize(data);

validateSelectedDayId(order);

const selectedDayId = loadItem('selectedDayId') || order[0];

yield put({ type: types.GET_DAYS_SUCCESS, byId, order, selectedDayId });
yield put({ type: activeTaskTypes.ACTIVE_TASK_REQUEST });
if (selectedDayId && order.length > 0) {
Expand Down

0 comments on commit a7ca5c3

Please # to comment.