From f3745fe4bb0c379dd164f00b3b228fe612f80966 Mon Sep 17 00:00:00 2001 From: Lourdesjupo Date: Fri, 1 Mar 2024 19:58:38 +0100 Subject: [PATCH] Create a new dateBase fix errors correct time errors add a timezone in the backend to indicate that the time is UTC. Fix errors in trackList endpoints and add times and counters. --- server/src/index.js | 17 +++--- src/components/AddTask_TrackTime.jsx | 7 +-- src/components/TrackedTimeItem.jsx | 84 +++++++++------------------- src/components/TrackedTimeList.jsx | 4 +- src/services/ApiAllTracksAndTime.js | 7 --- src/services/ApiOneTime.js | 2 +- src/services/ApiOneTimeAdd.js | 2 +- src/services/ApiTrackTimeRecord.js | 10 +++- src/services/ApiTrackedTime.js | 8 ++- src/services/CalculateTime.js | 1 + 10 files changed, 56 insertions(+), 86 deletions(-) diff --git a/server/src/index.js b/server/src/index.js index 3f0cdb7..13e62fe 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -35,7 +35,8 @@ async function connectDb() { user: process.env.DB_USER, password: process.env.DB_PASS, database: process.env.DB_DATABASE_NAME, - port:process.env.DB_PORT + port:process.env.DB_PORT, + timezone: 'Z' }); await connection.connect(); @@ -67,7 +68,7 @@ server.post('/api/addonetaskchecked', async (req, res) => { console.log('llamo'); const body = req.body; let insert = - 'INSERT INTO oneTaskCompleted (fkOneTask) VALUE (?)'; + 'INSERT INTO oneTaskCompleted (fkoneTask) VALUE (?)'; const connect = await connectDb(); const [resultInsert] = await connect.query(insert, [ body.id @@ -82,7 +83,7 @@ server.post('/api/addonetaskchecked', async (req, res) => { server.get('/api/getonetimetasks', async (req,res)=>{ const connect = await connectDb(); //const selectAll = "SELECT * from oneTask " - const selectAll = "SELECT oneTask.id,oneTask.name,oneTask.iconName,oneTask.createdAt,oneTask.alertQty,oneTask.alertUnits,completedDate FROM oneTask LEFT JOIN( SELECT fkOneTask, MAX(completedDate) as completedDate FROM oneTaskCompleted GROUP BY fkOneTask) lastComplete ON oneTask.id = lastComplete.fkOneTask" + const selectAll = "SELECT oneTask.idoneTask,oneTask.name,oneTask.iconName,oneTask.createdAt,oneTask.alertQty,oneTask.alertUnits,completedDate FROM oneTask LEFT JOIN( SELECT fkoneTask, MAX(completedDate) as completedDate FROM oneTaskCompleted GROUP BY fkoneTask) lastComplete ON oneTask.idoneTask = lastComplete.fkoneTask" const[tasks] = await connect.query(selectAll) @@ -95,7 +96,7 @@ server.post('/api/addonetaskchecked', async (req, res) => { server.get('/api/getonetimecompletedata/:id', async (req, res) => { const id = req.params.id; const connect = await connectDb(); - const selectAll = `select * FROM oneTask, oneTaskCompleted WHERE oneTask.id = oneTaskCompleted.fkOneTask AND id = ${id} order by completedDate DESC LIMIT 1`; + const selectAll = `select * FROM oneTask, oneTaskCompleted WHERE oneTask.idoneTask = oneTaskCompleted.fkoneTask AND id = ${id} order by completedDate DESC LIMIT 1`; const [tasks] = await connect.query(selectAll); console.log(tasks); @@ -127,7 +128,7 @@ server.post('/api/addtracktask', async (req, res) => { //Get lista de tareas TrackList server.get('/api/tracktasklist', async (req, res) => { - const selectAll = 'SELECT * from trackList '; + const selectAll = 'SELECT tl.idtrackList, tl.nameTask, tl.color, tl.estimatedTime, tl.elapsedTime, tt.startTime FROM trackList as tl LEFT JOIN trackTime as tt ON tl.idtrackList = tt.fktrackList WHERE tt.stopTime is NULL'; const connect = await connectDb(); const [result] = await connect.query(selectAll); console.log(result); @@ -143,8 +144,8 @@ server.post('/api/addtracktask', async (req, res) => { const connect = await connectDb(); let insert if(req.body.clicked === 'start'){ - insert = 'INSERT INTO trackTime (fkTrackList,startTime) VALUES (?, ?)'; - const [resultInsert] = await connect.query(insert, [req.body.idTimeTrack, hoy]) + insert = 'INSERT INTO trackTime (fktrackList,startTime) VALUES (?, ?)'; + const [resultInsert] = await connect.query(insert, [req.body.idtrackTime, hoy]) res.json( resultInsert ); @@ -168,7 +169,7 @@ server.post('/api/addtracktask', async (req, res) => { server.get('/api/allTracks/:id', async (req, res) => { const id = req.params.id; - const allTaskTimes = `select * FROM trackTime, trackList WHERE trackList.id = trackTime.fkTrackList AND id= ? order by stopTime desc LIMIT 1;`; + const allTaskTimes = `select * FROM trackTime, trackList WHERE trackList.idtrackList = trackTime.fktrackList AND id= ? order by stopTime desc LIMIT 1;`; const connect = await connectDb(); const [result] = await connect.query(allTaskTimes, [id]); connect.end(); diff --git a/src/components/AddTask_TrackTime.jsx b/src/components/AddTask_TrackTime.jsx index 3a4c61a..543cde1 100644 --- a/src/components/AddTask_TrackTime.jsx +++ b/src/components/AddTask_TrackTime.jsx @@ -4,6 +4,8 @@ import { useState } from 'react'; import addTrackTask from '../services/ApiTrackTimeAdd'; import { Link } from 'react-router-dom'; +// ESTIMATED TIME = ESTIMATED TIME MINS (El tiempo está calculado en minutos.) + function AddTask_TrackTime() { const [color, setColor] = useState('#ffffff'); const [mins, setMins] = useState(0); @@ -14,11 +16,6 @@ function AddTask_TrackTime() { const mins2 =parseInt(mins) const total = hoursInMinutes + mins2 - console.log('total', total) - - - - const handleName = (ev) => { setName(ev.target.value); }; diff --git a/src/components/TrackedTimeItem.jsx b/src/components/TrackedTimeItem.jsx index a1c5b0a..8c541fe 100644 --- a/src/components/TrackedTimeItem.jsx +++ b/src/components/TrackedTimeItem.jsx @@ -14,57 +14,25 @@ import addTimeTrack from '../services/ApiTrackTimeRecord'; import allTracks from '../services/ApiAllTracksAndTime'; import { useEffect } from 'react'; -function TrackedTimeItem({ id, nameTask, color, estimatedTime, totalTime }) { - const [record, setRecord] = useState('play'); - const [recordText, setRecordText] = useState('▶ Iniciar trabajo'); +function TrackedTimeItem({ id, nameTask, color, estimatedTime, elapsedTime, startTime }) { + const [taskStatus, setTaskStatus] = useState(startTime ? 'running':'stopped'); +// console.log('tipo de startTime',typeof startTime, 'startTime:', startTime) + const [now, setNow] = useState (new Date()) + const sessionTimeSecs = Math.round((now - startTime)/1000) + const totalMinutes = Math.floor(sessionTimeSecs / 60) + const hours = Math.floor(totalMinutes / 60) + const minutes = totalMinutes % 60 + const secs = sessionTimeSecs % 60 + const timer = `${hours < 10 ? '0' : ''}${hours} : ${minutes < 10 ? '0' : ''}${minutes} : ${secs < 10 ? '0' : ''}${secs}` - //{id: start: , stop: } - const [timer, setTimer] = useState({ + useEffect(()=>{ + setInterval(()=>{ + setNow(new Date()) + },60000) + },[]) - }); - const [currentTask, setCurrentTask] = useState({}); - const [startTime, setStartTime] = useState(''); - const [diff, setDiff] = useState('') - //const now = ; - console.log('DIFFF', diff ) + const handleClick = (id) => { - // useEffect(()=>{ - // if(diff) - // },[]) - - - - const handleRecord = (id) => { - if (record === 'play') { - setRecord('stop'); - //Envío el id y que se ha clickado start - addTimeTrack({ clicked: 'start', idTimeTrack: id }); - //const now = new Date().getTime() - // setInitialTime({id, startTime:now}) - //Me traigo todas las tareas y las seteo en allTasks - allTracks(id).then((data) => { - setCurrentTask(data); - const date = new Date(data.startTime); - const miliseconds = date.getTime(); - setStartTime(miliseconds); - setDiff(new Date(+new Date() - startTime)) - //const mili = miliseconds.getTime() - // console.log('datitos', miliseconds); - - setTimer({hours:'00',minutes:'00',seconds:'00'}) - }); - - return setRecordText('▢ Detener trabajo'); - } else { - setRecord('play'); - addTimeTrack({ clicked: 'stop', idTimeTrack: id }); - allTracks().then((data) => { - //setAllTasks(data); - }); - // const now = new Date().getTime() - //setInitialTime({id, startTime:now}) - return setRecordText('▶ Iniciar trabajo'); - } }; return ( @@ -89,25 +57,27 @@ function TrackedTimeItem({ id, nameTask, color, estimatedTime, totalTime }) { {/* */} - - Tiempo estimado finalización tarea: {estimatedTime} - - Tiempo restante: {estimatedTime - totalTime} + {estimatedTime !== 0 && + (<> + Tiempo estimado finalización tarea: {estimatedTime} + Tiempo restante: {estimatedTime - elapsedTime} - Tiempo de trabajo: {totalTime} - + ) + } + {taskStatus === 'running' && (Tiempo de trabajo: {timer}) } + {/* {timer.hours}: {timer.minutes}:{timer.seconds} - + */} diff --git a/src/components/TrackedTimeList.jsx b/src/components/TrackedTimeList.jsx index 5771f84..572de87 100644 --- a/src/components/TrackedTimeList.jsx +++ b/src/components/TrackedTimeList.jsx @@ -10,6 +10,7 @@ function TrackedTimeList(){ console.log('listas de tareasm', listTasks) useEffect (()=>{ getTrackedListTasks().then((trackedListTask)=>{ + // @TODO: PONER VALIDACIONES DEL TRACKED LIST EJ;que los tipos sean correctos y que existan los datos (campos no vacios) setListTasks(trackedListTask) }) @@ -27,7 +28,8 @@ console.log('listas de tareasm', listTasks) nameTask={task.name} color={task.color} estimatedTime={task.estimatedTime} - totalTime={task.totalTime} + elapsedTime={task.elapsedTime} + startTime={task.startTime} /> ) })} diff --git a/src/services/ApiAllTracksAndTime.js b/src/services/ApiAllTracksAndTime.js index 3e7d56c..5379bec 100644 --- a/src/services/ApiAllTracksAndTime.js +++ b/src/services/ApiAllTracksAndTime.js @@ -3,13 +3,6 @@ const allTracks = async (id)=>{ `${import.meta.env.VITE_CHRONOLOGICA_API}/api/allTracks/${id}` ); const dataJson = await response.json(); - - // const start = dataJson[0].startTime - - // const time = start.split('T') - // const hours = time[1].split('.') - // const[horas, minutos, segundos] = hours[0].split(':') - const time = dataJson[0].startTime; console.log('tiempo', time); diff --git a/src/services/ApiOneTime.js b/src/services/ApiOneTime.js index 78a7252..78b516a 100644 --- a/src/services/ApiOneTime.js +++ b/src/services/ApiOneTime.js @@ -7,7 +7,7 @@ export const getOneTimeListTasks = async () => { const dataJson = await response.json(); const result = dataJson.map((task) => { return { - id: task.id, + id: task.idoneTask, name: task.name, icon: task.iconName, create: task.createdAt, diff --git a/src/services/ApiOneTimeAdd.js b/src/services/ApiOneTimeAdd.js index 5775d32..f3e9647 100644 --- a/src/services/ApiOneTimeAdd.js +++ b/src/services/ApiOneTimeAdd.js @@ -1,6 +1,6 @@ function AddOneTask(data) { -return fetch(`${import.meta.env.CHRONOLOGICA_API}/api/addonetask`, { +return fetch(`${import.meta.env.VITE_CHRONOLOGICA_API}/api/addonetask`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(data), diff --git a/src/services/ApiTrackTimeRecord.js b/src/services/ApiTrackTimeRecord.js index c826fc2..0ff1d18 100644 --- a/src/services/ApiTrackTimeRecord.js +++ b/src/services/ApiTrackTimeRecord.js @@ -1,8 +1,12 @@ -function addTimeTrack (param) { - fetch(`${import.meta.env.CHRONOLOGICA_API}/api/addTimeTrack`, { + + + +function addTimeTrack (command, id) { + console.log( command, id ); + fetch(`${import.meta.env.VITE_CHRONOLOGICA_API}/api/addTimeTrack`, { method: 'POST', headers: { 'content-type': 'application/json' }, - body: JSON.stringify(param), + body: JSON.stringify(command, id ), }) .then((response) => response.json()) .then((result) => { diff --git a/src/services/ApiTrackedTime.js b/src/services/ApiTrackedTime.js index adc8987..1e2ecb3 100644 --- a/src/services/ApiTrackedTime.js +++ b/src/services/ApiTrackedTime.js @@ -5,10 +5,12 @@ export const getTrackedListTasks = async ()=> { const dataJson= await response.json() const result = dataJson.map((task) => { return { - id: task.id, + id: task.idtrackList, name: task.nameTask, - color:task.color, - estimatedTime: task.estimatedTime + color: task.color, + estimatedTime: task.estimatedTime, + elapsedTime: task.elapsedTime, + startTime: task.startTime ? new Date(task.startTime) : undefined }; }); diff --git a/src/services/CalculateTime.js b/src/services/CalculateTime.js index 09d2f68..a357ec6 100644 --- a/src/services/CalculateTime.js +++ b/src/services/CalculateTime.js @@ -28,6 +28,7 @@ function CalculateTime(response) { } lastClick = new Date(response.lastComplete); + //console.log('lastClick:', lastClick, 'and last complete: ', response.lastComplete) const result = sumarDias(lastClick, daysForReminder);