Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Team Descriptions Bugs #40

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/backend/db/team-descriptors.js
Original file line number Diff line number Diff line change
@@ -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) =>
Expand All @@ -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;
}
Expand All @@ -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) => {
Expand All @@ -71,7 +74,7 @@ const getTeamDesc = (db) => () =>
});

const updateTeamDesc = (db) => (data) =>
db("team_desc")
db('team_desc')
.update({
...data,
images: JSON.stringify(data.images),
Expand Down
1 change: 1 addition & 0 deletions src/backend/migrations/20200926132328_team-descriptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
1 change: 1 addition & 0 deletions src/backend/models/team-descriptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/backend/seeds/03_team-descriptors.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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))},
]);
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/pages/team-descriptions/Copies.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
49 changes: 29 additions & 20 deletions src/frontend/pages/team-descriptions/hooks/team-form.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -30,23 +30,23 @@ const reducer = (state, action) => {
teamId: action.payload.teamId,
},
};
case "LOAD_FAILURE":
case 'LOAD_FAILURE':
return {
...state,
loading: false,
userFriendlyErrorMessage: action.payload,
};

// Redux stores acting as react states:
case "UPDATE_TEAM_NAME":
case 'UPDATE_TEAM_NAME':
return {
...state,
form: {
...state.form,
teamName: action.payload,
},
};
case "UPDATE_DESCRIPTION":
case 'UPDATE_DESCRIPTION':
return {
...state,
form: {
Expand Down Expand Up @@ -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],
Expand All @@ -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]);
Expand All @@ -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);
}
Expand Down