Skip to content

Commit 12adb74

Browse files
committed
settings mock data working
1 parent bcb59e5 commit 12adb74

File tree

3 files changed

+77
-84
lines changed

3 files changed

+77
-84
lines changed

dist/main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Settings.vue

Lines changed: 74 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -32,101 +32,94 @@ export default {
3232
},
3333
statusMessage: "",
3434
apiData: {
35-
root_url: typeof yunusPluginData !== 'undefined' ? yunusPluginData.root_url : 'http://localhost:5173',
35+
root_url: typeof yunusPluginData !== 'undefined' ? yunusPluginData.root_url.replace(/\/?$/, '/') : 'http://localhost:5173/',
3636
nonce: typeof yunusPluginData !== 'undefined' ? yunusPluginData.nonce : 'development_nonce'
3737
}
3838
};
3939
},
4040
methods: {
4141
async loadSettings() {
42-
// Mock data for local development
43-
if (this.apiData.root_url === 'http://localhost:5173') {
44-
console.log("Mock loading settings locally");
45-
this.settings.numRows = 5;
46-
this.settings.dateFormat = 'human';
47-
this.statusMessage = "Settings loaded (Mock)";
48-
return;
49-
}
50-
51-
try {
52-
const apiUrl = `${this.apiData.root_url}/wp-json/yunus/v1/get-settings`;
53-
const headers = this.apiData.nonce !== 'development_nonce' ? { 'X-WP-Nonce': this.apiData.nonce } : {};
54-
55-
const response = await fetch(apiUrl, { headers });
56-
console.log(`API URL: ${apiUrl}, Status: ${response.status}`);
57-
58-
// Check for HTTP errors
59-
if (!response.ok) {
60-
throw new Error(`HTTP error! status: ${response.status}`);
61-
}
62-
63-
// Check if the response is JSON
64-
const contentType = response.headers.get("content-type");
65-
if (!contentType || !contentType.includes("application/json")) {
66-
const text = await response.text();
67-
console.error("Non-JSON response:", text);
68-
throw new Error("Expected JSON response, received HTML or other format.");
69-
}
70-
71-
const result = await response.json();
42+
if (this.apiData.root_url === 'http://localhost:5173/') {
43+
console.log("Mock loading settings locally");
44+
this.settings.numRows = 5;
45+
this.settings.dateFormat = 'human';
46+
this.statusMessage = "Settings loaded (Mock)";
47+
return;
48+
}
7249
73-
if (result) {
74-
this.settings.numRows = result.numRows || 5;
75-
this.settings.dateFormat = result.dateFormat || 'human';
76-
}
77-
} catch (error) {
78-
console.error("Error loading settings:", error);
79-
this.statusMessage = "Error loading settings. Please check console for details.";
80-
}
81-
},
82-
async saveSettings() {
83-
if (this.apiData.root_url === 'http://localhost:5173') {
84-
console.log("Mock saving settings locally:", this.settings);
85-
this.statusMessage = "Settings saved successfully! (Mock)";
86-
return;
87-
}
50+
try {
51+
const apiUrl = `${this.apiData.root_url}wp-json/yunus/v1/get-settings`;
52+
const headers = { 'X-WP-Nonce': this.apiData.nonce };
8853
89-
try {
90-
const apiUrl = `${this.apiData.root_url}/wp-json/yunus/v1/update-setting`;
91-
const headers = {
92-
'X-WP-Nonce': this.apiData.nonce,
93-
'Content-Type': 'application/json'
94-
};
54+
console.log(`API URL (loadSettings): ${apiUrl}`);
9555
96-
const response = await fetch(apiUrl, {
97-
method: 'POST',
98-
headers: headers,
99-
body: JSON.stringify({
100-
setting_key: 'settings',
101-
setting_value: this.settings
102-
})
103-
});
56+
const response = await fetch(apiUrl, { headers });
10457
105-
console.log(`API URL: ${apiUrl}, Status: ${response.status}`);
58+
if (!response.ok) {
59+
throw new Error(`HTTP error! status: ${response.status}`);
60+
}
10661
107-
if (!response.ok) {
108-
throw new Error(`HTTP error! status: ${response.status}`);
109-
}
62+
const contentType = response.headers.get("content-type");
63+
if (!contentType || !contentType.includes("application/json")) {
64+
const text = await response.text();
65+
console.error("Non-JSON response:", text);
66+
throw new Error("Expected JSON response, received HTML or other format.");
67+
}
11068
111-
const contentType = response.headers.get("content-type");
112-
if (!contentType || !contentType.includes("application/json")) {
113-
const text = await response.text();
114-
console.error("Non-JSON response:", text);
115-
throw new Error("Expected JSON response, received HTML or other format.");
116-
}
69+
const result = await response.json();
11770
118-
const result = await response.json();
71+
if (result) {
72+
this.settings.numRows = result.numRows || 5;
73+
this.settings.dateFormat = result.dateFormat || 'human';
74+
}
75+
} catch (error) {
76+
console.error("Error loading settings:", error);
77+
this.statusMessage = "Error loading settings. Please check console for details.";
78+
}
79+
},
80+
async saveSettings() {
81+
if (this.apiData.root_url === 'http://localhost:5173/') {
82+
console.log("Mock saving settings locally:", this.settings);
83+
this.statusMessage = "Settings saved successfully! (Mock)";
84+
return;
85+
}
11986
120-
if (result.success) {
121-
this.statusMessage = "Settings saved successfully!";
122-
} else {
123-
this.statusMessage = "Failed to save settings.";
87+
try {
88+
const apiUrl = `${this.apiData.root_url}wp-json/yunus/v1/update-setting`;
89+
const headers = {
90+
'X-WP-Nonce': this.apiData.nonce,
91+
'Content-Type': 'application/json'
92+
};
93+
94+
console.log("API URL (saveSettings):", apiUrl);
95+
96+
const response = await fetch(apiUrl, {
97+
method: 'POST',
98+
headers: headers,
99+
body: JSON.stringify({
100+
setting_key: 'settings',
101+
setting_value: this.settings
102+
})
103+
});
104+
105+
if (!response.ok) {
106+
throw new Error(`HTTP error! status: ${response.status}`);
107+
}
108+
109+
const contentType = response.headers.get("content-type");
110+
if (!contentType || !contentType.includes("application/json")) {
111+
const text = await response.text();
112+
console.error("Non-JSON response:", text);
113+
throw new Error("Expected JSON response, received HTML or other format.");
114+
}
115+
116+
const result = await response.json();
117+
this.statusMessage = result.success ? "Settings saved successfully!" : "Failed to save settings.";
118+
} catch (error) {
119+
console.error("Error saving settings:", error);
120+
this.statusMessage = "Error saving settings. Please check console for details.";
121+
}
124122
}
125-
} catch (error) {
126-
console.error("Error saving settings:", error);
127-
this.statusMessage = "Error saving settings. Please check console for details.";
128-
}
129-
}
130123
},
131124
mounted() {
132125
this.loadSettings();
@@ -166,4 +159,4 @@ p {
166159
margin-top: 10px;
167160
color: #42b983;
168161
}
169-
</style>
162+
</style>

yunus_admin_viewer_plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,4 @@ function yunus_get_settings_endpoint() {
158158
];
159159

160160
return rest_ensure_response($settings);
161-
}
161+
}

0 commit comments

Comments
 (0)