From 3de775db5b8407240aaf8e82dec52ec7883def21 Mon Sep 17 00:00:00 2001 From: wajeht <58354193+wajeht@users.noreply.github.com> Date: Sun, 24 Jul 2022 13:51:14 -0400 Subject: [PATCH] feat: the ability to complete session up new session details --- .../api/v1/exercises/exercises.controller.js | 2 + .../components/dashboard/SessionDetails.vue | 268 ++++++++++++++++-- 2 files changed, 251 insertions(+), 19 deletions(-) diff --git a/src/apps/api/v1/exercises/exercises.controller.js b/src/apps/api/v1/exercises/exercises.controller.js index b0c32699..f890079a 100644 --- a/src/apps/api/v1/exercises/exercises.controller.js +++ b/src/apps/api/v1/exercises/exercises.controller.js @@ -115,6 +115,8 @@ export async function patchExerciseNote(req, res) { `Something went wrong while updating a note for gmid: ${body.gains_meta_id}!`, ); + logger.info(`User id: ${body.user_id} has updated gains meta to ${JSON.stringify(body)}!`); + return res.status(StatusCodes.CREATED).json({ status: 'success', request_url: req.originalUrl, diff --git a/src/apps/ui/components/dashboard/SessionDetails.vue b/src/apps/ui/components/dashboard/SessionDetails.vue index e38f4af6..25a06955 100644 --- a/src/apps/ui/components/dashboard/SessionDetails.vue +++ b/src/apps/ui/components/dashboard/SessionDetails.vue @@ -15,7 +15,7 @@ import { // nodejs import dayjs from 'dayjs'; import { v4 as uuidv4 } from 'uuid'; -import { pickBy } from 'lodash-es'; +import { pickBy, pick } from 'lodash-es'; // vue import { ref, reactive, onMounted, watch } from 'vue'; @@ -68,6 +68,9 @@ const addASetExerciseIndex = ref(null); const addASetDismissButton = ref(null); const addASetEnableDisableOtherFields = ref(false); +const completeCurrentSessionShowHideOtherFields = ref(false); +const completeCurrentSessionLoading = ref(false); + // watches // update exercise db as changes in categories watch(chooseExerciseCategoryId, async (currentValue, oldValue) => { @@ -352,17 +355,39 @@ function clearDataAndDismissAddASetModal() { modal.hide(); } +function clearDataAndDismissCompleteCurrentSessionModal() { + const modal = bootstrap.Modal.getOrCreateInstance( + document.getElementById(`complete-current-session-${random_uuid.value}`), + ); + modal.hide(); +} + async function handleCompleteCurrentSession() { try { - const body = { - end_date: gainsCurrentDateTime(), - user_id: userStore.user.id, - // json: JSON.stringify(currentSessionDetails.logs), - }; - - loading.value = true; - - const res = await api.patch(`/api/v1/sessions/${sid.value}`, body); + // set end time and user id + currentSessionDetails.end_date = currentSessionDetails.end_date || gainsCurrentDateTime(); + currentSessionDetails.user_id = userStore.user.id; + completeCurrentSessionLoading.value = true; + + // filter valid data + const validData = pick(currentSessionDetails, [ + 'user_id', + 'name', + 'block_id', + 'start_date', + 'end_date', + 'body_weight', + 'hours_of_sleep', + 'caffeine_intake', + 'calories_prior_session', + 'session_rpe', + 'notes', + ]); + + // only sent back non empty data + const nonEmpty = pickBy(validData, (value, key) => value !== null); + + const res = await api.patch(`/api/v1/sessions/${sid.value}`, nonEmpty); const json = await res.json(); if (!res.ok) { @@ -373,11 +398,13 @@ async function handleCompleteCurrentSession() { } } - loading.value = false; + completeCurrentSessionLoading.value = false; + clearDataAndDismissCompleteCurrentSessionModal(); router.push('/dashboard/sessions'); } catch (e) { - loading.value = false; + completeCurrentSessionLoading.value = false; + clearDataAndDismissCompleteCurrentSessionModal(); alert.type = 'danger'; if (Array.isArray(e)) { alert.msg = e.map((cur) => cur.msg).join(' '); @@ -803,7 +830,7 @@ async function handleDeleteSession() { -