From 608319386be6b7ea0ea168456d267ceaf2dcea10 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 9 May 2018 20:10:23 +0200 Subject: [PATCH] API & Client: can't delete a sport w/ activities --- mpwo_api/mpwo_api/activities/models.py | 3 ++- .../src/components/Admin/generic/AdminDetail.jsx | 13 +++++++++---- .../src/components/Admin/generic/AdminPage.jsx | 8 ++++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/mpwo_api/mpwo_api/activities/models.py b/mpwo_api/mpwo_api/activities/models.py index 4e9a0e4a..e12bebd3 100644 --- a/mpwo_api/mpwo_api/activities/models.py +++ b/mpwo_api/mpwo_api/activities/models.py @@ -20,7 +20,8 @@ class Sport(db.Model): def serialize(self): return { 'id': self.id, - 'label': self.label + 'label': self.label, + '_can_be_deleted': len(self.activities) == 0 } diff --git a/mpwo_client/src/components/Admin/generic/AdminDetail.jsx b/mpwo_client/src/components/Admin/generic/AdminDetail.jsx index 77cb9bd8..6950aa8a 100644 --- a/mpwo_client/src/components/Admin/generic/AdminDetail.jsx +++ b/mpwo_client/src/components/Admin/generic/AdminDetail.jsx @@ -45,8 +45,9 @@ class AdminDetail extends React.Component {
event.preventDefault()} > - { results.map(result => ( - Object.keys(result).map(key => ( + { Object.keys(results[0]) + .filter(key => key.charAt(0) !== '_') + .map(key => (
)) - ))} + } {isInEdition ? (
onDataDelete(event, target)} + title={results[0]._can_be_deleted + ? '' + : 'Can\'t be deleted, associated data exist'} value="Delete" /> 0) { - Object.keys(results[0]).map(key => tbKeys.push(key)) + Object.keys(results[0]) + .filter(key => key.charAt(0) !== '_') + .map(key => tbKeys.push(key)) } const title = target.charAt(0).toUpperCase() + target.slice(1) @@ -43,7 +45,9 @@ export default function AdminPage(props) { { results.map((result, idx) => ( // eslint-disable-next-line react/no-array-index-key - { Object.keys(result).map(key => { + { Object.keys(result) + .filter(key => key.charAt(0) !== '_') + .map(key => { if (key === 'id') { return (