diff --git a/backend/app/controllers/tags_controller.rb b/backend/app/controllers/tags_controller.rb index f7012f7..23a0391 100644 --- a/backend/app/controllers/tags_controller.rb +++ b/backend/app/controllers/tags_controller.rb @@ -2,7 +2,11 @@ class TagsController < ApplicationController before_action :set_tags, only: %i[ show update destroy ] def index - @tags = Tag.all + if params[:post].present? + @tags = Tag.joins(:posts).where(posts: { id: params[:post] }) + else + @tags = Tag.all + end render json: @tags end diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index d52e463..8b8803c 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -13,7 +13,10 @@ const App = () => {
- + + } /> + } /> +
} /> diff --git a/frontend/src/components/TagSidebar.tsx b/frontend/src/components/TagSidebar.tsx index 8a10518..4b2bd8b 100644 --- a/frontend/src/components/TagSidebar.tsx +++ b/frontend/src/components/TagSidebar.tsx @@ -1,6 +1,6 @@ import React, { useEffect, useState } from 'react' import axios from 'axios' -import { Link } from 'react-router-dom' +import { Link, useParams } from 'react-router-dom' import { API_BASE_URL } from '../config' import TagSearch from './TagSearch' @@ -17,12 +17,14 @@ const tagNameMap: { [key: string]: string } = { nico: 'ニコニコタグ' } const TagSidebar: React.FC = () => { + const { postId } = useParams<{ postId?: number }> () const [tags, setTags] = useState ({ }) useEffect(() => { const fetchTags = async () => { try { - const response = await axios.get (`${API_BASE_URL}/tags`) + const response = await axios.get (`${API_BASE_URL}/tags` + + (postId ? `?post=${ postId }` : '')) const tagsTmp: TagByCategory = { } for (const tag of response.data) { @@ -37,7 +39,7 @@ const TagSidebar: React.FC = () => { } fetchTags() - }, []) + }, [postId]) return (