Skip to content

Commit 73556a9

Browse files
committed
Use theme path to change theme.
1 parent 245bc7d commit 73556a9

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

admin/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@
9696
});
9797
let themesResponse = await res.json();
9898
for (t of themesResponse['themes']) {
99-
themes.push({ name: t.name, license: t.license, description: t.description, selected: t.name == configResponse['theme'] });
99+
themes.push({ id: t.id, name: t.name, license: t.license, description: t.description, selected: t.name == configResponse['theme'] });
100100
}
101101
themes.sort((a, b) => a.name.localeCompare(b.name));
102102
}
103103

104-
async function saveConfig(themes, desiredThemeName) {
104+
async function saveConfig(themes, desiredThemeId) {
105105
let endpoint = `${getApiBaseUrl()}/api/config`;
106106
let headers = {
107107
...{
@@ -114,11 +114,11 @@
114114
let response = await fetch(endpoint, {
115115
method: 'PUT',
116116
headers: headers,
117-
body: JSON.stringify({ theme: desiredThemeName }),
117+
body: JSON.stringify({ theme: desiredThemeId }),
118118
});
119119
if (response.ok) {
120120
for (let theme of themes) {
121-
theme.selected = theme.name === desiredThemeName;
121+
theme.selected = theme.id === desiredThemeId;
122122
}
123123
} else {
124124
alert("Error changing theme!");
@@ -188,7 +188,7 @@ <h2 class="card-title" x-text="theme.name"></h2>
188188
<p>License: <span x-text="theme.license"></span></p>
189189
<div class="justify-end card-actions">
190190
<button :class="{'btn-primary': !theme.selected}" class="btn"
191-
x-on:click="await saveConfig(themes, theme.name);">Select</button>
191+
x-on:click="await saveConfig(themes, theme.id);">Select</button>
192192
</div>
193193
</div>
194194
</div>

src/main.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ struct PutSiteConfigRequestBody {
9393
theme: String,
9494
}
9595

96+
#[derive(Deserialize, Serialize)]
97+
struct ThemeMetadata {
98+
pub id: String,
99+
pub name: String,
100+
pub description: String,
101+
pub license: Option<String>,
102+
}
103+
104+
impl ThemeMetadata {
105+
fn from_config_with_id(c: ThemeConfig, id: String) -> Self {
106+
ThemeMetadata {
107+
id,
108+
name: c.name,
109+
description: c.description,
110+
license: c.license,
111+
}
112+
}
113+
}
114+
96115
static BLOSSOM_CONTENT_TYPES: phf::Set<&'static str> = phf_set! {
97116
"audio/mpeg",
98117
"image/gif",
@@ -586,11 +605,16 @@ async fn handle_get_themes(request: Request<State>) -> tide::Result<Response> {
586605
return Err(tide::Error::from_str(StatusCode::InternalServerError, ""));
587606
};
588607

608+
let themes = themes
609+
.iter()
610+
.map(|(k, t)| ThemeMetadata::from_config_with_id(t.config.clone(), k.to_string()))
611+
.collect::<Vec<ThemeMetadata>>();
612+
589613
Ok(Response::builder(StatusCode::Ok)
590-
.content_type(mime::JSON)
591-
.header("Access-Control-Allow-Origin", "*")
592-
.body(serde_json::to_string(&json!({"themes": themes.values().map(|t| t.config.clone()).collect::<Vec<ThemeConfig>>()}))?)
593-
.build())
614+
.content_type(mime::JSON)
615+
.header("Access-Control-Allow-Origin", "*")
616+
.body(serde_json::to_string(&json!({"themes": themes}))?)
617+
.build())
594618
}
595619

596620
async fn handle_get_theme(request: Request<State>) -> tide::Result<Response> {

0 commit comments

Comments
 (0)