Skip to content

Commit 3402e1c

Browse files
awesomerobotCvX
andauthored
DEV: Refactor for readability and add tests for logo display logic (#65)
Co-authored-by: Jarek Radosz <jradosz@gmail.com>
1 parent 02598d0 commit 3402e1c

File tree

2 files changed

+53
-38
lines changed

2 files changed

+53
-38
lines changed

javascripts/discourse/components/brand-header-contents.gjs

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,68 @@ export default class BrandHeaderContents extends Component {
77
@service site;
88

99
get shouldShow() {
10-
return !this.site.mobileView || settings.show_bar_on_mobile;
10+
return this.site.desktopView || settings.show_bar_on_mobile;
1111
}
1212

13-
get brandLogo() {
14-
const mobileView = this.site.mobileView;
15-
const mobileLogoUrl = settings.mobile_logo_url || "";
16-
const showMobileLogo = mobileView && mobileLogoUrl.length > 0;
17-
const logoUrl = settings.logo_url || "";
18-
const logoDarkUrl = settings.logo_dark_url || "";
19-
const title = settings.brand_name;
20-
21-
return {
22-
mobileUrl: showMobileLogo ? mobileLogoUrl : null,
23-
lightImg: {
24-
url: logoUrl,
25-
},
26-
darkImg: {
27-
url: logoDarkUrl,
28-
},
29-
title,
30-
};
13+
get mobileLogoUrl() {
14+
return this.site.mobileView ? settings.mobile_logo_url : null;
3115
}
3216

33-
get hasIcons() {
34-
return settings.icons && settings.icons.length > 0;
17+
get lightLogo() {
18+
return { url: settings.logo_url || "" };
3519
}
3620

37-
get hasLinks() {
38-
return settings.links && settings.links.length > 0;
21+
get darkLogo() {
22+
return { url: settings.logo_dark_url || "" };
3923
}
4024

4125
<template>
4226
<div class="title">
4327
<a href={{settings.website_url}}>
44-
{{#if this.brandLogo.mobileUrl}}
28+
{{#if this.mobileLogoUrl}}
4529
<img
4630
id="brand-logo"
4731
class="logo-big"
48-
src={{this.brandLogo.mobileUrl}}
49-
title={{this.brandLogo.title}}
32+
src={{this.mobileLogoUrl}}
33+
title={{settings.brand_name}}
5034
/>
51-
{{else}}
35+
{{else if this.lightLogo.url}}
5236
<LightDarkImg
5337
id="brand-logo"
5438
class="logo-big"
55-
@lightImg={{this.brandLogo.lightImg}}
56-
@darkImg={{this.brandLogo.darkImg}}
57-
title={{this.brandLogo.title}}
39+
@lightImg={{this.lightLogo}}
40+
@darkImg={{this.darkLogo}}
41+
title={{settings.brand_name}}
5842
/>
43+
{{else}}
44+
<h2 id="brand-text-logo" class="text-logo">
45+
{{settings.brand_name}}
46+
</h2>
5947
{{/if}}
6048
</a>
6149
</div>
6250

63-
{{#if this.hasLinks}}
51+
{{#if settings.links}}
6452
<nav class="links">
6553
<ul class="nav {{if this.shouldShow 'nav-pills'}}">
66-
{{#each settings.links as |tl|}}
54+
{{#each settings.links as |link|}}
6755
<li>
68-
<a href={{tl.url}} target={{tl.target}}>
69-
{{tl.text}}
56+
<a href={{link.url}} target={{link.target}}>
57+
{{link.text}}
7058
</a>
7159
</li>
7260
{{/each}}
7361
</ul>
7462
</nav>
7563
{{/if}}
7664

77-
{{#if this.hasIcons}}
65+
{{#if settings.icons}}
7866
<div class="panel">
7967
<ul class="icons">
80-
{{#each settings.icons as |il|}}
68+
{{#each settings.icons as |iconLink|}}
8169
<li>
82-
<a href={{il.url}} target={{il.target}}>
83-
{{dIcon il.icon_name}}
70+
<a href={{iconLink.url}} target={{iconLink.target}}>
71+
{{dIcon iconLink.icon_name}}
8472
</a>
8573
</li>
8674
{{/each}}

spec/system/viewing_brand_header_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
expect(page).not_to have_css(".b-header")
2121
end
2222

23+
it "renders as a dropdown on mobile", mobile: true do
24+
theme.update_setting(:show_bar_on_mobile, false)
25+
theme.save!
26+
27+
visit("/")
28+
29+
expect(page).to have_css("#toggle-hamburger-brand-menu")
30+
end
31+
2332
it "should display the brand header with the correct title and links" do
2433
theme.update_setting(:website_url, "http://some.url.com")
2534
theme.update_setting(:brand_name, "some name")
@@ -63,4 +72,22 @@
6372
'a[href="http://some.url.com/some-pencil-link"][target="_blank"] .d-icon-pencil',
6473
)
6574
end
75+
76+
it "shows the brand name when no logo is uploaded" do
77+
theme.update_setting(:brand_name, "some name")
78+
theme.save!
79+
80+
visit("/")
81+
82+
expect(page).to have_css("#brand-text-logo", text: "some name")
83+
end
84+
85+
it "does not show the brand name when a logo is uploaded" do
86+
theme.update_setting(:logo_url, "http://example.com/logo.png")
87+
theme.save!
88+
89+
visit("/")
90+
91+
expect(page).to have_css('img#brand-logo[src="http://example.com/logo.png"]', visible: :all)
92+
end
6693
end

0 commit comments

Comments
 (0)