add optional chaining, remove conditional to fetch on component mount

This commit is contained in:
Alec Armbruster 2023-06-23 14:12:29 -04:00
parent d0a40f6690
commit 60e8486d5b
No known key found for this signature in database
GPG key ID: 52BC7C84E960FD1B
14 changed files with 51 additions and 78 deletions

View file

@ -79,10 +79,8 @@ export class Communities extends Component<any, CommunitiesState> {
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic) {
await this.refetch(); await this.refetch();
} }
}
get documentTitle(): string { get documentTitle(): string {
return `${I18NextService.i18n.t("communities")} - ${ return `${I18NextService.i18n.t("communities")} - ${
@ -91,7 +89,7 @@ export class Communities extends Component<any, CommunitiesState> {
} }
renderListings() { renderListings() {
switch (this.state.listCommunitiesResponse.state) { switch (this.state.listCommunitiesResponse?.state) {
case "loading": case "loading":
return ( return (
<h5> <h5>

View file

@ -221,9 +221,7 @@ export class Community extends Component<
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic) {
await Promise.all([this.fetchCommunity(), this.fetchData()]); await Promise.all([this.fetchCommunity(), this.fetchData()]);
}
setupTippy(); setupTippy();
} }

View file

@ -101,10 +101,8 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic) {
await this.fetchData(); await this.fetchData();
} }
}
get documentTitle(): string { get documentTitle(): string {
return `${I18NextService.i18n.t("admin_settings")} - ${ return `${I18NextService.i18n.t("admin_settings")} - ${
@ -114,7 +112,7 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
render() { render() {
const federationData = const federationData =
this.state.instancesRes.state === "success" this.state.instancesRes?.state === "success"
? this.state.instancesRes.data.federated_instances ? this.state.instancesRes.data.federated_instances
: undefined; : undefined;
@ -281,7 +279,7 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
} }
bannedUsers() { bannedUsers() {
switch (this.state.bannedRes.state) { switch (this.state.bannedRes?.state) {
case "loading": case "loading":
return ( return (
<h5> <h5>
@ -289,7 +287,7 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
</h5> </h5>
); );
case "success": { case "success": {
const bans = this.state.bannedRes.data.banned; const bans = this.state.bannedRes?.data.banned;
return ( return (
<> <>
<h5>{I18NextService.i18n.t("banned_users")}</h5> <h5>{I18NextService.i18n.t("banned_users")}</h5>

View file

@ -45,10 +45,8 @@ export class Instances extends Component<any, InstancesState> {
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic) {
await this.fetchInstances(); await this.fetchInstances();
} }
}
async fetchInstances() { async fetchInstances() {
this.setState({ this.setState({

View file

@ -837,7 +837,7 @@ export class Modlog extends Component<
} }
renderModlogTable() { renderModlogTable() {
switch (this.state.res.state) { switch (this.state.res?.state) {
case "loading": case "loading":
return ( return (
<h5> <h5>

View file

@ -177,10 +177,8 @@ export class Inbox extends Component<any, InboxState> {
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic) {
await this.refetch(); await this.refetch();
} }
}
get documentTitle(): string { get documentTitle(): string {
const mui = UserService.Instance.myUserInfo; const mui = UserService.Instance.myUserInfo;
@ -195,11 +193,11 @@ export class Inbox extends Component<any, InboxState> {
if (this.state.unreadOrAll == UnreadOrAll.Unread) { if (this.state.unreadOrAll == UnreadOrAll.Unread) {
const { repliesRes, mentionsRes, messagesRes } = this.state; const { repliesRes, mentionsRes, messagesRes } = this.state;
const replyCount = const replyCount =
repliesRes.state == "success" ? repliesRes.data.replies.length : 0; repliesRes?.state == "success" ? repliesRes.data.replies.length : 0;
const mentionCount = const mentionCount =
mentionsRes.state == "success" ? mentionsRes.data.mentions.length : 0; mentionsRes?.state == "success" ? mentionsRes.data.mentions.length : 0;
const messageCount = const messageCount =
messagesRes.state == "success" messagesRes?.state == "success"
? messagesRes.data.private_messages.length ? messagesRes.data.private_messages.length
: 0; : 0;

View file

@ -10,7 +10,6 @@ import {
setIsoData, setIsoData,
updatePersonBlock, updatePersonBlock,
} from "@utils/app"; } from "@utils/app";
import { restoreScrollPosition, saveScrollPosition } from "@utils/browser";
import { import {
capitalizeFirstLetter, capitalizeFirstLetter,
futureDaysToUnixTime, futureDaysToUnixTime,
@ -216,16 +215,10 @@ export class Profile extends Component<
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic) {
await this.fetchUserData(); await this.fetchUserData();
}
setupTippy(); setupTippy();
} }
componentWillUnmount() {
saveScrollPosition(this.context);
}
async fetchUserData() { async fetchUserData() {
const { page, sort, view } = getProfileQueryParams(); const { page, sort, view } = getProfileQueryParams();
@ -240,12 +233,12 @@ export class Profile extends Component<
auth: myAuth(), auth: myAuth(),
}), }),
}); });
restoreScrollPosition(this.context);
this.setPersonBlock(); this.setPersonBlock();
} }
get amCurrentUser() { get amCurrentUser() {
if (this.state.personRes.state === "success") { if (this.state.personRes?.state === "success") {
return ( return (
UserService.Instance.myUserInfo?.local_user_view.person.id === UserService.Instance.myUserInfo?.local_user_view.person.id ===
this.state.personRes.data.person_view.person.id this.state.personRes.data.person_view.person.id
@ -302,7 +295,7 @@ export class Profile extends Component<
} }
renderPersonRes() { renderPersonRes() {
switch (this.state.personRes.state) { switch (this.state.personRes?.state) {
case "loading": case "loading":
return ( return (
<h5> <h5>

View file

@ -68,9 +68,7 @@ export class RegistrationApplications extends Component<
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic) {
await this.refetch(); await this.refetch();
}
setupTippy(); setupTippy();
} }
@ -84,7 +82,7 @@ export class RegistrationApplications extends Component<
} }
renderApps() { renderApps() {
switch (this.state.appsRes.state) { switch (this.state.appsRes?.state) {
case "loading": case "loading":
return ( return (
<h5> <h5>
@ -92,7 +90,7 @@ export class RegistrationApplications extends Component<
</h5> </h5>
); );
case "success": { case "success": {
const apps = this.state.appsRes.data.registration_applications; const apps = this.state.appsRes?.data.registration_applications;
return ( return (
<div className="row"> <div className="row">
<div className="col-12"> <div className="col-12">

View file

@ -129,10 +129,8 @@ export class Reports extends Component<any, ReportsState> {
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic) {
await this.refetch(); await this.refetch();
} }
}
get documentTitle(): string { get documentTitle(): string {
const mui = UserService.Instance.myUserInfo; const mui = UserService.Instance.myUserInfo;
@ -329,6 +327,11 @@ export class Reports extends Component<any, ReportsState> {
get buildCombined(): ItemType[] { get buildCombined(): ItemType[] {
const commentRes = this.state.commentReportsRes; const commentRes = this.state.commentReportsRes;
if (!commentRes) {
return [];
}
const comments = const comments =
commentRes.state == "success" commentRes.state == "success"
? commentRes.data.comment_reports.map(this.commentReportToItemType) ? commentRes.data.comment_reports.map(this.commentReportToItemType)

View file

@ -228,8 +228,8 @@ export class Settings extends Component<any, SettingsState> {
} }
async componentDidMount() { async componentDidMount() {
setupTippy();
this.setState({ themeList: await fetchThemeList() }); this.setState({ themeList: await fetchThemeList() });
setupTippy();
} }
get documentTitle(): string { get documentTitle(): string {

View file

@ -103,7 +103,7 @@ export class CreatePost extends Component<
id: communityId, id: communityId,
auth, auth,
}); });
if (res.state === "success") { if (res?.state === "success") {
this.setState({ this.setState({
selectedCommunityChoice: { selectedCommunityChoice: {
label: res.data.community_view.community.title, label: res.data.community_view.community.title,
@ -116,8 +116,6 @@ export class CreatePost extends Component<
} }
async componentDidMount() { async componentDidMount() {
// TODO test this
if (!this.state.isIsomorphic) {
const { communityId } = getCreatePostQueryParams(); const { communityId } = getCreatePostQueryParams();
const initialCommunitiesRes = await fetchCommunitiesForOptions( const initialCommunitiesRes = await fetchCommunitiesForOptions(
@ -128,9 +126,7 @@ export class CreatePost extends Component<
initialCommunitiesRes, initialCommunitiesRes,
}); });
if ( if (communityId?.toString() !== this.state.selectedCommunityChoice?.value) {
communityId?.toString() !== this.state.selectedCommunityChoice?.value
) {
await this.fetchCommunity(); await this.fetchCommunity();
} else if (!communityId) { } else if (!communityId) {
this.setState({ this.setState({
@ -139,7 +135,6 @@ export class CreatePost extends Component<
}); });
} }
} }
}
get documentTitle(): string { get documentTitle(): string {
return `${I18NextService.i18n.t("create_post")} - ${ return `${I18NextService.i18n.t("create_post")} - ${
@ -181,8 +176,8 @@ export class CreatePost extends Component<
selectedCommunityChoice={selectedCommunityChoice} selectedCommunityChoice={selectedCommunityChoice}
onSelectCommunity={this.handleSelectedCommunityChange} onSelectCommunity={this.handleSelectedCommunityChange}
initialCommunities={ initialCommunities={
this.state.initialCommunitiesRes.state === "success" this.state.initialCommunitiesRes?.state === "success"
? this.state.initialCommunitiesRes.data.communities ? this.state.initialCommunitiesRes?.data.communities
: [] : []
} }
/> />
@ -224,7 +219,7 @@ export class CreatePost extends Component<
async handlePostCreate(form: CreatePostI) { async handlePostCreate(form: CreatePostI) {
const res = await HttpService.client.createPost(form); const res = await HttpService.client.createPost(form);
if (res.state === "success") { if (res?.state === "success") {
const postId = res.data.post_view.post.id; const postId = res.data.post_view.post.id;
this.props.history.replace(`/post/${postId}`); this.props.history.replace(`/post/${postId}`);
} else { } else {

View file

@ -270,9 +270,7 @@ export class Post extends Component<any, PostState> {
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic) {
await this.fetchPost(); await this.fetchPost();
}
autosize(document.querySelectorAll("textarea")); autosize(document.querySelectorAll("textarea"));

View file

@ -54,10 +54,8 @@ export class CreatePrivateMessage extends Component<
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic) {
await this.fetchPersonDetails(); await this.fetchPersonDetails();
} }
}
static async fetchInitialData({ static async fetchInitialData({
client, client,

View file

@ -332,7 +332,6 @@ export class Search extends Component<any, SearchState> {
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic) {
const promises = [this.fetchCommunities()]; const promises = [this.fetchCommunities()];
if (this.state.searchText) { if (this.state.searchText) {
promises.push(this.search()); promises.push(this.search());
@ -340,7 +339,6 @@ export class Search extends Component<any, SearchState> {
await Promise.all(promises); await Promise.all(promises);
} }
}
async fetchCommunities() { async fetchCommunities() {
this.setState({ communitiesRes: { state: "loading" } }); this.setState({ communitiesRes: { state: "loading" } });