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,9 +79,7 @@ export class Communities extends Component<any, CommunitiesState> {
}
async componentDidMount() {
if (!this.state.isIsomorphic) {
await this.refetch();
}
await this.refetch();
}
get documentTitle(): string {
@ -91,7 +89,7 @@ export class Communities extends Component<any, CommunitiesState> {
}
renderListings() {
switch (this.state.listCommunitiesResponse.state) {
switch (this.state.listCommunitiesResponse?.state) {
case "loading":
return (
<h5>

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -332,14 +332,12 @@ export class Search extends Component<any, SearchState> {
}
async componentDidMount() {
if (!this.state.isIsomorphic) {
const promises = [this.fetchCommunities()];
if (this.state.searchText) {
promises.push(this.search());
}
await Promise.all(promises);
const promises = [this.fetchCommunities()];
if (this.state.searchText) {
promises.push(this.search());
}
await Promise.all(promises);
}
async fetchCommunities() {