Skip to content

Commit

Permalink
timestamps in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
davay42 committed Dec 27, 2020
1 parent 6ca46cb commit 79826dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
37 changes: 23 additions & 14 deletions src/components/log/list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
</header>

<form v-if="state.edit" @submit.prevent>
<input id="date" type="date" v-model="log.date" />
<input type="date" v-model="state.date" />
<input type="time" v-model="state.time" />
<select name="type" v-model="log.type">
<option :value="type" v-for="type in types" :key="type">
{{ type }}
Expand All @@ -29,11 +30,11 @@
<div
class="log"
v-for="log in chronoLogs"
:key="log.date"
:key="log.timestamp"
:class="{ [log.type]: true }"
>
<div class="type">{{ log.type }}</div>
<div class="date">{{ log.date }}</div>
<div class="date">{{ format(log.timestamp) }}</div>
<div class="text">{{ log.text }}</div>
</div>
</transition-group>
Expand All @@ -44,7 +45,7 @@
import { defineComponent, reactive, ref, watchEffect } from "vue";
import { format } from "timeago.js";
import { gun } from "../../store/gun-db";
import { notify } from "../../store/history";
import { error, notify } from "../../store/history";
interface Log {
date: string;
Expand All @@ -59,14 +60,16 @@ export default defineComponent({
},
setup(props) {
const log = reactive({
date: null,
timestamp: Date.now(),
type: "update",
text: "logged",
});
const state = reactive({
open: false,
edit: false,
total: 0,
date: new Date().toLocaleDateString("sv-SE"),
time: `${new Date().getHours()}:${new Date().getMinutes()}`,
});
const types = ["create", "edit", "update", "start", "finish"];
const logs = reactive({});
Expand All @@ -81,16 +84,18 @@ export default defineComponent({
const chronoLogs = ref([]);
watchEffect(() => {
let values = Object.values(logs).filter((l: any) => l.date && l.type);
let values = Object.values(logs).filter(
(l: any) => l.timestamp && l.type
);
state.total = values.length;
let sorted = values.sort((a: any, b: any) => {
if (!a.date) {
if (!a.timestamp) {
return -1;
}
if (!b.date) {
if (!b.timestamp) {
return 1;
}
let diff = Number(Date.parse(b.date)) - Number(Date.parse(a.date));
let diff = b.timestamp - a.timestamp;
return diff;
});
if (!state.open) {
Expand All @@ -101,13 +106,17 @@ export default defineComponent({
});
function addLog() {
if (Date.parse(log.date) > Date.now()) {
notify("Logs are for past events, not future plans.");
if (Date.parse(state.date + "T" + state.time)) {
log.timestamp = Date.parse(state.date + "T" + state.time);
} else {
error("Incorrect date and time");
}
if (log.timestamp > Date.now()) {
error("Logs are for past events, not future plans.");
return;
}
let now = log.date ? log.date : Date.now();
gun.get(props.id).get("log").set(log);
log.date = log.type = log.text = null;
gun.get(props.id).get("log").get(Date.now()).put(log);
state.open = false;
}
return {
chronoLogs,
Expand Down
21 changes: 15 additions & 6 deletions src/use/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export function useItem(id: string) {
gun
.get(id)
.get('log')
.set({
.get(Date.now())
.put({
type: 'edit',
date: now.toLocaleDateString('sv-SE'),
timestamp: Date.now(),
text: `${field} edited`,
})
edit[field] = false
Expand All @@ -56,16 +57,15 @@ export interface Item {

export function generateItem(type: string, data?: any, parent?: string): Item {
const item = { ...data }
let now = new Date()
Object.assign(item, {
title: truncate(data.title) || generateWords(2),
description: data.description || generateWords(100),
type: type,
parent: parent || null,
createdAt: now.getTime(),
createdAt: Date.now(),
log: {
[now.getTime()]: {
date: now.toLocaleDateString('sv-SE'),
[Date.now()]: {
timestamp: Date.now(),
type: 'created',
text: data.title,
},
Expand All @@ -83,6 +83,15 @@ export async function createItem(type: string, data?: any, parent?: string) {
let publicItem = await db.get(type).get(uuid).put(privateItem)
if (parent) {
gun.get(parent).get(type).get(uuid).put(privateItem)
gun
.get(parent)
.get('log')
.get(Date.now())
.put({
timestamp: Date.now(),
type: 'added',
text: `${type} ${generated.title} added.`,
})
}
return soul(publicItem)
} catch (err) {
Expand Down

0 comments on commit 79826dc

Please sign in to comment.