-
Notifications
You must be signed in to change notification settings - Fork 1
Add site banner #2111
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
Open
StenOskar
wants to merge
8
commits into
master
Choose a base branch
from
stenhalse/web-50-banner-over-navbar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add site banner #2111
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0d15ba9
Add site banner
StenOskar 8bab9b8
Fix import and lint issues
StenOskar b9dc1f6
Update Navbar.module.scss
StenOskar 394336a
Simplify SiteBanner and fix bugs
StenOskar f461cc9
Fix ruff format
StenOskar 20ea8da
Update 0006_sitebanner.py
StenOskar c7a8290
Update: single active banner & frontend update
StenOskar fe873d0
Update SiteBanner.tsx
StenOskar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Generated by Django 5.2.11 on 2026-04-23 13:57 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('samfundet', '0005_medlemsinfo'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='SiteBanner', | ||
| fields=[ | ||
| ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('version', models.PositiveIntegerField(blank=True, default=0, editable=False, null=True)), | ||
| ('created_at', models.DateTimeField(blank=True, editable=False, null=True)), | ||
| ('updated_at', models.DateTimeField(blank=True, editable=False, null=True)), | ||
| ('text_nb', models.CharField(max_length=128)), | ||
| ('text_en', models.CharField(max_length=128)), | ||
| ('url', models.CharField(blank=True, max_length=500, null=True)), | ||
| ('new_tab', models.BooleanField(default=False)), | ||
| ('start_at', models.DateTimeField()), | ||
| ('end_at', models.DateTimeField(blank=True, null=True)), | ||
| ('created_by', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)), | ||
| ('updated_by', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)), | ||
| ], | ||
| options={ | ||
| 'verbose_name': 'Site banner', | ||
| 'verbose_name_plural': 'Site banners', | ||
| }, | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from django.db import models | ||
| from django.utils import timezone | ||
|
|
||
| from root.utils.mixins import CustomBaseModel | ||
|
|
||
|
|
||
| class SiteBanner(CustomBaseModel): | ||
| text_nb = models.CharField(max_length=128) | ||
| text_en = models.CharField(max_length=128) | ||
|
|
||
| url = models.CharField(max_length=500, blank=True, null=True) | ||
| new_tab = models.BooleanField(default=False) | ||
|
|
||
| start_at = models.DateTimeField() | ||
| end_at = models.DateTimeField(null=True, blank=True) | ||
|
|
||
| class Meta: | ||
| verbose_name = 'Site banner' | ||
| verbose_name_plural = 'Site banners' | ||
|
|
||
| def __str__(self) -> str: | ||
| return f'{self.text_nb[:40]}' | ||
|
|
||
| @classmethod | ||
| def active(cls) -> models.QuerySet: | ||
| now = timezone.now() | ||
| return cls.objects.filter( | ||
| start_at__lte=now, | ||
| ).filter( | ||
| models.Q(end_at__isnull=True) | models.Q(end_at__gte=now), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from rest_framework import decorators | ||
| from rest_framework.request import Request | ||
| from rest_framework.response import Response | ||
| from rest_framework.viewsets import ReadOnlyModelViewSet | ||
| from rest_framework.permissions import AllowAny | ||
|
|
||
| from django.db import models | ||
|
|
||
| from samfundet.serializers import SiteBannerSerializer | ||
| from samfundet.models.site_banner import SiteBanner | ||
|
|
||
|
|
||
| class SiteBannerView(ReadOnlyModelViewSet): | ||
| permission_classes = [AllowAny] | ||
| serializer_class = SiteBannerSerializer | ||
|
|
||
| def get_queryset(self) -> models.QuerySet: | ||
| return SiteBanner.active().order_by('-start_at', '-created_at') | ||
|
|
||
| @decorators.action(detail=False, methods=['get'], url_path='active') | ||
| def active(self, request: Request) -> Response: | ||
| banner = self.get_queryset().first() | ||
| if banner is None: | ||
| return Response(None) | ||
| serializer = self.get_serializer(banner) | ||
| return Response(serializer.data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| @use 'src/constants' as *; | ||
|
|
||
| .banner { | ||
| position: fixed; | ||
| top: 0; | ||
| left: 0; | ||
| right: 0; | ||
| z-index: 11; | ||
| background: $red-samf; | ||
| color: $white; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| padding: 0.5rem 1rem; | ||
| overflow: hidden; | ||
| } | ||
|
|
||
| .content { | ||
| width: 100%; | ||
| max-width: 1200px; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| gap: 0.75rem; | ||
| text-align: center; | ||
| overflow: hidden; | ||
| } | ||
|
|
||
| .text { | ||
| flex: 1; | ||
| min-width: 0; | ||
| text-align: center; | ||
| white-space: nowrap; | ||
| overflow: hidden; | ||
| text-overflow: ellipsis; | ||
| } | ||
|
|
||
| .link { | ||
| color: inherit; | ||
| text-decoration: underline; | ||
|
|
||
| &:focus-visible { | ||
| outline: 2px solid currentcolor; | ||
| outline-offset: 2px; | ||
| } | ||
| } | ||
|
|
||
| @media (max-width: 900px) { | ||
| .content { | ||
| flex-direction: column; | ||
| gap: 0.35rem; | ||
| } | ||
|
|
||
| .text { | ||
| white-space: normal; | ||
| overflow-wrap: anywhere; | ||
| word-break: break-word; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import { useLayoutEffect, useRef } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { getActiveSiteBanner } from '~/api'; | ||
| import { useScrollY } from '~/hooks'; | ||
| import { siteBannerKeys } from '~/queryKeys'; | ||
| import { dbT } from '~/utils'; | ||
| import styles from './SiteBanner.module.scss'; | ||
|
|
||
| type Props = { | ||
| hideAfterPx?: number; | ||
| }; | ||
|
|
||
| export function SiteBanner({ hideAfterPx = 1000 }: Props): JSX.Element | null { | ||
| const ref = useRef<HTMLDivElement | null>(null); | ||
| const { i18n } = useTranslation(); | ||
| const scrollY = useScrollY(); | ||
| const isCoveredByNavbar = scrollY > hideAfterPx; | ||
|
|
||
| const { data: visibleBanner } = useQuery({ | ||
| queryKey: siteBannerKeys.active(), | ||
| queryFn: getActiveSiteBanner, | ||
| }); | ||
|
|
||
| useLayoutEffect(() => { | ||
| const root = document.documentElement; | ||
| const setOffset = (px: number) => root.style.setProperty('--site-banner-offset', `${px}px`); | ||
| const el = ref.current; | ||
|
|
||
| if (!visibleBanner || !el) { | ||
| setOffset(0); | ||
| return; | ||
| } | ||
|
|
||
| const updateOffset = () => { | ||
| setOffset(el.offsetHeight); | ||
| }; | ||
|
|
||
| updateOffset(); | ||
|
|
||
| const observer = new ResizeObserver(updateOffset); | ||
| observer.observe(el); | ||
| window.addEventListener('resize', updateOffset); | ||
|
|
||
| return () => { | ||
| observer.disconnect(); | ||
| window.removeEventListener('resize', updateOffset); | ||
| setOffset(0); | ||
| }; | ||
| }, [visibleBanner]); | ||
|
|
||
| useLayoutEffect(() => { | ||
| document.documentElement.classList.toggle('site-banner-covered', isCoveredByNavbar); | ||
|
|
||
| return () => { | ||
| document.documentElement.classList.remove('site-banner-covered'); | ||
| }; | ||
| }, [isCoveredByNavbar]); | ||
|
|
||
| if (!visibleBanner) return null; | ||
|
|
||
| const content = <span className={styles.text}>{dbT(visibleBanner, 'text', i18n.language)}</span>; | ||
|
|
||
| return ( | ||
| <div ref={ref} className={styles.banner} role="status" aria-live="polite"> | ||
| <div className={styles.content}> | ||
| {visibleBanner.url ? ( | ||
| <a | ||
| className={styles.link} | ||
| href={visibleBanner.url} | ||
| target={visibleBanner.new_tab ? '_blank' : undefined} | ||
| rel={visibleBanner.new_tab ? 'noopener noreferrer' : undefined} | ||
| > | ||
| {content} | ||
| </a> | ||
| ) : ( | ||
| content | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hadde vært fint med en funksjon som returnerer alle bannere i databasen for senere bruk, ikke hast.