115 行
4.1 KiB
TypeScript
115 行
4.1 KiB
TypeScript
import { apiDelete, apiGet, apiPost, apiPut } from '@/lib/api'
|
|
import { queryRecordParams, setQueryRecords } from '@/lib/queryState'
|
|
import { switchLocalePath } from '@/lib/locales'
|
|
|
|
import type { LocaleCode } from '@/lib/locales'
|
|
import type { LocaleSwitchRequest } from '@/lib/routeLocales'
|
|
import type { FetchPostsParams, Post, PostVersion } from '@/types'
|
|
|
|
|
|
export const fetchPosts = async (
|
|
{ url, title, tags, tagReferences, match, createdFrom, createdTo, updatedFrom, updatedTo,
|
|
originalCreatedFrom, originalCreatedTo, page, limit, order }: FetchPostsParams,
|
|
locale: LocaleCode = 'ja',
|
|
): Promise<{
|
|
posts: Post[]
|
|
count: number }> =>
|
|
await apiGet ('/posts', { locale, params: {
|
|
...(url && { url }),
|
|
...(title && { title }),
|
|
...(tags && { tags }),
|
|
...(tagReferences && queryRecordParams (new URLSearchParams (tagReferences), 'tag_refs')),
|
|
...(match && { match }),
|
|
...(createdFrom && { created_from: createdFrom }),
|
|
...(createdTo && { created_to: createdTo }),
|
|
...(updatedFrom && { updated_from: updatedFrom }),
|
|
...(updatedTo && { updated_to: updatedTo }),
|
|
...(originalCreatedFrom && { original_created_from: originalCreatedFrom }),
|
|
...(originalCreatedTo && { original_created_to: originalCreatedTo }),
|
|
...(page && { page }),
|
|
...(limit && { limit }),
|
|
...(order && { order }) } })
|
|
|
|
|
|
export const switchPostFilterLocale = async (
|
|
{ location, locale, targetLocale }: LocaleSwitchRequest,
|
|
): Promise<string> => {
|
|
const query = new URLSearchParams (location.search)
|
|
const tags = query.get ('tags')
|
|
if (tags == null || tags.trim () === '')
|
|
return switchLocalePath (location, targetLocale)
|
|
|
|
const { references } = await apiGet<{ references: {
|
|
name: string
|
|
tagId?: number
|
|
externalTagId?: number
|
|
negative?: boolean
|
|
missing?: boolean }[] }> ('/posts/tag_filters', {
|
|
locale, params: { tags, target_locale: targetLocale,
|
|
...queryRecordParams (query, 'tag_refs') } })
|
|
query.set ('tags', references.map (reference => reference.name).join (' '))
|
|
setQueryRecords (query, 'tag_refs', references.map (reference => ({
|
|
name: reference.name,
|
|
negative: reference.negative,
|
|
...(reference.tagId != null && { tag_id: reference.tagId }),
|
|
...(reference.externalTagId != null && { external_tag_id: reference.externalTagId }),
|
|
...(reference.missing && { missing: true }) })))
|
|
return switchLocalePath ({ ...location, search: `?${query}` }, targetLocale)
|
|
}
|
|
|
|
|
|
export const fetchPost = async (
|
|
id: string,
|
|
locale: LocaleCode = 'ja',
|
|
): Promise<Post> => await apiGet (`/posts/${ id }`, { locale })
|
|
|
|
|
|
export const fetchPostChanges = async (
|
|
{ post, tag, externalTag, page, limit }: {
|
|
post?: string
|
|
tag?: string
|
|
externalTag?: string
|
|
page: number
|
|
limit: number },
|
|
locale: LocaleCode = 'ja',
|
|
): Promise<{
|
|
versions: PostVersion[]
|
|
count: number }> =>
|
|
await apiGet ('/posts/versions', { locale, params: { ...(post && { post }),
|
|
...(tag && { tag }),
|
|
...(externalTag && {
|
|
external_tag: externalTag }),
|
|
page, limit } })
|
|
|
|
|
|
export const updatePost = async (
|
|
post: { id: number
|
|
title: string | null
|
|
tags: string
|
|
duration: string | null
|
|
parentPostIds: string
|
|
originalCreatedFrom: string | null
|
|
originalCreatedBefore: string | null },
|
|
{ baseVersionNo, force, merge }: {
|
|
baseVersionNo?: number
|
|
force?: boolean
|
|
merge?: boolean },
|
|
locale: LocaleCode = 'ja',
|
|
) =>
|
|
await apiPut<Post> (
|
|
`/posts/${ post.id }`,
|
|
{ title: post.title,
|
|
tags: post.tags,
|
|
duration: post.duration,
|
|
parent_post_ids: post.parentPostIds,
|
|
original_created_from: post.originalCreatedFrom,
|
|
original_created_before: post.originalCreatedBefore },
|
|
{ locale, params: { ...(baseVersionNo && { base_version_no: String (baseVersionNo) }),
|
|
force: force ? '1' : '0',
|
|
merge: merge ? '1' : '0' } })
|
|
|
|
|
|
export const toggleViewedFlg = async (id: string, viewed: boolean): Promise<void> => {
|
|
await (viewed ? apiPost : apiDelete) (`/posts/${ id }/viewed`)
|
|
}
|