This commit is contained in:
@@ -5,7 +5,7 @@ import PostOriginalCreatedTimeField from '@/components/PostOriginalCreatedTimeFi
|
|||||||
import Label from '@/components/common/Label'
|
import Label from '@/components/common/Label'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { toast } from '@/components/ui/use-toast'
|
import { toast } from '@/components/ui/use-toast'
|
||||||
import { apiPut } from '@/lib/api'
|
import { updatePost } from '@/lib/posts'
|
||||||
|
|
||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
|
|
||||||
@@ -44,12 +44,10 @@ export default (({ post, onSave }: Props) => {
|
|||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
const data = await apiPut<Post> (
|
const data =
|
||||||
`/posts/${ post.id }`,
|
await updatePost ({ id: post.id, versionNo: post.versionNo + 1,
|
||||||
{ title, tags, parent_post_ids: parentPostIds,
|
title, tags, parentPostIds,
|
||||||
original_created_from: originalCreatedFrom,
|
originalCreatedFrom, originalCreatedBefore })
|
||||||
original_created_before: originalCreatedBefore },
|
|
||||||
{ headers: { 'Content-Type': 'multipart/form-data' } })
|
|
||||||
onSave ({ ...post,
|
onSave ({ ...post,
|
||||||
title: data.title,
|
title: data.title,
|
||||||
tags: data.tags,
|
tags: data.tags,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { apiDelete, apiGet, apiPost } from '@/lib/api'
|
import { apiDelete, apiGet, apiPost, apiPut } from '@/lib/api'
|
||||||
|
|
||||||
import type { FetchPostsParams, Post, PostVersion } from '@/types'
|
import type { FetchPostsParams, Post, PostVersion } from '@/types'
|
||||||
|
|
||||||
@@ -42,6 +42,25 @@ export const fetchPostChanges = async (
|
|||||||
page, limit } })
|
page, limit } })
|
||||||
|
|
||||||
|
|
||||||
|
export const updatePost = async (
|
||||||
|
post: { id: number
|
||||||
|
versionNo: number
|
||||||
|
title: string | null
|
||||||
|
tags: string
|
||||||
|
parentPostIds: string
|
||||||
|
originalCreatedFrom: string | null
|
||||||
|
originalCreatedBefore: string | null },
|
||||||
|
) =>
|
||||||
|
await apiPut<Post> (
|
||||||
|
`/posts/${ post.id }`,
|
||||||
|
{ version_no: post.versionNo,
|
||||||
|
title: post.title,
|
||||||
|
tags: post.tags,
|
||||||
|
parent_post_ids: post.parentPostIds,
|
||||||
|
original_created_from: post.originalCreatedFrom,
|
||||||
|
original_created_before: post.originalCreatedBefore })
|
||||||
|
|
||||||
|
|
||||||
export const toggleViewedFlg = async (id: string, viewed: boolean): Promise<void> => {
|
export const toggleViewedFlg = async (id: string, viewed: boolean): Promise<void> => {
|
||||||
await (viewed ? apiPost : apiDelete) (`/posts/${ id }/viewed`)
|
await (viewed ? apiPost : apiDelete) (`/posts/${ id }/viewed`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,13 +11,14 @@ import Pagination from '@/components/common/Pagination'
|
|||||||
import MainArea from '@/components/layout/MainArea'
|
import MainArea from '@/components/layout/MainArea'
|
||||||
import { toast } from '@/components/ui/use-toast'
|
import { toast } from '@/components/ui/use-toast'
|
||||||
import { SITE_TITLE } from '@/config'
|
import { SITE_TITLE } from '@/config'
|
||||||
import { apiPut } from '@/lib/api'
|
import { fetchPostChanges, updatePost } from '@/lib/posts'
|
||||||
import { fetchPostChanges } from '@/lib/posts'
|
|
||||||
import { postsKeys, tagsKeys } from '@/lib/queryKeys'
|
import { postsKeys, tagsKeys } from '@/lib/queryKeys'
|
||||||
import { fetchTag } from '@/lib/tags'
|
import { fetchTag } from '@/lib/tags'
|
||||||
import { cn, dateString, originalCreatedAtString } from '@/lib/utils'
|
import { cn, dateString, originalCreatedAtString } from '@/lib/utils'
|
||||||
|
|
||||||
import type { FC } from 'react'
|
import type { FC, MouseEvent } from 'react'
|
||||||
|
|
||||||
|
import type { PostVersion } from '@/types'
|
||||||
|
|
||||||
|
|
||||||
const renderDiff = (diff: { current: string | null; prev: string | null }) => (
|
const renderDiff = (diff: { current: string | null; prev: string | null }) => (
|
||||||
@@ -62,6 +63,45 @@ export default (() => {
|
|||||||
|
|
||||||
const qc = useQueryClient ()
|
const qc = useQueryClient ()
|
||||||
|
|
||||||
|
const handleRevert = async (e: MouseEvent<HTMLAnchorElement>, change: PostVersion) => {
|
||||||
|
e.preventDefault ()
|
||||||
|
|
||||||
|
if (!(confirm (`『${ change.title.current || change.url.current }』を版 ${
|
||||||
|
change.versionNo } に差戻します.\nよろしいですか?`)))
|
||||||
|
return
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
const id = change.postId
|
||||||
|
const versionNo = change.latestVersionNo + 1
|
||||||
|
const title = change.title.current
|
||||||
|
const tags =
|
||||||
|
change.tags
|
||||||
|
.filter (t => t.type !== 'removed')
|
||||||
|
.map (t => t.name)
|
||||||
|
.filter (t => t.slice (0, 5) !== 'nico:')
|
||||||
|
.join (' ')
|
||||||
|
const parentPostIds =
|
||||||
|
(change.parentPosts ?? [])
|
||||||
|
.filter (p => p.type !== 'removed')
|
||||||
|
.map (p => p.id)
|
||||||
|
.join (' ')
|
||||||
|
const originalCreatedFrom = change.originalCreatedFrom.current
|
||||||
|
const originalCreatedBefore = change.originalCreatedBefore.current
|
||||||
|
await updatePost ({ id, versionNo, title, tags, parentPostIds,
|
||||||
|
originalCreatedFrom, originalCreatedBefore })
|
||||||
|
|
||||||
|
qc.invalidateQueries ({ queryKey: postsKeys.root })
|
||||||
|
qc.invalidateQueries ({ queryKey: tagsKeys.root })
|
||||||
|
|
||||||
|
toast ({ description: '差戻しました.' })
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
toast ({ description: '差戻に失敗……' })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
useEffect (() => {
|
useEffect (() => {
|
||||||
document.querySelector ('table')?.scrollIntoView ({ behavior: 'smooth' })
|
document.querySelector ('table')?.scrollIntoView ({ behavior: 'smooth' })
|
||||||
}, [location.search])
|
}, [location.search])
|
||||||
@@ -231,46 +271,7 @@ export default (() => {
|
|||||||
{dateString (change.createdAt)}
|
{dateString (change.createdAt)}
|
||||||
</td>
|
</td>
|
||||||
<td className="p-2">
|
<td className="p-2">
|
||||||
<a
|
<a href="#" onClick={async e => await handleRevert (e, change)}>
|
||||||
href="#"
|
|
||||||
onClick={async e => {
|
|
||||||
e.preventDefault ()
|
|
||||||
|
|
||||||
if (!(confirm (
|
|
||||||
`『${ change.title.current
|
|
||||||
|| change.url.current }』を版 ${
|
|
||||||
change.versionNo } に差戻します.\nよろしいですか?`)))
|
|
||||||
return
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await apiPut (
|
|
||||||
`/posts/${ change.postId }`,
|
|
||||||
{ title: change.title.current,
|
|
||||||
tags: change.tags
|
|
||||||
.filter (t => t.type !== 'removed')
|
|
||||||
.map (t => t.name)
|
|
||||||
.filter (t => t.slice (0, 5) !== 'nico:')
|
|
||||||
.join (' '),
|
|
||||||
parent_post_ids:
|
|
||||||
(change.parentPosts ?? [])
|
|
||||||
.filter (p => p.type !== 'removed')
|
|
||||||
.map (p => p.id)
|
|
||||||
.join (' '),
|
|
||||||
original_created_from:
|
|
||||||
change.originalCreatedFrom.current,
|
|
||||||
original_created_before:
|
|
||||||
change.originalCreatedBefore.current })
|
|
||||||
|
|
||||||
qc.invalidateQueries ({ queryKey: postsKeys.root })
|
|
||||||
qc.invalidateQueries ({ queryKey: tagsKeys.root })
|
|
||||||
toast ({ description: '差戻しました.' })
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
toast ({ description: '差戻に失敗……' })
|
|
||||||
}
|
|
||||||
}}>
|
|
||||||
復元
|
復元
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
@@ -121,6 +121,7 @@ export type Platform = typeof PLATFORMS[number]
|
|||||||
|
|
||||||
export type Post = {
|
export type Post = {
|
||||||
id: number
|
id: number
|
||||||
|
versionNo: number
|
||||||
url: string
|
url: string
|
||||||
title: string | null
|
title: string | null
|
||||||
thumbnail: string | null
|
thumbnail: string | null
|
||||||
@@ -146,6 +147,7 @@ export type PostTagChange = {
|
|||||||
|
|
||||||
export type PostVersion = {
|
export type PostVersion = {
|
||||||
postId: number
|
postId: number
|
||||||
|
latestVersionNo: number
|
||||||
versionNo: number
|
versionNo: number
|
||||||
eventType: 'create' | 'update' | 'discard' | 'restore'
|
eventType: 'create' | 'update' | 'discard' | 'restore'
|
||||||
title: { current: string | null; prev: string | null }
|
title: { current: string | null; prev: string | null }
|
||||||
|
|||||||
Reference in New Issue
Block a user