bugfix + patch

- fix update function
- added patch
This commit is contained in:
cv
2021-04-10 16:32:57 +02:00
parent ba8274819e
commit 85de94e077
3 changed files with 80 additions and 51 deletions
@@ -18,7 +18,7 @@ import { showErrorToast } from '../../../common/helpers/toastManager';
import style from './List.module.css'; import style from './List.module.css';
export default function EventListItem(props) { export default function EventListItem(props) {
const { data, selected, delay, index, eventsHandler, updateData } = props; const { data, selected, delay, index, eventsHandler } = props;
const [more, setMore] = useState(false); const [more, setMore] = useState(false);
const [timeStart, setTimeStart] = useState(0); const [timeStart, setTimeStart] = useState(0);
@@ -34,10 +34,10 @@ export default function EventListItem(props) {
// validate field // validate field
if (field in data) { if (field in data) {
// create object with new field // create object with new field
const newData = { ...data, [field]: value }; const newData = { id: data.id, [field]: value };
// request update in parent // request update in parent
updateData(index, newData); eventsHandler('patch', newData);
} else { } else {
showErrorToast('Field Error: ' + field); showErrorToast('Field Error: ' + field);
} }
+34 -8
View File
@@ -34,7 +34,11 @@ exports.eventsGetById = async (req, res) => {
// Returns - // Returns -
exports.eventsPost = async (req, res) => { exports.eventsPost = async (req, res) => {
// TODO: Validate event // TODO: Validate event
if (req.body) { if (!req.body) {
res.sendStatus(400);
return;
}
// ensure structure // ensure structure
let newEvent = {}; let newEvent = {};
req.body.id = nanoid(10); req.body.id = nanoid(10);
@@ -77,10 +81,6 @@ exports.eventsPost = async (req, res) => {
} }
console.log('added', events); console.log('added', events);
res.sendStatus(201); res.sendStatus(201);
} else {
console.log('nothing to add', req.body);
res.sendStatus(400);
}
}; };
// Create controller for PUT request to '/events/:id' // Create controller for PUT request to '/events/:id'
@@ -88,12 +88,40 @@ exports.eventsPost = async (req, res) => {
exports.eventsPut = async (req, res) => { exports.eventsPut = async (req, res) => {
const itemIndex = events.findIndex((e) => e.id == req.body.id); const itemIndex = events.findIndex((e) => e.id == req.body.id);
if (!itemIndex) res.sendStatus(400); // Item with index not found
if (itemIndex === -1) {
res.sendStatus(400);
return;
}
// Torbjorn: bad syntax?
const newEvents = replaceAt(events, itemIndex, req.body); const newEvents = replaceAt(events, itemIndex, req.body);
events = [...newEvents]; events = [...newEvents];
res.sendStatus(200); res.sendStatus(200);
}; };
// Create controller for PATCH request to '/events/:id'
// Returns -
exports.eventsPatch = async (req, res) => {
const itemIndex = events.findIndex((e) => e.id == req.body.id);
// Item with index not found
if (itemIndex === -1) {
res.sendStatus(400);
return;
}
// Get current object
const eventToUpdate = events[itemIndex];
// Update and replace
const updatedEvent = { ...eventToUpdate, ...req.body };
const newEvents = replaceAt(events, itemIndex, updatedEvent);
events = [...newEvents];
res.sendStatus(200);
};
// Create controller for DELETE request to '/events/' // Create controller for DELETE request to '/events/'
// Returns - // Returns -
exports.eventsDelete = async (req, res) => { exports.eventsDelete = async (req, res) => {
@@ -103,8 +131,6 @@ exports.eventsDelete = async (req, res) => {
const itemIndex = events.findIndex((e) => e.id == req.params.id); const itemIndex = events.findIndex((e) => e.id == req.params.id);
console.log('found at index', itemIndex);
// Torbjorn: this syntax is very bad // Torbjorn: this syntax is very bad
if (itemIndex === -1) { if (itemIndex === -1) {
res.sendStatus(400); res.sendStatus(400);
+3
View File
@@ -19,6 +19,9 @@ router.post('/', eventsController.eventsPost);
// create route between controller and '/events/:id' endpoint // create route between controller and '/events/:id' endpoint
router.put('/', eventsController.eventsPut); router.put('/', eventsController.eventsPut);
// create route between controller and '/events/:id' endpoint
router.patch('/', eventsController.eventsPatch);
// create route between controller and '/events/:id' endpoint // create route between controller and '/events/:id' endpoint
router.delete('/:id', eventsController.eventsDelete); router.delete('/:id', eventsController.eventsDelete);