From fcdd7241094261241966bc7f77a00fe3ec43fb23 Mon Sep 17 00:00:00 2001 From: Kevin Lau Date: Thu, 23 Feb 2023 23:09:08 -0500 Subject: [PATCH 1/5] removed required from the description field, throw error if team name is empty --- src/frontend/pages/team-descriptions/Copies.js | 2 +- src/frontend/pages/team-descriptions/hooks/team-form.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/frontend/pages/team-descriptions/Copies.js b/src/frontend/pages/team-descriptions/Copies.js index fcc5770..0d1e05e 100644 --- a/src/frontend/pages/team-descriptions/Copies.js +++ b/src/frontend/pages/team-descriptions/Copies.js @@ -1,6 +1,6 @@ export const teamCopies = { NAME_LABEL: 'Team name (required)', - DESCRIPTION_LABEL: 'Description (required)', + DESCRIPTION_LABEL: 'Description', NAME_PLACEHOLDER: 'Team name', DESCRIPTION_PLACEHOLDER: 'Description', diff --git a/src/frontend/pages/team-descriptions/hooks/team-form.js b/src/frontend/pages/team-descriptions/hooks/team-form.js index c40d822..6aa9525 100644 --- a/src/frontend/pages/team-descriptions/hooks/team-form.js +++ b/src/frontend/pages/team-descriptions/hooks/team-form.js @@ -122,6 +122,11 @@ const useTeamForm = (teamId, input = {}) => { const saveForm = useCallback(async () => { // Send data to server: try { + // Handle error on empty team name + if (!state.form.teamName){ + throw new Error('Please enter teamname') + } + if (state.exists) { await api.teams.updateTeam(state.form, teamId); } else { From 4d3c1ed928b88eecb7831abc1c26cc2f2ba51cd9 Mon Sep 17 00:00:00 2001 From: Kevin Lau Date: Thu, 16 Mar 2023 14:18:32 -0400 Subject: [PATCH 2/5] add last updated field for team descriptions table --- src/backend/db/team-descriptors.js | 3 +++ src/backend/models/team-descriptors.js | 1 + 2 files changed, 4 insertions(+) diff --git a/src/backend/db/team-descriptors.js b/src/backend/db/team-descriptors.js index 6d305b3..b2ea3bf 100644 --- a/src/backend/db/team-descriptors.js +++ b/src/backend/db/team-descriptors.js @@ -1,4 +1,5 @@ import { toTeamDescriptors, toTeamDesc } from "../models/team-descriptors"; +const { parseTimeFromRequest } = require('../utils/db-dates'); /* TEAMS API ENDPOINTS */ @@ -10,6 +11,7 @@ const addTeamDescriptor = (db) => (teamDescriptor) => .insert({ team_name: teamDescriptor.teamName, description: teamDescriptor.description, + updated_at: parseTimeFromRequest(teamDescriptor.lastUpdated) }) .returning("id") .then((response) => { @@ -25,6 +27,7 @@ const updateTeamDescriptorById = (db) => (id, teamDescriptor) => .update({ team_name: teamDescriptor.teamName, description: teamDescriptor.description, + updated_at: parseTimeFromRequest(teamDescriptor.lastUpdated) }); const deleteTeamDescriptorById = (db) => (teamDescriptorId) => diff --git a/src/backend/models/team-descriptors.js b/src/backend/models/team-descriptors.js index 7dac490..84c35b6 100644 --- a/src/backend/models/team-descriptors.js +++ b/src/backend/models/team-descriptors.js @@ -4,6 +4,7 @@ import renameProps from '../utils/rename-props'; export const toTeamDescriptor = (teamDescriptor) => renameProps(teamDescriptor, { team_name: 'teamName', + updated_at: 'lastUpdated', }); export const toTeamDescriptors = R.map(toTeamDescriptor); From 8c8968168353466826a1dbe446b5210db975c1b8 Mon Sep 17 00:00:00 2001 From: Kevin Lau Date: Thu, 16 Mar 2023 14:20:48 -0400 Subject: [PATCH 3/5] add lastupdated field to migrations and seed --- src/backend/migrations/20200926132328_team-descriptors.js | 1 + src/backend/seeds/03_team-descriptors.js | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/backend/migrations/20200926132328_team-descriptors.js b/src/backend/migrations/20200926132328_team-descriptors.js index d2f2fba..7d0c028 100644 --- a/src/backend/migrations/20200926132328_team-descriptors.js +++ b/src/backend/migrations/20200926132328_team-descriptors.js @@ -2,6 +2,7 @@ exports.up = knex => knex.schema.createTable('team_descriptors', table => { table.increments('id'); // P-key table.string('team_name'); table.string('description'); + table.datetime("updated_at", options={useTz: false}).notNullable(); }); exports.down = knex => knex.schema.dropTableIfExists('team_descriptors'); diff --git a/src/backend/seeds/03_team-descriptors.js b/src/backend/seeds/03_team-descriptors.js index a1db797..61cb61a 100644 --- a/src/backend/seeds/03_team-descriptors.js +++ b/src/backend/seeds/03_team-descriptors.js @@ -1,4 +1,5 @@ const { ENV_IS_STAGING_OR_PROD } = require('../knexfile'); +const { parseTimeFromRequest } = require('../utils/db-dates'); if (!ENV_IS_STAGING_OR_PROD) { exports.seed = function (knex) { @@ -8,8 +9,8 @@ if (!ENV_IS_STAGING_OR_PROD) { .then(function () { // Inserts seed entries return knex('team_descriptors').insert([ - { team_name: 'WebTeam', description: 'A team' }, - { team_name: 'Team Hub', description: 'Another Team' }, + { team_name: 'WebTeams', description: 'A team', updated_at: parseTimeFromRequest(new Date(1609450000000))}, + { team_name: 'Team Hub', description: 'Another Team', updated_at: parseTimeFromRequest(new Date(2147483647000))}, ]); }); }; From e36ef35a1dfcb18cc4f20fd20747de3177d14b2e Mon Sep 17 00:00:00 2001 From: Kevin Lau Date: Fri, 17 Mar 2023 21:05:51 -0400 Subject: [PATCH 4/5] add current timestamp on save form --- .../team-descriptions/hooks/team-form.js | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/frontend/pages/team-descriptions/hooks/team-form.js b/src/frontend/pages/team-descriptions/hooks/team-form.js index 6aa9525..5ad4760 100644 --- a/src/frontend/pages/team-descriptions/hooks/team-form.js +++ b/src/frontend/pages/team-descriptions/hooks/team-form.js @@ -1,25 +1,25 @@ -import { useReducer, useCallback, useEffect } from "react"; -import { useHistory } from "react-router-dom"; +import { useReducer, useCallback, useEffect } from 'react'; +import { useHistory } from 'react-router-dom'; -import api from "../../../api"; -import useTeams from "../../../hooks/teams"; +import api from '../../../api'; +import useTeams from '../../../hooks/teams'; const initialState = (inputState) => ({ loading: true, - userFriendlyErrorMessage: "", + userFriendlyErrorMessage: '', exists: false, form: { teamId: 0, - teamName: "", - description: "", - lastUpdated: "", + teamName: '', + description: '', + lastUpdated: '', ...inputState, // May overwrite any of the above defaults }, }); const reducer = (state, action) => { switch (action.type) { - case "LOAD_SUCCESS": + case 'LOAD_SUCCESS': return { ...state, loading: false, @@ -30,7 +30,7 @@ const reducer = (state, action) => { teamId: action.payload.teamId, }, }; - case "LOAD_FAILURE": + case 'LOAD_FAILURE': return { ...state, loading: false, @@ -38,7 +38,7 @@ const reducer = (state, action) => { }; // Redux stores acting as react states: - case "UPDATE_TEAM_NAME": + case 'UPDATE_TEAM_NAME': return { ...state, form: { @@ -46,7 +46,7 @@ const reducer = (state, action) => { teamName: action.payload, }, }; - case "UPDATE_DESCRIPTION": + case 'UPDATE_DESCRIPTION': return { ...state, form: { @@ -77,7 +77,7 @@ const useTeamForm = (teamId, input = {}) => { const team = teams.filter((team) => team.id === teamId); if (team.length <= 1) { dispatch({ - type: "LOAD_SUCCESS", + type: 'LOAD_SUCCESS', payload: { teamId, teamData: team[0], @@ -86,11 +86,11 @@ const useTeamForm = (teamId, input = {}) => { }); } else { throw new Error( - "Failed to Load Resources, please refresh the page. If you continue to experience difficulties, please contact the web team (ERR_MULTIPLE_TEAMS_CONFLICT)" + 'Failed to Load Resources, please refresh the page. If you continue to experience difficulties, please contact the web team (ERR_MULTIPLE_TEAMS_CONFLICT)', ); } } catch (err) { - dispatch({ type: "LOAD_FAILURE", payload: err }); + dispatch({ type: 'LOAD_FAILURE', payload: err }); } } }, [state.loading, dispatch, teamId, teams]); @@ -100,35 +100,39 @@ const useTeamForm = (teamId, input = {}) => { */ const updateName = useCallback( (teamName) => { - dispatch({ type: "UPDATE_TEAM_NAME", payload: teamName }); + dispatch({ type: 'UPDATE_TEAM_NAME', payload: teamName }); }, - [dispatch] + [dispatch], ); const updateDescription = useCallback( (description) => { - dispatch({ type: "UPDATE_DESCRIPTION", payload: description }); + dispatch({ type: 'UPDATE_DESCRIPTION', payload: description }); }, - [dispatch] + [dispatch], ); /** * Save and close Functions */ const closeForm = useCallback(() => { - history.push("/team-descriptions"); + history.push('/team-descriptions'); }, [history]); const saveForm = useCallback(async () => { // Send data to server: try { // Handle error on empty team name - if (!state.form.teamName){ - throw new Error('Please enter teamname') + if (!state.form.teamName) { + throw new Error('Please enter teamname'); } + state.form.lastUpdated = Date.now(); + + console.log(state.form); if (state.exists) { await api.teams.updateTeam(state.form, teamId); + console.log(state.form); } else { await api.teams.addTeam(state.form); } From 1562941aad71df0c645e990f64b3ababa81ef3a4 Mon Sep 17 00:00:00 2001 From: Kevin Lau Date: Fri, 17 Mar 2023 21:09:32 -0400 Subject: [PATCH 5/5] updated to module import --- src/backend/db/team-descriptors.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/backend/db/team-descriptors.js b/src/backend/db/team-descriptors.js index b2ea3bf..c2beec9 100644 --- a/src/backend/db/team-descriptors.js +++ b/src/backend/db/team-descriptors.js @@ -1,33 +1,33 @@ -import { toTeamDescriptors, toTeamDesc } from "../models/team-descriptors"; -const { parseTimeFromRequest } = require('../utils/db-dates'); +import { toTeamDescriptors, toTeamDesc } from '../models/team-descriptors'; +import { parseTimeFromRequest } from '../utils/db-dates'; /* TEAMS API ENDPOINTS */ const getTeamDescriptors = (db) => () => - db("team_descriptors").then(toTeamDescriptors); + db('team_descriptors').then(toTeamDescriptors); const addTeamDescriptor = (db) => (teamDescriptor) => - db("team_descriptors") + db('team_descriptors') .insert({ team_name: teamDescriptor.teamName, description: teamDescriptor.description, - updated_at: parseTimeFromRequest(teamDescriptor.lastUpdated) + updated_at: parseTimeFromRequest(teamDescriptor.lastUpdated), }) - .returning("id") + .returning('id') .then((response) => { console.log(response); return response; }); const updateTeamDescriptorById = (db) => (id, teamDescriptor) => - db("team_descriptors") + db('team_descriptors') .where({ id, }) .update({ team_name: teamDescriptor.teamName, description: teamDescriptor.description, - updated_at: parseTimeFromRequest(teamDescriptor.lastUpdated) + updated_at: parseTimeFromRequest(teamDescriptor.lastUpdated), }); const deleteTeamDescriptorById = (db) => (teamDescriptorId) => @@ -36,22 +36,22 @@ const deleteTeamDescriptorById = (db) => (teamDescriptorId) => DELETE FROM team_descriptors WHERE id = ? `, - teamDescriptorId + teamDescriptorId, ); /* TEAMS DESCRIPTION API ENDPOINTS */ const getTeamDesc = (db) => () => - db("team_desc") + db('team_desc') .where({ id: 1 }) .first() // There should only be one entry in this table and it's id should be 1 .then((res) => { let images; // images array is stored as string in database, convert back to array: try { const resImages = res.images; - if (typeof resImages === "string") { + if (typeof resImages === 'string') { images = JSON.parse(res.images); - } else if (typeof resImages === "object") { + } else if (typeof resImages === 'object') { // If array already images = resImages; } @@ -65,7 +65,7 @@ const getTeamDesc = (db) => () => images: images, }; delete body.id; // Since we should only have 1 entry. - console.log(res) + console.log(res); return toTeamDesc(body); }) .catch((err) => { @@ -74,7 +74,7 @@ const getTeamDesc = (db) => () => }); const updateTeamDesc = (db) => (data) => - db("team_desc") + db('team_desc') .update({ ...data, images: JSON.stringify(data.images),