Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
goswami34 committed Jan 25, 2025
1 parent 8dea043 commit 9a27096
Showing 1 changed file with 90 additions and 22 deletions.
112 changes: 90 additions & 22 deletions src/plugins/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,62 @@
value,
});

const token = localStorage.getItem("squarCraft_auth_token"); // Replace 'authToken' with the actual key used to store the token
console.log("token found", token);
if (!token) {
alert("User is not authenticated. Please log in.");
return;
}

if (selector && property && value) {
showProgressBar(); // Show progress bar
// Get token from local storage


// try {
// const response = await axios.post("http://localhost:8000/api/v1/modifications", {
// method: "POST",
// headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}` },
// body: JSON.stringify({
// pageId: "alkfja234", // Example pageId; replace as needed
// modifications: {
// [property]: value,
// },
// userId: "6790aa9c823ae33a79a3141e", // Example userId; replace as needed
// }),
// });
// const result = await response.json();

// console.log("Saving style to server...", result);

// if (response.ok) {
// console.log("Style saved globally.");
// alert("Style saved globally!");
// } else {
// throw new Error(result.message || "Failed to save style.");
// }
// } catch (error) {
// console.error("Error saving style:", error);
// alert("An error occurred while saving style.");
// }
try {
const response = await fetch("http://localhost:8000/api/v1/modifications", {
method: "POST",
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer token`, // Replace with actual token
},
body: JSON.stringify({
pageId: "alkfja234", // Example pageId; replace as needed
modifications: {
[property]: value,
},
userId: "6790aa9c823ae33a79a3141e", // Example userId; replace as needed
pageId: "6794a9639818e399ec6ae11c", // Replace with dynamic pageId
modifications: { [property]: value },
userId: "6794773d006930e0ea42adf9", // Replace with dynamic userId
}),
});
const result = await response.json();

console.log("Saving style to server...", result);

if (response.ok) {
console.log("Style saved globally.");
alert("Style saved globally!");
} else {
const result = await response.json();
throw new Error(result.message || "Failed to save style.");
}
} catch (error) {
Expand All @@ -171,27 +204,62 @@
});

// Fetch and apply saved styles globally on all routes
try {
const response = await fetch("http://localhost:8000/api/v1/modifications");
const result = await response.json();
// try {
// const response = await fetch("http://localhost:8000/api/v1/modifications", {

// });
// const result = await response.json();

// console.log("Fetching saved styles...", result);

console.log("Fetching saved styles...", result);
// if (!response.ok) throw new Error(result.message || "Failed to fetch saved styles.");

if (!response.ok) throw new Error(result.message || "Failed to fetch saved styles.");
// const savedStyles = result.modification;

const savedStyles = result.modification;
// if (savedStyles && savedStyles.modifications) {
// const modifications = savedStyles.modifications;
// for (const [property, value] of Object.entries(modifications)) {
// const elements = document.querySelectorAll("*"); // Apply globally or change the selector as needed
// elements.forEach((el) => {
// el.style[property] = value;
// console.log(`Applied saved style ${property}: ${value}`);
// });
// }
// }

if (savedStyles && savedStyles.modifications) {
const modifications = savedStyles.modifications;
for (const [property, value] of Object.entries(modifications)) {
const elements = document.querySelectorAll("*"); // Apply globally or change the selector as needed
// console.log("Saved styles applied globally.");
// } catch (error) {
// console.error("Error fetching saved styles:", error);
// }
const token = localStorage.getItem("squarCraft_auth_token"); // Replace 'authToken' with the actual key used to store the token
console.log(token);
if (!token) {
alert("User is not authenticated. Please log in.");
return;
}
try {
const response = await fetch(
"http://localhost:8000/api/v1/get-modifications?pageId=6794a9639818e399ec6ae11c&userId=6794773d006930e0ea42adf9",
{
headers: {
"Authorization": `Bearer token`, // Replace with actual token
},
}
);

if (!response.ok) throw new Error("Failed to fetch saved styles.");

const result = await response.json();

const savedStyles = result.modifications;
if (savedStyles) {
for (const [property, value] of Object.entries(savedStyles)) {
const elements = document.querySelectorAll("*"); // Apply globally or update the selector
elements.forEach((el) => {
el.style[property] = value;
console.log(`Applied saved style ${property}: ${value}`);
});
}
}

console.log("Saved styles applied globally.");
} catch (error) {
console.error("Error fetching saved styles:", error);
Expand Down

0 comments on commit 9a27096

Please sign in to comment.