-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/app setting panel notifications #43
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
pavlovskakristina
wants to merge
17
commits into
dev
Choose a base branch
from
feature/app-setting-panel--notifications
base: dev
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c72947c
Dodanie pliku Powiadomienia.tsx i edytowanie pliku footer_Ustawienia.tsx
pavlovskakristina 9218af1
Dodanie panelu powiadomień i poprawki w ustawieniach
pavlovskakristina 9f1f8cc
feat: Dodanie przycisku powrotu do ustawień
pavlovskakristina 21cde4f
Merge branch 'dev' into feature/app-setting-panel--notifications
pavlovskakristina 9558b72
feat: dodanie trwałego zapisu ustawień powiadomień w cookies
pavlovskakristina 351fe2c
feat: useNotifications - blokowanie powiadomien na podstawie cookies
pavlovskakristina 06d6d59
feat: Powiadomienia - aktualizacja tytulow powiadomien
pavlovskakristina 7c9675c
feat: Utworzenie pliku z konfiguracja i mapowaniem notificationConfig.ts
pavlovskakristina e909566
feat: integracja wspólnej konfiguracji powiadomień w hooku i widoku
pavlovskakristina 4a8ca41
refactor: ujednolicenie wielkości liter (/powiadomienia)
pavlovskakristina 21f5763
refactor: zastpienie biblioteki js-cookie natywnym rozwiazaniem
pavlovskakristina 95e6da6
Merge remote-tracking branch 'origin/dev' into feature/app-setting-pa…
pavlovskakristina 5405501
feat: dodanie mozliwosci sprawdzenia, czy cookie dzialaja zgodnie z z…
pavlovskakristina f10d0bb
Odinstalowanie js-cookie i ts-cookie
pavlovskakristina 7b16626
fix: Naprawa przycisku 'zezwól na powiadomienia' - znika po kliknięciu
pavlovskakristina a8db91f
fix: Naprawa przycisku 'zezwól na powiadomienia' - hook
pavlovskakristina 639598c
feat: Dodanie przy kazdym powiadomieniu body (pochodzenie)
pavlovskakristina 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
| export interface AppNotificationConfig { | ||
| id: string; | ||
| title: string; | ||
| body: string; | ||
| } | ||
|
|
||
| export const NOTIFICATIONS: AppNotificationConfig[] = [ | ||
| { id: "1", title: "Cykliczne powiadomienie ⏰", body: "To powiadomienie wysyła się automatycznie z naszego harmonogramu." }, | ||
| { id: "2", title: "Czas na przerwę! ☕", body: "To powiadomienie wysyła się automatycznie z naszego harmonogramu." }, | ||
| { id: "3", title: "Czas na ćwiczenie! 🔵", body: "To powiadomienie wysyła się automatycznie z naszego harmonogramu." }, | ||
| { id: "4", title: "Zadbaj o nawodnienie! 💧", body: "To powiadomienie wysyła się automatycznie z naszego harmonogramu." }, | ||
| ]; | ||
|
|
||
| // Mapowanie tytułu na id – używane w useNotifications | ||
| export const NOTIFICATION_TITLE_TO_ID: Record<string, string> = Object.fromEntries( | ||
| NOTIFICATIONS.map((n) => [n.title, n.id]) | ||
| ); |
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,152 @@ | ||
| import { Bell, SquareArrowLeft } from "lucide-react"; | ||
| import { useState, useEffect } from "react"; | ||
| import { Link } from "react-router-dom"; | ||
| import { NOTIFICATIONS } from "../lib/notificationConfig"; | ||
| import { useNotifications } from "../hooks/useNotifications"; | ||
|
|
||
|
|
||
| interface UstawieniaPowiadomien { | ||
| id: string; | ||
| title: string; | ||
| active: boolean; | ||
| icon: React.ReactNode; | ||
| } | ||
|
|
||
| // COOKIES | ||
| const COOKIE_KEY = "user_notifications_preferences"; | ||
|
|
||
| function getCookie(key: string): string | undefined { | ||
| return document.cookie.split("; ") | ||
| .find(row => row.startsWith(key + "=")) | ||
| ?.split("=")[1]; | ||
| } | ||
|
|
||
| function setCookie(key: string, value: string, days: number): void { | ||
| const expires = new Date(); | ||
| expires.setDate(expires.getDate() + days); | ||
| document.cookie = `${key}=${value}; expires=${expires.toUTCString()}; path=/`; | ||
| } | ||
|
|
||
|
|
||
| function Powiadomienia() { | ||
|
|
||
| // Sprawdzamy czy przeglądarka obsługuje powiadomienia | ||
| const [pushSupported, setPushSupported] = useState<boolean | null>(null); | ||
| const { permission, requestPermission } = useNotifications(); | ||
|
|
||
| useEffect(() => { | ||
| const supported = "Notification" in window | ||
| && "serviceWorker" in navigator | ||
| && "PushManager" in window; | ||
| setPushSupported(supported); | ||
| }, []); | ||
|
|
||
| // Stan przechowujący wybory użytkownika | ||
| const [settings, setSettings] = useState<UstawieniaPowiadomien[]>(() => { | ||
| const savedCookies = getCookie(COOKIE_KEY); | ||
|
|
||
| const defaultSettings = NOTIFICATIONS.map((n) => ({ | ||
| ...n, | ||
| active: true, | ||
| icon: <Bell size={17} />, | ||
| })); | ||
|
|
||
|
|
||
| if (savedCookies) { | ||
| try { | ||
| const parsed = JSON.parse(savedCookies); | ||
| // Łączymy ikony z zapisanymi stanami powiadomien | ||
| return defaultSettings.map(ds => ({ | ||
| ...ds, | ||
| active: parsed[ds.id] ?? ds.active | ||
| })); | ||
| } catch (error) { | ||
| // W przypadku wystąpienia błędu przy cookies, ustawiamy domyśne ustawienia | ||
| console.error("Błąd podczas odczytywania cookies:", error); // wyswietlenie bledu | ||
| return defaultSettings; | ||
| } | ||
| } | ||
| return defaultSettings; | ||
| }); | ||
|
|
||
| // Funkcja przełączająca stan konkretnego powiadomienia i zapisująca nowy stan do COOKIES | ||
| const toggle = (id: string) => { | ||
| const newSettings = settings.map(item => | ||
| item.id === id ? { ...item, active: !item.active } : item | ||
| ); | ||
|
|
||
| setSettings(newSettings); | ||
|
|
||
|
|
||
| // Zapisywanie uproszczonego obiektu do COOKIES {id, active} | ||
| const configToSave = newSettings.reduce((acc, curr) => ({ | ||
| ...acc, [curr.id]: curr.active | ||
| }), {}); | ||
|
|
||
| // Po 365 dniach COOKIES zostaje usuniete | ||
| setCookie(COOKIE_KEY, JSON.stringify(configToSave), 365); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="min-h-screen w-full bg-gray-100 flex flex-col items-center p-10 relative"> | ||
|
|
||
| {/* Przycisk powrotu */} | ||
| <Link | ||
| to="/Ustawienia" | ||
| className="absolute left-6 top-10 p-3 bg-white text-black rounded-full shadow-lg active:scale-95 transition-all hover:bg-gray-50" | ||
| > | ||
| <SquareArrowLeft size={20} /> | ||
| </Link> | ||
|
|
||
| {/* Nagłówek strony */} | ||
| <div className="w-full max-w-md flex items-center mb-8"> | ||
| <h1 className="text-4xl font-bold text-gray-800">Powiadomienia</h1> | ||
| </div> | ||
|
|
||
| {/* Lista opcji do wyboru */} | ||
| <div className="w-full max-w-md space-y-3"> | ||
| {settings.map((item) => ( | ||
| <div | ||
| key={item.id} | ||
| onClick={() => toggle(item.id)} | ||
| className="flex items-center justify-between p-4 bg-white rounded-2xl shadow-sm cursor-pointer active:bg-gray-50 transition-colors" | ||
| > | ||
| {/* Ikonka i naglowek powiadomienia */} | ||
| <div className="flex items-center gap-4"> | ||
| <div className="p-2 bg-blue-50 text-blue-600 rounded-lg"> | ||
| {item.icon} | ||
| </div> | ||
|
|
||
| <span className="font-medium text-gray-600"> | ||
| {item.title}</span> | ||
| </div> | ||
|
|
||
| {/* Toggle - przycisk switch */} | ||
| <div className={`w-12 h-6 rounded-full transition-colors relative ${item.active ? 'bg-green-500' : 'bg-gray-300'}`}> | ||
| <div className={`absolute top-1 w-4 h-4 bg-white rounded-full transition-all ${item.active ? 'left-7' : 'left-1'}`} /> | ||
| </div> | ||
| </div> | ||
| ))} | ||
| </div> | ||
|
|
||
| <div className="mt-auto mb-6 text-center px-4"> | ||
|
|
||
| {pushSupported === false ? ( // przeglądarka nie obsługuje push | ||
| <p className="text-red-400 text-sm"> | ||
| Twoja przeglądarka nie obsługuje powiadomień push. | ||
| </p> | ||
| ) | ||
| : pushSupported === true && permission !== "granted" ? ( // przeglądarka obsługuje push | ||
| <button | ||
| onClick={requestPermission} | ||
| className="px-4 py-2 bg-blue-500 text-white rounded-xl text-sm" | ||
| > | ||
| Włącz powiadomienia | ||
| </button> | ||
| ) : null} | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default Powiadomienia |
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.
AppNotificationConfig- ale już istnieje coś bardzo podobnego:src/hooks/useTimeNotification.tsThere 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.
Hej
notificationConfig.ts to współdzielona konfiguracja dla całej aplikacji.
NOTIFICATIONS jest używane w Powiadomienia.tsx do renderowania listy ( czyli to miejsce tak naprawdę odpowiada za to, co widzi użytkownik w powiadomieniach). A NOTIFICATION_TITLE_TO_ID w useNotifications.ts do sprawdzania preferencji przed wysłaniem powiadomienia.
Gdybym dała to do useTimeNotification.ts to najprawdopodobniej pojawiłby się błąd. Bo useTimeNotification.ts odpowiada za obliczanie czasu, a nie za sprawdzanie preferencji użytkownika. (czyli plik Powiadomienia.tsx musiałby wtedy importować konfigurację z hooka timera)