This commit is contained in:
abias 2023-06-15 22:39:04 -04:00
parent 88842a52c0
commit 2a16c85ed0
10 changed files with 114 additions and 156 deletions

View file

@ -102,9 +102,9 @@ import { PostListings } from "../post/post-listings";
import { CommunityLink } from "./community-link"; import { CommunityLink } from "./community-link";
type CommunityData = RouteDataResponse<{ type CommunityData = RouteDataResponse<{
communityResponse: GetCommunityResponse; communityRes: GetCommunityResponse;
postsResponse?: GetPostsResponse; postsRes: GetPostsResponse;
commentsResponse?: GetCommentsResponse; commentsRes: GetCommentsResponse;
}>; }>;
interface State { interface State {
@ -201,37 +201,15 @@ export class Community extends Component<
// Only fetch the data if coming from another route // Only fetch the data if coming from another route
if (FirstLoadService.isFirstLoad) { if (FirstLoadService.isFirstLoad) {
const { const { communityRes, commentsRes, postsRes } = this.isoData.routeData;
communityResponse: communityRes,
commentsResponse: commentsRes,
postsResponse: postsRes,
} = this.isoData.routeData;
this.state = { this.state = {
...this.state, ...this.state,
isIsomorphic: true, isIsomorphic: true,
commentsRes,
communityRes,
postsRes,
}; };
if (communityRes.state === "success") {
this.state = {
...this.state,
communityRes,
};
}
if (postsRes?.state === "success") {
this.state = {
...this.state,
postsRes,
};
}
if (commentsRes?.state === "success") {
this.state = {
...this.state,
commentsRes,
};
}
} }
} }
@ -279,9 +257,10 @@ export class Community extends Component<
const page = getPageFromString(urlPage); const page = getPageFromString(urlPage);
let postsResponse: RequestState<GetPostsResponse> | undefined = undefined; let postsResponse: RequestState<GetPostsResponse> = { state: "empty" };
let commentsResponse: RequestState<GetCommentsResponse> | undefined = let commentsResponse: RequestState<GetCommentsResponse> = {
undefined; state: "empty",
};
if (dataType === DataType.Post) { if (dataType === DataType.Post) {
const getPostsForm: GetPosts = { const getPostsForm: GetPosts = {
@ -310,9 +289,9 @@ export class Community extends Component<
} }
return { return {
communityResponse: await client.getCommunity(communityForm), communityRes: await client.getCommunity(communityForm),
commentsResponse, commentsRes: commentsResponse,
postsResponse, postsRes: postsResponse,
}; };
} }

View file

@ -34,8 +34,8 @@ import { SiteForm } from "./site-form";
import { TaglineForm } from "./tagline-form"; import { TaglineForm } from "./tagline-form";
type AdminSettingsData = RouteDataResponse<{ type AdminSettingsData = RouteDataResponse<{
bannedPersonsResponse: BannedPersonsResponse; bannedRes: BannedPersonsResponse;
federatedInstancesResponse: GetFederatedInstancesResponse; instancesRes: GetFederatedInstancesResponse;
}>; }>;
interface AdminSettingsState { interface AdminSettingsState {
@ -72,10 +72,7 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
// Only fetch the data if coming from another route // Only fetch the data if coming from another route
if (FirstLoadService.isFirstLoad) { if (FirstLoadService.isFirstLoad) {
const { const { bannedRes, instancesRes } = this.isoData.routeData;
bannedPersonsResponse: bannedRes,
federatedInstancesResponse: instancesRes,
} = this.isoData.routeData;
this.state = { this.state = {
...this.state, ...this.state,
@ -91,10 +88,10 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
client, client,
}: InitialFetchRequest): Promise<AdminSettingsData> { }: InitialFetchRequest): Promise<AdminSettingsData> {
return { return {
bannedPersonsResponse: await client.getBannedPersons({ bannedRes: await client.getBannedPersons({
auth: auth as string, auth: auth as string,
}), }),
federatedInstancesResponse: await client.getFederatedInstances({ instancesRes: await client.getFederatedInstances({
auth: auth as string, auth: auth as string,
}), }),
}; };

View file

@ -119,9 +119,9 @@ interface HomeProps {
} }
type HomeData = RouteDataResponse<{ type HomeData = RouteDataResponse<{
postsResponse?: GetPostsResponse; postsRes: GetPostsResponse;
commentsResponse?: GetCommentsResponse; commentsRes: GetCommentsResponse;
trendingResponse: ListCommunitiesResponse; trendingCommunitiesRes: ListCommunitiesResponse;
}>; }>;
function getDataTypeFromQuery(type?: string): DataType { function getDataTypeFromQuery(type?: string): DataType {
@ -236,37 +236,30 @@ export class Home extends Component<any, HomeState> {
// Only fetch the data if coming from another route // Only fetch the data if coming from another route
if (FirstLoadService.isFirstLoad) { if (FirstLoadService.isFirstLoad) {
const { const {
trendingResponse: trendingCommunitiesRes, trendingCommunitiesRes: trendingCommunitiesRes,
commentsResponse: commentsRes, commentsRes: commentsRes,
postsResponse: postsRes, postsRes: postsRes,
} = this.isoData.routeData; } = this.isoData.routeData;
this.state = { this.state = {
...this.state, ...this.state,
trendingCommunitiesRes, trendingCommunitiesRes,
commentsRes,
postsRes,
tagline: getRandomFromList(this.state?.siteRes?.taglines ?? []) tagline: getRandomFromList(this.state?.siteRes?.taglines ?? [])
?.content, ?.content,
isIsomorphic: true, isIsomorphic: true,
}; };
if (commentsRes?.state === "success") {
this.state = {
...this.state,
commentsRes,
};
}
if (postsRes?.state === "success") {
this.state = {
...this.state,
postsRes,
};
}
} }
} }
async componentDidMount() { async componentDidMount() {
if (!this.state.isIsomorphic || !this.isoData.routeData.length) { if (
!this.state.isIsomorphic ||
!Object.values(this.isoData.routeData).some(
res => res.state === "success" || res.state === "failed"
)
) {
await Promise.all([this.fetchTrendingCommunities(), this.fetchData()]); await Promise.all([this.fetchTrendingCommunities(), this.fetchData()]);
} }
@ -290,9 +283,10 @@ export class Home extends Component<any, HomeState> {
const page = urlPage ? Number(urlPage) : 1; const page = urlPage ? Number(urlPage) : 1;
let postsResponse: RequestState<GetPostsResponse> | undefined = undefined; let postsRes: RequestState<GetPostsResponse> = { state: "empty" };
let commentsResponse: RequestState<GetCommentsResponse> | undefined = let commentsRes: RequestState<GetCommentsResponse> = {
undefined; state: "empty",
};
if (dataType === DataType.Post) { if (dataType === DataType.Post) {
const getPostsForm: GetPosts = { const getPostsForm: GetPosts = {
@ -304,7 +298,7 @@ export class Home extends Component<any, HomeState> {
auth, auth,
}; };
postsResponse = await client.getPosts(getPostsForm); postsRes = await client.getPosts(getPostsForm);
} else { } else {
const getCommentsForm: GetComments = { const getCommentsForm: GetComments = {
page, page,
@ -315,7 +309,7 @@ export class Home extends Component<any, HomeState> {
auth, auth,
}; };
commentsResponse = await client.getComments(getCommentsForm); commentsRes = await client.getComments(getCommentsForm);
} }
const trendingCommunitiesForm: ListCommunities = { const trendingCommunitiesForm: ListCommunities = {
@ -326,9 +320,11 @@ export class Home extends Component<any, HomeState> {
}; };
return { return {
trendingResponse: await client.listCommunities(trendingCommunitiesForm), trendingCommunitiesRes: await client.listCommunities(
commentsResponse, trendingCommunitiesForm
postsResponse, ),
commentsRes,
postsRes,
}; };
} }

View file

@ -59,11 +59,11 @@ export class Instances extends Component<any, InstancesState> {
}); });
} }
static async fetchInitialData( static async fetchInitialData({
req: InitialFetchRequest client,
): Promise<InstancesData> { }: InitialFetchRequest): Promise<InstancesData> {
return { return {
federatedInstancesResponse: await req.client.getFederatedInstances({}), federatedInstancesResponse: await client.getFederatedInstances({}),
}; };
} }

View file

@ -77,10 +77,10 @@ type View =
| AdminPurgeCommentView; | AdminPurgeCommentView;
type ModlogData = RouteDataResponse<{ type ModlogData = RouteDataResponse<{
modlogResponse: GetModlogResponse; res: GetModlogResponse;
communityResponse?: GetCommunityResponse; communityRes: GetCommunityResponse;
modUserResponse?: GetPersonDetailsResponse; modUserResponse: GetPersonDetailsResponse;
userResponse?: GetPersonDetailsResponse; userResponse: GetPersonDetailsResponse;
}>; }>;
interface ModlogType { interface ModlogType {
@ -662,33 +662,23 @@ export class Modlog extends Component<
// Only fetch the data if coming from another route // Only fetch the data if coming from another route
if (FirstLoadService.isFirstLoad) { if (FirstLoadService.isFirstLoad) {
const { const { res, communityRes, modUserResponse, userResponse } =
modlogResponse: res, this.isoData.routeData;
communityResponse: communityRes,
modUserResponse,
userResponse,
} = this.isoData.routeData;
this.state = { this.state = {
...this.state, ...this.state,
res, res,
communityRes,
}; };
if (communityRes?.state === "success") { if (modUserResponse.state === "success") {
this.state = {
...this.state,
communityRes,
};
}
if (modUserResponse?.state === "success") {
this.state = { this.state = {
...this.state, ...this.state,
modSearchOptions: [personToChoice(modUserResponse.data.person_view)], modSearchOptions: [personToChoice(modUserResponse.data.person_view)],
}; };
} }
if (userResponse?.state === "success") { if (userResponse.state === "success") {
this.state = { this.state = {
...this.state, ...this.state,
userSearchOptions: [personToChoice(userResponse.data.person_view)], userSearchOptions: [personToChoice(userResponse.data.person_view)],
@ -1002,8 +992,9 @@ export class Modlog extends Component<
auth, auth,
}; };
let communityResponse: RequestState<GetCommunityResponse> | undefined = let communityResponse: RequestState<GetCommunityResponse> = {
undefined; state: "empty",
};
if (communityId) { if (communityId) {
const communityForm: GetCommunity = { const communityForm: GetCommunity = {
@ -1014,8 +1005,9 @@ export class Modlog extends Component<
communityResponse = await client.getCommunity(communityForm); communityResponse = await client.getCommunity(communityForm);
} }
let modUserResponse: RequestState<GetPersonDetailsResponse> | undefined = let modUserResponse: RequestState<GetPersonDetailsResponse> = {
undefined; state: "empty",
};
if (modId) { if (modId) {
const getPersonForm: GetPersonDetails = { const getPersonForm: GetPersonDetails = {
@ -1026,8 +1018,9 @@ export class Modlog extends Component<
modUserResponse = await client.getPersonDetails(getPersonForm); modUserResponse = await client.getPersonDetails(getPersonForm);
} }
let userResponse: RequestState<GetPersonDetailsResponse> | undefined = let userResponse: RequestState<GetPersonDetailsResponse> = {
undefined; state: "empty",
};
if (userId) { if (userId) {
const getPersonForm: GetPersonDetails = { const getPersonForm: GetPersonDetails = {
@ -1039,8 +1032,8 @@ export class Modlog extends Component<
} }
return { return {
modlogResponse: await client.getModlog(modlogForm), res: await client.getModlog(modlogForm),
communityResponse, communityRes: communityResponse,
modUserResponse, modUserResponse,
userResponse, userResponse,
}; };

View file

@ -92,9 +92,9 @@ enum ReplyEnum {
} }
type InboxData = RouteDataResponse<{ type InboxData = RouteDataResponse<{
repliesResponse: GetRepliesResponse; repliesRes: GetRepliesResponse;
personMentionsResponse: GetPersonMentionsResponse; mentionsRes: GetPersonMentionsResponse;
privateMessagesResponse: PrivateMessagesResponse; messagesRes: PrivateMessagesResponse;
}>; }>;
type ReplyType = { type ReplyType = {
@ -167,11 +167,7 @@ export class Inbox extends Component<any, InboxState> {
// Only fetch the data if coming from another route // Only fetch the data if coming from another route
if (FirstLoadService.isFirstLoad) { if (FirstLoadService.isFirstLoad) {
const { const { mentionsRes, messagesRes, repliesRes } = this.isoData.routeData;
personMentionsResponse: mentionsRes,
privateMessagesResponse: messagesRes,
repliesResponse: repliesRes,
} = this.isoData.routeData;
this.state = { this.state = {
...this.state, ...this.state,
@ -702,7 +698,7 @@ export class Inbox extends Component<any, InboxState> {
const sort: CommentSortType = "New"; const sort: CommentSortType = "New";
return { return {
personMentionsResponse: auth mentionsRes: auth
? await client.getPersonMentions({ ? await client.getPersonMentions({
sort, sort,
unread_only: true, unread_only: true,
@ -711,7 +707,7 @@ export class Inbox extends Component<any, InboxState> {
auth, auth,
}) })
: { state: "empty" }, : { state: "empty" },
privateMessagesResponse: auth messagesRes: auth
? await client.getPrivateMessages({ ? await client.getPrivateMessages({
unread_only: true, unread_only: true,
page: 1, page: 1,
@ -719,7 +715,7 @@ export class Inbox extends Component<any, InboxState> {
auth, auth,
}) })
: { state: "empty" }, : { state: "empty" },
repliesResponse: auth repliesRes: auth
? await client.getReplies({ ? await client.getReplies({
sort, sort,
unread_only: true, unread_only: true,

View file

@ -58,9 +58,9 @@ enum MessageEnum {
} }
type ReportsData = RouteDataResponse<{ type ReportsData = RouteDataResponse<{
commentReportsResponse: ListCommentReportsResponse; commentReportsRes: ListCommentReportsResponse;
postReportsResponse: ListPostReportsResponse; postReportsRes: ListPostReportsResponse;
privateMessageReportsResponse?: ListPrivateMessageReportsResponse; messageReportsRes: ListPrivateMessageReportsResponse;
}>; }>;
type ItemType = { type ItemType = {
@ -106,11 +106,8 @@ export class Reports extends Component<any, ReportsState> {
// Only fetch the data if coming from another route // Only fetch the data if coming from another route
if (FirstLoadService.isFirstLoad) { if (FirstLoadService.isFirstLoad) {
const { const { commentReportsRes, postReportsRes, messageReportsRes } =
commentReportsResponse: commentReportsRes, this.isoData.routeData;
postReportsResponse: postReportsRes,
privateMessageReportsResponse: messageReportsRes,
} = this.isoData.routeData;
this.state = { this.state = {
...this.state, ...this.state,
@ -122,7 +119,7 @@ export class Reports extends Component<any, ReportsState> {
if (amAdmin()) { if (amAdmin()) {
this.state = { this.state = {
...this.state, ...this.state,
messageReportsRes: messageReportsRes ?? { state: "empty" }, messageReportsRes: messageReportsRes,
}; };
} }
} }
@ -515,10 +512,9 @@ export class Reports extends Component<any, ReportsState> {
}; };
const data: ReportsData = { const data: ReportsData = {
commentReportsResponse: await client.listCommentReports( commentReportsRes: await client.listCommentReports(commentReportsForm),
commentReportsForm postReportsRes: await client.listPostReports(postReportsForm),
), messageReportsRes: { state: "empty" },
postReportsResponse: await client.listPostReports(postReportsForm),
}; };
if (amAdmin()) { if (amAdmin()) {
@ -529,8 +525,9 @@ export class Reports extends Component<any, ReportsState> {
auth: auth as string, auth: auth as string,
}; };
data.privateMessageReportsResponse = data.messageReportsRes = await client.listPrivateMessageReports(
await client.listPrivateMessageReports(privateMessageReportsForm); privateMessageReportsForm
);
} }
return data; return data;

View file

@ -81,6 +81,13 @@ export class CreatePost extends Component<
const { communityResponse: communityRes, initialCommunitiesRes } = const { communityResponse: communityRes, initialCommunitiesRes } =
this.isoData.routeData; this.isoData.routeData;
this.state = {
...this.state,
loading: false,
initialCommunitiesRes,
isIsomorphic: true,
};
if (communityRes?.state === "success") { if (communityRes?.state === "success") {
const communityChoice: Choice = { const communityChoice: Choice = {
label: communityRes.data.community_view.community.title, label: communityRes.data.community_view.community.title,
@ -92,13 +99,6 @@ export class CreatePost extends Component<
selectedCommunityChoice: communityChoice, selectedCommunityChoice: communityChoice,
}; };
} }
this.state = {
...this.state,
loading: false,
initialCommunitiesRes,
isIsomorphic: true,
};
} }
} }

View file

@ -72,11 +72,11 @@ interface SearchProps {
} }
type SearchData = RouteDataResponse<{ type SearchData = RouteDataResponse<{
communityResponse?: GetCommunityResponse; communityResponse: GetCommunityResponse;
listCommunitiesResponse?: ListCommunitiesResponse; listCommunitiesResponse: ListCommunitiesResponse;
creatorDetailsResponse?: GetPersonDetailsResponse; creatorDetailsResponse: GetPersonDetailsResponse;
searchResponse?: SearchResponse; searchResponse: SearchResponse;
resolveObjectResponse?: ResolveObjectResponse; resolveObjectResponse: ResolveObjectResponse;
}>; }>;
type FilterType = "creator" | "community"; type FilterType = "creator" | "community";
@ -365,11 +365,12 @@ export class Search extends Component<any, SearchState> {
query: { communityId, creatorId, q, type, sort, listingType, page }, query: { communityId, creatorId, q, type, sort, listingType, page },
}: InitialFetchRequest<QueryParams<SearchProps>>): Promise<SearchData> { }: InitialFetchRequest<QueryParams<SearchProps>>): Promise<SearchData> {
const community_id = getIdFromString(communityId); const community_id = getIdFromString(communityId);
let communityResponse: RequestState<GetCommunityResponse> | undefined = let communityResponse: RequestState<GetCommunityResponse> = {
undefined; state: "empty",
let listCommunitiesResponse: };
| RequestState<ListCommunitiesResponse> let listCommunitiesResponse: RequestState<ListCommunitiesResponse> = {
| undefined = undefined; state: "empty",
};
if (community_id) { if (community_id) {
const getCommunityForm: GetCommunity = { const getCommunityForm: GetCommunity = {
id: community_id, id: community_id,
@ -391,9 +392,9 @@ export class Search extends Component<any, SearchState> {
} }
const creator_id = getIdFromString(creatorId); const creator_id = getIdFromString(creatorId);
let creatorDetailsResponse: let creatorDetailsResponse: RequestState<GetPersonDetailsResponse> = {
| RequestState<GetPersonDetailsResponse> state: "empty",
| undefined = undefined; };
if (creator_id) { if (creator_id) {
const getCreatorForm: GetPersonDetails = { const getCreatorForm: GetPersonDetails = {
person_id: creator_id, person_id: creator_id,
@ -405,9 +406,10 @@ export class Search extends Component<any, SearchState> {
const query = getSearchQueryFromQuery(q); const query = getSearchQueryFromQuery(q);
let searchResponse: RequestState<SearchResponse> | undefined = undefined; let searchResponse: RequestState<SearchResponse> = { state: "empty" };
let resolveObjectResponse: RequestState<ResolveObjectResponse> | undefined = let resolveObjectResponse: RequestState<ResolveObjectResponse> = {
undefined; state: "empty",
};
if (query) { if (query) {
const form: SearchForm = { const form: SearchForm = {
@ -429,9 +431,7 @@ export class Search extends Component<any, SearchState> {
q: query, q: query,
auth, auth,
}; };
resolveObjectResponse = await client resolveObjectResponse = await client.resolveObject(resolveObjectForm);
.resolveObject(resolveObjectForm)
.catch(() => undefined);
} }
} }
} }

View file

@ -1499,7 +1499,7 @@ export function newVote(voteType: VoteType, myVote?: number): number {
} }
export type RouteDataResponse<T extends Record<string, any>> = { export type RouteDataResponse<T extends Record<string, any>> = {
[K in keyof T]: RequestState<Exclude<T[K], undefined>>; [K in keyof T]: RequestState<T[K]>;
}; };
function sleep(millis: number): Promise<void> { function sleep(millis: number): Promise<void> {