import Router from 'next/router';
import { parse, stringify } from 'qs';
import { ofType } from 'redux-observable';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import type { Action } from '@reduxjs/toolkit';

import { loggedInEventAction } from '../slices';

export default (action$: Observable<Action>) =>
  action$.pipe(
    ofType(loggedInEventAction.type),
    tap(() => {
      const url = parse(window.location.search, { ignoreQueryPrefix: true });

      delete url.token;

      Router.push(`${window.location.pathname}?${stringify(url)}`);
    }),
  );
