Skip to content

Commit

Permalink
Merge pull request #222 from mikebarkmin/19-dealing-with-the-data
Browse files Browse the repository at this point in the history
19 dealing with the data
  • Loading branch information
mikebarkmin authored Jun 26, 2020
2 parents b5df6b3 + b408600 commit 96166a1
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ At the moment these activities can be tracked:
* Sleep
* Measurement

The data can be exported by clicking export in the bottom right-hand corner.

[Screenshots at the features page](https://github.com/mikebarkmin/baby-tracker/wiki/Features)

## Translations
Expand Down
41 changes: 39 additions & 2 deletions client/src/components/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { lighten } from 'polished';
import packageJSON from '../../package.json';
import useLocalStorage from '../hooks/useLocalStorage';
import useSocket from '../hooks/useSocket';
import { Trans } from '@lingui/macro';
import Button from './Button';

const Flex = styled.div`
display: flex;
Expand All @@ -19,7 +21,7 @@ const Root = styled.footer`
bottom: 0px;
height: 30px;
right: 0px;
background-color: ${props => lighten(0.5, props.theme.secondary)};
background-color: ${(props) => lighten(0.5, props.theme.secondary)};
align-items: center;
font-size: 0.8rem;
color: grey;
Expand All @@ -30,12 +32,14 @@ const OnlineIcon = styled.div`
height: 10px;
margin: 8px;
border-radius: 50%;
background-color: ${props =>
background-color: ${(props) =>
props.online ? props.theme.success : props.theme.error};
`;

function OnlineStatus() {
const [online, setOnline] = useState(false);
const [exporting, setExporting] = useState(false);
const [baby] = useLocalStorage('baby', null);
const socket = useSocket();

socket.on('connect', () => {
Expand All @@ -45,9 +49,42 @@ function OnlineStatus() {
socket.on('disconnect', () => {
setOnline(socket.connected);
});

function exportData() {
if (!exporting) {
setExporting(true);
socket.emit('baby/export', (d) => {
if (d.data) {
const blob1 = new Blob([JSON.stringify(d.data, null, 2)], {
type: 'text/plain;charset=utf-8',
});
const url = window.URL || window.webkitURL;
const link = url.createObjectURL(blob1);
const a = document.createElement('a');
const now = new Date();
a.download = `${
baby.shortId
}-${now.getFullYear()}${now.getMonth()}${now.getDate()}.json`;
a.href = link;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
setExporting(false);
});
}
}

return (
<Flex>
<OnlineIcon online={online} /> {online ? 'Online' : 'Offline'}{' '}
<Button
onClick={exportData}
disabled={exporting}
style={{ padding: 1, borderWidth: 1, fontWeight: 500 }}
>
<Trans>export</Trans>
</Button>
</Flex>
);
}
Expand Down
58 changes: 40 additions & 18 deletions server/src/modules/baby.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Schema, model } from 'mongoose';
import isEmail from 'validator/lib/isEmail';
import shortid from 'shortid';
import diaper from './diaper';
import nursing from './nursing';
import sleep from './sleep';
import food from './food';
import measurement from './measurement';

const familyNames = [
'Funkelndes Hippo',
Expand All @@ -12,7 +17,7 @@ const familyNames = [
'Schöne Fuchs',
'Gewitzte Frosch',
'Knurrige Hamster',
'Weise Panda'
'Weise Panda',
];

const roomsNames = {};
Expand All @@ -39,7 +44,7 @@ function getFreeName(room) {
function freeName(room, name) {
let roomNames = roomsNames[room];
if (roomNames) {
roomsNames[room] = roomNames.filter(n => n !== name);
roomsNames[room] = roomNames.filter((n) => n !== name);
}
}

Expand All @@ -50,16 +55,16 @@ export const schema = new Schema(
type: String,
validate: {
validator: isEmail,
message: 'invalid email'
}
message: 'invalid email',
},
},
shortId: {
type: String,
default: shortid.generate
}
default: shortid.generate,
},
},
{
timestamps: true
timestamps: true,
}
);

Expand All @@ -69,35 +74,35 @@ export function handler(socket) {
socket.baby = null;
socket.name = null;

socket.on('baby/create', function(name, callback) {
socket.on('baby/create', function (name, callback) {
const baby = new Model({ name });
baby
.save()
.then(doc => {
.then((doc) => {
socket.emit('baby/new', { baby: doc });
callback({ msg: 'success', baby: doc });
})
.catch(e => {
.catch((e) => {
console.log(e);
callback({ msg: e });
});
});

socket.on('baby/get', function(callback) {
socket.on('baby/get', function (callback) {
if (socket.baby !== null) {
callback({ msg: 'success', baby: socket.baby });
} else {
callback({ msg: 'you need to join a baby first.' });
}
});

socket.on('baby/update', function(baby) {
socket.on('baby/update', function (baby) {
if (socket.baby !== null) {
socket.baby = {
...socket.baby,
...baby,
...socket.baby._id,
...socket.baby.shortId
...socket.baby.shortId,
};
socket.baby.save().then(() => {
socket
Expand All @@ -107,9 +112,9 @@ export function handler(socket) {
}
});

socket.on('baby/join', function(shortId, callback) {
socket.on('baby/join', function (shortId, callback) {
Model.findOne({ shortId })
.then(baby => {
.then((baby) => {
socket.baby = baby;
socket.name = getFreeName(shortId);
socket.join(baby.shortId);
Expand All @@ -121,14 +126,31 @@ export function handler(socket) {
});
});

socket.on('baby/leave', function(callback) {
socket.on('baby/leave', function (callback) {
if (socket.baby !== null) {
socket.leave(socket.baby.shortId);
}
callback({ msg: 'success' });
});

socket.on('disconnect', function() {
socket.on('baby/export', async function (callback) {
if (socket.baby !== null) {
const babyId = socket.baby._id;
const data = {
baby: socket.baby,
diaper: await diaper.Model.find({ babyId }),
food: await food.Model.find({ babyId }),
measurement: await measurement.Model.find({ babyId }),
nursing: await nursing.Model.find({ babyId }),
sleep: await sleep.Model.find({ babyId }),
};
callback({ msg: 'success', data });
} else {
callback({ msg: 'you need to join a baby first.' });
}
});

socket.on('disconnect', function () {
if (socket.baby !== null) {
socket.to(socket.baby.shortId).emit('baby/leaved', { name: socket.name });
freeName(socket.baby.shortId, socket.name);
Expand All @@ -139,5 +161,5 @@ export function handler(socket) {
export default {
schema,
Model,
handler
handler,
};

0 comments on commit 96166a1

Please sign in to comment.