From 7b053ce30a623247eff30f710f535342dc86b9be Mon Sep 17 00:00:00 2001 From: "@wwwjim" Date: Wed, 9 Sep 2020 03:01:49 -0700 Subject: [PATCH] slates: adds limit per user --- components/sidebars/SidebarCreateSlate.js | 10 +++++++++- pages/api/slates/create.js | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/components/sidebars/SidebarCreateSlate.js b/components/sidebars/SidebarCreateSlate.js index 9e1689e9..faaa1744 100644 --- a/components/sidebars/SidebarCreateSlate.js +++ b/components/sidebars/SidebarCreateSlate.js @@ -4,6 +4,8 @@ import * as Constants from "~/common/constants"; import * as System from "~/components/system"; import * as Validations from "~/common/validations"; +const SLATE_LIMIT = 20; + export default class SidebarCreateSlate extends React.Component { state = { name: "", @@ -11,6 +13,11 @@ export default class SidebarCreateSlate extends React.Component { }; _handleSubmit = async () => { + if (this.props.viewer.slates.length >= SLATE_LIMIT) { + alert("You have reached the limit of 20 Slates."); + return; + } + this.setState({ loading: true }); if (!Validations.slatename(this.state.name)) { @@ -72,7 +79,8 @@ export default class SidebarCreateSlate extends React.Component { full style={{ marginTop: 48 }} onClick={this._handleSubmit} - loading={this.state.loading}> + loading={this.state.loading} + > Create {this.state.name} diff --git a/pages/api/slates/create.js b/pages/api/slates/create.js index 39402384..d7133f9a 100644 --- a/pages/api/slates/create.js +++ b/pages/api/slates/create.js @@ -2,6 +2,8 @@ import * as Utilities from "~/node_common/utilities"; import * as Data from "~/node_common/data"; import * as Strings from "~/common/strings"; +const SLATE_LIMIT = 20; + export default async (req, res) => { const id = Utilities.getIdFromCookie(req); if (!id) { @@ -38,6 +40,13 @@ export default async (req, res) => { .send({ decorator: "SERVER_EXISTING_SLATE", error: true }); } + const slates = await Data.getSlatesByUserId({ userId: id }); + if (slates.length >= SLATE_LIMIT) { + return res + .status(500) + .send({ decorator: "SERVER_SLATE_LIMIT", error: true }); + } + const slate = await Data.createSlate({ slatename: Strings.createSlug(req.body.data.name), data: {