This commit is contained in:
@@ -11,7 +11,10 @@ class PostsController < ApplicationController
|
|||||||
|
|
||||||
def random
|
def random
|
||||||
post = filtered_posts.order('RAND()').first
|
post = filtered_posts.order('RAND()').first
|
||||||
|
return head :not_found unless post
|
||||||
|
|
||||||
viewed = current_user&.viewed?(post) || false
|
viewed = current_user&.viewed?(post) || false
|
||||||
|
|
||||||
render json: (post
|
render json: (post
|
||||||
.as_json(include: { tags: { only: [:id, :name, :category] } })
|
.as_json(include: { tags: { only: [:id, :name, :category] } })
|
||||||
.merge(viewed: viewed))
|
.merge(viewed: viewed))
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ const TagSearch: React.FC = () => {
|
|||||||
setSuggestionsVsbl (false)
|
setSuggestionsVsbl (false)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if (e.key === 'Enter' && search.length && (!(suggestionsVsbl) || activeIndex < 0))
|
if (e.key === 'Enter' && (!(suggestionsVsbl) || activeIndex < 0))
|
||||||
{
|
{
|
||||||
navigate (`/posts?${ (new URLSearchParams ({ tags: search })).toString () }`)
|
navigate (`/posts?${ (new URLSearchParams ({ tags: search })).toString () }`)
|
||||||
setSuggestionsVsbl (false)
|
setSuggestionsVsbl (false)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import toCamel from 'camelcase-keys'
|
import toCamel from 'camelcase-keys'
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { Link, useNavigate, useParams } from 'react-router-dom'
|
import { Link, useLocation, useNavigate, useParams } from 'react-router-dom'
|
||||||
|
|
||||||
import TagSearch from '@/components/TagSearch'
|
import TagSearch from '@/components/TagSearch'
|
||||||
import SectionTitle from '@/components/common/SectionTitle'
|
import SectionTitle from '@/components/common/SectionTitle'
|
||||||
@@ -20,6 +20,11 @@ export default ({ posts }: Props) => {
|
|||||||
|
|
||||||
const [tags, setTags] = useState<TagByCategory> ({ })
|
const [tags, setTags] = useState<TagByCategory> ({ })
|
||||||
|
|
||||||
|
const location = useLocation ()
|
||||||
|
const query = new URLSearchParams (location.search)
|
||||||
|
const tagsQuery = query.get ('tags') ?? ''
|
||||||
|
const anyFlg = query.get ('match') === 'any'
|
||||||
|
|
||||||
useEffect (() => {
|
useEffect (() => {
|
||||||
const tagsTmp: TagByCategory = { }
|
const tagsTmp: TagByCategory = { }
|
||||||
for (const post of posts)
|
for (const post of posts)
|
||||||
@@ -54,13 +59,25 @@ export default ({ posts }: Props) => {
|
|||||||
</>))}
|
</>))}
|
||||||
</ul>
|
</ul>
|
||||||
<SectionTitle>関聯</SectionTitle>
|
<SectionTitle>関聯</SectionTitle>
|
||||||
|
{posts.length && (
|
||||||
<a href="#"
|
<a href="#"
|
||||||
onClick={ev => {
|
onClick={ev => {
|
||||||
ev.preventDefault ()
|
ev.preventDefault ()
|
||||||
void (axios.get (`${ API_BASE_URL }/posts/random`)
|
void ((async () => {
|
||||||
.then (res => navigate (`/posts/${ res.data.id }`)))
|
try
|
||||||
|
{
|
||||||
|
const { data } = await axios.get (`${ API_BASE_URL }/posts/random`,
|
||||||
|
{ params: { tags: tagsQuery.split (' ').filter (e => e !== '').join (','),
|
||||||
|
match: (anyFlg ? 'any' : 'all') } })
|
||||||
|
navigate (`/posts/${ (data as Post).id }`)
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}) ())
|
||||||
}}>
|
}}>
|
||||||
ランダム
|
ランダム
|
||||||
</a>
|
</a>)}
|
||||||
</SidebarComponent>)
|
</SidebarComponent>)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user