Merge pull request #295 from Discours/fix/logout

subscriptions after logout fix
This commit is contained in:
Kosta 2023-11-02 19:46:33 +02:00 committed by GitHub
commit b03b59002a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 8 deletions

View File

@ -33,7 +33,7 @@ export const AllAuthorsView = (props: AllAuthorsViewProps) => {
const [searchQuery, setSearchQuery] = createSignal('')
onMount(() => {
createEffect(() => {
if (!searchParams().by) {
changeSearchParam({
by: 'shouts'

View File

@ -36,7 +36,7 @@ export const AllTopicsView = (props: AllTopicsViewProps) => {
const { subscriptions } = useSession()
onMount(() => {
createEffect(() => {
if (!searchParams().by) {
changeSearchParam({
by: 'shouts'

View File

@ -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<MySubscriptionsQueryResult>({
topics: [],
authors: []
})
const [subscriptions, setSubscriptions] = createSignal<MySubscriptionsQueryResult>(EMPTY_SUBSCRIPTIONS)
const { t } = useLocalize()
const {
actions: { showSnackbar }
@ -74,7 +76,11 @@ export const SessionProvider = (props: { children: JSX.Element }) => {
const loadSubscriptions = async (): Promise<void> => {
const result = await apiClient.getMySubscriptions()
setSubscriptions(result)
if (result) {
setSubscriptions(result)
} else {
setSubscriptions(EMPTY_SUBSCRIPTIONS)
}
}
const [session, { refetch: loadSession, mutate }] = createResource<AuthResult>(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") })
}