fix: Cannot duplicate document with title close to max title length

closes #1610
This commit is contained in:
Tom Moor 2020-10-27 19:15:35 -07:00
parent abd3a1ee12
commit 7ac724909d
4 changed files with 13 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import { observer } from "mobx-react";
import * as React from "react"; import * as React from "react";
import Textarea from "react-autosize-textarea"; import Textarea from "react-autosize-textarea";
import styled from "styled-components"; import styled from "styled-components";
import { MAX_TITLE_LENGTH } from "shared/constants";
import parseTitle from "shared/utils/parseTitle"; import parseTitle from "shared/utils/parseTitle";
import Document from "models/Document"; import Document from "models/Document";
import ClickablePadding from "components/ClickablePadding"; import ClickablePadding from "components/ClickablePadding";
@ -106,7 +107,7 @@ class DocumentEditor extends React.Component<Props> {
readOnly={readOnly} readOnly={readOnly}
disabled={readOnly} disabled={readOnly}
autoFocus={!title} autoFocus={!title}
maxLength={100} maxLength={MAX_TITLE_LENGTH}
/> />
<DocumentMetaWithViews <DocumentMetaWithViews
isDraft={isDraft} isDraft={isDraft}

View File

@ -2,6 +2,7 @@
import invariant from "invariant"; import invariant from "invariant";
import { find, orderBy, filter, compact, omitBy } from "lodash"; import { find, orderBy, filter, compact, omitBy } from "lodash";
import { observable, action, computed, runInAction } from "mobx"; import { observable, action, computed, runInAction } from "mobx";
import { MAX_TITLE_LENGTH } from "shared/constants";
import naturalSort from "shared/utils/naturalSort"; import naturalSort from "shared/utils/naturalSort";
import BaseStore from "stores/BaseStore"; import BaseStore from "stores/BaseStore";
import RootStore from "stores/RootStore"; import RootStore from "stores/RootStore";
@ -445,12 +446,17 @@ export default class DocumentsStore extends BaseStore<Document> {
@action @action
duplicate = async (document: Document): * => { duplicate = async (document: Document): * => {
const append = " (duplicate)";
const res = await client.post("/documents.create", { const res = await client.post("/documents.create", {
publish: !!document.publishedAt, publish: !!document.publishedAt,
parentDocumentId: document.parentDocumentId, parentDocumentId: document.parentDocumentId,
collectionId: document.collectionId, collectionId: document.collectionId,
template: document.template, template: document.template,
title: `${document.title} (duplicate)`, title: `${document.title.slice(
0,
MAX_TITLE_LENGTH - append.length
)}${append}`,
text: document.text, text: document.text,
}); });
invariant(res && res.data, "Data should be available"); invariant(res && res.data, "Data should be available");

View File

@ -5,6 +5,7 @@ import randomstring from "randomstring";
import Sequelize, { Transaction } from "sequelize"; import Sequelize, { Transaction } from "sequelize";
import MarkdownSerializer from "slate-md-serializer"; import MarkdownSerializer from "slate-md-serializer";
import isUUID from "validator/lib/isUUID"; import isUUID from "validator/lib/isUUID";
import { MAX_TITLE_LENGTH } from "../../shared/constants";
import parseTitle from "../../shared/utils/parseTitle"; import parseTitle from "../../shared/utils/parseTitle";
import unescape from "../../shared/utils/unescape"; import unescape from "../../shared/utils/unescape";
import { Collection, User } from "../models"; import { Collection, User } from "../models";
@ -90,8 +91,8 @@ const Document = sequelize.define(
type: DataTypes.STRING, type: DataTypes.STRING,
validate: { validate: {
len: { len: {
args: [0, 100], args: [0, MAX_TITLE_LENGTH],
msg: "Document title must be less than 100 characters", msg: `Document title must be less than ${MAX_TITLE_LENGTH} characters`,
}, },
}, },
}, },

View File

@ -2,3 +2,4 @@
export const USER_PRESENCE_INTERVAL = 5000; export const USER_PRESENCE_INTERVAL = 5000;
export const MAX_AVATAR_DISPLAY = 6; export const MAX_AVATAR_DISPLAY = 6;
export const MAX_TITLE_LENGTH = 100;