diff --git a/src/backend/db/team-descriptors.js b/src/backend/db/team-descriptors.js index 6d305b3..c2beec9 100644 --- a/src/backend/db/team-descriptors.js +++ b/src/backend/db/team-descriptors.js @@ -1,30 +1,33 @@ -import { toTeamDescriptors, toTeamDesc } from "../models/team-descriptors"; +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), }) - .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), }); const deleteTeamDescriptorById = (db) => (teamDescriptorId) => @@ -33,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; } @@ -62,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) => { @@ -71,7 +74,7 @@ const getTeamDesc = (db) => () => }); const updateTeamDesc = (db) => (data) => - db("team_desc") + db('team_desc') .update({ ...data, images: JSON.stringify(data.images), 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/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); 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))}, ]); }); }; 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..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,30 +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'); + } + + 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); }