client: add / auto-increment number at end of copied posts instead of 'copy of' text

This commit is contained in:
Max Leiter 2022-04-11 19:57:41 -07:00
parent 83def0ec86
commit 481d4ae36c
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: A3512F2F2F17EBDA
2 changed files with 33 additions and 1 deletions

View file

@ -23,6 +23,7 @@ import getPostPath from "@lib/get-post-path"
import EditDocumentList from "@components/edit-document-list"
import { ChangeEvent } from "react"
import DatePicker from "react-datepicker"
import getTitleForPostCopy from "@lib/get-title-for-post-copy"
const Post = ({
initialPost,
@ -52,7 +53,6 @@ const Post = ({
// the /new/from/{id} route fetches an initial post
useEffect(() => {
if (initialPost) {
setTitle(`Copy of ${initialPost.title}`)
setDocs(
initialPost.files?.map((doc) => ({
title: doc.title,
@ -60,9 +60,12 @@ const Post = ({
id: doc.id
})) || emptyDoc
)
setTitle(getTitleForPostCopy(initialPost.title))
}
}, [emptyDoc, initialPost])
const [passwordModalVisible, setPasswordModalVisible] = useState(false)
const sendRequest = useCallback(

View file

@ -0,0 +1,29 @@
const replaceLastInString = (
string: string,
search: string,
replace: string
): string => {
const index = string.lastIndexOf(search);
if (index === -1) {
return string;
}
return string.substring(0, index) + replace + string.substring(index + search.length);
}
const getTitleForPostCopy = (
title: string,
) => {
const numberAtEndOfTitle = title.split(" ").pop()
if (numberAtEndOfTitle) {
const number = parseInt(numberAtEndOfTitle)
if (number) {
return replaceLastInString(title, numberAtEndOfTitle, (number + 1).toString())
} else {
return title + " 1"
}
} else {
return title + " 1"
}
}
export default getTitleForPostCopy