-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.js
More file actions
113 lines (95 loc) · 3.84 KB
/
Copy pathfrontend.js
File metadata and controls
113 lines (95 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(function() {
'use strict';
if (typeof filecheck_params === 'undefined') return;
// Short-polling helper to wait for the async CDN library to attach window.Filecheck
function whenReady() {
return new Promise(function(resolve, reject) {
var started = Date.now();
(function tick() {
if (window.Filecheck && window.Filecheck.mount) return resolve();
if (Date.now() - started > 10000) return reject(new Error('Filecheck failed to load after 10s'));
setTimeout(tick, 50);
})();
});
}
var STORAGE_KEY_PREFIX = 'fc_job_';
function getStoredJobId(productId) {
try {
return localStorage.getItem(STORAGE_KEY_PREFIX + productId) || null;
} catch (e) {
return null;
}
}
function storeJobId(productId, jobId) {
try {
if (jobId) {
localStorage.setItem(STORAGE_KEY_PREFIX + productId, jobId);
} else {
localStorage.removeItem(STORAGE_KEY_PREFIX + productId);
}
} catch (e) { /* storage unavailable */ }
}
function saveJobIdToSession(productId, jobId) {
var body = new URLSearchParams({
action: 'filecheck_save_job',
nonce: filecheck_params.nonce,
product_id: productId,
job_id: jobId || '',
});
fetch(filecheck_params.ajax_url, {
method: 'POST',
credentials: 'same-origin',
body: body,
}).catch(function(err) {
console.warn('[Filecheck WooCommerce] Session save failed:', err);
});
}
function init() {
var p = filecheck_params;
var slotSelector = '#fc-slot-' + p.product_id;
var slot = document.querySelector(slotSelector);
if (!slot) return;
var form = slot.closest('form.cart');
whenReady().then(function() {
var config = {
...(window?.FILECHECK_CONFIG || {}),
publishableKey: p.publishable_key,
workflowId: p.workflow_id,
mountSelector: slotSelector,
agentId: p.agent_id || null,
};
if (p.connector_id) config.connectorId = p.connector_id;
// Resume a previous job if the customer refreshes the page
var resumeJobId = getStoredJobId(p.product_id);
if (resumeJobId) config.jobId = resumeJobId;
var el = window.Filecheck.mount(config);
if (el) {
el.on('status', function(e) {
// Persist jobId locally so a page refresh can resume
storeJobId(p.product_id, e.jobId || null);
// Persist to WC session so AJAX cart plugins can read it server-side
saveJobIdToSession(p.product_id, e.jobId || null);
// Keep hidden input in sync for server-side cart validation
if (form) {
var jobInput = form.querySelector('input[name="filecheck_job_id"]');
if (jobInput) jobInput.value = e.jobId || '';
}
});
}
// Clear stored jobId once the customer successfully adds to cart,
// so the next visit starts fresh
if (form) {
form.addEventListener('submit', function() {
storeJobId(p.product_id, null);
}, { once: true });
}
}).catch(function(err) {
console.error('[Filecheck WooCommerce] Initialization error:', err);
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();