779 lines
30 KiB
HTML
779 lines
30 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Edit Journey – Journey Mapper</title>
|
||
|
||
<!-- Open Props CSS -->
|
||
<link rel="stylesheet" href="https://unpkg.com/open-props"/>
|
||
<link rel="stylesheet" href="https://unpkg.com/open-props/normalize.min.css"/>
|
||
|
||
<!-- Font Awesome -->
|
||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
||
|
||
<!-- Google Fonts -->
|
||
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
|
||
|
||
<!-- Responsive Design -->
|
||
<link rel="stylesheet" href="css/style.css">
|
||
<link rel="stylesheet" href="css/responsive.css">
|
||
|
||
<style>
|
||
* { box-sizing: border-box; }
|
||
body {
|
||
font-family: var(--font-sans);
|
||
background: var(--gray-0);
|
||
color: var(--gray-9);
|
||
line-height: var(--font-lineheight-3);
|
||
margin: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
min-height: 100vh;
|
||
}
|
||
|
||
/* Header */
|
||
.site-header {
|
||
background: var(--gray-9);
|
||
padding: var(--size-4) var(--size-6);
|
||
border-bottom: 1px solid var(--surface-4);
|
||
}
|
||
.site-header .container {
|
||
max-width: 1400px;
|
||
margin: 0 auto;
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
flex-wrap: wrap;
|
||
gap: var(--size-2);
|
||
}
|
||
.site-title {
|
||
margin: 0;
|
||
font-size: var(--font-size-4);
|
||
font-weight: var(--font-weight-6);
|
||
}
|
||
.site-title a {
|
||
color: var(--indigo-4);
|
||
text-decoration: none;
|
||
}
|
||
.site-nav {
|
||
display: flex;
|
||
gap: var(--size-4);
|
||
}
|
||
.site-nav a {
|
||
color: var(--gray-2);
|
||
text-decoration: none;
|
||
font-weight: var(--font-weight-5);
|
||
transition: color 0.2s;
|
||
padding: var(--size-1) var(--size-2);
|
||
border-radius: var(--radius-2);
|
||
}
|
||
.site-nav a:hover,
|
||
.site-nav a.active {
|
||
color: var(--indigo-4);
|
||
background: var(--surface-2);
|
||
}
|
||
|
||
/* User menu */
|
||
.user-menu {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: var(--size-2);
|
||
}
|
||
.user-menu .username {
|
||
color: var(--gray-2);
|
||
font-weight: var(--font-weight-5);
|
||
}
|
||
.logout-btn {
|
||
background: var(--indigo-7);
|
||
color: white;
|
||
border: none;
|
||
border-radius: var(--radius-2);
|
||
padding: var(--size-1) var(--size-3);
|
||
cursor: pointer;
|
||
font-size: var(--font-size-1);
|
||
font-weight: var(--font-weight-5);
|
||
transition: background 0.2s;
|
||
}
|
||
.logout-btn:hover {
|
||
background: var(--indigo-8);
|
||
}
|
||
|
||
/* Main content */
|
||
.editor-container {
|
||
max-width: 1000px;
|
||
margin: var(--size-6) auto;
|
||
padding: 0 var(--size-4);
|
||
}
|
||
.editor-form {
|
||
background: var(--surface-1);
|
||
border-radius: var(--radius-3);
|
||
padding: var(--size-6);
|
||
box-shadow: var(--shadow-2);
|
||
}
|
||
.form-group {
|
||
margin-bottom: var(--size-4);
|
||
}
|
||
.sharing-panel {
|
||
display: none;
|
||
background: var(--surface-2);
|
||
border: 1px solid var(--surface-4);
|
||
border-radius: var(--radius-2);
|
||
padding: var(--size-4);
|
||
margin-bottom: var(--size-4);
|
||
}
|
||
.sharing-panel.visible {
|
||
display: block;
|
||
}
|
||
.sharing-panel h3 {
|
||
margin-top: 0;
|
||
margin-bottom: var(--size-2);
|
||
font-size: var(--font-size-3);
|
||
}
|
||
.sharing-grid {
|
||
display: grid;
|
||
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
||
gap: var(--size-4);
|
||
}
|
||
.sharing-user-list {
|
||
display: grid;
|
||
gap: var(--size-2);
|
||
}
|
||
.sharing-user {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: var(--size-2);
|
||
padding: var(--size-2);
|
||
border-radius: var(--radius-2);
|
||
background: var(--surface-1);
|
||
}
|
||
.sharing-user input {
|
||
width: auto;
|
||
}
|
||
.sharing-help {
|
||
color: var(--gray-6);
|
||
font-size: var(--font-size-1);
|
||
margin-top: 0;
|
||
}
|
||
.empty-share-list {
|
||
color: var(--gray-6);
|
||
font-style: italic;
|
||
margin: 0;
|
||
}
|
||
label {
|
||
display: block;
|
||
margin-bottom: var(--size-2);
|
||
font-weight: var(--font-weight-6);
|
||
color: var(--gray-8);
|
||
}
|
||
input[type="text"],
|
||
input[type="date"],
|
||
input[type="file"],
|
||
textarea,
|
||
select {
|
||
width: 100%;
|
||
padding: var(--size-2) var(--size-3);
|
||
border: 1px solid var(--surface-4);
|
||
border-radius: var(--radius-2);
|
||
background: var(--surface-2);
|
||
color: var(--text-1);
|
||
font-family: inherit;
|
||
font-size: var(--font-size-2);
|
||
}
|
||
textarea {
|
||
min-height: 120px;
|
||
resize: vertical;
|
||
}
|
||
.marker-card {
|
||
background: var(--surface-2);
|
||
border-radius: var(--radius-2);
|
||
padding: var(--size-4);
|
||
margin-bottom: var(--size-4);
|
||
border: 1px solid var(--surface-4);
|
||
position: relative;
|
||
}
|
||
.marker-header {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
margin-bottom: var(--size-2);
|
||
cursor: move;
|
||
}
|
||
.marker-header h4 {
|
||
margin: 0;
|
||
font-size: var(--font-size-3);
|
||
}
|
||
.remove-marker {
|
||
background: none;
|
||
border: none;
|
||
color: var(--red-6);
|
||
cursor: pointer;
|
||
font-size: var(--font-size-3);
|
||
}
|
||
.remove-marker:hover {
|
||
color: var(--red-8);
|
||
}
|
||
.marker-coords {
|
||
font-size: var(--font-size-0);
|
||
color: var(--gray-6);
|
||
margin-bottom: var(--size-2);
|
||
}
|
||
.image-preview-grid {
|
||
display: grid;
|
||
grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
|
||
gap: var(--size-2);
|
||
margin-top: var(--size-2);
|
||
}
|
||
.image-preview {
|
||
position: relative;
|
||
aspect-ratio: 1;
|
||
border-radius: var(--radius-2);
|
||
overflow: hidden;
|
||
border: 1px solid var(--surface-4);
|
||
background: var(--surface-1);
|
||
}
|
||
.image-preview img {
|
||
width: 100%;
|
||
height: 100%;
|
||
object-fit: cover;
|
||
display: block;
|
||
}
|
||
.remove-image {
|
||
position: absolute;
|
||
top: 4px;
|
||
right: 4px;
|
||
width: 24px;
|
||
height: 24px;
|
||
border: none;
|
||
border-radius: var(--radius-round);
|
||
background: rgba(0, 0, 0, 0.7);
|
||
color: white;
|
||
cursor: pointer;
|
||
display: inline-flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
.btn {
|
||
padding: var(--size-2) var(--size-4);
|
||
border: none;
|
||
border-radius: var(--radius-2);
|
||
cursor: pointer;
|
||
font-size: var(--font-size-2);
|
||
font-weight: var(--font-weight-5);
|
||
display: inline-flex;
|
||
align-items: center;
|
||
gap: var(--size-2);
|
||
transition: all 0.2s var(--ease-2);
|
||
box-shadow: var(--shadow-2);
|
||
background: var(--gray-7);
|
||
color: white;
|
||
text-decoration: none;
|
||
}
|
||
.btn-primary {
|
||
background: var(--indigo-7);
|
||
}
|
||
.btn-primary:hover {
|
||
background: var(--indigo-8);
|
||
}
|
||
.btn-success {
|
||
background: var(--green-7);
|
||
}
|
||
.btn-success:hover {
|
||
background: var(--green-8);
|
||
}
|
||
.btn-danger {
|
||
background: var(--red-7);
|
||
}
|
||
.btn-danger:hover {
|
||
background: var(--red-8);
|
||
}
|
||
.btn-outline {
|
||
background: transparent;
|
||
border: 1px solid var(--surface-4);
|
||
color: var(--text-2);
|
||
box-shadow: none;
|
||
}
|
||
.btn-outline:hover {
|
||
background: var(--surface-3);
|
||
}
|
||
.button-group {
|
||
display: flex;
|
||
gap: var(--size-3);
|
||
margin-top: var(--size-6);
|
||
flex-wrap: wrap;
|
||
}
|
||
.toast {
|
||
position: fixed;
|
||
bottom: var(--size-4);
|
||
right: var(--size-4);
|
||
background: var(--green-7);
|
||
color: white;
|
||
padding: var(--size-2) var(--size-4);
|
||
border-radius: var(--radius-2);
|
||
display: none;
|
||
z-index: 1100;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<header class="site-header">
|
||
<div class="container">
|
||
<h1 class="site-title"><a href="map-page.html">Journey Mapper</a></h1>
|
||
<div style="display: flex; align-items: center; gap: var(--size-4);">
|
||
<nav class="site-nav">
|
||
<a href="map-page.html">Map</a>
|
||
<a href="blog-list.html">Blog</a>
|
||
</nav>
|
||
<div class="user-menu" id="user-menu"></div>
|
||
</div>
|
||
</div>
|
||
</header>
|
||
|
||
<main class="editor-container">
|
||
<div class="editor-form">
|
||
<h2 id="form-title">Edit Journey</h2>
|
||
<form id="journey-form">
|
||
<div class="form-group">
|
||
<label for="journey-title">Title</label>
|
||
<input type="text" id="journey-title" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="journey-description">Description</label>
|
||
<textarea id="journey-description" rows="4"></textarea>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="journey-visibility">Visibility</label>
|
||
<select id="journey-visibility">
|
||
<option value="private">Private (only you)</option>
|
||
<option value="public">Public (anyone can view)</option>
|
||
<option value="shared">Shared (with specific users)</option>
|
||
</select>
|
||
</div>
|
||
<div id="sharing-panel" class="sharing-panel">
|
||
<h3><i class="fas fa-user-group"></i> Share With Users</h3>
|
||
<p class="sharing-help">Viewers can open the journey. Editors can also update its chapters, markers, and images.</p>
|
||
<div class="sharing-grid">
|
||
<div>
|
||
<label>Can View</label>
|
||
<div id="shared-read-users" class="sharing-user-list"></div>
|
||
</div>
|
||
<div>
|
||
<label>Can Edit</label>
|
||
<div id="shared-edit-users" class="sharing-user-list"></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<h3><i class="fas fa-map-marker-alt"></i> Chapters (Markers)</h3>
|
||
<div id="markers-container"></div>
|
||
<div class="button-group">
|
||
<button type="button" id="add-marker" class="btn btn-outline"><i class="fas fa-plus"></i> Add Chapter</button>
|
||
</div>
|
||
|
||
<div class="button-group">
|
||
<button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Save Journey</button>
|
||
<a href="blog-list.html" class="btn"><i class="fas fa-times"></i> Cancel</a>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</main>
|
||
|
||
<div id="toast" class="toast"></div>
|
||
|
||
<script src="js/auth.js"></script>
|
||
<script>
|
||
// ==================== GLOBALS ====================
|
||
let currentJourneyId = null;
|
||
let currentJourneyOwnerId = null;
|
||
let markersData = [];
|
||
let availableUsers = [];
|
||
let sharedReadIds = [];
|
||
let sharedEditIds = [];
|
||
|
||
const urlParams = new URLSearchParams(window.location.search);
|
||
const journeyId = urlParams.get('id');
|
||
const isNew = urlParams.has('new');
|
||
|
||
// ==================== UI HELPERS ====================
|
||
function showToast(message, isError = false) {
|
||
const toast = document.getElementById('toast');
|
||
toast.textContent = message;
|
||
toast.style.backgroundColor = isError ? 'var(--red-7)' : 'var(--green-7)';
|
||
toast.style.display = 'block';
|
||
setTimeout(() => { toast.style.display = 'none'; }, 3000);
|
||
}
|
||
|
||
function getUploadUrl(path) {
|
||
if (!path) return '';
|
||
if (path.startsWith('http')) return path;
|
||
return API_BASE.replace('/api', path);
|
||
}
|
||
|
||
function escapeAttribute(value) {
|
||
return String(value || '').replace(/[&<>"']/g, (char) => ({
|
||
'&': '&',
|
||
'<': '<',
|
||
'>': '>',
|
||
'"': '"',
|
||
"'": '''
|
||
})[char]);
|
||
}
|
||
|
||
function normalizeMarkerImages(marker) {
|
||
const images = marker.images || (marker.image ? [marker.image] : []);
|
||
return { ...marker, images };
|
||
}
|
||
|
||
function canManageSharing() {
|
||
return !currentJourneyOwnerId || currentJourneyOwnerId === currentUser?.id;
|
||
}
|
||
|
||
function uniqueIds(ids) {
|
||
return [...new Set((ids || []).map((id) => parseInt(id)).filter(Number.isInteger))];
|
||
}
|
||
|
||
async function loadUsers() {
|
||
try {
|
||
const res = await fetch(`${API_BASE}/users`, { credentials: 'include' });
|
||
if (!res.ok) throw new Error('Could not load users');
|
||
availableUsers = await res.json();
|
||
} catch (err) {
|
||
availableUsers = [];
|
||
showToast('Sharing users could not be loaded', true);
|
||
}
|
||
}
|
||
|
||
function getUserCheckboxHtml(user, group, checked, disabled) {
|
||
return `
|
||
<label class="sharing-user">
|
||
<input type="checkbox" class="${group}-user" value="${user.id}" ${checked ? 'checked' : ''} ${disabled ? 'disabled' : ''}>
|
||
<span>${escapeHtml(user.username)}</span>
|
||
</label>
|
||
`;
|
||
}
|
||
|
||
function renderSharingControls() {
|
||
const panel = document.getElementById('sharing-panel');
|
||
const visibilitySelect = document.getElementById('journey-visibility');
|
||
const visibility = visibilitySelect.value;
|
||
const readContainer = document.getElementById('shared-read-users');
|
||
const editContainer = document.getElementById('shared-edit-users');
|
||
const isShared = visibility === 'shared';
|
||
const disabled = !canManageSharing();
|
||
|
||
visibilitySelect.disabled = disabled;
|
||
panel.classList.toggle('visible', isShared);
|
||
if (!isShared) return;
|
||
|
||
if (!availableUsers.length) {
|
||
readContainer.innerHTML = '<p class="empty-share-list">No other users found.</p>';
|
||
editContainer.innerHTML = '<p class="empty-share-list">No other users found.</p>';
|
||
return;
|
||
}
|
||
|
||
readContainer.innerHTML = availableUsers.map((user) =>
|
||
getUserCheckboxHtml(user, 'shared-read', sharedReadIds.includes(user.id), disabled)
|
||
).join('');
|
||
editContainer.innerHTML = availableUsers.map((user) =>
|
||
getUserCheckboxHtml(user, 'shared-edit', sharedEditIds.includes(user.id), disabled)
|
||
).join('');
|
||
|
||
document.querySelectorAll('.shared-read-user').forEach((input) => {
|
||
input.addEventListener('change', updateSharedUsersFromForm);
|
||
});
|
||
document.querySelectorAll('.shared-edit-user').forEach((input) => {
|
||
input.addEventListener('change', updateSharedUsersFromForm);
|
||
});
|
||
}
|
||
|
||
function updateSharedUsersFromForm() {
|
||
sharedReadIds = uniqueIds([...document.querySelectorAll('.shared-read-user:checked')].map((input) => input.value));
|
||
sharedEditIds = uniqueIds([...document.querySelectorAll('.shared-edit-user:checked')].map((input) => input.value));
|
||
sharedReadIds = uniqueIds([...sharedReadIds, ...sharedEditIds]);
|
||
renderSharingControls();
|
||
}
|
||
|
||
function getSharingPayload(visibility) {
|
||
if (visibility !== 'shared') {
|
||
return { shared_read: [], shared_edit: [] };
|
||
}
|
||
return {
|
||
shared_read: uniqueIds([...sharedReadIds, ...sharedEditIds]),
|
||
shared_edit: uniqueIds(sharedEditIds)
|
||
};
|
||
}
|
||
|
||
function renderMarkerImages(marker, idx) {
|
||
const images = marker.images || [];
|
||
if (!images.length) {
|
||
return '';
|
||
}
|
||
|
||
return `
|
||
<div class="image-preview-grid">
|
||
${images.map((image, imageIdx) => {
|
||
const url = typeof image === 'string' ? image : image.url;
|
||
const alt = typeof image === 'string' ? 'Chapter image' : image.originalName || 'Chapter image';
|
||
if (!url) return '';
|
||
return `
|
||
<div class="image-preview">
|
||
<img src="${escapeAttribute(getUploadUrl(url))}" alt="${escapeAttribute(alt)}">
|
||
<button type="button" class="remove-image" data-index="${idx}" data-image-index="${imageIdx}" aria-label="Remove image">
|
||
<i class="fas fa-times"></i>
|
||
</button>
|
||
</div>
|
||
`;
|
||
}).join('')}
|
||
</div>
|
||
`;
|
||
}
|
||
|
||
async function uploadMarkerImages(files) {
|
||
if (!files.length) return [];
|
||
|
||
const formData = new FormData();
|
||
files.forEach((file) => formData.append('images', file));
|
||
|
||
const res = await fetch(`${API_BASE}/uploads/images`, {
|
||
method: 'POST',
|
||
body: formData,
|
||
credentials: 'include'
|
||
});
|
||
const data = await res.json();
|
||
if (!res.ok) throw new Error(data.error || 'Image upload failed');
|
||
return data.images || [];
|
||
}
|
||
|
||
// ==================== RENDER MARKERS ====================
|
||
function renderMarkers() {
|
||
const container = document.getElementById('markers-container');
|
||
if (!markersData.length) {
|
||
container.innerHTML = '<p class="empty-state" style="text-align:center; color: var(--gray-6);">No chapters yet. Click "Add Chapter" to create one.</p>';
|
||
return;
|
||
}
|
||
container.innerHTML = markersData.map((marker, idx) => `
|
||
<div class="marker-card" data-marker-index="${idx}">
|
||
<div class="marker-header">
|
||
<h4><i class="fas fa-map-pin"></i> Chapter ${idx+1}</h4>
|
||
<button type="button" class="remove-marker" data-index="${idx}"><i class="fas fa-trash-alt"></i></button>
|
||
</div>
|
||
<div class="marker-coords">
|
||
<i class="fas fa-location-dot"></i> ${marker.lat.toFixed(6)}, ${marker.lng.toFixed(6)}
|
||
</div>
|
||
<div class="form-group">
|
||
<label>Chapter Title</label>
|
||
<input type="text" class="marker-title" data-index="${idx}" value="${escapeHtml(marker.title || '')}" placeholder="Title">
|
||
</div>
|
||
<div class="form-group">
|
||
<label>Date (optional)</label>
|
||
<input type="date" class="marker-date" data-index="${idx}" value="${marker.date || ''}">
|
||
</div>
|
||
<div class="form-group">
|
||
<label>Description</label>
|
||
<textarea class="marker-description" data-index="${idx}" rows="3" placeholder="Write about this chapter...">${escapeHtml(marker.description || '')}</textarea>
|
||
</div>
|
||
<div class="form-group">
|
||
<label>Images</label>
|
||
<input type="file" class="marker-images" data-index="${idx}" accept="image/*" multiple>
|
||
${renderMarkerImages(marker, idx)}
|
||
</div>
|
||
<div class="form-group">
|
||
<label>Video URL</label>
|
||
<input type="text" class="marker-video" data-index="${idx}" value="${escapeHtml(marker.videoUrl || '')}" placeholder="https://youtu.be/...">
|
||
</div>
|
||
</div>
|
||
`).join('');
|
||
|
||
// Attach remove listeners
|
||
document.querySelectorAll('.remove-marker').forEach(btn => {
|
||
btn.addEventListener('click', (e) => {
|
||
const idx = parseInt(btn.dataset.index);
|
||
markersData.splice(idx, 1);
|
||
renderMarkers();
|
||
});
|
||
});
|
||
|
||
// Attach input listeners to update markersData
|
||
document.querySelectorAll('.marker-title').forEach(input => {
|
||
input.addEventListener('input', (e) => {
|
||
const idx = parseInt(input.dataset.index);
|
||
markersData[idx].title = input.value;
|
||
});
|
||
});
|
||
document.querySelectorAll('.marker-date').forEach(input => {
|
||
input.addEventListener('input', (e) => {
|
||
const idx = parseInt(input.dataset.index);
|
||
markersData[idx].date = input.value;
|
||
});
|
||
});
|
||
document.querySelectorAll('.marker-description').forEach(textarea => {
|
||
textarea.addEventListener('input', (e) => {
|
||
const idx = parseInt(textarea.dataset.index);
|
||
markersData[idx].description = textarea.value;
|
||
});
|
||
});
|
||
document.querySelectorAll('.marker-images').forEach(input => {
|
||
input.addEventListener('change', async () => {
|
||
const idx = parseInt(input.dataset.index);
|
||
try {
|
||
const uploadedImages = await uploadMarkerImages(Array.from(input.files));
|
||
markersData[idx].images = [...(markersData[idx].images || []), ...uploadedImages];
|
||
showToast('Image uploaded');
|
||
renderMarkers();
|
||
} catch (err) {
|
||
showToast(err.message, true);
|
||
}
|
||
});
|
||
});
|
||
document.querySelectorAll('.remove-image').forEach(btn => {
|
||
btn.addEventListener('click', () => {
|
||
const idx = parseInt(btn.dataset.index);
|
||
const imageIdx = parseInt(btn.dataset.imageIndex);
|
||
markersData[idx].images = (markersData[idx].images || []).filter((_, i) => i !== imageIdx);
|
||
renderMarkers();
|
||
});
|
||
});
|
||
document.querySelectorAll('.marker-video').forEach(input => {
|
||
input.addEventListener('input', (e) => {
|
||
const idx = parseInt(input.dataset.index);
|
||
markersData[idx].videoUrl = input.value;
|
||
});
|
||
});
|
||
}
|
||
|
||
// ==================== LOAD JOURNEY ====================
|
||
async function loadJourney(id) {
|
||
console.log('Loading journey with id:', id);
|
||
try {
|
||
const res = await fetch(`${API_BASE}/journeys/${id}`, { credentials: 'include' });
|
||
if (!res.ok) {
|
||
console.error('Failed to fetch journey, status:', res.status);
|
||
throw new Error('Journey not found');
|
||
}
|
||
const journey = await res.json();
|
||
console.log('Journey data:', journey);
|
||
currentJourneyId = journey.id;
|
||
currentJourneyOwnerId = journey.owner_id;
|
||
document.getElementById('journey-title').value = journey.title;
|
||
document.getElementById('journey-description').value = journey.description || '';
|
||
document.getElementById('journey-visibility').value = journey.visibility || 'private';
|
||
sharedReadIds = uniqueIds(journey.shared_read || []);
|
||
sharedEditIds = uniqueIds(journey.shared_edit || []);
|
||
markersData = (journey.markers || []).map(normalizeMarkerImages);
|
||
console.log('Markers data loaded:', markersData);
|
||
renderSharingControls();
|
||
renderMarkers();
|
||
document.getElementById('form-title').textContent = 'Edit Journey';
|
||
} catch (err) {
|
||
console.error('Error loading journey:', err);
|
||
showToast('Error loading journey: ' + err.message, true);
|
||
// Optionally redirect after a delay
|
||
setTimeout(() => window.location.href = 'blog-list.html', 2000);
|
||
}
|
||
}
|
||
|
||
// ==================== SAVE JOURNEY ====================
|
||
async function saveJourney(event) {
|
||
event.preventDefault();
|
||
const title = document.getElementById('journey-title').value.trim();
|
||
const description = document.getElementById('journey-description').value.trim();
|
||
const visibility = document.getElementById('journey-visibility').value;
|
||
|
||
if (!title) {
|
||
showToast('Please enter a title', true);
|
||
return;
|
||
}
|
||
|
||
// Build markers array from current form data (already in markersData)
|
||
const markers = markersData.map(m => ({
|
||
lat: m.lat,
|
||
lng: m.lng,
|
||
title: m.title || '',
|
||
date: m.date || '',
|
||
description: m.description || '',
|
||
images: m.images || [],
|
||
videoUrl: m.videoUrl || ''
|
||
}));
|
||
|
||
const payload = { title, description, markers };
|
||
if (canManageSharing()) {
|
||
payload.visibility = visibility;
|
||
Object.assign(payload, getSharingPayload(visibility));
|
||
}
|
||
console.log('Saving payload:', payload);
|
||
|
||
try {
|
||
let res;
|
||
if (isNew) {
|
||
res = await fetch(`${API_BASE}/journeys`, {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify(payload),
|
||
credentials: 'include'
|
||
});
|
||
} else {
|
||
res = await fetch(`${API_BASE}/journeys/${currentJourneyId}`, {
|
||
method: 'PUT',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify(payload),
|
||
credentials: 'include'
|
||
});
|
||
}
|
||
if (!res.ok) throw new Error('Save failed');
|
||
const data = await res.json();
|
||
console.log('Save response:', data);
|
||
showToast('Journey saved!');
|
||
// Redirect to read view
|
||
window.location.href = `blog-post.html?id=${data.id}`;
|
||
} catch (err) {
|
||
console.error('Error saving journey:', err);
|
||
showToast('Error saving journey: ' + err.message, true);
|
||
}
|
||
}
|
||
|
||
// ==================== ADD MARKER ====================
|
||
function addMarker() {
|
||
console.log('Adding new marker');
|
||
markersData.push({
|
||
lat: 46.8182,
|
||
lng: 8.2275,
|
||
title: 'New Chapter',
|
||
date: '',
|
||
description: '',
|
||
images: [],
|
||
videoUrl: ''
|
||
});
|
||
renderMarkers();
|
||
}
|
||
|
||
// ==================== INITIALIZATION ====================
|
||
document.addEventListener('DOMContentLoaded', async () => {
|
||
const authenticated = await checkAuthAndRedirect();
|
||
if (!authenticated) return;
|
||
updateUserMenu();
|
||
|
||
if (!isNew && journeyId) {
|
||
await loadUsers();
|
||
await loadJourney(journeyId);
|
||
} else if (isNew) {
|
||
currentJourneyId = null;
|
||
currentJourneyOwnerId = currentUser.id;
|
||
document.getElementById('form-title').textContent = 'New Journey';
|
||
markersData = [];
|
||
sharedReadIds = [];
|
||
sharedEditIds = [];
|
||
await loadUsers();
|
||
renderSharingControls();
|
||
renderMarkers();
|
||
} else {
|
||
window.location.href = 'blog-list.html';
|
||
}
|
||
|
||
document.getElementById('journey-form').addEventListener('submit', saveJourney);
|
||
document.getElementById('add-marker').addEventListener('click', addMarker);
|
||
document.getElementById('journey-visibility').addEventListener('change', renderSharingControls);
|
||
});
|
||
</script>
|
||
</body>
|
||
</html>
|