cleanup semicolon from js
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { t, i18n } from '../../i18n.js';
|
||||
import { router } from '../../router.js';
|
||||
import { auth } from '../../services/auth.js';
|
||||
import { directus } from '../../services/directus.js';
|
||||
import { SUPPORTED_CURRENCIES } from '../../services/currency.js';
|
||||
import { t, i18n } from '../../i18n.js'
|
||||
import { router } from '../../router.js'
|
||||
import { auth } from '../../services/auth.js'
|
||||
import { directus } from '../../services/directus.js'
|
||||
import { SUPPORTED_CURRENCIES } from '../../services/currency.js'
|
||||
|
||||
class PageCreate extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
super()
|
||||
this.formData = {
|
||||
title: '',
|
||||
description: '',
|
||||
@@ -19,23 +19,23 @@ class PageCreate extends HTMLElement {
|
||||
location: '',
|
||||
shipping: false,
|
||||
moneroAddress: ''
|
||||
};
|
||||
this.imageFiles = [];
|
||||
this.imagePreviews = [];
|
||||
this.categories = [];
|
||||
this.submitting = false;
|
||||
}
|
||||
this.imageFiles = []
|
||||
this.imagePreviews = []
|
||||
this.categories = []
|
||||
this.submitting = false
|
||||
}
|
||||
|
||||
async connectedCallback() {
|
||||
// Check if logged in
|
||||
if (!auth.isLoggedIn()) {
|
||||
this.showLoginRequired();
|
||||
return;
|
||||
this.showLoginRequired()
|
||||
return
|
||||
}
|
||||
|
||||
await this.loadCategories();
|
||||
this.render();
|
||||
this.unsubscribe = i18n.subscribe(() => this.render());
|
||||
await this.loadCategories()
|
||||
this.render()
|
||||
this.unsubscribe = i18n.subscribe(() => this.render())
|
||||
}
|
||||
|
||||
showLoginRequired() {
|
||||
@@ -46,29 +46,29 @@ class PageCreate extends HTMLElement {
|
||||
<button class="btn btn-primary btn-lg" id="login-btn">${t('auth.login')}</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
`
|
||||
this.querySelector('#login-btn')?.addEventListener('click', () => {
|
||||
const authModal = document.querySelector('auth-modal');
|
||||
const authModal = document.querySelector('auth-modal')
|
||||
if (authModal) {
|
||||
authModal.show('login');
|
||||
authModal.show('login')
|
||||
authModal.addEventListener('login', () => {
|
||||
this.connectedCallback();
|
||||
}, { once: true });
|
||||
this.connectedCallback()
|
||||
}, { once: true })
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
async loadCategories() {
|
||||
try {
|
||||
this.categories = await directus.getCategories();
|
||||
this.categories = await directus.getCategories()
|
||||
} catch (e) {
|
||||
console.error('Failed to load categories:', e);
|
||||
this.categories = [];
|
||||
console.error('Failed to load categories:', e)
|
||||
this.categories = []
|
||||
}
|
||||
}
|
||||
|
||||
disconnectedCallback() {
|
||||
if (this.unsubscribe) this.unsubscribe();
|
||||
if (this.unsubscribe) this.unsubscribe()
|
||||
}
|
||||
|
||||
render() {
|
||||
@@ -220,69 +220,68 @@ class PageCreate extends HTMLElement {
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
`;
|
||||
`
|
||||
|
||||
this.setupEventListeners();
|
||||
this.setupEventListeners()
|
||||
}
|
||||
|
||||
setupEventListeners() {
|
||||
const form = this.querySelector('#create-form');
|
||||
const cancelBtn = this.querySelector('#cancel-btn');
|
||||
const imageInput = this.querySelector('#images');
|
||||
const form = this.querySelector('#create-form')
|
||||
const cancelBtn = this.querySelector('#cancel-btn')
|
||||
const imageInput = this.querySelector('#images')
|
||||
|
||||
form.addEventListener('submit', (e) => this.handleSubmit(e));
|
||||
cancelBtn.addEventListener('click', () => router.back());
|
||||
form.addEventListener('submit', (e) => this.handleSubmit(e))
|
||||
cancelBtn.addEventListener('click', () => router.back())
|
||||
|
||||
form.querySelectorAll('input:not([type="checkbox"]), textarea, select').forEach(input => {
|
||||
input.addEventListener('input', (e) => {
|
||||
if (e.target.name) {
|
||||
this.formData[e.target.name] = e.target.value;
|
||||
this.formData[e.target.name] = e.target.value
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
// Checkbox handler
|
||||
const shippingCheckbox = this.querySelector('#shipping');
|
||||
const shippingCheckbox = this.querySelector('#shipping')
|
||||
shippingCheckbox?.addEventListener('change', (e) => {
|
||||
this.formData.shipping = e.target.checked;
|
||||
});
|
||||
this.formData.shipping = e.target.checked
|
||||
})
|
||||
|
||||
imageInput?.addEventListener('change', (e) => this.handleImageSelect(e));
|
||||
imageInput?.addEventListener('change', (e) => this.handleImageSelect(e))
|
||||
}
|
||||
|
||||
|
||||
handleImageSelect(e) {
|
||||
const files = Array.from(e.target.files);
|
||||
const files = Array.from(e.target.files)
|
||||
|
||||
files.forEach(file => {
|
||||
if (this.imageFiles.length >= 5) return;
|
||||
if (this.imageFiles.length >= 5) return
|
||||
this.imageFiles.push(file)
|
||||
|
||||
this.imageFiles.push(file);
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = (event) => {
|
||||
this.imagePreviews.push(event.target.result);
|
||||
this.updateImagePreviews();
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
const reader = new FileReader()
|
||||
reader.onload = (e) => {
|
||||
this.imagePreviews.push(e.target.result)
|
||||
this.updateImagePreviews()
|
||||
}
|
||||
reader.readAsDataURL(file)
|
||||
})
|
||||
}
|
||||
|
||||
updateImagePreviews() {
|
||||
const container = this.querySelector('#image-previews');
|
||||
const uploadArea = this.querySelector('#upload-area');
|
||||
const container = this.querySelector('#image-previews')
|
||||
const uploadArea = this.querySelector('#upload-area')
|
||||
|
||||
if (container) {
|
||||
container.innerHTML = this.renderImagePreviews();
|
||||
this.setupRemoveListeners();
|
||||
container.innerHTML = this.renderImagePreviews()
|
||||
this.setupRemoveListeners()
|
||||
}
|
||||
|
||||
if (uploadArea) {
|
||||
uploadArea.style.display = this.imageFiles.length >= 5 ? 'none' : 'flex';
|
||||
uploadArea.style.display = this.imageFiles.length >= 5 ? 'none' : 'flex'
|
||||
}
|
||||
}
|
||||
|
||||
renderImagePreviews() {
|
||||
if (this.imagePreviews.length === 0) return '';
|
||||
if (this.imagePreviews.length === 0) return ''
|
||||
|
||||
return this.imagePreviews.map((src, index) => /* html */`
|
||||
<div class="image-preview">
|
||||
@@ -294,37 +293,37 @@ class PageCreate extends HTMLElement {
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
`).join('');
|
||||
`).join('')
|
||||
}
|
||||
|
||||
setupRemoveListeners() {
|
||||
this.querySelectorAll('.remove-image').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
const index = parseInt(e.currentTarget.dataset.index);
|
||||
this.imageFiles.splice(index, 1);
|
||||
this.imagePreviews.splice(index, 1);
|
||||
this.updateImagePreviews();
|
||||
});
|
||||
});
|
||||
const index = parseInt(e.currentTarget.dataset.index)
|
||||
this.imageFiles.splice(index, 1)
|
||||
this.imagePreviews.splice(index, 1)
|
||||
this.updateImagePreviews()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async handleSubmit(e) {
|
||||
e.preventDefault();
|
||||
e.preventDefault()
|
||||
|
||||
if (this.submitting) return;
|
||||
this.submitting = true;
|
||||
if (this.submitting) return
|
||||
this.submitting = true
|
||||
|
||||
const form = e.target;
|
||||
const submitBtn = form.querySelector('[type="submit"]');
|
||||
submitBtn.disabled = true;
|
||||
submitBtn.textContent = t('create.publishing');
|
||||
const form = e.target
|
||||
const submitBtn = form.querySelector('[type="submit"]')
|
||||
submitBtn.disabled = true
|
||||
submitBtn.textContent = t('create.publishing')
|
||||
|
||||
try {
|
||||
// Upload images first
|
||||
let imageIds = [];
|
||||
let imageIds = []
|
||||
if (this.imageFiles.length > 0) {
|
||||
const uploadedFiles = await directus.uploadMultipleFiles(this.imageFiles);
|
||||
imageIds = uploadedFiles.map(f => f.id);
|
||||
const uploadedFiles = await directus.uploadMultipleFiles(this.imageFiles)
|
||||
imageIds = uploadedFiles.map(f => f.id)
|
||||
}
|
||||
|
||||
// Create listing
|
||||
@@ -340,38 +339,38 @@ class PageCreate extends HTMLElement {
|
||||
shipping: this.formData.shipping,
|
||||
monero_address: this.formData.moneroAddress,
|
||||
status: 'published'
|
||||
};
|
||||
}
|
||||
|
||||
// Add images if uploaded
|
||||
if (imageIds.length > 0) {
|
||||
listingData.images = imageIds.map((id, index) => ({
|
||||
directus_files_id: id,
|
||||
sort: index
|
||||
}));
|
||||
}))
|
||||
}
|
||||
|
||||
const listing = await directus.createListing(listingData);
|
||||
const listing = await directus.createListing(listingData)
|
||||
|
||||
router.navigate(`/listing/${listing.id}`);
|
||||
router.navigate(`/listing/${listing.id}`)
|
||||
} catch (error) {
|
||||
console.error('Failed to create listing:', error);
|
||||
submitBtn.disabled = false;
|
||||
submitBtn.textContent = t('create.publish');
|
||||
this.submitting = false;
|
||||
alert(error.message || 'Failed to create listing');
|
||||
console.error('Failed to create listing:', error)
|
||||
submitBtn.disabled = false
|
||||
submitBtn.textContent = t('create.publish')
|
||||
this.submitting = false
|
||||
alert(error.message || 'Failed to create listing')
|
||||
}
|
||||
}
|
||||
|
||||
escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
const div = document.createElement('div')
|
||||
div.textContent = text
|
||||
return div.innerHTML
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define('page-create', PageCreate);
|
||||
customElements.define('page-create', PageCreate)
|
||||
|
||||
const style = document.createElement('style');
|
||||
const style = document.createElement('style')
|
||||
style.textContent = /* css */`
|
||||
page-create .create-page {
|
||||
max-width: 600px;
|
||||
@@ -509,5 +508,5 @@ style.textContent = /* css */`
|
||||
justify-content: flex-end;
|
||||
margin-top: var(--space-xl);
|
||||
}
|
||||
`;
|
||||
document.head.appendChild(style);
|
||||
`
|
||||
document.head.appendChild(style)
|
||||
|
||||
Reference in New Issue
Block a user