Skip to content

Commit

Permalink
Merge pull request UnlockedLabs#53 from calisio/main
Browse files Browse the repository at this point in the history
Pulls and renders categories in left menu
  • Loading branch information
nokierae authored Jan 18, 2024
2 parents 917f426 + 3e86f76 commit 328e02e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 33 deletions.
6 changes: 5 additions & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public function run(): void
DB::table('categories')->insert([
'name' => 'Unlocked Labs',
'rank' => '1',
'links' => '["http://www.unlockedlabs.org/"]',
'links' => '
[
{"Unlocked Labs Website":"http://www.unlockedlabs.org/"},
{"Unlocked Labs LinkedIn":"https://www.linkedin.com/company/labs-unlocked/"}
]',
]);
}
}
81 changes: 49 additions & 32 deletions resources/js/Components/LeftMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
import axios from "axios";
import Brand from "./Brand";
import {
HomeIcon,
ArchiveBoxIcon,
BookOpenIcon,
} from "@heroicons/react/24/solid";
import useSWR from "swr";
import { Category, CategoryLink } from "@/common";

function CategoryItem({ categoryName, linksArray, rank }: Category) {
const linksList = linksArray.map((linkPair: { [x: string]: string }) => {
const key = Object.keys(linkPair)[0];
return (
<li key={key.concat(rank.toString())}>
<a href={linkPair[key]}>{key}</a>
</li>
);
});
return (
<li>
<details>
<summary>
<ArchiveBoxIcon className="w-4" />
{categoryName}
</summary>
<ul>{linksList}</ul>
</details>
</li>
);
}

function getCategoryItems(
data: { data: { name: string; rank: number; links: CategoryLink[] }[] },
error: any,
isLoading: boolean,
) {
if (error) return <div>failed to load</div>;
if (isLoading) return <div>loading...</div>;
return data.data.map((category) => {
return (
<CategoryItem
key={category.rank}
categoryName={category.name}
linksArray={category.links}
rank={category.rank}
/>
);
});
}

export default function LeftMenu() {
const { data, error, isLoading } = useSWR("/api/v1/categories");

const categoryItems = getCategoryItems(data, error, isLoading);

return (
<ul className="menu bg-base-100 w-72">
<li>
Expand All @@ -24,38 +72,7 @@ export default function LeftMenu() {
<BookOpenIcon className="w-4" /> Courses
</a>
</li>
<li>
<details>
<summary>
<ArchiveBoxIcon className="w-4" />
Category 1
</summary>
<ul>
<li>
<a>Link 1</a>
</li>
<li>
<a>Link 2</a>
</li>
</ul>
</details>
</li>
<li>
<details>
<summary>
<ArchiveBoxIcon className="w-4" />
Category 2
</summary>
<ul>
<li>
<a>Link 1</a>
</li>
<li>
<a>Link 2</a>
</li>
</ul>
</details>
</li>
{categoryItems}
</ul>
);
}
10 changes: 10 additions & 0 deletions resources/js/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ export interface User {
role: string;
email: string;
}

export interface Category {
categoryName: string;
linksArray: Array<CategoryLink>;
rank: number;
}

export interface CategoryLink {
[linkName: string]: string;
}

0 comments on commit 328e02e

Please sign in to comment.