mirror of
https://github.com/cpvalente/ontime.git
synced 2026-09-13 10:09:35 +00:00
bugfix + patch
- fix update function - added patch
This commit is contained in:
@@ -18,7 +18,7 @@ import { showErrorToast } from '../../../common/helpers/toastManager';
|
||||
import style from './List.module.css';
|
||||
|
||||
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 [timeStart, setTimeStart] = useState(0);
|
||||
@@ -34,10 +34,10 @@ export default function EventListItem(props) {
|
||||
// validate field
|
||||
if (field in data) {
|
||||
// create object with new field
|
||||
const newData = { ...data, [field]: value };
|
||||
const newData = { id: data.id, [field]: value };
|
||||
|
||||
// request update in parent
|
||||
updateData(index, newData);
|
||||
eventsHandler('patch', newData);
|
||||
} else {
|
||||
showErrorToast('Field Error: ' + field);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,11 @@ exports.eventsGetById = async (req, res) => {
|
||||
// Returns -
|
||||
exports.eventsPost = async (req, res) => {
|
||||
// TODO: Validate event
|
||||
if (req.body) {
|
||||
if (!req.body) {
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
// ensure structure
|
||||
let newEvent = {};
|
||||
req.body.id = nanoid(10);
|
||||
@@ -77,10 +81,6 @@ exports.eventsPost = async (req, res) => {
|
||||
}
|
||||
console.log('added', events);
|
||||
res.sendStatus(201);
|
||||
} else {
|
||||
console.log('nothing to add', req.body);
|
||||
res.sendStatus(400);
|
||||
}
|
||||
};
|
||||
|
||||
// Create controller for PUT request to '/events/:id'
|
||||
@@ -88,12 +88,40 @@ exports.eventsPost = async (req, res) => {
|
||||
exports.eventsPut = async (req, res) => {
|
||||
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);
|
||||
events = [...newEvents];
|
||||
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/'
|
||||
// Returns -
|
||||
exports.eventsDelete = async (req, res) => {
|
||||
@@ -103,8 +131,6 @@ exports.eventsDelete = async (req, res) => {
|
||||
|
||||
const itemIndex = events.findIndex((e) => e.id == req.params.id);
|
||||
|
||||
console.log('found at index', itemIndex);
|
||||
|
||||
// Torbjorn: this syntax is very bad
|
||||
if (itemIndex === -1) {
|
||||
res.sendStatus(400);
|
||||
|
||||
@@ -19,6 +19,9 @@ router.post('/', eventsController.eventsPost);
|
||||
// create route between controller and '/events/:id' endpoint
|
||||
router.put('/', eventsController.eventsPut);
|
||||
|
||||
// create route between controller and '/events/:id' endpoint
|
||||
router.patch('/', eventsController.eventsPatch);
|
||||
|
||||
// create route between controller and '/events/:id' endpoint
|
||||
router.delete('/:id', eventsController.eventsDelete);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user