diff --git a/src/components/Views/AllAuthors.tsx b/src/components/Views/AllAuthors.tsx index 8802c52e..e7e65014 100644 --- a/src/components/Views/AllAuthors.tsx +++ b/src/components/Views/AllAuthors.tsx @@ -33,7 +33,7 @@ export const AllAuthorsView = (props: AllAuthorsViewProps) => { const [searchQuery, setSearchQuery] = createSignal('') - onMount(() => { + createEffect(() => { if (!searchParams().by) { changeSearchParam({ by: 'shouts' diff --git a/src/components/Views/AllTopics.tsx b/src/components/Views/AllTopics.tsx index b3a37296..946cce58 100644 --- a/src/components/Views/AllTopics.tsx +++ b/src/components/Views/AllTopics.tsx @@ -36,7 +36,7 @@ export const AllTopicsView = (props: AllTopicsViewProps) => { const { subscriptions } = useSession() - onMount(() => { + createEffect(() => { if (!searchParams().by) { changeSearchParam({ by: 'shouts' diff --git a/src/context/session.tsx b/src/context/session.tsx index 2b0d3b90..041ba7fe 100644 --- a/src/context/session.tsx +++ b/src/context/session.tsx @@ -41,12 +41,14 @@ export function useSession() { return useContext(SessionContext) } +const EMPTY_SUBSCRIPTIONS = { + topics: [], + authors: [] +} + export const SessionProvider = (props: { children: JSX.Element }) => { const [isSessionLoaded, setIsSessionLoaded] = createSignal(false) - const [subscriptions, setSubscriptions] = createSignal({ - topics: [], - authors: [] - }) + const [subscriptions, setSubscriptions] = createSignal(EMPTY_SUBSCRIPTIONS) const { t } = useLocalize() const { actions: { showSnackbar } @@ -74,7 +76,11 @@ export const SessionProvider = (props: { children: JSX.Element }) => { const loadSubscriptions = async (): Promise => { const result = await apiClient.getMySubscriptions() - setSubscriptions(result) + if (result) { + setSubscriptions(result) + } else { + setSubscriptions(EMPTY_SUBSCRIPTIONS) + } } const [session, { refetch: loadSession, mutate }] = createResource(getSession, { @@ -96,9 +102,11 @@ export const SessionProvider = (props: { children: JSX.Element }) => { const [isAuthWithCallback, setIsAuthWithCallback] = createSignal(null) - const requireAuthentication = (callback: () => void, modalSource: AuthModalSource) => { + const requireAuthentication = async (callback: () => void, modalSource: AuthModalSource) => { setIsAuthWithCallback(() => callback) + await loadSession() + if (!isAuthenticated()) { showModal('auth', modalSource) } @@ -120,6 +128,7 @@ export const SessionProvider = (props: { children: JSX.Element }) => { // TODO: call backend to revoke token mutate(null) resetToken() + setSubscriptions(EMPTY_SUBSCRIPTIONS) showSnackbar({ body: t("You've successfully logged out") }) }