Blog

Blog

// Initialize TinyMCE Editor

tinymce.init({ selector: '#postContent' });


// Check admin session

function checkAdminSession() {

// Simulate session check (replace with backend AJAX in production)

const isAdmin = sessionStorage.getItem('isAdmin') === 'true';

if (isAdmin) {

document.querySelectorAll('.admin-only').forEach(el => el.style.display = 'block');

document.getElementById('loginContainer').style.display = 'none';

}

}


// Handle Admin Login

document.getElementById('adminLoginForm'). = function (e) {

e.preventDefault();

const username = document.getElementById('username').value;

const password = document.getElementById('password').value;


// Simulate login (replace with AJAX in production)

if (username === 'admin' && password === 'password') {

sessionStorage.setItem('isAdmin', 'true');

checkAdminSession();

} else {

alert('Invalid credentials!');

}

};


// Handle New Post Modal

document.getElementById('newPostBtn'). = function () {

$('#newPostModal').modal('show');

};


// Handle New Post Submission

document.getElementById('newPostForm'). = function (e) {

e.preventDefault();

const title = document.getElementById('postTitle').value;

const content = tinymce.get('postContent').getContent();

const image = document.getElementById('postImage').files[0];


// Simulate saving post (replace with backend AJAX in production)

const reader = new FileReader();

reader. = function () {

const postHTML = `

${title}

${title}

${content}

`;

document.getElementById('blogPosts').insertAdjacentHTML('afterbegin', postHTML);

$('#newPostModal').modal('hide');

};

if (image) {

reader.readAsDataURL(image);

} else {

alert('Please upload an image.');

}

};


// Load Blog Posts (Simulated)

function loadBlogPosts() {

const blogPosts = [

{

title: "Sample Post 1",

content: "This is a sample blog post.",

image: "https://via.placeholder.com/150"

},

{

title: "Sample Post 2",

content: "Another example post with more text.",

image: "https://via.placeholder.com/150"

}

];


const blogPostsContainer = document.getElementById('blogPosts');

blogPosts.forEach(post => {

const postHTML = `

${post.title}

${post.title}

${post.content}

`;

blogPostsContainer.insertAdjacentHTML('beforeend', postHTML);

});

}


// Initialize Page

checkAdminSession();

loadBlogPosts();