Skip to content
Snippets Groups Projects
Commit fdbc66f3 authored by Richard Githuba's avatar Richard Githuba
Browse files

now getting the categoryName via the api

parent d129e39b
No related branches found
No related tags found
No related merge requests found
// get category from url params // get category from url params
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const categoryId = window.location.pathname.split('/')[2]; // Assuming /categories/{id} const categoryId = window.location.pathname.split('/')[2]; // Assuming /categories/{id}
const categoryName = urlParams.get('categoryName'); let categoryName = "";
// fetch category headings from api // fetch category headings from api
const apiUrl = '/getInfoByCategoryId'; const apiUrl = '/getInfoByCategoryId';
async function getCategoryName(){
try{
const response = await fetch(`/api/categories/${categoryId}`)
if (!response.ok) throw new Error (`Http error! status: ${response.status}`)
const category = await response.json()
categoryName = category.categoryTitle;
console.log('Category Name:', categoryName);
} catch (error) {
console.error('Error fetching category', error);
}
}
async function fetchCategoryHeadings() { async function fetchCategoryHeadings() {
try { try {
const response = await fetch(`${apiUrl}/${categoryId}`); const response = await fetch(`${apiUrl}/${categoryId}`);
...@@ -33,9 +46,6 @@ function renderHeadings(headings) { ...@@ -33,9 +46,6 @@ function renderHeadings(headings) {
const title = document.createElement('h2') const title = document.createElement('h2')
title.innerHTML = `<i class="bi bi-file-earmark-text"></i> ${heading.infoTitle} ` title.innerHTML = `<i class="bi bi-file-earmark-text"></i> ${heading.infoTitle} `
// const description = document.createElement('p');
// description.textContent = heading.description
const link = document.createElement('a') const link = document.createElement('a')
link.href = `/info/${heading.informationId}`; link.href = `/info/${heading.informationId}`;
link.classList.add('heading-link'); link.classList.add('heading-link');
...@@ -47,19 +57,19 @@ function renderHeadings(headings) { ...@@ -47,19 +57,19 @@ function renderHeadings(headings) {
link.appendChild(readMore); link.appendChild(readMore);
listItem.appendChild(title) listItem.appendChild(title)
// listItem.appendChild(description)
listItem.appendChild(link) listItem.appendChild(link)
container.appendChild(listItem) container.appendChild(listItem)
}); });
} }
// change the title of the page bases on category async function init() {
await getCategoryName();
// change the title of the page based on category
document.getElementById('category-name').textContent = categoryName ? `${categoryName}` : 'Category Name Not Found'; document.getElementById('category-name').textContent = categoryName ? `${categoryName}` : 'Category Name Not Found';
// Fetch and render the category headings when the page loads // Fetch and render the category headings when the page loads
fetchCategoryHeadings(); fetchCategoryHeadings();
// Get the category id from the url // Get the category id from the url
function getCategoryIdFromUrl() { function getCategoryIdFromUrl() {
const path = window.location.pathname; const path = window.location.pathname;
...@@ -80,7 +90,6 @@ function generateAddInfoPageUrl() { ...@@ -80,7 +90,6 @@ function generateAddInfoPageUrl() {
generateAddInfoPageUrl(); generateAddInfoPageUrl();
// Function to toggle edit mode to enable multi-select for deletion // Function to toggle edit mode to enable multi-select for deletion
function toggleEditMode() { function toggleEditMode() {
const container = document.getElementById('headings-container'); const container = document.getElementById('headings-container');
...@@ -188,3 +197,6 @@ async function deleteSelectedItems() { ...@@ -188,3 +197,6 @@ async function deleteSelectedItems() {
// Attach event listeners to toggle edit button and perform deletion // Attach event listeners to toggle edit button and perform deletion
document.getElementById('edit-mode-button').addEventListener('click', toggleEditMode); document.getElementById('edit-mode-button').addEventListener('click', toggleEditMode);
document.getElementById('delete-button').addEventListener('click', deleteSelectedItems); document.getElementById('delete-button').addEventListener('click', deleteSelectedItems);
}
init();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment