Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: Serialize complete logo instead of just logo url and enable href feature on logo click #1116

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions backend/experiment/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,13 @@ def test_serialize_experiment(self):
name='Test Experiment',
description='This is a test experiment',
image=Image.objects.create(
title='Test',
description='',
file='test-image.jpg',
alt='Test',
href='https://www.example.com'
href='https://www.example.com',
rel='',
target='_self',
),
theme_config=create_theme_config()
)
Expand All @@ -193,7 +197,15 @@ def test_serialize_experiment(self):
)
self.assertEqual(
serialized_experiment['image'], {
'file': f'{settings.BASE_URL}/upload/test-image.jpg', 'href': 'https://www.example.com', 'alt': 'Test'}
'title': 'Test',
'description': '',
'file': f'{settings.BASE_URL}/upload/test-image.jpg',
'href': 'https://www.example.com',
'alt': 'Test',
'rel': '',
'target': '_self',
'tags': []
}
)

def test_get_experiment(self):
Expand Down
5 changes: 5 additions & 0 deletions backend/image/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ def serialize_image(image: Image) -> dict:
'file': f'{settings.BASE_URL.strip("/")}/{settings.MEDIA_URL.strip("/")}/{image.file}',
'href': image.href,
'alt': image.alt,
'rel': image.rel,
'target': image.target,
'tags': image.tags,
'title': image.title,
'description': image.description,
}
2 changes: 1 addition & 1 deletion backend/theme/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def serialize_theme(theme: ThemeConfig) -> dict:
'description': theme.description,
'headingFontUrl': theme.heading_font_url,
'bodyFontUrl': theme.body_font_url,
'logoUrl': f'{settings.BASE_URL.strip("/")}/{settings.MEDIA_URL.strip("/")}/{str(theme.logo_image.file)}' if theme.logo_image else None,
'logo': serialize_image(theme.logo_image) if theme.logo_image else None,
'backgroundUrl': f'{settings.BASE_URL.strip("/")}/{settings.MEDIA_URL.strip("/")}/{str(theme.background_image.file)}' if theme.background_image else None,
'footer': serialize_footer(theme.footer) if hasattr(theme, 'footer') else None,
'header': serialize_header(theme.header) if hasattr(theme, 'header') else None
Expand Down
66 changes: 54 additions & 12 deletions backend/theme/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ class ThemeConfigSerializerTest(TestCase):
@classmethod
def setUpTestData(cls):
logo_image = Image.objects.create(
title='Image',
description='',
file='someimage.jpg',
href='someurl.com',
alt='Alt text'
href='https://example.com',
alt='Alt text',
target='_self',
rel='',
)
background_image = Image.objects.create(
file='anotherimage.png',
href='another.url.com',
alt='Another alt text'
href='https://other.example.com',
alt='Another alt text',
target='',
)
cls.theme = ThemeConfig.objects.create(
name='Default',
Expand Down Expand Up @@ -47,13 +52,23 @@ def test_footer_serializer(self):
'logos': [
{
'file': f'{settings.BASE_URL}{settings.MEDIA_URL}someimage.jpg',
'href': 'someurl.com',
'alt': 'Alt text'
'href': 'https://example.com',
'alt': 'Alt text',
'title': 'Image',
'description': '',
'rel': '',
'target': '_self',
'tags': [],
},
{
'file': f'{settings.BASE_URL}{settings.MEDIA_URL}anotherimage.png',
'href': 'another.url.com',
'alt': 'Another alt text'
'href': 'https://other.example.com',
'alt': 'Another alt text',
'title': '',
'description': '',
'rel': '',
'tags': [],
'target': '',
}
],
'privacy': '<p>Some privacy message</p>'
Expand All @@ -78,7 +93,16 @@ def test_theme_config_serializer(self):
'description': 'Default theme configuration',
'headingFontUrl': 'https://example.com/heading_font',
'bodyFontUrl': 'https://example.com/body_font',
'logoUrl': f'{settings.BASE_URL}{settings.MEDIA_URL}someimage.jpg',
'logo': {
'file': f'{settings.BASE_URL}{settings.MEDIA_URL}someimage.jpg',
'href': 'https://example.com',
'alt': 'Alt text',
'title': 'Image',
'description': '',
'rel': '',
'tags': [],
'target': '_self',
},
'backgroundUrl': f'{settings.BASE_URL}{settings.MEDIA_URL}anotherimage.png',
'footer': serialize_footer(self.footer),
'header': serialize_header(self.header),
Expand All @@ -95,7 +119,7 @@ def test_theme_serialization_no_image(self):
'description': 'Default theme configuration',
'headingFontUrl': 'https://example.com/heading_font',
'bodyFontUrl': 'https://example.com/body_font',
'logoUrl': None,
'logo': None,
'backgroundUrl': None,
'footer': serialize_footer(self.footer),
'header': serialize_header(self.header),
Expand All @@ -109,7 +133,16 @@ def test_theme_serialization_no_footer(self):
'description': 'Default theme configuration',
'headingFontUrl': 'https://example.com/heading_font',
'bodyFontUrl': 'https://example.com/body_font',
'logoUrl': f'{settings.BASE_URL}{settings.MEDIA_URL}someimage.jpg',
'logo': {
'file': f'{settings.BASE_URL}{settings.MEDIA_URL}someimage.jpg',
'href': 'https://example.com',
'alt': 'Alt text',
'title': 'Image',
'description': '',
'rel': '',
'target': '_self',
'tags': [],
},
'backgroundUrl': f'{settings.BASE_URL}{settings.MEDIA_URL}anotherimage.png',
'header': serialize_header(self.header),
'footer': None,
Expand All @@ -123,7 +156,16 @@ def test_theme_serialization_no_header(self):
'description': 'Default theme configuration',
'headingFontUrl': 'https://example.com/heading_font',
'bodyFontUrl': 'https://example.com/body_font',
'logoUrl': f'{settings.BASE_URL}{settings.MEDIA_URL}someimage.jpg',
'logo': {
'file': f'{settings.BASE_URL}{settings.MEDIA_URL}someimage.jpg',
'href': 'https://example.com',
'alt': 'Alt text',
'title': 'Image',
'description': '',
'rel': '',
'target': '_self',
'tags': [],
},
'backgroundUrl': f'{settings.BASE_URL}{settings.MEDIA_URL}anotherimage.png',
'header': None,
'footer': serialize_footer(self.footer),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ const theme = {
bodyFontUrl: 'bodyFontUrl.com',
description: 'Description of the theme',
headingFontUrl: 'headingFontUrl.com',
logoUrl: 'logoUrl.com',
logo: {
title: 'Logo title',
description: 'Logo description',
file: 'logo.jpg',
alt: 'Logo alt',
href: 'https://www.example.com',
rel: 'noopener noreferrer',
target: '_blank'
},
name: 'Awesome theme',
footer: {
disclaimer: 'disclaimer',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ const collectionWithTheme = {
bodyFontUrl: 'font/url.com',
description: 'description of the theme',
headingFontUrl: 'another/font/url.com',
logoUrl: 'where/is/the/logo.jpg',
logo: {
title: 'Logo title',
description: 'Logo description',
file: 'logo.jpg',
alt: 'Logo alt',
href: 'https://www.example.com',
rel: 'noopener noreferrer',
target: '_blank'
},
name: 'Collection name',
header: header
}
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/components/Logo/Logo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import useBoundStore from "@/util/stores";
const Logo: React.FC<{ logoClickConfirm: string | null }> = ({ logoClickConfirm = null }) => {
const theme = useBoundStore((state) => state.theme);

const logoUrl = theme?.logoUrl ?? LOGO_URL;
const { alt, title, file, target, rel } = theme?.logo || {};
const href = theme?.logo?.href || URLS.AMLHome;
const logoUrl = file ?? LOGO_URL;

// Handle click on logo, to optionally confirm navigating
const onLogoClick = (e) => {
Expand All @@ -25,16 +27,21 @@ const Logo: React.FC<{ logoClickConfirm: string | null }> = ({ logoClickConfirm
className: "aha__logo",
"aria-label": "Logo",
style: { backgroundImage: `url(${logoUrl})` },
href,
alt: alt || LOGO_TITLE,
title: title || LOGO_TITLE,
target: target || "_self",
rel: rel || "noopener noreferrer",
};

return (
<>
{URLS.AMLHome.startsWith("http") ? (
<a href={URLS.AMLHome} {...logoProps}>
<a {...logoProps}>
{LOGO_TITLE}
</a>
) : (
<Link to={URLS.AMLHome} {...logoProps}>
<Link to={href} {...logoProps}>
{LOGO_TITLE}
</Link>
)}
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/types/Image.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export default interface Image {
title: string;
description: string;
file: string;
href: string;
alt: string;
href: string;
rel: string;
target: '_blank' | '_self' | '_parent' | '_top' | string;
}
4 changes: 3 additions & 1 deletion frontend/src/types/Theme.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Image from "./Image";

export interface Header {
nextExperimentButtonText: string;
aboutButtonText: string;
Expand All @@ -20,7 +22,7 @@ export default interface Theme {
bodyFontUrl: string;
description: string;
headingFontUrl: string;
logoUrl: string;
logo: Image;
name: string;
footer: Footer | null;
header: Header | null;
Expand Down
Loading