Skip to content

Commit

Permalink
chore(): fix calls and add more error handlers (#906)
Browse files Browse the repository at this point in the history
* chore(): fix calls and add more error handlers

* chore(): add error handlers to trending widget, minor other fixes

* chore(): fixes
  • Loading branch information
SeverS committed Mar 3, 2021
1 parent 75bc116 commit f391e05
Show file tree
Hide file tree
Showing 23 changed files with 492 additions and 361 deletions.
2 changes: 1 addition & 1 deletion apps/akasha/src/bootstrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const application: Application = {
logo: { type: LogoTypeSource.ICON, value: 'appAkasha' },
widgets: {
[userPostsRoute]: [ProfileCardWidget],
[fullPostRoute]: [ProfileCardWidget],
[fullPostRoute]: [TrendingWidget, ProfileCardWidget],
[routes[FEED]]: [TrendingWidget],
},
};
8 changes: 2 additions & 6 deletions apps/akasha/src/components/app-routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ const AppRoutes: React.FC<RootComponentProps & AppRoutesProps> = props => {
<Route path={routes[FEED]}>
<FeedPage
{...props}
ethAddress={loginState.ethAddress}
currentUserCalled={loginState.currentUserCalled}
pubKey={loginState.pubKey}
loginState={loginState}
flagged={flagged}
reportModalOpen={reportModalOpen}
setFlagged={setFlagged}
Expand All @@ -90,9 +88,7 @@ const AppRoutes: React.FC<RootComponentProps & AppRoutesProps> = props => {
<Route path={`${routes[POST]}/:postId`}>
<PostPage
{...props}
ethAddress={loginState.ethAddress}
currentUserCalled={loginState.currentUserCalled}
pubKey={loginState.pubKey}
loginState={loginState}
flagged={flagged}
reportModalOpen={reportModalOpen}
setFlagged={setFlagged}
Expand Down
45 changes: 22 additions & 23 deletions apps/akasha/src/components/feed-page/feed-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import routes, { POST } from '../../routes';
import { application as loginWidget } from '@akashaproject/ui-widget-login/lib/bootstrap';
import Parcel from 'single-spa-react/parcel';
import usePosts, { PublishPostData } from '@akashaproject/ui-awf-hooks/lib/use-posts';
import { UseLoginState } from '@akashaproject/ui-awf-hooks/lib/use-login-state';

const {
Box,
Expand All @@ -35,9 +36,7 @@ export interface FeedPageProps {
singleSpa: any;
logger: any;
showLoginModal: () => void;
ethAddress: string | null;
currentUserCalled: boolean;
pubKey: string | null;
loginState: UseLoginState;
flagged: string;
reportModalOpen: boolean;
setFlagged: React.Dispatch<React.SetStateAction<string>>;
Expand All @@ -53,9 +52,7 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
setFlagged,
setReportModalOpen,
showLoginModal,
ethAddress,
currentUserCalled,
pubKey,
loginState,
onError,
sdkModules,
logger,
Expand All @@ -70,17 +67,20 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
});

React.useEffect(() => {
if (pubKey) {
loginProfileActions.getProfileData({ pubKey });
if (loginState.pubKey) {
loginProfileActions.getProfileData({ pubKey: loginState.pubKey });
}
}, [pubKey]);
}, [loginState.pubKey]);

React.useEffect(() => {
if (currentUserCalled) {
if (loginState.currentUserCalled) {
postsActions.resetPostIds();
postsActions.getPosts({ limit: 5 });
if (loginState.ethAddress) {
bookmarkActions.getBookmarks();
}
}
}, [currentUserCalled]);
}, [loginState.currentUserCalled, loginState.ethAddress]);

const {
size,
Expand All @@ -91,14 +91,13 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
const locale = (i18n.languages[0] || 'en') as ILocale;

const [bookmarkState, bookmarkActions] = useBookmarks({
pubKey,
onError,
dbService: sdkModules.db,
});
const [, errorActions] = useErrors({ logger });

const [postsState, postsActions] = usePosts({
user: ethAddress,
user: loginState.ethAddress,
postsService: sdkModules.posts,
ipfsService: sdkModules.commons.ipfsService,
onError: errorActions.createError,
Expand All @@ -108,7 +107,7 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
const req: { limit: number; offset?: string } = {
limit: payload.limit,
};
if (!postsState.isFetchingPosts && currentUserCalled) {
if (!postsState.isFetchingPosts && loginState.currentUserCalled) {
postsActions.getPosts(req);
}
};
Expand All @@ -125,7 +124,7 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
props.singleSpa.navigateToUrl(`/profile/${profilePubKey}`);
};
const handleEntryBookmark = (entryId: string) => {
if (!pubKey) {
if (!loginState.pubKey) {
return showLoginModal();
}
if (bookmarkState.bookmarks.findIndex(bm => bm.entryId === entryId) >= 0) {
Expand All @@ -134,7 +133,7 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
return bookmarkActions.bookmarkPost(entryId);
};
const handleEntryRepost = (_withComment: boolean, entryData: any) => {
if (!pubKey) {
if (!loginState.pubKey) {
showLoginModal();
} else {
setCurrentEmbedEntry(entryData);
Expand Down Expand Up @@ -191,7 +190,7 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
const handleNavigateToPost = redirectToPost(props.singleSpa.navigateToUrl);

const handleEntryPublish = async (data: PublishPostData) => {
if (!ethAddress && !pubKey) {
if (!loginState.ethAddress && !loginState.pubKey) {
showLoginModal();
return;
}
Expand Down Expand Up @@ -244,7 +243,7 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
reportLabel={t('Report')}
blockLabel={t('Block User')}
closeLabel={t('Close')}
user={ethAddress ? ethAddress : ''}
user={loginState.ethAddress ? loginState.ethAddress : ''}
contentId={flagged}
contentType="post"
baseUrl={constants.BASE_FLAG_URL}
Expand All @@ -262,7 +261,7 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
slotId={props.layout.app.modalSlotId}
avatar={loginProfile.avatar}
showModal={showEditor}
ethAddress={ethAddress as any}
ethAddress={loginState.ethAddress as any}
postLabel={t('Publish')}
placeholderLabel={t('Write something')}
discardPostLabel={t('Discard Post')}
Expand All @@ -288,9 +287,9 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
hasMoreItems={!!postsState.nextPostIndex}
usePlaceholders={true}
listHeader={
ethAddress ? (
loginState.ethAddress ? (
<EditorPlaceholder
ethAddress={ethAddress}
ethAddress={loginState.ethAddress}
onClick={handleToggleEditor}
style={{ marginTop: 8 }}
avatar={loginProfile.avatar}
Expand All @@ -316,7 +315,7 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
logger={logger}
globalChannel={globalChannel}
bookmarkState={bookmarkState}
ethAddress={ethAddress}
ethAddress={loginState.ethAddress}
locale={locale}
onBookmark={handleEntryBookmark}
onNavigate={handleNavigateToPost}
Expand All @@ -338,7 +337,7 @@ const FeedPage: React.FC<FeedPageProps & RootComponentProps> = props => {
globalChannel,
isMobile,
feedItems: postsState.postIds,
loggedEthAddress: ethAddress,
loggedEthAddress: loginState.ethAddress,
pendingEntries: postsState.pendingPosts,
})}
/>
Expand Down
52 changes: 26 additions & 26 deletions apps/akasha/src/components/post-page/post-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import PostRenderer from './post-renderer';
import { getPendingComments } from './post-page-pending-comments';
import routes, { POST } from '../../routes';
import { IAkashaError, RootComponentProps } from '@akashaproject/ui-awf-typings';
import { UseLoginState } from '@akashaproject/ui-awf-hooks/lib/use-login-state';

const {
Box,
Expand All @@ -38,9 +39,7 @@ const {
} = DS;

interface IPostPage {
ethAddress: string | null;
currentUserCalled: boolean;
pubKey: string | null;
loginState: UseLoginState;
flagged: string;
reportModalOpen: boolean;
setFlagged: React.Dispatch<React.SetStateAction<string>>;
Expand All @@ -57,13 +56,12 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
globalChannel,
flagged,
reportModalOpen,
currentUserCalled,
setFlagged,
setReportModalOpen,
showLoginModal,
logger,
navigateToUrl,
ethAddress,
loginState,
isMobile,
} = props;

Expand All @@ -72,7 +70,7 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
const [, errorActions] = useErrors({ logger });

const [postsState, postsActions] = usePosts({
user: ethAddress,
user: loginState.ethAddress,
postsService: sdkModules.posts,
ipfsService: sdkModules.commons.ipfsService,
onError: errorActions.createError,
Expand All @@ -99,14 +97,13 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
});

React.useEffect(() => {
if (props.pubKey) {
loginProfileActions.getProfileData({ pubKey: props.pubKey });
if (loginState.pubKey) {
loginProfileActions.getProfileData({ pubKey: loginState.pubKey });
}
}, [props.pubKey]);
}, [loginState.pubKey]);

const [bookmarkState, bookmarkActions] = useBookmarks({
dbService: sdkModules.db,
pubKey: props.pubKey,
onError: errorActions.createError,
});

Expand All @@ -117,10 +114,10 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
});

React.useEffect(() => {
if (ethAddress && entryData?.author.ethAddress) {
followActions.isFollowing(ethAddress, entryData.author.ethAddress);
if (loginState.ethAddress && entryData?.author.ethAddress) {
followActions.isFollowing(loginState.ethAddress, entryData.author.ethAddress);
}
}, [ethAddress, entryData?.author.ethAddress]);
}, [loginState.ethAddress, entryData?.author.ethAddress]);

const handleFollow = () => {
if (entryData?.author.ethAddress) {
Expand Down Expand Up @@ -152,11 +149,14 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {

React.useEffect(() => {
// this is used to initialise comments when navigating to other post ids
if (postId && currentUserCalled) {
if (postId && loginState.currentUserCalled) {
postsActions.getPost(postId);
handleLoadMore({ limit: 5, postID: postId });
if (loginState.ethAddress) {
bookmarkActions.getBookmarks();
}
}
}, [postId, currentUserCalled]);
}, [postId, loginState.currentUserCalled, loginState.ethAddress]);

const bookmarked = React.useMemo(() => {
if (
Expand All @@ -178,7 +178,7 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
};

const handleEntryBookmark = (entryId: string) => {
if (!ethAddress) {
if (!loginState.ethAddress) {
return showLoginModal();
}
if (bookmarkState.bookmarks.findIndex(bm => bm.entryId === entryId) >= 0) {
Expand All @@ -188,7 +188,7 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
};

const handleCommentBookmark = (commentId: string) => {
if (!ethAddress) {
if (!loginState.ethAddress) {
return showLoginModal();
}
if (bookmarkState.bookmarks.findIndex(bm => bm.entryId === commentId) >= 0) {
Expand Down Expand Up @@ -222,7 +222,7 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
content: any;
textContent: any;
}) => {
if (!ethAddress) {
if (!loginState.ethAddress) {
showLoginModal();
return;
}
Expand Down Expand Up @@ -335,7 +335,7 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
reportLabel={t('Report')}
blockLabel={t('Block User')}
closeLabel={t('Close')}
user={ethAddress ? ethAddress : ''}
user={loginState.ethAddress ? loginState.ethAddress : ''}
contentId={flagged}
contentType="post"
baseUrl={constants.BASE_FLAG_URL}
Expand Down Expand Up @@ -394,14 +394,14 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
shareLabel={t('Share')}
copyLinkLabel={t('Copy Link')}
flagAsLabel={t('Report Post')}
loggedProfileEthAddress={ethAddress}
loggedProfileEthAddress={loginState.ethAddress}
locale={locale}
bookmarkLabel={t('Save')}
bookmarkedLabel={t('Saved')}
onRepost={() => {
return;
}}
onEntryFlag={handleEntryFlag(entryData.entryId, ethAddress)}
onEntryFlag={handleEntryFlag(entryData.entryId, loginState.ethAddress)}
handleFollowAuthor={handleFollow}
handleUnfollowAuthor={handleUnfollow}
isFollowingAuthor={isFollowing}
Expand All @@ -422,16 +422,16 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
)}
</ErrorInfoCard>
</Box>
{!ethAddress && (
{!loginState.ethAddress && (
<Box margin="medium">
<EditorPlaceholder onClick={showLoginModal} ethAddress={null} />
</Box>
)}
{ethAddress && (
{loginState.ethAddress && (
<Box margin="medium">
<CommentEditor
avatar={loginProfile.avatar}
ethAddress={ethAddress}
ethAddress={loginState.ethAddress}
postLabel={t('Reply')}
placeholderLabel={t('Write something')}
onPublish={handlePublish}
Expand Down Expand Up @@ -473,7 +473,7 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
logger={logger}
globalChannel={globalChannel}
bookmarkState={bookmarkState}
ethAddress={ethAddress}
ethAddress={loginState.ethAddress}
locale={locale}
onBookmark={handleCommentBookmark}
onNavigate={handleNavigateToPost}
Expand All @@ -492,7 +492,7 @@ const PostPage: React.FC<IPostPage & RootComponentProps> = props => {
isMobile,
sdkModules,
feedItems: postsState.postIds,
loggedEthAddress: ethAddress,
loggedEthAddress: loginState.ethAddress,
pendingComments: postsState.pendingComments,
})}
/>
Expand Down

0 comments on commit f391e05

Please sign in to comment.