fix: add check for when notifications is empty

This commit is contained in:
Jordan Knott 2021-11-04 10:55:34 -05:00
parent 799d7f3ad0
commit de6fe78004
9 changed files with 76 additions and 37 deletions

View File

@ -427,7 +427,7 @@ const NotificationPopup: React.FC = ({ children }) => {
onCompleted: (d) => {
setData((prev) => ({
hasNextPage: d.notified.pageInfo.hasNextPage,
cursor: d.notified.pageInfo.endCursor,
cursor: d.notified.pageInfo.endCursor ?? '',
nodes: [...prev.nodes, ...d.notified.notified],
}));
},
@ -482,7 +482,7 @@ const NotificationPopup: React.FC = ({ children }) => {
updateQuery: (prev, { fetchMoreResult }) => {
if (!fetchMoreResult) return prev;
setData((d) => ({
cursor: fetchMoreResult.notified.pageInfo.endCursor,
cursor: fetchMoreResult.notified.pageInfo.endCursor ?? '',
hasNextPage: fetchMoreResult.notified.pageInfo.hasNextPage,
nodes: [...d.nodes, ...fetchMoreResult.notified.notified],
}));

View File

@ -894,7 +894,7 @@ export type OwnersList = {
export type PageInfo = {
__typename?: 'PageInfo';
endCursor: Scalars['String'];
endCursor?: Maybe<Scalars['String']>;
hasNextPage: Scalars['Boolean'];
};

View File

@ -149,14 +149,19 @@ SELECT notified_id, nn.notification_id, nn.user_id, read, read_at, n.notificatio
LEFT JOIN user_account ON user_account.user_id = n.caused_by
WHERE (n.created_on, n.notification_id) < ($1::timestamptz, $2::uuid)
AND nn.user_id = $3::uuid
AND ($4::boolean = false OR nn.read = false)
AND ($5::boolean = false OR n.action_type = ANY($6::text[]))
ORDER BY n.created_on DESC
LIMIT $4::int
LIMIT $7::int
`
type GetNotificationsForUserIDCursorParams struct {
CreatedOn time.Time `json:"created_on"`
NotificationID uuid.UUID `json:"notification_id"`
UserID uuid.UUID `json:"user_id"`
EnableUnread bool `json:"enable_unread"`
EnableActionType bool `json:"enable_action_type"`
ActionType []string `json:"action_type"`
LimitRows int32 `json:"limit_rows"`
}
@ -190,6 +195,9 @@ func (q *Queries) GetNotificationsForUserIDCursor(ctx context.Context, arg GetNo
arg.CreatedOn,
arg.NotificationID,
arg.UserID,
arg.EnableUnread,
arg.EnableActionType,
pq.Array(arg.ActionType),
arg.LimitRows,
)
if err != nil {

View File

@ -39,5 +39,7 @@ SELECT * FROM notification_notified AS nn
LEFT JOIN user_account ON user_account.user_id = n.caused_by
WHERE (n.created_on, n.notification_id) < (@created_on::timestamptz, @notification_id::uuid)
AND nn.user_id = @user_id::uuid
AND (@enable_unread::boolean = false OR nn.read = false)
AND (@enable_action_type::boolean = false OR n.action_type = ANY(@action_type::text[]))
ORDER BY n.created_on DESC
LIMIT @limit_rows::int;

View File

@ -3295,7 +3295,7 @@ input NotifiedInput {
}
type PageInfo {
endCursor: String!
endCursor: String
hasNextPage: Boolean!
}
@ -13127,14 +13127,11 @@ func (ec *executionContext) _PageInfo_endCursor(ctx context.Context, field graph
return graphql.Null
}
if resTmp == nil {
if !graphql.HasFieldError(ctx, fc) {
ec.Errorf(ctx, "must not be null")
}
return graphql.Null
}
res := resTmp.(string)
res := resTmp.(*string)
fc.Result = res
return ec.marshalNString2string(ctx, field.Selections, res)
return ec.marshalOString2ᚖstring(ctx, field.Selections, res)
}
func (ec *executionContext) _PageInfo_hasNextPage(ctx context.Context, field graphql.CollectedField, obj *PageInfo) (ret graphql.Marshaler) {
@ -22809,9 +22806,6 @@ func (ec *executionContext) _PageInfo(ctx context.Context, sel ast.SelectionSet,
out.Values[i] = graphql.MarshalString("PageInfo")
case "endCursor":
out.Values[i] = ec._PageInfo_endCursor(ctx, field, obj)
if out.Values[i] == graphql.Null {
invalids++
}
case "hasNextPage":
out.Values[i] = ec._PageInfo_hasNextPage(ctx, field, obj)
if out.Values[i] == graphql.Null {

View File

@ -405,7 +405,7 @@ type OwnersList struct {
}
type PageInfo struct {
EndCursor string `json:"endCursor"`
EndCursor *string `json:"endCursor"`
HasNextPage bool `json:"hasNextPage"`
}

View File

@ -163,11 +163,30 @@ func (r *queryResolver) Notified(ctx context.Context, input NotifiedInput) (*Not
log.WithError(err).Error("error decoding cursor")
return &NotifiedResult{}, err
}
enableRead := false
enableActionType := false
actionTypes := []string{}
switch input.Filter {
case NotificationFilterUnread:
enableRead = true
break
case NotificationFilterMentioned:
enableActionType = true
actionTypes = []string{"COMMENT_MENTIONED"}
break
case NotificationFilterAssigned:
enableActionType = true
actionTypes = []string{"TASK_ASSIGNED"}
break
}
n, err := r.Repository.GetNotificationsForUserIDCursor(ctx, db.GetNotificationsForUserIDCursorParams{
CreatedOn: t,
NotificationID: id,
LimitRows: int32(input.Limit + 1),
UserID: userID,
EnableUnread: enableRead,
EnableActionType: enableActionType,
ActionType: actionTypes,
})
if err != nil {
log.WithError(err).Error("error decoding fetching notifications")
@ -180,11 +199,14 @@ func (r *queryResolver) Notified(ctx context.Context, input NotifiedInput) (*Not
"cursorId": id,
"limit": input.Limit,
}).Info("fetched notified")
endCursor := n[len(n)-1]
var endCursor *db.GetNotificationsForUserIDCursorRow
if len(n) != 0 {
endCursor = &n[len(n)-1]
if len(n) == input.Limit+1 {
hasNextPage = true
n = n[:len(n)-1]
endCursor = n[len(n)-1]
endCursor = &n[len(n)-1]
}
}
userNotifications := []Notified{}
for _, notified := range n {
@ -206,9 +228,14 @@ func (r *queryResolver) Notified(ctx context.Context, input NotifiedInput) (*Not
}
userNotifications = append(userNotifications, n)
}
var endCursorEncoded *string
if endCursor != nil {
eCur := utils.EncodeCursor(endCursor.CreatedOn, endCursor.NotificationID)
endCursorEncoded = &eCur
}
pageInfo := &PageInfo{
HasNextPage: hasNextPage,
EndCursor: utils.EncodeCursor(endCursor.CreatedOn, endCursor.NotificationID),
EndCursor: endCursorEncoded,
}
log.WithField("pageInfo", pageInfo).Info("created page info")
return &NotifiedResult{
@ -249,11 +276,14 @@ func (r *queryResolver) Notified(ctx context.Context, input NotifiedInput) (*Not
"nLen": len(n),
"limit": input.Limit,
}).Info("fetched notified")
endCursor := n[len(n)-1]
var endCursor *db.GetNotificationsForUserIDPagedRow
if len(n) != 0 {
endCursor = &n[len(n)-1]
if len(n) == input.Limit+1 {
hasNextPage = true
n = n[:len(n)-1]
endCursor = n[len(n)-1]
endCursor = &n[len(n)-1]
}
}
userNotifications := []Notified{}
for _, notified := range n {
@ -275,9 +305,14 @@ func (r *queryResolver) Notified(ctx context.Context, input NotifiedInput) (*Not
}
userNotifications = append(userNotifications, n)
}
var endCursorEncoded *string
if endCursor != nil {
eCur := utils.EncodeCursor(endCursor.CreatedOn, endCursor.NotificationID)
endCursorEncoded = &eCur
}
pageInfo := &PageInfo{
HasNextPage: hasNextPage,
EndCursor: utils.EncodeCursor(endCursor.CreatedOn, endCursor.NotificationID),
EndCursor: endCursorEncoded,
}
log.WithField("pageInfo", pageInfo).Info("created page info")
return &NotifiedResult{

View File

@ -26,7 +26,7 @@ input NotifiedInput {
}
type PageInfo {
endCursor: String!
endCursor: String
hasNextPage: Boolean!
}

View File

@ -26,7 +26,7 @@ input NotifiedInput {
}
type PageInfo {
endCursor: String!
endCursor: String
hasNextPage: Boolean!
}