Skip to content

Commit

Permalink
associations page
Browse files Browse the repository at this point in the history
  • Loading branch information
isidzukuri committed Jan 3, 2025
1 parent d28f8f2 commit f82746f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import './App.css';
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Layout from "./pages/Layout";
import Thoughts from "./pages/Thoughts";
import ThoughtsAssociations from "./pages/ThoughtsAssociations";
import NoPage from "./pages/NoPage";
import socket from './user_socket';

Expand All @@ -13,6 +14,7 @@ export default function App() {
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Thoughts />} />
<Route path="/thoughts/:id/associations" element={<ThoughtsAssociations />} />
<Route path="*" element={<NoPage />} />
</Route>
</Routes>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Layout = () => {
<Container>
<Nav>
<Nav.Item>
<Nav.Link to="/">Home</Nav.Link>
<Nav.Link href="/">Home</Nav.Link>
</Nav.Item>
</Nav>
<Outlet />
Expand Down
45 changes: 45 additions & 0 deletions src/pages/ThoughtsAssociations.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import React, { useState, useEffect } from "react";
import ThoughtsList from './components/Thoughts/List';

import CrudDataRepository from '../repositories/crudDataRepository';
import CrudAdapter from '../adapters/crudAdapter';
import { useParams } from "react-router-dom";

const ThoughtsAssociations = () => {
const { id } = useParams();
const [associatons, setAssociatons] = useState([]);
const [thought, setThought] = useState(null);

const apiAdapter = CrudAdapter(process.env.REACT_APP_BE_API_URL);
const thoughtsRepository = CrudDataRepository(apiAdapter, 'api/thoughts');
const associatonsRepository = CrudDataRepository(apiAdapter, 'api/associations/');

useEffect(() => {
const fetchAssociations = async () => {
const data = await associatonsRepository.getItemById(id);
setAssociatons(data);
};
fetchAssociations();

const fetchThought = async () => {
const data = await thoughtsRepository.getItemById(id);

setThought(data.content.replace(/\n/g, "<br />"));
};
fetchThought();
}, [id]);

return <Row>
<Col>
<h4>Thought: </h4>
<div dangerouslySetInnerHTML={{ __html: thought}} />

<hr/>
<ThoughtsList items={associatons}/>
</Col>
</Row>;
};

export default ThoughtsAssociations;
2 changes: 1 addition & 1 deletion src/pages/components/Thoughts/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function ThoughtsList({items}) {

items.reduceRight((_, item) => {
rows.push(
<ThoughtsListItem item={item}/>
<ThoughtsListItem key={item.id} item={item}/>
);
}, 0);

Expand Down
4 changes: 2 additions & 2 deletions src/pages/components/Thoughts/ListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export default function ThoughtsListItem({item}) {
let navigate = useNavigate();

const goToAssociations = () => {
navigate("/api/thoughts/"+item.id+"/associations");
navigate("/thoughts/"+item.id+"/associations");
};

return (
<Stack key={item.id} direction="horizontal" gap={3} className="wide-list-item" >
<Stack direction="horizontal" gap={3} className="wide-list-item" >
<div className="me-auto" dangerouslySetInnerHTML={{ __html: item.content.replace(/\n/g, "<br />")}} />
<Button variant="outline-info" size="sm" onClick={goToAssociations}>
View associations
Expand Down

0 comments on commit f82746f

Please sign in to comment.