client: add / auto-increment number at end of copied posts instead of 'copy of' text
This commit is contained in:
parent
83def0ec86
commit
481d4ae36c
2 changed files with 33 additions and 1 deletions
|
@ -23,6 +23,7 @@ import getPostPath from "@lib/get-post-path"
|
||||||
import EditDocumentList from "@components/edit-document-list"
|
import EditDocumentList from "@components/edit-document-list"
|
||||||
import { ChangeEvent } from "react"
|
import { ChangeEvent } from "react"
|
||||||
import DatePicker from "react-datepicker"
|
import DatePicker from "react-datepicker"
|
||||||
|
import getTitleForPostCopy from "@lib/get-title-for-post-copy"
|
||||||
|
|
||||||
const Post = ({
|
const Post = ({
|
||||||
initialPost,
|
initialPost,
|
||||||
|
@ -52,7 +53,6 @@ const Post = ({
|
||||||
// the /new/from/{id} route fetches an initial post
|
// the /new/from/{id} route fetches an initial post
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (initialPost) {
|
if (initialPost) {
|
||||||
setTitle(`Copy of ${initialPost.title}`)
|
|
||||||
setDocs(
|
setDocs(
|
||||||
initialPost.files?.map((doc) => ({
|
initialPost.files?.map((doc) => ({
|
||||||
title: doc.title,
|
title: doc.title,
|
||||||
|
@ -60,9 +60,12 @@ const Post = ({
|
||||||
id: doc.id
|
id: doc.id
|
||||||
})) || emptyDoc
|
})) || emptyDoc
|
||||||
)
|
)
|
||||||
|
|
||||||
|
setTitle(getTitleForPostCopy(initialPost.title))
|
||||||
}
|
}
|
||||||
}, [emptyDoc, initialPost])
|
}, [emptyDoc, initialPost])
|
||||||
|
|
||||||
|
|
||||||
const [passwordModalVisible, setPasswordModalVisible] = useState(false)
|
const [passwordModalVisible, setPasswordModalVisible] = useState(false)
|
||||||
|
|
||||||
const sendRequest = useCallback(
|
const sendRequest = useCallback(
|
||||||
|
|
29
client/lib/get-title-for-post-copy.ts
Normal file
29
client/lib/get-title-for-post-copy.ts
Normal 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
|
Loading…
Add table
Reference in a new issue