Make login experience smoother

This commit is contained in:
abias 2023-06-10 17:51:11 -04:00
parent 93562425a6
commit 8994427c42
6 changed files with 36 additions and 25 deletions

View file

@ -121,13 +121,10 @@ function getDataTypeFromQuery(type?: string): DataType {
} }
function getListingTypeFromQuery(type?: string): ListingType { function getListingTypeFromQuery(type?: string): ListingType {
console.log(`Listing type: ${type}`);
const myListingType = const myListingType =
UserService.Instance.myUserInfo?.local_user_view?.local_user UserService.Instance.myUserInfo?.local_user_view?.local_user
?.default_listing_type; ?.default_listing_type;
console.log(`my listing type: ${myListingType}`);
return (type ? (type as ListingType) : myListingType) ?? "Local"; return (type ? (type as ListingType) : myListingType) ?? "Local";
} }

View file

@ -3,7 +3,7 @@ import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { UserService } from "../../services"; import { UserService } from "../../services";
import { HttpService, RequestState } from "../../services/HttpService"; import { HttpService, RequestState } from "../../services/HttpService";
import { isBrowser, setIsoData, toast, validEmail } from "../../utils"; import { isBrowser, myAuth, setIsoData, toast, validEmail } from "../../utils";
import { HtmlTags } from "../common/html-tags"; import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon"; import { Spinner } from "../common/icon";
@ -178,8 +178,16 @@ export class Login extends Component<any, State> {
case "success": { case "success": {
UserService.Instance.login(loginRes.data); UserService.Instance.login(loginRes.data);
i.props.history.push("/"); const site = await HttpService.client.getSite({
location.reload(); auth: myAuth(),
});
if (site.state === "success") {
UserService.Instance.myUserInfo = site.data.my_user;
}
i.props.history.replace("/");
break; break;
} }
} }

View file

@ -16,6 +16,7 @@ import {
isBrowser, isBrowser,
joinLemmyUrl, joinLemmyUrl,
mdToHtml, mdToHtml,
myAuth,
setIsoData, setIsoData,
toast, toast,
validEmail, validEmail,
@ -471,8 +472,14 @@ export class Signup extends Component<any, State> {
// Only log them in if a jwt was set // Only log them in if a jwt was set
if (data.jwt) { if (data.jwt) {
UserService.Instance.login(data); UserService.Instance.login(data);
i.props.history.push("/communities");
location.reload(); const site = await HttpService.client.getSite({ auth: myAuth() });
if (site.state === "success") {
UserService.Instance.myUserInfo = site.data.my_user;
}
i.props.history.replace("/communities");
} else { } else {
if (data.verify_email_sent) { if (data.verify_email_sent) {
toast(i18n.t("verify_email_sent")); toast(i18n.t("verify_email_sent"));

View file

@ -3,7 +3,7 @@ import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { HttpService, UserService } from "../../services"; import { HttpService, UserService } from "../../services";
import { RequestState } from "../../services/HttpService"; import { RequestState } from "../../services/HttpService";
import { capitalizeFirstLetter, setIsoData } from "../../utils"; import { capitalizeFirstLetter, myAuth, setIsoData } from "../../utils";
import { HtmlTags } from "../common/html-tags"; import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon"; import { Spinner } from "../common/icon";
@ -131,11 +131,16 @@ export class PasswordChange extends Component<any, State> {
}), }),
}); });
if (i.state.passwordChangeRes.state == "success") { if (i.state.passwordChangeRes.state === "success") {
const data = i.state.passwordChangeRes.data; const data = i.state.passwordChangeRes.data;
UserService.Instance.login(data); UserService.Instance.login(data);
this.props.history.push("/");
location.reload(); const site = await HttpService.client.getSite({ auth: myAuth() });
if (site.state === "success") {
UserService.Instance.myUserInfo = site.data.my_user;
}
this.props.history.replace("/");
} }
} }
} }

View file

@ -273,16 +273,10 @@ export class Post extends Component<any, PostState> {
document.addEventListener("scroll", this.commentScrollDebounced); document.addEventListener("scroll", this.commentScrollDebounced);
} }
componentDidUpdate(_lastProps: any) { async componentDidUpdate(_lastProps: any) {
// Necessary if you are on a post and you click another post (same route) // Necessary if you are on a post and you click another post (same route)
if (_lastProps.location.pathname !== _lastProps.history.location.pathname) { if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
// TODO Couldnt get a refresh working. This does for now. await this.fetchPost();
location.reload();
// const currentId = this.props.match.params.id;
// WebSocketService.Instance.getPost(currentId);
// this.context.refresh();
// this.context.router.history.push(_lastProps.location.pathname);
} }
} }

View file

@ -18,12 +18,12 @@ interface JwtInfo {
} }
export class UserService { export class UserService {
private static _instance: UserService; static #instance: UserService;
public myUserInfo?: MyUserInfo; public myUserInfo?: MyUserInfo;
public jwtInfo?: JwtInfo; public jwtInfo?: JwtInfo;
private constructor() { private constructor() {
this.setJwtInfo(); this.#setJwtInfo();
} }
public login(res: LoginResponse) { public login(res: LoginResponse) {
@ -32,7 +32,7 @@ export class UserService {
if (res.jwt) { if (res.jwt) {
toast(i18n.t("logged_in")); toast(i18n.t("logged_in"));
IsomorphicCookie.save("jwt", res.jwt, { expires, secure: isHttps() }); IsomorphicCookie.save("jwt", res.jwt, { expires, secure: isHttps() });
this.setJwtInfo(); this.#setJwtInfo();
} }
} }
@ -63,7 +63,7 @@ export class UserService {
} }
} }
private setJwtInfo() { #setJwtInfo() {
const jwt: string | undefined = IsomorphicCookie.load("jwt"); const jwt: string | undefined = IsomorphicCookie.load("jwt");
if (jwt) { if (jwt) {
@ -72,6 +72,6 @@ export class UserService {
} }
public static get Instance() { public static get Instance() {
return this._instance || (this._instance = new this()); return this.#instance || (this.#instance = new this());
} }
} }