feat: add falah-souq marketplace plugin and native falah_wallet widget
- [falah-souq] new standalone plugin with [souq_marketplace] shortcode Fiverr-style service grid with Islamic luxury design (Emerald/Gold/Cream/Navy) Custom post type 'souq_service', category filter bar, card hover states - [falah_wallet] replace iframe with native balance+transactions widget Balance card with loading state, send/receive action buttons Recent transactions list, login-gated with smooth reveal CSS tokens matching Islamic design system - Rebuild falah-shortcodes.zip with updated wallet widget
This commit is contained in:
@@ -273,4 +273,150 @@
|
||||
})
|
||||
.catch(() => {});
|
||||
|
||||
// ── Falah Wallet ──────────────────────────────────────────────────────────
|
||||
|
||||
const walletWidget = document.getElementById('falah-wallet-widget');
|
||||
if (walletWidget) {
|
||||
const balanceEl = document.getElementById('falah-wallet-balance');
|
||||
const balanceFiat = document.getElementById('falah-wallet-balance-fiat');
|
||||
const btnSend = document.getElementById('falah-wallet-btn-send');
|
||||
const btnReceive = document.getElementById('falah-wallet-btn-receive');
|
||||
const txList = document.getElementById('falah-wallet-tx-list');
|
||||
const loginPrompt = document.getElementById('falah-wallet-login-prompt');
|
||||
|
||||
const sendModal = document.getElementById('falah-wallet-send-modal');
|
||||
const receiveModal = document.getElementById('falah-wallet-receive-modal');
|
||||
|
||||
function openModal(el) { el.style.display = 'flex'; document.body.style.overflow = 'hidden'; }
|
||||
function closeModal(el) { el.style.display = 'none'; document.body.style.overflow = ''; }
|
||||
|
||||
if (btnSend) btnSend.addEventListener('click', () => sendModal && openModal(sendModal));
|
||||
if (btnReceive) btnReceive.addEventListener('click', () => receiveModal && openModal(receiveModal));
|
||||
|
||||
document.getElementById('falah-wallet-send-backdrop')?.addEventListener('click', () => closeModal(sendModal));
|
||||
document.getElementById('falah-wallet-send-close')?.addEventListener('click', () => closeModal(sendModal));
|
||||
document.getElementById('falah-wallet-receive-backdrop')?.addEventListener('click', () => closeModal(receiveModal));
|
||||
document.getElementById('falah-wallet-receive-close')?.addEventListener('click', () => closeModal(receiveModal));
|
||||
|
||||
// Copy address
|
||||
document.getElementById('falah-wallet-copy-addr')?.addEventListener('click', function () {
|
||||
const addr = document.getElementById('falah-wallet-address-text')?.textContent || '';
|
||||
if (!addr) return;
|
||||
navigator.clipboard?.writeText(addr).then(() => {
|
||||
this.title = 'Copied!';
|
||||
setTimeout(() => { this.title = 'Copy address'; }, 1500);
|
||||
});
|
||||
});
|
||||
|
||||
// Send form
|
||||
document.getElementById('falah-wallet-send-submit')?.addEventListener('click', function () {
|
||||
const to = document.getElementById('falah-wallet-send-to')?.value.trim();
|
||||
const amount = parseFloat(document.getElementById('falah-wallet-send-amount')?.value);
|
||||
const errEl = document.getElementById('falah-wallet-send-err');
|
||||
|
||||
if (!to || !to.startsWith('0x') || to.length < 10) {
|
||||
if (errEl) errEl.textContent = 'Please enter a valid recipient address.';
|
||||
return;
|
||||
}
|
||||
if (!amount || amount <= 0) {
|
||||
if (errEl) errEl.textContent = 'Please enter a valid amount.';
|
||||
return;
|
||||
}
|
||||
if (errEl) errEl.textContent = '';
|
||||
|
||||
// Redirect to wallet app with prefilled params
|
||||
const base = (cfg.mobileUrl || 'https://falahos.my/mobile').replace(/\/$/, '');
|
||||
const url = `${base}/wallet/send?to=${encodeURIComponent(to)}&amount=${encodeURIComponent(amount)}`;
|
||||
window.open(url, '_blank', 'noopener,noreferrer');
|
||||
closeModal(sendModal);
|
||||
});
|
||||
|
||||
// Fetch wallet data from Falah Mobile REST API
|
||||
function fetchWalletData() {
|
||||
const base = (cfg.mobileUrl || 'https://falahos.my/mobile').replace(/\/$/, '');
|
||||
|
||||
fetch(`${base}/api/wallet/balance`, {
|
||||
credentials: 'include',
|
||||
headers: { 'X-WP-Nonce': cfg.nonce || '' },
|
||||
})
|
||||
.then(r => {
|
||||
if (r.status === 401 || r.status === 403) throw new Error('unauthenticated');
|
||||
if (!r.ok) throw new Error('api_error');
|
||||
return r.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (balanceEl) balanceEl.textContent = (data.balance || '0') + ' ' + (data.currency || 'FLH');
|
||||
if (balanceFiat && data.fiat_value) balanceFiat.textContent = '≈ ' + data.fiat_value;
|
||||
if (btnSend) btnSend.disabled = false;
|
||||
if (btnReceive) btnReceive.disabled = false;
|
||||
|
||||
// Render receive address + QR
|
||||
if (data.address) {
|
||||
const addrEl = document.getElementById('falah-wallet-address-text');
|
||||
if (addrEl) addrEl.textContent = data.address;
|
||||
const qrInner = document.getElementById('falah-wallet-qr-inner');
|
||||
if (qrInner) {
|
||||
const img = document.createElement('img');
|
||||
img.alt = 'Wallet QR Code';
|
||||
img.src = `https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(data.address)}&size=160x160&margin=4`;
|
||||
img.width = 160; img.height = 160;
|
||||
qrInner.innerHTML = '';
|
||||
qrInner.appendChild(img);
|
||||
}
|
||||
}
|
||||
|
||||
return fetch(`${base}/api/wallet/transactions?limit=5`, {
|
||||
credentials: 'include',
|
||||
headers: { 'X-WP-Nonce': cfg.nonce || '' },
|
||||
});
|
||||
})
|
||||
.then(r => r && r.ok ? r.json() : null)
|
||||
.then(data => {
|
||||
if (!txList || !data) return;
|
||||
const items = Array.isArray(data.transactions) ? data.transactions : [];
|
||||
txList.innerHTML = '';
|
||||
if (!items.length) {
|
||||
txList.innerHTML = '<div class="falah-wallet-tx-empty">No transactions yet</div>';
|
||||
return;
|
||||
}
|
||||
items.forEach(tx => {
|
||||
const type = tx.type === 'sent' ? 'sent' : 'received';
|
||||
const icon = type === 'sent' ? '↑' : '↓';
|
||||
const sign = type === 'sent' ? '−' : '+';
|
||||
const date = tx.date ? new Date(tx.date).toLocaleDateString('en-GB', { day: 'numeric', month: 'short' }) : '';
|
||||
const desc = tx.description || (type === 'sent' ? 'Sent' : 'Received');
|
||||
const amount = (tx.amount || '0') + ' ' + (tx.currency || 'FLH');
|
||||
|
||||
const row = document.createElement('div');
|
||||
row.className = 'falah-wallet-tx-item';
|
||||
row.innerHTML = `
|
||||
<div class="falah-wallet-tx-icon ${type}">${icon}</div>
|
||||
<div class="falah-wallet-tx-info">
|
||||
<div class="falah-wallet-tx-desc">${escapeHtml(desc)}</div>
|
||||
<div class="falah-wallet-tx-date">${escapeHtml(date)}</div>
|
||||
</div>
|
||||
<div class="falah-wallet-tx-amount ${type}">${sign}${escapeHtml(amount)}</div>
|
||||
`;
|
||||
txList.appendChild(row);
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
if (err.message === 'unauthenticated') {
|
||||
if (balanceEl) balanceEl.textContent = '—';
|
||||
if (loginPrompt) loginPrompt.style.display = '';
|
||||
if (txList) txList.innerHTML = '';
|
||||
} else {
|
||||
if (balanceEl) balanceEl.textContent = '—';
|
||||
if (txList) txList.innerHTML = '<div class="falah-wallet-tx-empty">Could not load wallet. <a href="' + (cfg.mobileUrl || '') + '/wallet" target="_blank" rel="noopener">Open wallet app →</a></div>';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fetchWalletData();
|
||||
}
|
||||
|
||||
function escapeHtml(str) {
|
||||
return String(str).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
|
||||
}
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user