Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

WIP notifications modal.

Matt Roberts8f71a8733d93
diff --git a/app/cmd/routes.go b/app/cmd/routes.go
index 06548073e..e761492b6 100644
--- a/app/cmd/routes.go
+++ b/app/cmd/routes.go
@@ -71,7 +71,7 @@ func routes(r *web.Engine) *web.Engine {
}
}
- //Starting from this step, a Tenant is required
+ // Starting from this step, a Tenant is required
r.Use(middlewares.RequireTenant())
r.Get("/sitemap.xml", handlers.Sitemap())
@@ -96,7 +96,7 @@ func routes(r *web.Engine) *web.Engine {
r.Get("/oauth/:provider/token", handlers.OAuthToken())
r.Get("/oauth/:provider/echo", handlers.OAuthEcho())
- //If tenant is pending, block it from using any other route
+ // If tenant is pending, block it from using any other route
r.Use(middlewares.BlockPendingTenants())
r.Get("/signin", handlers.SignInPage())
@@ -106,7 +106,7 @@ func routes(r *web.Engine) *web.Engine {
r.Post("/_api/signin/complete", handlers.CompleteSignInProfile())
r.Post("/_api/signin", handlers.SignInByEmail())
- //Block if it's private tenant with unauthenticated user
+ // Block if it's private tenant with unauthenticated user
r.Use(middlewares.CheckTenantPrivacy())
r.Get("/", handlers.Index())
@@ -115,7 +115,7 @@ func routes(r *web.Engine) *web.Engine {
ui := r.Group()
{
- //From this step, a User is required
+ // From this step, a User is required
ui.Use(middlewares.IsAuthenticated())
ui.Get("/settings", handlers.UserSettings())
@@ -129,6 +129,7 @@ func routes(r *web.Engine) *web.Engine {
ui.Post("/_api/user/change-email", handlers.ChangeUserEmail())
ui.Post("/_api/notifications/read-all", handlers.ReadAllNotifications())
ui.Get("/_api/notifications/unread/total", handlers.TotalUnreadNotifications())
+ ui.Get("/_api/notifications/unread", handlers.GetAllNotifications())
// From this step, only Collaborators and Administrators are allowed
ui.Use(middlewares.IsAuthorized(enum.RoleCollaborator, enum.RoleAdministrator))
@@ -146,7 +147,7 @@ func routes(r *web.Engine) *web.Engine {
ui.Get("/admin/authentication", handlers.ManageAuthentication())
ui.Get("/_api/admin/oauth/:provider", handlers.GetOAuthConfig())
- //From this step, only Administrators are allowed
+ // From this step, only Administrators are allowed
ui.Use(middlewares.IsAuthorized(enum.RoleAdministrator))
ui.Get("/admin/export", handlers.Page("Export · Site Settings", "", "Administration/pages/Export.page"))
diff --git a/app/handlers/notification.go b/app/handlers/notification.go
index f39221785..e00af53ed 100644
--- a/app/handlers/notification.go
+++ b/app/handlers/notification.go
@@ -9,6 +9,18 @@ import (
"github.com/getfider/fider/app/pkg/web"
)
+// GetAllNotifications will get all the notifications for the new modal
+func GetAllNotifications() web.HandlerFunc {
+ return func(c *web.Context) error {
+ q := &query.GetActiveNotifications{}
+ if err := bus.Dispatch(c, q); err != nil {
+ return c.Failure(err)
+ }
+
+ return c.Ok(q.Result)
+ }
+}
+
// TotalUnreadNotifications returns the total number of unread notifications
func TotalUnreadNotifications() web.HandlerFunc {
return func(c *web.Context) error {
@@ -65,7 +77,6 @@ func ReadNotification() web.HandlerFunc {
// ReadAllNotifications marks all unread notifications as read
func ReadAllNotifications() web.HandlerFunc {
return func(c *web.Context) error {
-
if err := bus.Dispatch(c, &cmd.MarkAllNotificationsAsRead{}); err != nil {
return c.Failure(err)
}
diff --git a/app/models/entity/notification.go b/app/models/entity/notification.go
index 385876920..28dc81275 100644
--- a/app/models/entity/notification.go
+++ b/app/models/entity/notification.go
@@ -1,12 +1,21 @@
package entity
-import "time"
+import (
+ "time"
+
+ "github.com/getfider/fider/app/models/enum"
+)
// Notification is the system generated notification entity
type Notification struct {
- ID int `json:"id" db:"id"`
- Title string `json:"title" db:"title"`
- Link string `json:"link" db:"link"`
- Read bool `json:"read" db:"read"`
- CreatedAt time.Time `json:"createdAt" db:"created_at"`
+ ID int `json:"id" db:"id"`
+ Title string `json:"title" db:"title"`
+ Link string `json:"link" db:"link"`
+ Read bool `json:"read" db:"read"`
+ CreatedAt time.Time `json:"createdAt" db:"created_at"`
+ AuthorName string `json:"authorName" db:"name"`
+ AuthorID int `json:"-" db:"author_id"`
+ AvatarBlobKey string `json:"-" db:"avatar_bkey"`
+ AvatarType enum.AvatarType `json:"-" db:"avatar_type"`
+ AvatarURL string `json:"avatarURL,omitempty"`
}
diff --git a/app/services/sqlstore/postgres/notification.go b/app/services/sqlstore/postgres/notification.go
index 2e7d0fc50..5832e9edc 100644
--- a/app/services/sqlstore/postgres/notification.go
+++ b/app/services/sqlstore/postgres/notification.go
@@ -102,16 +102,22 @@ func getNotificationByID(ctx context.Context, q *query.GetNotificationByID) erro
func getActiveNotifications(ctx context.Context, q *query.GetActiveNotifications) error {
return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
- q.Result = []*entity.Notification{}
err := trx.Select(&q.Result, `
- SELECT id, title, link, read, created_at
- FROM notifications
- WHERE tenant_id = $1 AND user_id = $2
- AND (read = false OR updated_at > CURRENT_DATE - INTERVAL '30 days')
+ SELECT n.id, n.title, n.link, n.read, n.created_at, n.author_id, u.avatar_type, u.avatar_bkey, u.name
+ FROM notifications n
+ LEFT JOIN users u ON u.id = n.author_id
+ WHERE n.tenant_id = $1 AND n.user_id = $2
+ AND (n.read = false OR n.updated_at > CURRENT_DATE - INTERVAL '30 days')
`, tenant.ID, user.ID)
if err != nil {
return errors.Wrap(err, "failed to get active notifications")
}
+
+ // Iterate over notifications and build avatar URL
+ for i := range q.Result {
+ q.Result[i].AvatarURL = buildAvatarURL(ctx, q.Result[i].AvatarType, int(q.Result[i].ID), q.Result[i].AuthorName, q.Result[i].AvatarBlobKey)
+ }
+
return nil
})
}
diff --git a/public/components/NotificationIndicator.tsx b/public/components/NotificationIndicator.tsx
index 9813db0e5..63d1d0a88 100644
--- a/public/components/NotificationIndicator.tsx
+++ b/public/components/NotificationIndicator.tsx
@@ -3,12 +3,42 @@ import "./NotificationIndicator.scss"
import React, { useEffect, useState } from "react"
import IconBell from "@fider/assets/images/heroicons-bell.svg"
import { useFider } from "@fider/hooks"
-import { actions } from "@fider/services"
-import { Icon } from "./common"
+import { actions, Fider } from "@fider/services"
+import { Avatar, Icon, Markdown, Moment } from "./common"
+import { Dropdown } from "./common/Dropdown"
+import { Notification } from "@fider/models"
+import { HStack, VStack } from "./layout"
+
+export const NotificationItem = ({ notification }: { notification: Notification }) => {
+ return (
+ <HStack spacing={4} className="px-3">
+ <Avatar user={{ name: notification.authorName, avatarURL: notification.avatarURL }} />
+ <div>
+ <Markdown className="c-notification-indicator-text" text={notification.title} style="full" />
+ <span className="text-muted">
+ <Moment locale={Fider.currentLocale} date={notification.createdAt} />
+ </span>
+ </div>
+ </HStack>
+ )
+}
+
+const NotificationIcon = ({ unreadNotifications }: { unreadNotifications: number }) => {
+ return (
+ <>
+ <span className="c-notification-indicator mr-3">
+ <Icon sprite={IconBell} className="h-6 text-gray-500" />
+ {unreadNotifications > 0 && <div className="c-notification-indicator-unread-counter" />}
+ </span>
+ </>
+ )
+}
export const NotificationIndicator = () => {
const fider = useFider()
const [unreadNotifications, setUnreadNotifications] = useState(0)
+ const [showingNotifications, setShowingNotifications] = useState(false)
+ const [notifications, setNotifications] = useState<Notification[] | undefined>()
useEffect(() => {
if (fider.session.isAuthenticated) {
@@ -20,10 +50,36 @@ export const NotificationIndicator = () => {
}
}, [fider.session.isAuthenticated])
+ useEffect(() => {
+ if (showingNotifications) {
+ actions.getAllNotifications().then((result) => {
+ if (result) {
+ setNotifications(result.data)
+ }
+ })
+ }
+ }, [showingNotifications])
+
return (
- <a href="/notifications" className="c-notification-indicator mr-3">
- <Icon sprite={IconBell} className="h-6 text-gray-500" />
- {unreadNotifications > 0 && <div className="c-notification-indicator-unread-counter" />}
- </a>
+ <Dropdown
+ wide={true}
+ position="left"
+ onToggled={() => setShowingNotifications(!showingNotifications)}
+ renderHandle={<NotificationIcon unreadNotifications={unreadNotifications} />}
+ >
+ <div>
+ {showingNotifications && notifications !== undefined && notifications?.length > 0 && (
+ <>
+ <p className="text-display text-center my-6 bg-gray-50">No new notifications</p>
+ <p className="text-title px-4 bg-gray-50">Previous notifications</p>
+ <VStack spacing={2} className="c-notifications-container py-2" divide={true}>
+ {notifications.map((n) => (
+ <NotificationItem key={n.id} notification={n} />
+ ))}
+ </VStack>
+ </>
+ )}
+ </div>
+ </Dropdown>
)
}
diff --git a/public/components/common/Dropdown.scss b/public/components/common/Dropdown.scss
index 265bf97ca..005b84568 100644
--- a/public/components/common/Dropdown.scss
+++ b/public/components/common/Dropdown.scss
@@ -29,6 +29,11 @@
&--left {
right: 0;
}
+
+ &--wide {
+ max-width: sizing(120);
+ }
+
}
&__listitem {
diff --git a/public/components/common/Dropdown.tsx b/public/components/common/Dropdown.tsx
index 8da23d576..61e36c197 100644
--- a/public/components/common/Dropdown.tsx
+++ b/public/components/common/Dropdown.tsx
@@ -42,7 +42,9 @@ const Divider = () => {
interface DropdownProps {
renderHandle: JSX.Element
position?: "left" | "right"
+ onToggled?: (isOpen: boolean) => void
children: React.ReactNode
+ wide?: boolean
}
interface DropdownContextFuncs {
@@ -57,12 +59,19 @@ export const Dropdown = (props: DropdownProps) => {
const [isOpen, setIsOpen] = useState(false)
const position = props.position || "right"
+ const changeToggleState = (newState: boolean) => {
+ setIsOpen(newState)
+ if (props.onToggled) {
+ props.onToggled(newState)
+ }
+ }
+
const toggleIsOpen = () => {
- setIsOpen(!isOpen)
+ changeToggleState(!isOpen)
}
const close = () => {
- setIsOpen(false)
+ changeToggleState(false)
}
const handleClick = (e: MouseEvent) => {
@@ -82,6 +91,7 @@ export const Dropdown = (props: DropdownProps) => {
}, [])
const listClassName = classSet({
+ "c-dropdown__list--wide": props.wide,
"c-dropdown__list shadow-lg": true,
[`c-dropdown__list--${position}`]: position === "left",
})
diff --git a/public/models/notification.ts b/public/models/notification.ts
index b85940827..a2cece932 100644
--- a/public/models/notification.ts
+++ b/public/models/notification.ts
@@ -4,4 +4,6 @@ export interface Notification {
link: string
read: boolean
createdAt: string
+ authorName: string
+ avatarURL: string
}
diff --git a/public/pages/Home/components/PostsContainer.tsx b/public/pages/Home/components/PostsContainer.tsx
index 9478ec749..70958f636 100644
--- a/public/pages/Home/components/PostsContainer.tsx
+++ b/public/pages/Home/components/PostsContainer.tsx
@@ -113,6 +113,7 @@ export class PostsContainer extends React.Component<PostsContainerProps, PostsCo
<div className="c-posts-container__search-col">
<Input
field="query"
+ className="mb-0"
icon={this.state.query ? IconX : IconSearch}
onIconClick={this.state.query ? this.clearSearch : undefined}
placeholder={t({ id: "home.postscontainer.query.placeholder", message: "Search" })}
diff --git a/public/services/actions/notification.ts b/public/services/actions/notification.ts
index b2b7fe857..cb681338d 100644
--- a/public/services/actions/notification.ts
+++ b/public/services/actions/notification.ts
@@ -1,4 +1,5 @@
import { http, Result } from "@fider/services"
+import { Notification } from "@fider/models"
export const getTotalUnreadNotifications = async (): Promise<Result<number>> => {
return http.get<{ total: number }>("/_api/notifications/unread/total").then((result) => {
@@ -10,6 +11,10 @@ export const getTotalUnreadNotifications = async (): Promise<Result<number>> =>
})
}
+export const getAllNotifications = async (): Promise<Result<Notification[]>> => {
+ return http.get<Notification[]>("/_api/notifications/unread")
+}
+
export const markAllAsRead = async (): Promise<Result> => {
return await http.post("/_api/notifications/read-all")
}