Skip to content

Commit

Permalink
feat(notifications): order, theme fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ceddybi committed Mar 31, 2024
1 parent b7f4413 commit 862bae1
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/app/html/html.wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const HtmlPageWrapper = ({ children, ...otherProps }: any) => {
console.log('HtmlPageWrapper', { pathname, hasJs });

return (
<CoreUIRoot cssVars>
<CoreUIRoot cssVars theme={otherProps.theme}>
{/* @ts-ignore */}
<AccentRegion
// {...generateAccentRegionProps("#28ff00")}
Expand Down
32 changes: 32 additions & 0 deletions src/app/html/notifications/[slug]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use server';

// import { logger } from '@/lib/Logger';
import { isEmpty } from 'lodash';
import { NextResponse } from 'next/server';

import {
getNotificationNextPath,
readNotifications,
} from '@/lib/hooksServer/notifications';

export const GET = async (
req: Request,
{ params }: { params: { slug: string } },
) => {
try {
const id = params.slug;
const notifications = await readNotifications({ id });
if (!notifications || isEmpty(notifications)) {
throw new Error('Notifications not found');
}
const notification = notifications[0];
if (!notification) {
throw new Error('Notification not found');
}
const nextPath = getNotificationNextPath(notification);
return NextResponse.redirect(new URL(nextPath, req.url));
} catch (error) {
console.log('An error occurred while fetching notifications', error);
return NextResponse.redirect(new URL('/html/notifications', req.url));
}
};
12 changes: 7 additions & 5 deletions src/components/navHTML/RightNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const AuthRightNav = ({
(b) => (b.model || '').toLowerCase() === BadgeType.Notification,
);

const notificationBadgeCount = notificationBadge?.count || 0;

const avatarUri = cdnPath(currentUser && currentUser.avatar); // TODO default image

const username = currentUser.username || '';
Expand Down Expand Up @@ -146,11 +148,11 @@ const AuthRightNav = ({
variant={ButtonIconType.Primary}
/>

{notificationBadge && notificationBadge.count && (
<div style={{ marginLeft: '-15px', marginTop: '-10px' }}>
<NumberBadge value={notificationBadge.count} />
</div>
)}
{notificationBadgeCount > 0 && (
<div style={{ marginLeft: '-15px', marginTop: '-10px' }}>
<NumberBadge value={notificationBadge.count} />
</div>
)}
</Layout>
</Link>
</Layout>
Expand Down
14 changes: 14 additions & 0 deletions src/components/types.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ export type Query = {
getCityByState: Array<CityType>;
getFeePrices: FeePrices;
getMoneyInEscrow?: Maybe<Scalars['Float']>;
getNotification?: Maybe<Notification>;
getOrderRating?: Maybe<OrderRatingType>;
getOrderRatings: OrderRatingOutputPagination;
getPgpKey?: Maybe<PgpPublicKey>;
Expand All @@ -1012,6 +1013,7 @@ export type Query = {
posts: PostPagination;
reactionGet: Reaction;
reactionPagination: ReactionPagination;
readNotifications: Array<Notification>;
searchAdListingPublic?: Maybe<AdsListingOutputPagination>;
transactionById?: Maybe<Transaction>;
transactionGet: Transaction;
Expand Down Expand Up @@ -1126,6 +1128,11 @@ export type QueryGetCityByStateArgs = {
};


export type QueryGetNotificationArgs = {
id: Scalars['String'];
};


export type QueryGetOrderRatingArgs = {
orderId: Scalars['String'];
};
Expand Down Expand Up @@ -1247,6 +1254,13 @@ export type QueryReactionPaginationArgs = {
};


export type QueryReadNotificationsArgs = {
before?: InputMaybe<Scalars['DateTime']>;
id?: InputMaybe<Scalars['String']>;
limit?: InputMaybe<Scalars['Float']>;
};


export type QuerySearchAdListingPublicArgs = {
after?: InputMaybe<Scalars['DateTime']>;
before?: InputMaybe<Scalars['DateTime']>;
Expand Down
33 changes: 21 additions & 12 deletions src/containers/Notification/Notification.html.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';

import {
Background,
CoreText,
Display,
FlexDirection,
Expand All @@ -10,12 +11,13 @@ import {
SVGAsset,
} from '@uuixjs/uuixweb';
import { BorderRadius } from '@uuixjs/uuixweb-lib';
import Link from 'next/link';
import React from 'react';

import type { NotificationType } from '@/components/types.generated';
import type { Notification } from '@/components/types.generated';

interface Props {
notifications?: NotificationType[];
notifications?: Notification[];
// todo pagination
}

Expand All @@ -29,23 +31,30 @@ export const NotificationsContainerHtml = (props: Props) => {
<Layout
display={Display.Flex}
justifyContent={JustifyContent.Center}
margin={{ top: 2 }}
margin={{ top: 2, bottom: 2 }}
>
<CoreText as="h3">Notifications</CoreText>
</Layout>

{notifications.map((notification) => (
<Layout
<Link
key={notification.id}
padding={2}
display={Display.Flex}
border={BorderRadius.None}
href={`/html/notifications/${notification.id}`}
>
<Icon asset={SVGAsset.Account} />
<CoreText as="h5" bold>
{notification.message}
</CoreText>{' '}
</Layout>
<Layout
padding={2}
display={Display.Flex}
border={BorderRadius.None}
background={
notification.read ? Background.Alt2 : Background.Inherit
}
>
<Icon asset={SVGAsset.Account} />
<CoreText as="h5" bold>
{notification.message}
</CoreText>{' '}
</Layout>
</Link>
))}
</Layout>
);
Expand Down
9 changes: 9 additions & 0 deletions src/lib/gql/notifications/notification.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ export const GET_NOTIFICATIONS = gql`
}
${NotificationFragment}
`;

export const READ_NOTIFICATIONS = gql`
query ReadNotification($id: String, $before: DateTime, $limit: Float) {
data: readNotifications(id: $id, before: $before, limit: $limit) {
...NotificationFragment
}
}
${NotificationFragment}
`;
48 changes: 46 additions & 2 deletions src/lib/hooksServer/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import pickBy from 'lodash/pickBy';

import type {
Badge,
Notification,
NotificationPagination,
QueryGetBadgesArgs,
QueryNotificationsArgs,
QueryReadNotificationsArgs,
} from '@/components/types.generated';

import { getClient } from '../apollo-wrapper.server';
import { GET_BADGES, GET_NOTIFICATIONS } from '../gql/notifications';
import {
GET_BADGES,
GET_NOTIFICATIONS,
READ_NOTIFICATIONS,
} from '../gql/notifications';

export const fetchBadges = async (
props: QueryGetBadgesArgs,
Expand Down Expand Up @@ -57,5 +63,43 @@ export const fetchNotifications = async (
return undefined;
};

// readNotification,
export const readNotifications = async (
props: QueryReadNotificationsArgs,
): Promise<Notification[] | undefined> => {
const [errorData, data] = await awaitTo(
getClient().query<{ data: Notification[] }>({
query: READ_NOTIFICATIONS,
variables: pickBy(props, identity),
fetchPolicy: 'no-cache',
}),
);

if (errorData) {
console.error(errorData);
}

if (data && data.data) {
return data.data.data;
}

return undefined;
};

export const getNotificationNextPath = (notification: Notification): string => {
const notificationSource = (notification.source || '').toLowerCase();

switch (notificationSource) {
case 'user':
return `/html/u/${notification.sourceId}`;
case 'vendor':
return `/html/store/${notification.sourceId}`;
case 'ordertype':
case 'order':
return `/html/order/${notification.sourceId}`;
case 'notification':
default:
return '/html/notifications';
}
};

// deleteNotification, ....

0 comments on commit 862bae1

Please sign in to comment.