Wrap Http client

This commit is contained in:
SleeplessOne1917 2023-06-05 22:14:39 -04:00
parent c71878ef6b
commit 20ba25760f
28 changed files with 537 additions and 728 deletions

View file

@ -60,7 +60,7 @@
"inferno-server": "^8.1.1", "inferno-server": "^8.1.1",
"isomorphic-cookie": "^1.2.4", "isomorphic-cookie": "^1.2.4",
"jwt-decode": "^3.1.2", "jwt-decode": "^3.1.2",
"lemmy-js-client": "0.17.2-rc.20", "lemmy-js-client": "0.17.2-rc.23",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"markdown-it": "^13.0.1", "markdown-it": "^13.0.1",
"markdown-it-container": "^3.0.0", "markdown-it-container": "^3.0.0",

View file

@ -8,11 +8,7 @@ import {
} from "lemmy-js-client"; } from "lemmy-js-client";
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { UserService } from "../../services"; import { UserService } from "../../services";
import { import { HttpService, RequestState } from "../../services/HttpService";
HttpService,
RequestState,
apiWrapper,
} from "../../services/HttpService";
import { import {
amAdmin, amAdmin,
canCreateCommunity, canCreateCommunity,
@ -421,28 +417,29 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
if (auth) { if (auth) {
this.setState({ unreadInboxCountRes: { state: "loading" } }); this.setState({ unreadInboxCountRes: { state: "loading" } });
this.setState({ this.setState({
unreadInboxCountRes: await apiWrapper( unreadInboxCountRes: await HttpService.wrappedClient.getUnreadCount({
HttpService.client.getUnreadCount({ auth }) auth,
), }),
}); });
if (this.moderatesSomething) { if (this.moderatesSomething) {
this.setState({ unreadReportCountRes: { state: "loading" } }); this.setState({ unreadReportCountRes: { state: "loading" } });
this.setState({ this.setState({
unreadReportCountRes: await apiWrapper( unreadReportCountRes: await HttpService.wrappedClient.getReportCount({
HttpService.client.getReportCount({ auth }) auth,
), }),
}); });
} }
if (amAdmin()) { if (amAdmin()) {
this.setState({ unreadApplicationCountRes: { state: "loading" } }); this.setState({ unreadApplicationCountRes: { state: "loading" } });
this.setState({ this.setState({
unreadApplicationCountRes: await apiWrapper( unreadApplicationCountRes:
HttpService.client.getUnreadRegistrationApplicationCount({ await HttpService.wrappedClient.getUnreadRegistrationApplicationCount(
auth, {
}) auth,
), }
),
}); });
} }
} }

View file

@ -12,7 +12,6 @@ import { InitialFetchRequest } from "../../interfaces";
import { import {
HttpService, HttpService,
RequestState, RequestState,
apiWrapper,
apiWrapperIso, apiWrapperIso,
} from "../../services/HttpService"; } from "../../services/HttpService";
import { import {
@ -315,14 +314,11 @@ export class Communities extends Component<any, CommunitiesState> {
communityId: number; communityId: number;
follow: boolean; follow: boolean;
}) { }) {
const res = await apiWrapper( const res = await HttpService.wrappedClient.followCommunity({
HttpService.client.followCommunity({ community_id: data.communityId,
community_id: data.communityId, follow: data.follow,
follow: data.follow, auth: myAuthRequired(),
auth: myAuthRequired(), });
})
);
data.i.findAndUpdateCommunity(res); data.i.findAndUpdateCommunity(res);
} }
@ -332,15 +328,13 @@ export class Communities extends Component<any, CommunitiesState> {
const { listingType, page } = this.getCommunitiesQueryParams(); const { listingType, page } = this.getCommunitiesQueryParams();
this.setState({ this.setState({
listCommunitiesResponse: await apiWrapper( listCommunitiesResponse: await HttpService.wrappedClient.listCommunities({
HttpService.client.listCommunities({ type_: listingType,
type_: listingType, sort: "TopMonth",
sort: "TopMonth", limit: communityLimit,
limit: communityLimit, page,
page, auth: myAuth(),
auth: myAuth(), }),
})
),
}); });
window.scrollTo(0, 0); window.scrollTo(0, 0);

View file

@ -61,7 +61,6 @@ import { UserService } from "../../services";
import { import {
HttpService, HttpService,
RequestState, RequestState,
apiWrapper,
apiWrapperIso, apiWrapperIso,
} from "../../services/HttpService"; } from "../../services/HttpService";
import { import {
@ -224,12 +223,10 @@ export class Community extends Component<
async fetchCommunity() { async fetchCommunity() {
this.setState({ communityRes: { state: "loading" } }); this.setState({ communityRes: { state: "loading" } });
this.setState({ this.setState({
communityRes: await apiWrapper( communityRes: await HttpService.wrappedClient.getCommunity({
HttpService.client.getCommunity({ name: this.props.match.params.name,
name: this.props.match.params.name, auth: myAuth(),
auth: myAuth(), }),
})
),
}); });
} }
@ -585,32 +582,28 @@ export class Community extends Component<
if (dataType === DataType.Post) { if (dataType === DataType.Post) {
this.setState({ postsRes: { state: "loading" } }); this.setState({ postsRes: { state: "loading" } });
this.setState({ this.setState({
postsRes: await apiWrapper( postsRes: await HttpService.wrappedClient.getPosts({
HttpService.client.getPosts({ page,
page, limit: fetchLimit,
limit: fetchLimit, sort,
sort, type_: "All",
type_: "All", community_name: name,
community_name: name, saved_only: false,
saved_only: false, auth: myAuth(),
auth: myAuth(), }),
})
),
}); });
} else { } else {
this.setState({ commentsRes: { state: "loading" } }); this.setState({ commentsRes: { state: "loading" } });
this.setState({ this.setState({
commentsRes: await apiWrapper( commentsRes: await HttpService.wrappedClient.getComments({
HttpService.client.getComments({ page,
page, limit: fetchLimit,
limit: fetchLimit, sort: postToCommentSortType(sort),
sort: postToCommentSortType(sort), type_: "All",
type_: "All", community_name: name,
community_name: name, saved_only: false,
saved_only: false, auth: myAuth(),
auth: myAuth(), }),
})
),
}); });
} }
@ -619,23 +612,20 @@ export class Community extends Component<
} }
async handleDeleteCommunity(form: DeleteCommunity) { async handleDeleteCommunity(form: DeleteCommunity) {
const deleteCommunityRes = await apiWrapper( const deleteCommunityRes = await HttpService.wrappedClient.deleteCommunity(
HttpService.client.deleteCommunity(form) form
); );
this.updateCommunity(deleteCommunityRes); this.updateCommunity(deleteCommunityRes);
} }
async handleAddModToCommunity(form: AddModToCommunity) { async handleAddModToCommunity(form: AddModToCommunity) {
const addModRes = await apiWrapper( const addModRes = await HttpService.wrappedClient.addModToCommunity(form);
HttpService.client.addModToCommunity(form)
);
this.updateModerators(addModRes); this.updateModerators(addModRes);
} }
async handleFollow(form: FollowCommunity) { async handleFollow(form: FollowCommunity) {
const followCommunityRes = await apiWrapper( const followCommunityRes = await HttpService.wrappedClient.followCommunity(
HttpService.client.followCommunity(form) form
); );
this.updateCommunity(followCommunityRes); this.updateCommunity(followCommunityRes);
@ -650,68 +640,60 @@ export class Community extends Component<
} }
async handlePurgeCommunity(form: PurgeCommunity) { async handlePurgeCommunity(form: PurgeCommunity) {
const purgeCommunityRes = await apiWrapper( const purgeCommunityRes = await HttpService.wrappedClient.purgeCommunity(
HttpService.client.purgeCommunity(form) form
); );
this.purgeItem(purgeCommunityRes); this.purgeItem(purgeCommunityRes);
} }
async handlePurgePerson(form: PurgePerson) { async handlePurgePerson(form: PurgePerson) {
const purgePersonRes = await apiWrapper( const purgePersonRes = await HttpService.wrappedClient.purgePerson(form);
HttpService.client.purgePerson(form)
);
this.purgeItem(purgePersonRes); this.purgeItem(purgePersonRes);
} }
async handlePurgeComment(form: PurgeComment) { async handlePurgeComment(form: PurgeComment) {
const purgeCommentRes = await apiWrapper( const purgeCommentRes = await HttpService.wrappedClient.purgeComment(form);
HttpService.client.purgeComment(form)
);
this.purgeItem(purgeCommentRes); this.purgeItem(purgeCommentRes);
} }
async handlePurgePost(form: PurgePost) { async handlePurgePost(form: PurgePost) {
const purgeRes = await apiWrapper(HttpService.client.purgePost(form)); const purgeRes = await HttpService.wrappedClient.purgePost(form);
this.purgeItem(purgeRes); this.purgeItem(purgeRes);
} }
async handleBlockCommunity(form: BlockCommunity) { async handleBlockCommunity(form: BlockCommunity) {
const blockCommunityRes = await apiWrapper( const blockCommunityRes = await HttpService.wrappedClient.blockCommunity(
HttpService.client.blockCommunity(form) form
); );
if (blockCommunityRes.state == "success") { if (blockCommunityRes.state == "success") {
updateCommunityBlock(blockCommunityRes.data); updateCommunityBlock(blockCommunityRes.data);
} }
} }
async handleBlockPerson(form: BlockPerson) { async handleBlockPerson(form: BlockPerson) {
const blockPersonRes = await apiWrapper( const blockPersonRes = await HttpService.wrappedClient.blockPerson(form);
HttpService.client.blockPerson(form)
);
if (blockPersonRes.state == "success") { if (blockPersonRes.state == "success") {
updatePersonBlock(blockPersonRes.data); updatePersonBlock(blockPersonRes.data);
} }
} }
async handleRemoveCommunity(form: RemoveCommunity) { async handleRemoveCommunity(form: RemoveCommunity) {
const removeCommunityRes = await apiWrapper( const removeCommunityRes = await HttpService.wrappedClient.removeCommunity(
HttpService.client.removeCommunity(form) form
); );
this.updateCommunity(removeCommunityRes); this.updateCommunity(removeCommunityRes);
} }
async handleEditCommunity(form: EditCommunity) { async handleEditCommunity(form: EditCommunity) {
const res = await apiWrapper(HttpService.client.editCommunity(form)); const res = await HttpService.wrappedClient.editCommunity(form);
this.updateCommunity(res); this.updateCommunity(res);
return res; return res;
} }
async handleCreateComment(form: CreateComment) { async handleCreateComment(form: CreateComment) {
const createCommentRes = await apiWrapper( const createCommentRes = await HttpService.wrappedClient.createComment(
HttpService.client.createComment(form) form
); );
this.createAndUpdateComments(createCommentRes); this.createAndUpdateComments(createCommentRes);
@ -719,100 +701,89 @@ export class Community extends Component<
} }
async handleEditComment(form: EditComment) { async handleEditComment(form: EditComment) {
const editCommentRes = await apiWrapper( const editCommentRes = await HttpService.wrappedClient.editComment(form);
HttpService.client.editComment(form)
);
this.findAndUpdateComment(editCommentRes); this.findAndUpdateComment(editCommentRes);
return editCommentRes; return editCommentRes;
} }
async handleDeleteComment(form: DeleteComment) { async handleDeleteComment(form: DeleteComment) {
const deleteCommentRes = await apiWrapper( const deleteCommentRes = await HttpService.wrappedClient.deleteComment(
HttpService.client.deleteComment(form) form
); );
this.findAndUpdateComment(deleteCommentRes); this.findAndUpdateComment(deleteCommentRes);
} }
async handleDeletePost(form: DeletePost) { async handleDeletePost(form: DeletePost) {
const deleteRes = await apiWrapper(HttpService.client.deletePost(form)); const deleteRes = await HttpService.wrappedClient.deletePost(form);
this.findAndUpdatePost(deleteRes); this.findAndUpdatePost(deleteRes);
} }
async handleRemovePost(form: RemovePost) { async handleRemovePost(form: RemovePost) {
const removeRes = await apiWrapper(HttpService.client.removePost(form)); const removeRes = await HttpService.wrappedClient.removePost(form);
this.findAndUpdatePost(removeRes); this.findAndUpdatePost(removeRes);
} }
async handleRemoveComment(form: RemoveComment) { async handleRemoveComment(form: RemoveComment) {
const removeCommentRes = await apiWrapper( const removeCommentRes = await HttpService.wrappedClient.removeComment(
HttpService.client.removeComment(form) form
); );
this.findAndUpdateComment(removeCommentRes); this.findAndUpdateComment(removeCommentRes);
} }
async handleSaveComment(form: SaveComment) { async handleSaveComment(form: SaveComment) {
const saveCommentRes = await apiWrapper( const saveCommentRes = await HttpService.wrappedClient.saveComment(form);
HttpService.client.saveComment(form)
);
this.findAndUpdateComment(saveCommentRes); this.findAndUpdateComment(saveCommentRes);
} }
async handleSavePost(form: SavePost) { async handleSavePost(form: SavePost) {
const saveRes = await apiWrapper(HttpService.client.savePost(form)); const saveRes = await HttpService.wrappedClient.savePost(form);
this.findAndUpdatePost(saveRes); this.findAndUpdatePost(saveRes);
} }
async handleFeaturePost(form: FeaturePost) { async handleFeaturePost(form: FeaturePost) {
const featureRes = await apiWrapper(HttpService.client.featurePost(form)); const featureRes = await HttpService.wrappedClient.featurePost(form);
this.findAndUpdatePost(featureRes); this.findAndUpdatePost(featureRes);
} }
async handleCommentVote(form: CreateCommentLike) { async handleCommentVote(form: CreateCommentLike) {
const voteRes = await apiWrapper(HttpService.client.likeComment(form)); const voteRes = await HttpService.wrappedClient.likeComment(form);
this.findAndUpdateComment(voteRes); this.findAndUpdateComment(voteRes);
} }
async handlePostVote(form: CreatePostLike) { async handlePostVote(form: CreatePostLike) {
const voteRes = await apiWrapper(HttpService.client.likePost(form)); const voteRes = await HttpService.wrappedClient.likePost(form);
this.findAndUpdatePost(voteRes); this.findAndUpdatePost(voteRes);
} }
async handleCommentReport(form: CreateCommentReport) { async handleCommentReport(form: CreateCommentReport) {
const reportRes = await apiWrapper( const reportRes = await HttpService.wrappedClient.createCommentReport(form);
HttpService.client.createCommentReport(form)
);
if (reportRes.state == "success") { if (reportRes.state == "success") {
toast(i18n.t("report_created")); toast(i18n.t("report_created"));
} }
} }
async handlePostReport(form: CreatePostReport) { async handlePostReport(form: CreatePostReport) {
const reportRes = await apiWrapper( const reportRes = await HttpService.wrappedClient.createPostReport(form);
HttpService.client.createPostReport(form)
);
if (reportRes.state == "success") { if (reportRes.state == "success") {
toast(i18n.t("report_created")); toast(i18n.t("report_created"));
} }
} }
async handleLockPost(form: LockPost) { async handleLockPost(form: LockPost) {
const lockRes = await apiWrapper(HttpService.client.lockPost(form)); const lockRes = await HttpService.wrappedClient.lockPost(form);
this.findAndUpdatePost(lockRes); this.findAndUpdatePost(lockRes);
} }
async handleDistinguishComment(form: DistinguishComment) { async handleDistinguishComment(form: DistinguishComment) {
const distinguishRes = await apiWrapper( const distinguishRes = await HttpService.wrappedClient.distinguishComment(
HttpService.client.distinguishComment(form) form
); );
this.findAndUpdateComment(distinguishRes); this.findAndUpdateComment(distinguishRes);
} }
async handleAddAdmin(form: AddAdmin) { async handleAddAdmin(form: AddAdmin) {
const addAdminRes = await apiWrapper(HttpService.client.addAdmin(form)); const addAdminRes = await HttpService.wrappedClient.addAdmin(form);
if (addAdminRes.state == "success") { if (addAdminRes.state == "success") {
this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s)); this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
@ -820,32 +791,31 @@ export class Community extends Component<
} }
async handleTransferCommunity(form: TransferCommunity) { async handleTransferCommunity(form: TransferCommunity) {
const transferCommunityRes = await apiWrapper( const transferCommunityRes =
HttpService.client.transferCommunity(form) await HttpService.wrappedClient.transferCommunity(form);
);
toast(i18n.t("transfer_community")); toast(i18n.t("transfer_community"));
this.updateCommunityFull(transferCommunityRes); this.updateCommunityFull(transferCommunityRes);
} }
async handleCommentReplyRead(form: MarkCommentReplyAsRead) { async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
const readRes = await apiWrapper( const readRes = await HttpService.wrappedClient.markCommentReplyAsRead(
HttpService.client.markCommentReplyAsRead(form) form
); );
this.findAndUpdateCommentReply(readRes); this.findAndUpdateCommentReply(readRes);
} }
async handlePersonMentionRead(form: MarkPersonMentionAsRead) { async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
// TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it. // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
await apiWrapper(HttpService.client.markPersonMentionAsRead(form)); await HttpService.wrappedClient.markPersonMentionAsRead(form);
} }
async handleBanFromCommunity(form: BanFromCommunity) { async handleBanFromCommunity(form: BanFromCommunity) {
const banRes = await apiWrapper(HttpService.client.banFromCommunity(form)); const banRes = await HttpService.wrappedClient.banFromCommunity(form);
this.updateBanFromCommunity(banRes); this.updateBanFromCommunity(banRes);
} }
async handleBanPerson(form: BanPerson) { async handleBanPerson(form: BanPerson) {
const banRes = await apiWrapper(HttpService.client.banPerson(form)); const banRes = await HttpService.wrappedClient.banPerson(form);
this.updateBan(banRes); this.updateBan(banRes);
} }

View file

@ -4,7 +4,7 @@ import {
GetSiteResponse, GetSiteResponse,
} from "lemmy-js-client"; } from "lemmy-js-client";
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { HttpService, apiWrapper } from "../../services/HttpService"; import { HttpService } from "../../services/HttpService";
import { enableNsfw, setIsoData } from "../../utils"; import { enableNsfw, setIsoData } from "../../utils";
import { HtmlTags } from "../common/html-tags"; import { HtmlTags } from "../common/html-tags";
import { CommunityForm } from "./community-form"; import { CommunityForm } from "./community-form";
@ -53,7 +53,7 @@ export class CreateCommunity extends Component<any, CreateCommunityState> {
} }
async handleCommunityCreate(form: CreateCommunityI) { async handleCommunityCreate(form: CreateCommunityI) {
const res = await apiWrapper(HttpService.client.createCommunity(form)); const res = await HttpService.wrappedClient.createCommunity(form);
if (res.state === "success") { if (res.state === "success") {
const name = res.data.community_view.community.name; const name = res.data.community_view.community.name;
this.props.history.replace(`/c/${name}`); this.props.history.replace(`/c/${name}`);

View file

@ -15,7 +15,6 @@ import { InitialFetchRequest } from "../../interfaces";
import { import {
HttpService, HttpService,
RequestState, RequestState,
apiWrapper,
apiWrapperIso, apiWrapperIso,
} from "../../services/HttpService"; } from "../../services/HttpService";
import { import {
@ -88,16 +87,12 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
const auth = myAuthRequired(); const auth = myAuthRequired();
this.setState({ this.setState({
bannedRes: await apiWrapper( bannedRes: await HttpService.wrappedClient.getBannedPersons({
HttpService.client.getBannedPersons({ auth,
auth, }),
}) instancesRes: await HttpService.wrappedClient.getFederatedInstances({
), auth,
instancesRes: await apiWrapper( }),
HttpService.client.getFederatedInstances({
auth,
})
),
}); });
} }
@ -251,7 +246,7 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
} }
async handleEditSite(form: EditSite) { async handleEditSite(form: EditSite) {
const editRes = await apiWrapper(HttpService.client.editSite(form)); const editRes = await HttpService.wrappedClient.editSite(form);
if (editRes.state === "success") { if (editRes.state === "success") {
toast(i18n.t("site_saved")); toast(i18n.t("site_saved"));
@ -267,9 +262,9 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
async handleLeaveAdminTeam(i: AdminSettings) { async handleLeaveAdminTeam(i: AdminSettings) {
i.setState({ leaveAdminTeamRes: { state: "loading" } }); i.setState({ leaveAdminTeamRes: { state: "loading" } });
this.setState({ this.setState({
leaveAdminTeamRes: await apiWrapper( leaveAdminTeamRes: await HttpService.wrappedClient.leaveAdmin({
HttpService.client.leaveAdmin({ auth: myAuthRequired() }) auth: myAuthRequired(),
), }),
}); });
if (this.state.leaveAdminTeamRes.state === "success") { if (this.state.leaveAdminTeamRes.state === "success") {
@ -279,21 +274,21 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
} }
async handleEditEmoji(form: EditCustomEmoji) { async handleEditEmoji(form: EditCustomEmoji) {
const res = await apiWrapper(HttpService.client.editCustomEmoji(form)); const res = await HttpService.wrappedClient.editCustomEmoji(form);
if (res.state === "success") { if (res.state === "success") {
updateEmojiDataModel(res.data.custom_emoji); updateEmojiDataModel(res.data.custom_emoji);
} }
} }
async handleDeleteEmoji(form: DeleteCustomEmoji) { async handleDeleteEmoji(form: DeleteCustomEmoji) {
const res = await apiWrapper(HttpService.client.deleteCustomEmoji(form)); const res = await HttpService.wrappedClient.deleteCustomEmoji(form);
if (res.state === "success") { if (res.state === "success") {
removeFromEmojiDataModel(res.data.id); removeFromEmojiDataModel(res.data.id);
} }
} }
async handleCreateEmoji(form: CreateCustomEmoji) { async handleCreateEmoji(form: CreateCustomEmoji) {
const res = await apiWrapper(HttpService.client.createCustomEmoji(form)); const res = await HttpService.wrappedClient.createCustomEmoji(form);
if (res.state === "success") { if (res.state === "success") {
updateEmojiDataModel(res.data.custom_emoji); updateEmojiDataModel(res.data.custom_emoji);
} }

View file

@ -54,7 +54,6 @@ import {
} from "../../interfaces"; } from "../../interfaces";
import { UserService } from "../../services"; import { UserService } from "../../services";
import { import {
apiWrapper,
apiWrapperIso, apiWrapperIso,
HttpService, HttpService,
RequestState, RequestState,
@ -745,14 +744,12 @@ export class Home extends Component<any, HomeState> {
async fetchTrendingCommunities() { async fetchTrendingCommunities() {
this.setState({ trendingCommunitiesRes: { state: "loading" } }); this.setState({ trendingCommunitiesRes: { state: "loading" } });
this.setState({ this.setState({
trendingCommunitiesRes: await apiWrapper( trendingCommunitiesRes: await HttpService.wrappedClient.listCommunities({
HttpService.client.listCommunities({ type_: "Local",
type_: "Local", sort: "Hot",
sort: "Hot", limit: trendingFetchLimit,
limit: trendingFetchLimit, auth: myAuth(),
auth: myAuth(), }),
})
),
}); });
} }
@ -763,30 +760,26 @@ export class Home extends Component<any, HomeState> {
if (dataType === DataType.Post) { if (dataType === DataType.Post) {
this.setState({ postsRes: { state: "loading" } }); this.setState({ postsRes: { state: "loading" } });
this.setState({ this.setState({
postsRes: await apiWrapper( postsRes: await HttpService.wrappedClient.getPosts({
HttpService.client.getPosts({ page,
page, limit: fetchLimit,
limit: fetchLimit, sort,
sort, saved_only: false,
saved_only: false, type_: listingType,
type_: listingType, auth,
auth, }),
})
),
}); });
} else { } else {
this.setState({ commentsRes: { state: "loading" } }); this.setState({ commentsRes: { state: "loading" } });
this.setState({ this.setState({
commentsRes: await apiWrapper( commentsRes: await HttpService.wrappedClient.getComments({
HttpService.client.getComments({ page,
page, limit: fetchLimit,
limit: fetchLimit, sort: postToCommentSortType(sort),
sort: postToCommentSortType(sort), saved_only: false,
saved_only: false, type_: listingType,
type_: listingType, auth,
auth, }),
})
),
}); });
} }
@ -832,143 +825,124 @@ export class Home extends Component<any, HomeState> {
async handleAddModToCommunity(form: AddModToCommunity) { async handleAddModToCommunity(form: AddModToCommunity) {
// TODO not sure what to do here // TODO not sure what to do here
await apiWrapper(HttpService.client.addModToCommunity(form)); await HttpService.wrappedClient.addModToCommunity(form);
} }
async handlePurgePerson(form: PurgePerson) { async handlePurgePerson(form: PurgePerson) {
const purgePersonRes = await apiWrapper( const purgePersonRes = await HttpService.wrappedClient.purgePerson(form);
HttpService.client.purgePerson(form)
);
this.purgeItem(purgePersonRes); this.purgeItem(purgePersonRes);
} }
async handlePurgeComment(form: PurgeComment) { async handlePurgeComment(form: PurgeComment) {
const purgeCommentRes = await apiWrapper( const purgeCommentRes = await HttpService.wrappedClient.purgeComment(form);
HttpService.client.purgeComment(form)
);
this.purgeItem(purgeCommentRes); this.purgeItem(purgeCommentRes);
} }
async handlePurgePost(form: PurgePost) { async handlePurgePost(form: PurgePost) {
const purgeRes = await apiWrapper(HttpService.client.purgePost(form)); const purgeRes = await HttpService.wrappedClient.purgePost(form);
this.purgeItem(purgeRes); this.purgeItem(purgeRes);
} }
async handleBlockPerson(form: BlockPerson) { async handleBlockPerson(form: BlockPerson) {
const blockPersonRes = await apiWrapper( const blockPersonRes = await HttpService.wrappedClient.blockPerson(form);
HttpService.client.blockPerson(form)
);
if (blockPersonRes.state == "success") { if (blockPersonRes.state == "success") {
updatePersonBlock(blockPersonRes.data); updatePersonBlock(blockPersonRes.data);
} }
} }
async handleCreateComment(form: CreateComment) { async handleCreateComment(form: CreateComment) {
const createCommentRes = await apiWrapper( const createCommentRes = await HttpService.wrappedClient.createComment(
HttpService.client.createComment(form) form
); );
this.createAndUpdateComments(createCommentRes); this.createAndUpdateComments(createCommentRes);
return createCommentRes; return createCommentRes;
} }
async handleEditComment(form: EditComment) { async handleEditComment(form: EditComment) {
const editCommentRes = await apiWrapper( const editCommentRes = await HttpService.wrappedClient.editComment(form);
HttpService.client.editComment(form)
);
this.findAndUpdateComment(editCommentRes); this.findAndUpdateComment(editCommentRes);
return editCommentRes; return editCommentRes;
} }
async handleDeleteComment(form: DeleteComment) { async handleDeleteComment(form: DeleteComment) {
const deleteCommentRes = await apiWrapper( const deleteCommentRes = await HttpService.wrappedClient.deleteComment(
HttpService.client.deleteComment(form) form
); );
this.findAndUpdateComment(deleteCommentRes); this.findAndUpdateComment(deleteCommentRes);
} }
async handleDeletePost(form: DeletePost) { async handleDeletePost(form: DeletePost) {
const deleteRes = await apiWrapper(HttpService.client.deletePost(form)); const deleteRes = await HttpService.wrappedClient.deletePost(form);
this.findAndUpdatePost(deleteRes); this.findAndUpdatePost(deleteRes);
} }
async handleRemovePost(form: RemovePost) { async handleRemovePost(form: RemovePost) {
const removeRes = await apiWrapper(HttpService.client.removePost(form)); const removeRes = await HttpService.wrappedClient.removePost(form);
this.findAndUpdatePost(removeRes); this.findAndUpdatePost(removeRes);
} }
async handleRemoveComment(form: RemoveComment) { async handleRemoveComment(form: RemoveComment) {
const removeCommentRes = await apiWrapper( const removeCommentRes = await HttpService.wrappedClient.removeComment(
HttpService.client.removeComment(form) form
); );
this.findAndUpdateComment(removeCommentRes); this.findAndUpdateComment(removeCommentRes);
} }
async handleSaveComment(form: SaveComment) { async handleSaveComment(form: SaveComment) {
const saveCommentRes = await apiWrapper( const saveCommentRes = await HttpService.wrappedClient.saveComment(form);
HttpService.client.saveComment(form)
);
this.findAndUpdateComment(saveCommentRes); this.findAndUpdateComment(saveCommentRes);
} }
async handleSavePost(form: SavePost) { async handleSavePost(form: SavePost) {
const saveRes = await apiWrapper(HttpService.client.savePost(form)); const saveRes = await HttpService.wrappedClient.savePost(form);
this.findAndUpdatePost(saveRes); this.findAndUpdatePost(saveRes);
} }
async handleFeaturePost(form: FeaturePost) { async handleFeaturePost(form: FeaturePost) {
const featureRes = await apiWrapper(HttpService.client.featurePost(form)); const featureRes = await HttpService.wrappedClient.featurePost(form);
this.findAndUpdatePost(featureRes); this.findAndUpdatePost(featureRes);
} }
async handleCommentVote(form: CreateCommentLike) { async handleCommentVote(form: CreateCommentLike) {
const voteRes = await apiWrapper(HttpService.client.likeComment(form)); const voteRes = await HttpService.wrappedClient.likeComment(form);
this.findAndUpdateComment(voteRes); this.findAndUpdateComment(voteRes);
} }
async handlePostVote(form: CreatePostLike) { async handlePostVote(form: CreatePostLike) {
const voteRes = await apiWrapper(HttpService.client.likePost(form)); const voteRes = await HttpService.wrappedClient.likePost(form);
this.findAndUpdatePost(voteRes); this.findAndUpdatePost(voteRes);
} }
async handleCommentReport(form: CreateCommentReport) { async handleCommentReport(form: CreateCommentReport) {
const reportRes = await apiWrapper( const reportRes = await HttpService.wrappedClient.createCommentReport(form);
HttpService.client.createCommentReport(form)
);
if (reportRes.state == "success") { if (reportRes.state == "success") {
toast(i18n.t("report_created")); toast(i18n.t("report_created"));
} }
} }
async handlePostReport(form: CreatePostReport) { async handlePostReport(form: CreatePostReport) {
const reportRes = await apiWrapper( const reportRes = await HttpService.wrappedClient.createPostReport(form);
HttpService.client.createPostReport(form)
);
if (reportRes.state == "success") { if (reportRes.state == "success") {
toast(i18n.t("report_created")); toast(i18n.t("report_created"));
} }
} }
async handleLockPost(form: LockPost) { async handleLockPost(form: LockPost) {
const lockRes = await apiWrapper(HttpService.client.lockPost(form)); const lockRes = await HttpService.wrappedClient.lockPost(form);
this.findAndUpdatePost(lockRes); this.findAndUpdatePost(lockRes);
} }
async handleDistinguishComment(form: DistinguishComment) { async handleDistinguishComment(form: DistinguishComment) {
const distinguishRes = await apiWrapper( const distinguishRes = await HttpService.wrappedClient.distinguishComment(
HttpService.client.distinguishComment(form) form
); );
this.findAndUpdateComment(distinguishRes); this.findAndUpdateComment(distinguishRes);
} }
async handleAddAdmin(form: AddAdmin) { async handleAddAdmin(form: AddAdmin) {
const addAdminRes = await apiWrapper(HttpService.client.addAdmin(form)); const addAdminRes = await HttpService.wrappedClient.addAdmin(form);
if (addAdminRes.state == "success") { if (addAdminRes.state == "success") {
this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s)); this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
@ -976,29 +950,29 @@ export class Home extends Component<any, HomeState> {
} }
async handleTransferCommunity(form: TransferCommunity) { async handleTransferCommunity(form: TransferCommunity) {
await apiWrapper(HttpService.client.transferCommunity(form)); await HttpService.wrappedClient.transferCommunity(form);
toast(i18n.t("transfer_community")); toast(i18n.t("transfer_community"));
} }
async handleCommentReplyRead(form: MarkCommentReplyAsRead) { async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
const readRes = await apiWrapper( const readRes = await HttpService.wrappedClient.markCommentReplyAsRead(
HttpService.client.markCommentReplyAsRead(form) form
); );
this.findAndUpdateCommentReply(readRes); this.findAndUpdateCommentReply(readRes);
} }
async handlePersonMentionRead(form: MarkPersonMentionAsRead) { async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
// TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it. // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
await apiWrapper(HttpService.client.markPersonMentionAsRead(form)); await HttpService.wrappedClient.markPersonMentionAsRead(form);
} }
async handleBanFromCommunity(form: BanFromCommunity) { async handleBanFromCommunity(form: BanFromCommunity) {
const banRes = await apiWrapper(HttpService.client.banFromCommunity(form)); const banRes = await HttpService.wrappedClient.banFromCommunity(form);
this.updateBanFromCommunity(banRes); this.updateBanFromCommunity(banRes);
} }
async handleBanPerson(form: BanPerson) { async handleBanPerson(form: BanPerson) {
const banRes = await apiWrapper(HttpService.client.banPerson(form)); const banRes = await HttpService.wrappedClient.banPerson(form);
this.updateBan(banRes); this.updateBan(banRes);
} }

View file

@ -9,7 +9,6 @@ import { InitialFetchRequest } from "../../interfaces";
import { import {
HttpService, HttpService,
RequestState, RequestState,
apiWrapper,
apiWrapperIso, apiWrapperIso,
} from "../../services/HttpService"; } from "../../services/HttpService";
import { isInitialRoute, relTags, setIsoData } from "../../utils"; import { isInitialRoute, relTags, setIsoData } from "../../utils";
@ -54,9 +53,7 @@ export class Instances extends Component<any, InstancesState> {
}); });
this.setState({ this.setState({
instancesRes: await apiWrapper( instancesRes: await HttpService.wrappedClient.getFederatedInstances({}),
HttpService.client.getFederatedInstances({})
),
}); });
} }

View file

@ -2,11 +2,7 @@ import { Component, linkEvent } from "inferno";
import { GetSiteResponse, LoginResponse } from "lemmy-js-client"; import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { UserService } from "../../services"; import { UserService } from "../../services";
import { import { HttpService, RequestState } from "../../services/HttpService";
HttpService,
RequestState,
apiWrapper,
} from "../../services/HttpService";
import { isBrowser, setIsoData, toast, validEmail } from "../../utils"; import { isBrowser, 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";
@ -164,14 +160,11 @@ export class Login extends Component<any, State> {
if (username_or_email && password) { if (username_or_email && password) {
i.setState({ loginRes: { state: "loading" } }); i.setState({ loginRes: { state: "loading" } });
const loginRes = await apiWrapper( const loginRes = await HttpService.wrappedClient.login({
HttpService.client.login({ username_or_email,
username_or_email, password,
password, totp_2fa_token,
totp_2fa_token, });
})
);
switch (loginRes.state) { switch (loginRes.state) {
case "failed": { case "failed": {
if (loginRes.msg === "missing_totp_token") { if (loginRes.msg === "missing_totp_token") {
@ -212,7 +205,7 @@ export class Login extends Component<any, State> {
event.preventDefault(); event.preventDefault();
let email = i.state.form.username_or_email; let email = i.state.form.username_or_email;
if (email) { if (email) {
const res = await apiWrapper(HttpService.client.passwordReset({ email })); const res = await HttpService.wrappedClient.passwordReset({ email });
if (res.state == "success") { if (res.state == "success") {
toast(i18n.t("reset_password_mail_sent")); toast(i18n.t("reset_password_mail_sent"));
} }

View file

@ -8,11 +8,7 @@ import {
} from "lemmy-js-client"; } from "lemmy-js-client";
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { UserService } from "../../services"; import { UserService } from "../../services";
import { import { HttpService, RequestState } from "../../services/HttpService";
HttpService,
RequestState,
apiWrapper,
} from "../../services/HttpService";
import { setIsoData } from "../../utils"; import { setIsoData } from "../../utils";
import { Spinner } from "../common/icon"; import { Spinner } from "../common/icon";
import { SiteForm } from "./site-form"; import { SiteForm } from "./site-form";
@ -180,7 +176,7 @@ export class Setup extends Component<any, State> {
answer: cForm.answer, answer: cForm.answer,
}; };
i.setState({ i.setState({
registerRes: await apiWrapper(HttpService.client.register(form)), registerRes: await HttpService.wrappedClient.register(form),
}); });
if (this.state.registerRes.state == "success") { if (this.state.registerRes.state == "success") {
@ -195,7 +191,7 @@ export class Setup extends Component<any, State> {
} }
async handleCreateSite(form: CreateSite) { async handleCreateSite(form: CreateSite) {
const createRes = await apiWrapper(HttpService.client.createSite(form)); const createRes = await HttpService.wrappedClient.createSite(form);
if (createRes.state === "success") { if (createRes.state === "success") {
this.context.router.history.replace("/"); this.context.router.history.replace("/");
} }

View file

@ -11,11 +11,7 @@ import {
} from "lemmy-js-client"; } from "lemmy-js-client";
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { UserService } from "../../services"; import { UserService } from "../../services";
import { import { HttpService, RequestState } from "../../services/HttpService";
HttpService,
RequestState,
apiWrapper,
} from "../../services/HttpService";
import { import {
isBrowser, isBrowser,
joinLemmyUrl, joinLemmyUrl,
@ -102,7 +98,7 @@ export class Signup extends Component<any, State> {
async fetchCaptcha() { async fetchCaptcha() {
this.setState({ captchaRes: { state: "loading" } }); this.setState({ captchaRes: { state: "loading" } });
this.setState({ this.setState({
captchaRes: await apiWrapper(HttpService.client.getCaptcha({})), captchaRes: await HttpService.wrappedClient.getCaptcha({}),
}); });
this.setState(s => { this.setState(s => {
@ -441,20 +437,17 @@ export class Signup extends Component<any, State> {
if (cForm.username && cForm.password && cForm.password_verify) { if (cForm.username && cForm.password && cForm.password_verify) {
i.setState({ registerRes: { state: "loading" } }); i.setState({ registerRes: { state: "loading" } });
const registerRes = await apiWrapper( const registerRes = await HttpService.wrappedClient.register({
HttpService.client.register({ username: cForm.username,
username: cForm.username, password: cForm.password,
password: cForm.password, password_verify: cForm.password_verify,
password_verify: cForm.password_verify, email: cForm.email,
email: cForm.email, show_nsfw: cForm.show_nsfw,
show_nsfw: cForm.show_nsfw, captcha_uuid: cForm.captcha_uuid,
captcha_uuid: cForm.captcha_uuid, captcha_answer: cForm.captcha_answer,
captcha_answer: cForm.captcha_answer, honeypot: cForm.honeypot,
honeypot: cForm.honeypot, answer: cForm.answer,
answer: cForm.answer, });
})
);
switch (registerRes.state) { switch (registerRes.state) {
case "failed": { case "failed": {
toast(registerRes.msg, "danger"); toast(registerRes.msg, "danger");

View file

@ -33,7 +33,6 @@ import { InitialFetchRequest } from "../interfaces";
import { import {
HttpService, HttpService,
RequestState, RequestState,
apiWrapper,
apiWrapperIso, apiWrapperIso,
} from "../services/HttpService"; } from "../services/HttpService";
import { import {
@ -951,31 +950,27 @@ export class Modlog extends Component<
this.setState({ res: { state: "loading" } }); this.setState({ res: { state: "loading" } });
this.setState({ this.setState({
res: await apiWrapper( res: await HttpService.wrappedClient.getModlog({
HttpService.client.getModlog({ community_id: communityId,
community_id: communityId, page,
page, limit: fetchLimit,
limit: fetchLimit, type_: actionType,
type_: actionType, other_person_id: userId ?? undefined,
other_person_id: userId ?? undefined, mod_person_id: !this.isoData.site_res.site_view.local_site
mod_person_id: !this.isoData.site_res.site_view.local_site .hide_modlog_mod_names
.hide_modlog_mod_names ? modId ?? undefined
? modId ?? undefined : undefined,
: undefined, auth,
auth, }),
})
),
}); });
if (communityId) { if (communityId) {
this.setState({ communityRes: { state: "loading" } }); this.setState({ communityRes: { state: "loading" } });
this.setState({ this.setState({
communityRes: await apiWrapper( communityRes: await HttpService.wrappedClient.getCommunity({
HttpService.client.getCommunity({ id: communityId,
id: communityId, auth,
auth, }),
})
),
}); });
} }
} }

View file

@ -53,7 +53,6 @@ import { UserService } from "../../services";
import { import {
HttpService, HttpService,
RequestState, RequestState,
apiWrapper,
apiWrapperIso, apiWrapperIso,
} from "../../services/HttpService"; } from "../../services/HttpService";
import { import {
@ -724,40 +723,34 @@ export class Inbox extends Component<any, InboxState> {
this.setState({ repliesRes: { state: "loading" } }); this.setState({ repliesRes: { state: "loading" } });
this.setState({ this.setState({
repliesRes: await apiWrapper( repliesRes: await HttpService.wrappedClient.getReplies({
HttpService.client.getReplies({ sort,
sort, unread_only,
unread_only, page,
page, limit,
limit, auth,
auth, }),
})
),
}); });
this.setState({ mentionsRes: { state: "loading" } }); this.setState({ mentionsRes: { state: "loading" } });
this.setState({ this.setState({
mentionsRes: await apiWrapper( mentionsRes: await HttpService.wrappedClient.getPersonMentions({
HttpService.client.getPersonMentions({ sort,
sort, unread_only,
unread_only, page,
page, limit,
limit, auth,
auth, }),
})
),
}); });
this.setState({ messagesRes: { state: "loading" } }); this.setState({ messagesRes: { state: "loading" } });
this.setState({ this.setState({
messagesRes: await apiWrapper( messagesRes: await HttpService.wrappedClient.getPrivateMessages({
HttpService.client.getPrivateMessages({ unread_only,
unread_only, page,
page, limit,
limit, auth,
auth, }),
})
),
}); });
} }
@ -770,9 +763,9 @@ export class Inbox extends Component<any, InboxState> {
i.setState({ markAllAsReadRes: { state: "loading" } }); i.setState({ markAllAsReadRes: { state: "loading" } });
i.setState({ i.setState({
markAllAsReadRes: await apiWrapper( markAllAsReadRes: await HttpService.wrappedClient.markAllAsRead({
HttpService.client.markAllAsRead({ auth: myAuthRequired() }) auth: myAuthRequired(),
), }),
}); });
if (i.state.markAllAsReadRes.state == "success") { if (i.state.markAllAsReadRes.state == "success") {
@ -786,40 +779,33 @@ export class Inbox extends Component<any, InboxState> {
async handleAddModToCommunity(form: AddModToCommunity) { async handleAddModToCommunity(form: AddModToCommunity) {
// TODO not sure what to do here // TODO not sure what to do here
apiWrapper(HttpService.client.addModToCommunity(form)); HttpService.wrappedClient.addModToCommunity(form);
} }
async handlePurgePerson(form: PurgePerson) { async handlePurgePerson(form: PurgePerson) {
const purgePersonRes = await apiWrapper( const purgePersonRes = await HttpService.wrappedClient.purgePerson(form);
HttpService.client.purgePerson(form)
);
this.purgeItem(purgePersonRes); this.purgeItem(purgePersonRes);
} }
async handlePurgeComment(form: PurgeComment) { async handlePurgeComment(form: PurgeComment) {
const purgeCommentRes = await apiWrapper( const purgeCommentRes = await HttpService.wrappedClient.purgeComment(form);
HttpService.client.purgeComment(form)
);
this.purgeItem(purgeCommentRes); this.purgeItem(purgeCommentRes);
} }
async handlePurgePost(form: PurgePost) { async handlePurgePost(form: PurgePost) {
const purgeRes = await apiWrapper(HttpService.client.purgePost(form)); const purgeRes = await HttpService.wrappedClient.purgePost(form);
this.purgeItem(purgeRes); this.purgeItem(purgeRes);
} }
async handleBlockPerson(form: BlockPerson) { async handleBlockPerson(form: BlockPerson) {
const blockPersonRes = await apiWrapper( const blockPersonRes = await HttpService.wrappedClient.blockPerson(form);
HttpService.client.blockPerson(form)
);
if (blockPersonRes.state == "success") { if (blockPersonRes.state == "success") {
updatePersonBlock(blockPersonRes.data); updatePersonBlock(blockPersonRes.data);
} }
} }
async handleCreateComment(form: CreateComment) { async handleCreateComment(form: CreateComment) {
const res = await apiWrapper(HttpService.client.createComment(form)); const res = await HttpService.wrappedClient.createComment(form);
if (res.state === "success") { if (res.state === "success") {
toast(i18n.t("reply_sent")); toast(i18n.t("reply_sent"));
@ -830,7 +816,7 @@ export class Inbox extends Component<any, InboxState> {
} }
async handleEditComment(form: EditComment) { async handleEditComment(form: EditComment) {
const res = await apiWrapper(HttpService.client.editComment(form)); const res = await HttpService.wrappedClient.editComment(form);
if (res.state === "success") { if (res.state === "success") {
toast(i18n.t("edit")); toast(i18n.t("edit"));
@ -843,7 +829,7 @@ export class Inbox extends Component<any, InboxState> {
} }
async handleDeleteComment(form: DeleteComment) { async handleDeleteComment(form: DeleteComment) {
const res = await apiWrapper(HttpService.client.deleteComment(form)); const res = await HttpService.wrappedClient.deleteComment(form);
if (res.state == "success") { if (res.state == "success") {
toast(i18n.t("deleted")); toast(i18n.t("deleted"));
this.findAndUpdateComment(res); this.findAndUpdateComment(res);
@ -851,7 +837,7 @@ export class Inbox extends Component<any, InboxState> {
} }
async handleRemoveComment(form: RemoveComment) { async handleRemoveComment(form: RemoveComment) {
const res = await apiWrapper(HttpService.client.removeComment(form)); const res = await HttpService.wrappedClient.removeComment(form);
if (res.state == "success") { if (res.state == "success") {
toast(i18n.t("remove_comment")); toast(i18n.t("remove_comment"));
this.findAndUpdateComment(res); this.findAndUpdateComment(res);
@ -859,90 +845,82 @@ export class Inbox extends Component<any, InboxState> {
} }
async handleSaveComment(form: SaveComment) { async handleSaveComment(form: SaveComment) {
const res = await apiWrapper(HttpService.client.saveComment(form)); const res = await HttpService.wrappedClient.saveComment(form);
this.findAndUpdateComment(res); this.findAndUpdateComment(res);
} }
async handleCommentVote(form: CreateCommentLike) { async handleCommentVote(form: CreateCommentLike) {
const res = await apiWrapper(HttpService.client.likeComment(form)); const res = await HttpService.wrappedClient.likeComment(form);
this.findAndUpdateComment(res); this.findAndUpdateComment(res);
} }
async handleCommentReport(form: CreateCommentReport) { async handleCommentReport(form: CreateCommentReport) {
const reportRes = await apiWrapper( const reportRes = await HttpService.wrappedClient.createCommentReport(form);
HttpService.client.createCommentReport(form)
);
this.reportToast(reportRes); this.reportToast(reportRes);
} }
async handleDistinguishComment(form: DistinguishComment) { async handleDistinguishComment(form: DistinguishComment) {
const res = await apiWrapper(HttpService.client.distinguishComment(form)); const res = await HttpService.wrappedClient.distinguishComment(form);
this.findAndUpdateComment(res); this.findAndUpdateComment(res);
} }
async handleAddAdmin(form: AddAdmin) { async handleAddAdmin(form: AddAdmin) {
const addAdminRes = await apiWrapper(HttpService.client.addAdmin(form)); const addAdminRes = await HttpService.wrappedClient.addAdmin(form);
if (addAdminRes.state == "success") { if (addAdminRes.state === "success") {
this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s)); this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
} }
} }
async handleTransferCommunity(form: TransferCommunity) { async handleTransferCommunity(form: TransferCommunity) {
await apiWrapper(HttpService.client.transferCommunity(form)); await HttpService.wrappedClient.transferCommunity(form);
toast(i18n.t("transfer_community")); toast(i18n.t("transfer_community"));
} }
async handleCommentReplyRead(form: MarkCommentReplyAsRead) { async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
const res = await apiWrapper( const res = await HttpService.wrappedClient.markCommentReplyAsRead(form);
HttpService.client.markCommentReplyAsRead(form)
);
this.findAndUpdateCommentReply(res); this.findAndUpdateCommentReply(res);
} }
async handlePersonMentionRead(form: MarkPersonMentionAsRead) { async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
const res = await apiWrapper( const res = await HttpService.wrappedClient.markPersonMentionAsRead(form);
HttpService.client.markPersonMentionAsRead(form)
);
this.findAndUpdateMention(res); this.findAndUpdateMention(res);
} }
async handleBanFromCommunity(form: BanFromCommunity) { async handleBanFromCommunity(form: BanFromCommunity) {
const banRes = await apiWrapper(HttpService.client.banFromCommunity(form)); const banRes = await HttpService.wrappedClient.banFromCommunity(form);
this.updateBanFromCommunity(banRes); this.updateBanFromCommunity(banRes);
} }
async handleBanPerson(form: BanPerson) { async handleBanPerson(form: BanPerson) {
const banRes = await apiWrapper(HttpService.client.banPerson(form)); const banRes = await HttpService.wrappedClient.banPerson(form);
this.updateBan(banRes); this.updateBan(banRes);
} }
async handleDeleteMessage(form: DeletePrivateMessage) { async handleDeleteMessage(form: DeletePrivateMessage) {
const res = await apiWrapper(HttpService.client.deletePrivateMessage(form)); const res = await HttpService.wrappedClient.deletePrivateMessage(form);
this.findAndUpdateMessage(res); this.findAndUpdateMessage(res);
} }
async handleEditMessage(form: EditPrivateMessage) { async handleEditMessage(form: EditPrivateMessage) {
const res = await apiWrapper(HttpService.client.editPrivateMessage(form)); const res = await HttpService.wrappedClient.editPrivateMessage(form);
this.findAndUpdateMessage(res); this.findAndUpdateMessage(res);
} }
async handleMarkMessageAsRead(form: MarkPrivateMessageAsRead) { async handleMarkMessageAsRead(form: MarkPrivateMessageAsRead) {
const res = await apiWrapper( const res = await HttpService.wrappedClient.markPrivateMessageAsRead(form);
HttpService.client.markPrivateMessageAsRead(form)
);
this.findAndUpdateMessage(res); this.findAndUpdateMessage(res);
} }
async handleMessageReport(form: CreatePrivateMessageReport) { async handleMessageReport(form: CreatePrivateMessageReport) {
const res = await apiWrapper( const res = await HttpService.wrappedClient.createPrivateMessageReport(
HttpService.client.createPrivateMessageReport(form) form
); );
this.reportToast(res); this.reportToast(res);
} }
async handleCreateMessage(form: CreatePrivateMessage) { async handleCreateMessage(form: CreatePrivateMessage) {
const res = await apiWrapper(HttpService.client.createPrivateMessage(form)); const res = await HttpService.wrappedClient.createPrivateMessage(form);
this.setState(s => { this.setState(s => {
if (s.messagesRes.state == "success" && res.state == "success") { if (s.messagesRes.state == "success" && res.state == "success") {
s.messagesRes.data.private_messages.unshift( s.messagesRes.data.private_messages.unshift(
@ -956,7 +934,7 @@ export class Inbox extends Component<any, InboxState> {
findAndUpdateMessage(res: RequestState<PrivateMessageResponse>) { findAndUpdateMessage(res: RequestState<PrivateMessageResponse>) {
this.setState(s => { this.setState(s => {
if (s.messagesRes.state == "success" && res.state == "success") { if (s.messagesRes.state === "success" && res.state === "success") {
s.messagesRes.data.private_messages = editPrivateMessage( s.messagesRes.data.private_messages = editPrivateMessage(
res.data.private_message_view, res.data.private_message_view,
s.messagesRes.data.private_messages s.messagesRes.data.private_messages

View file

@ -2,7 +2,7 @@ import { Component, linkEvent } from "inferno";
import { GetSiteResponse, LoginResponse } from "lemmy-js-client"; 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, apiWrapper } from "../../services/HttpService"; import { RequestState } from "../../services/HttpService";
import { capitalizeFirstLetter, setIsoData } from "../../utils"; import { capitalizeFirstLetter, 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";
@ -124,13 +124,12 @@ export class PasswordChange extends Component<any, State> {
if (password && password_verify) { if (password && password_verify) {
i.setState({ i.setState({
passwordChangeRes: await apiWrapper( passwordChangeRes:
HttpService.client.passwordChangeAfterReset({ await HttpService.wrappedClient.passwordChangeAfterReset({
token: i.state.form.token, token: i.state.form.token,
password, password,
password_verify, password_verify,
}) }),
),
}); });
if (i.state.passwordChangeRes.state == "success") { if (i.state.passwordChangeRes.state == "success") {

View file

@ -52,7 +52,6 @@ import { UserService } from "../../services";
import { import {
HttpService, HttpService,
RequestState, RequestState,
apiWrapper,
apiWrapperIso, apiWrapperIso,
} from "../../services/HttpService"; } from "../../services/HttpService";
import { import {
@ -232,16 +231,14 @@ export class Profile extends Component<
this.setState({ personRes: { state: "empty" } }); this.setState({ personRes: { state: "empty" } });
this.setState({ this.setState({
personRes: await apiWrapper( personRes: await HttpService.wrappedClient.getPersonDetails({
HttpService.client.getPersonDetails({ username: this.props.match.params.username,
username: this.props.match.params.username, sort,
sort, saved_only: view === PersonDetailsView.Saved,
saved_only: view === PersonDetailsView.Saved, page,
page, limit: fetchLimit,
limit: fetchLimit, auth: myAuth(),
auth: myAuth(), }),
})
),
}); });
restoreScrollPosition(this.context); restoreScrollPosition(this.context);
this.setPersonBlock(); this.setPersonBlock();
@ -770,17 +767,14 @@ export class Profile extends Component<
i.setState({ removeData: false }); i.setState({ removeData: false });
} }
const res = await apiWrapper( const res = await HttpService.wrappedClient.banPerson({
HttpService.client.banPerson({ person_id: person.id,
person_id: person.id, ban,
ban, remove_data: removeData,
remove_data: removeData, reason: banReason,
reason: banReason, expires: futureDaysToUnixTime(banExpireDays),
expires: futureDaysToUnixTime(banExpireDays), auth: myAuthRequired(),
auth: myAuthRequired(), });
})
);
// TODO // TODO
this.updateBan(res); this.updateBan(res);
i.setState({ showBanDialog: false }); i.setState({ showBanDialog: false });
@ -788,14 +782,11 @@ export class Profile extends Component<
} }
async toggleBlockPerson(recipientId: number, block: boolean) { async toggleBlockPerson(recipientId: number, block: boolean) {
const res = await apiWrapper( const res = await HttpService.wrappedClient.blockPerson({
HttpService.client.blockPerson({ person_id: recipientId,
person_id: recipientId, block,
block, auth: myAuthRequired(),
auth: myAuthRequired(), });
})
);
if (res.state == "success") { if (res.state == "success") {
updatePersonBlock(res.data); updatePersonBlock(res.data);
} }
@ -811,41 +802,34 @@ export class Profile extends Component<
async handleAddModToCommunity(form: AddModToCommunity) { async handleAddModToCommunity(form: AddModToCommunity) {
// TODO not sure what to do here // TODO not sure what to do here
await apiWrapper(HttpService.client.addModToCommunity(form)); await HttpService.wrappedClient.addModToCommunity(form);
} }
async handlePurgePerson(form: PurgePerson) { async handlePurgePerson(form: PurgePerson) {
const purgePersonRes = await apiWrapper( const purgePersonRes = await HttpService.wrappedClient.purgePerson(form);
HttpService.client.purgePerson(form)
);
this.purgeItem(purgePersonRes); this.purgeItem(purgePersonRes);
} }
async handlePurgeComment(form: PurgeComment) { async handlePurgeComment(form: PurgeComment) {
const purgeCommentRes = await apiWrapper( const purgeCommentRes = await HttpService.wrappedClient.purgeComment(form);
HttpService.client.purgeComment(form)
);
this.purgeItem(purgeCommentRes); this.purgeItem(purgeCommentRes);
} }
async handlePurgePost(form: PurgePost) { async handlePurgePost(form: PurgePost) {
const purgeRes = await apiWrapper(HttpService.client.purgePost(form)); const purgeRes = await HttpService.wrappedClient.purgePost(form);
this.purgeItem(purgeRes); this.purgeItem(purgeRes);
} }
async handleBlockPersonAlt(form: BlockPerson) { async handleBlockPersonAlt(form: BlockPerson) {
const blockPersonRes = await apiWrapper( const blockPersonRes = await HttpService.wrappedClient.blockPerson(form);
HttpService.client.blockPerson(form) if (blockPersonRes.state === "success") {
);
if (blockPersonRes.state == "success") {
updatePersonBlock(blockPersonRes.data); updatePersonBlock(blockPersonRes.data);
} }
} }
async handleCreateComment(form: CreateComment) { async handleCreateComment(form: CreateComment) {
const createCommentRes = await apiWrapper( const createCommentRes = await HttpService.wrappedClient.createComment(
HttpService.client.createComment(form) form
); );
this.createAndUpdateComments(createCommentRes); this.createAndUpdateComments(createCommentRes);
@ -853,100 +837,89 @@ export class Profile extends Component<
} }
async handleEditComment(form: EditComment) { async handleEditComment(form: EditComment) {
const editCommentRes = await apiWrapper( const editCommentRes = await HttpService.wrappedClient.editComment(form);
HttpService.client.editComment(form)
);
this.findAndUpdateComment(editCommentRes); this.findAndUpdateComment(editCommentRes);
return editCommentRes; return editCommentRes;
} }
async handleDeleteComment(form: DeleteComment) { async handleDeleteComment(form: DeleteComment) {
const deleteCommentRes = await apiWrapper( const deleteCommentRes = await HttpService.wrappedClient.deleteComment(
HttpService.client.deleteComment(form) form
); );
this.findAndUpdateComment(deleteCommentRes); this.findAndUpdateComment(deleteCommentRes);
} }
async handleDeletePost(form: DeletePost) { async handleDeletePost(form: DeletePost) {
const deleteRes = await apiWrapper(HttpService.client.deletePost(form)); const deleteRes = await HttpService.wrappedClient.deletePost(form);
this.findAndUpdatePost(deleteRes); this.findAndUpdatePost(deleteRes);
} }
async handleRemovePost(form: RemovePost) { async handleRemovePost(form: RemovePost) {
const removeRes = await apiWrapper(HttpService.client.removePost(form)); const removeRes = await HttpService.wrappedClient.removePost(form);
this.findAndUpdatePost(removeRes); this.findAndUpdatePost(removeRes);
} }
async handleRemoveComment(form: RemoveComment) { async handleRemoveComment(form: RemoveComment) {
const removeCommentRes = await apiWrapper( const removeCommentRes = await HttpService.wrappedClient.removeComment(
HttpService.client.removeComment(form) form
); );
this.findAndUpdateComment(removeCommentRes); this.findAndUpdateComment(removeCommentRes);
} }
async handleSaveComment(form: SaveComment) { async handleSaveComment(form: SaveComment) {
const saveCommentRes = await apiWrapper( const saveCommentRes = await HttpService.wrappedClient.saveComment(form);
HttpService.client.saveComment(form)
);
this.findAndUpdateComment(saveCommentRes); this.findAndUpdateComment(saveCommentRes);
} }
async handleSavePost(form: SavePost) { async handleSavePost(form: SavePost) {
const saveRes = await apiWrapper(HttpService.client.savePost(form)); const saveRes = await HttpService.wrappedClient.savePost(form);
this.findAndUpdatePost(saveRes); this.findAndUpdatePost(saveRes);
} }
async handleFeaturePost(form: FeaturePost) { async handleFeaturePost(form: FeaturePost) {
const featureRes = await apiWrapper(HttpService.client.featurePost(form)); const featureRes = await HttpService.wrappedClient.featurePost(form);
this.findAndUpdatePost(featureRes); this.findAndUpdatePost(featureRes);
} }
async handleCommentVote(form: CreateCommentLike) { async handleCommentVote(form: CreateCommentLike) {
const voteRes = await apiWrapper(HttpService.client.likeComment(form)); const voteRes = await HttpService.wrappedClient.likeComment(form);
this.findAndUpdateComment(voteRes); this.findAndUpdateComment(voteRes);
} }
async handlePostVote(form: CreatePostLike) { async handlePostVote(form: CreatePostLike) {
const voteRes = await apiWrapper(HttpService.client.likePost(form)); const voteRes = await HttpService.wrappedClient.likePost(form);
this.findAndUpdatePost(voteRes); this.findAndUpdatePost(voteRes);
} }
async handleCommentReport(form: CreateCommentReport) { async handleCommentReport(form: CreateCommentReport) {
const reportRes = await apiWrapper( const reportRes = await HttpService.wrappedClient.createCommentReport(form);
HttpService.client.createCommentReport(form) if (reportRes.state === "success") {
);
if (reportRes.state == "success") {
toast(i18n.t("report_created")); toast(i18n.t("report_created"));
} }
} }
async handlePostReport(form: CreatePostReport) { async handlePostReport(form: CreatePostReport) {
const reportRes = await apiWrapper( const reportRes = await HttpService.wrappedClient.createPostReport(form);
HttpService.client.createPostReport(form) if (reportRes.state === "success") {
);
if (reportRes.state == "success") {
toast(i18n.t("report_created")); toast(i18n.t("report_created"));
} }
} }
async handleLockPost(form: LockPost) { async handleLockPost(form: LockPost) {
const lockRes = await apiWrapper(HttpService.client.lockPost(form)); const lockRes = await HttpService.wrappedClient.lockPost(form);
this.findAndUpdatePost(lockRes); this.findAndUpdatePost(lockRes);
} }
async handleDistinguishComment(form: DistinguishComment) { async handleDistinguishComment(form: DistinguishComment) {
const distinguishRes = await apiWrapper( const distinguishRes = await HttpService.wrappedClient.distinguishComment(
HttpService.client.distinguishComment(form) form
); );
this.findAndUpdateComment(distinguishRes); this.findAndUpdateComment(distinguishRes);
} }
async handleAddAdmin(form: AddAdmin) { async handleAddAdmin(form: AddAdmin) {
const addAdminRes = await apiWrapper(HttpService.client.addAdmin(form)); const addAdminRes = await HttpService.wrappedClient.addAdmin(form);
if (addAdminRes.state == "success") { if (addAdminRes.state == "success") {
this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s)); this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
@ -954,45 +927,45 @@ export class Profile extends Component<
} }
async handleTransferCommunity(form: TransferCommunity) { async handleTransferCommunity(form: TransferCommunity) {
await apiWrapper(HttpService.client.transferCommunity(form)); await HttpService.wrappedClient.transferCommunity(form);
toast(i18n.t("transfer_community")); toast(i18n.t("transfer_community"));
} }
async handleCommentReplyRead(form: MarkCommentReplyAsRead) { async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
const readRes = await apiWrapper( const readRes = await HttpService.wrappedClient.markCommentReplyAsRead(
HttpService.client.markCommentReplyAsRead(form) form
); );
this.findAndUpdateCommentReply(readRes); this.findAndUpdateCommentReply(readRes);
} }
async handlePersonMentionRead(form: MarkPersonMentionAsRead) { async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
// TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it. // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
await apiWrapper(HttpService.client.markPersonMentionAsRead(form)); await HttpService.wrappedClient.markPersonMentionAsRead(form);
} }
async handleBanFromCommunity(form: BanFromCommunity) { async handleBanFromCommunity(form: BanFromCommunity) {
const banRes = await apiWrapper(HttpService.client.banFromCommunity(form)); const banRes = await HttpService.wrappedClient.banFromCommunity(form);
this.updateBanFromCommunity(banRes); this.updateBanFromCommunity(banRes);
} }
async handleBanPerson(form: BanPerson) { async handleBanPerson(form: BanPerson) {
const banRes = await apiWrapper(HttpService.client.banPerson(form)); const banRes = await HttpService.wrappedClient.banPerson(form);
this.updateBan(banRes); this.updateBan(banRes);
} }
updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) { updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
// Maybe not necessary // Maybe not necessary
if (banRes.state == "success") { if (banRes.state === "success") {
this.setState(s => { this.setState(s => {
if (s.personRes.state == "success") { if (s.personRes.state == "success") {
s.personRes.data.posts s.personRes.data.posts
.filter(c => c.creator.id == banRes.data.person_view.person.id) .filter(c => c.creator.id === banRes.data.person_view.person.id)
.forEach( .forEach(
c => (c.creator_banned_from_community = banRes.data.banned) c => (c.creator_banned_from_community = banRes.data.banned)
); );
s.personRes.data.comments s.personRes.data.comments
.filter(c => c.creator.id == banRes.data.person_view.person.id) .filter(c => c.creator.id === banRes.data.person_view.person.id)
.forEach( .forEach(
c => (c.creator_banned_from_community = banRes.data.banned) c => (c.creator_banned_from_community = banRes.data.banned)
); );

View file

@ -12,7 +12,6 @@ import { UserService } from "../../services";
import { import {
HttpService, HttpService,
RequestState, RequestState,
apiWrapper,
apiWrapperIso, apiWrapperIso,
} from "../../services/HttpService"; } from "../../services/HttpService";
import { import {
@ -211,22 +210,18 @@ export class RegistrationApplications extends Component<
appsRes: { state: "loading" }, appsRes: { state: "loading" },
}); });
this.setState({ this.setState({
appsRes: await apiWrapper( appsRes: await HttpService.wrappedClient.listRegistrationApplications({
HttpService.client.listRegistrationApplications({ unread_only: unread_only,
unread_only: unread_only, page: this.state.page,
page: this.state.page, limit: fetchLimit,
limit: fetchLimit, auth: myAuthRequired(),
auth: myAuthRequired(), }),
})
),
}); });
} }
async handleApproveApplication(form: ApproveRegistrationApplication) { async handleApproveApplication(form: ApproveRegistrationApplication) {
const approveRes = await apiWrapper( const approveRes =
HttpService.client.approveRegistrationApplication(form) await HttpService.wrappedClient.approveRegistrationApplication(form);
);
this.setState(s => { this.setState(s => {
if (s.appsRes.state == "success" && approveRes.state == "success") { if (s.appsRes.state == "success" && approveRes.state == "success") {
s.appsRes.data.registration_applications = editRegistrationApplication( s.appsRes.data.registration_applications = editRegistrationApplication(

View file

@ -20,11 +20,7 @@ import {
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { InitialFetchRequest } from "../../interfaces"; import { InitialFetchRequest } from "../../interfaces";
import { HttpService, UserService } from "../../services"; import { HttpService, UserService } from "../../services";
import { import { RequestState, apiWrapperIso } from "../../services/HttpService";
RequestState,
apiWrapper,
apiWrapperIso,
} from "../../services/HttpService";
import { import {
amAdmin, amAdmin,
editCommentReport, editCommentReport,
@ -532,36 +528,33 @@ export class Reports extends Component<any, ReportsState> {
}; };
this.setState({ this.setState({
commentReportsRes: await apiWrapper( commentReportsRes: await HttpService.wrappedClient.listCommentReports(
HttpService.client.listCommentReports(form) form
),
postReportsRes: await apiWrapper(
HttpService.client.listPostReports(form)
), ),
postReportsRes: await HttpService.wrappedClient.listPostReports(form),
}); });
if (amAdmin()) { if (amAdmin()) {
this.setState({ this.setState({
messageReportsRes: await apiWrapper( messageReportsRes:
HttpService.client.listPrivateMessageReports(form) await HttpService.wrappedClient.listPrivateMessageReports(form),
),
}); });
} }
} }
async handleResolveCommentReport(form: ResolveCommentReport) { async handleResolveCommentReport(form: ResolveCommentReport) {
const res = await apiWrapper(HttpService.client.resolveCommentReport(form)); const res = await HttpService.wrappedClient.resolveCommentReport(form);
this.findAndUpdateCommentReport(res); this.findAndUpdateCommentReport(res);
} }
async handleResolvePostReport(form: ResolvePostReport) { async handleResolvePostReport(form: ResolvePostReport) {
const res = await apiWrapper(HttpService.client.resolvePostReport(form)); const res = await HttpService.wrappedClient.resolvePostReport(form);
this.findAndUpdatePostReport(res); this.findAndUpdatePostReport(res);
} }
async handleResolvePrivateMessageReport(form: ResolvePrivateMessageReport) { async handleResolvePrivateMessageReport(form: ResolvePrivateMessageReport) {
const res = await apiWrapper( const res = await HttpService.wrappedClient.resolvePrivateMessageReport(
HttpService.client.resolvePrivateMessageReport(form) form
); );
this.findAndUpdatePrivateMessageReport(res); this.findAndUpdatePrivateMessageReport(res);
} }

View file

@ -13,11 +13,7 @@ import {
} from "lemmy-js-client"; } from "lemmy-js-client";
import { i18n, languages } from "../../i18next"; import { i18n, languages } from "../../i18next";
import { UserService } from "../../services"; import { UserService } from "../../services";
import { import { HttpService, RequestState } from "../../services/HttpService";
HttpService,
RequestState,
apiWrapper,
} from "../../services/HttpService";
import { import {
Choice, Choice,
capitalizeFirstLetter, capitalizeFirstLetter,
@ -931,38 +927,31 @@ export class Settings extends Component<any, SettingsState> {
async handleBlockPerson({ value }: Choice) { async handleBlockPerson({ value }: Choice) {
if (value !== "0") { if (value !== "0") {
const res = await apiWrapper( const res = await HttpService.wrappedClient.blockPerson({
HttpService.client.blockPerson({ person_id: Number(value),
person_id: Number(value), block: true,
block: true, auth: myAuthRequired(),
auth: myAuthRequired(), });
})
);
this.personBlock(res); this.personBlock(res);
} }
} }
async handleUnblockPerson(i: { ctx: Settings; recipientId: number }) { async handleUnblockPerson(i: { ctx: Settings; recipientId: number }) {
const res = await apiWrapper( const res = await HttpService.wrappedClient.blockPerson({
HttpService.client.blockPerson({ person_id: i.recipientId,
person_id: i.recipientId, block: false,
block: false, auth: myAuthRequired(),
auth: myAuthRequired(), });
})
);
i.ctx.personBlock(res); i.ctx.personBlock(res);
} }
async handleBlockCommunity({ value }: Choice) { async handleBlockCommunity({ value }: Choice) {
if (value !== "0") { if (value !== "0") {
const res = await apiWrapper( const res = await HttpService.wrappedClient.blockCommunity({
HttpService.client.blockCommunity({ community_id: Number(value),
community_id: Number(value), block: true,
block: true, auth: myAuthRequired(),
auth: myAuthRequired(), });
})
);
this.communityBlock(res); this.communityBlock(res);
} }
} }
@ -970,13 +959,11 @@ export class Settings extends Component<any, SettingsState> {
async handleUnblockCommunity(i: { ctx: Settings; communityId: number }) { async handleUnblockCommunity(i: { ctx: Settings; communityId: number }) {
const auth = myAuth(); const auth = myAuth();
if (auth) { if (auth) {
const res = await apiWrapper( const res = await HttpService.wrappedClient.blockCommunity({
HttpService.client.blockCommunity({ community_id: i.communityId,
community_id: i.communityId, block: false,
block: false, auth: myAuthRequired(),
auth: myAuthRequired(), });
})
);
i.ctx.communityBlock(res); i.ctx.communityBlock(res);
} }
} }
@ -1150,13 +1137,10 @@ export class Settings extends Component<any, SettingsState> {
event.preventDefault(); event.preventDefault();
i.setState({ saveRes: { state: "loading" } }); i.setState({ saveRes: { state: "loading" } });
const saveRes = await apiWrapper( const saveRes = await HttpService.wrappedClient.saveUserSettings({
HttpService.client.saveUserSettings({ ...i.state.saveUserSettingsForm,
...i.state.saveUserSettingsForm, auth: myAuthRequired(),
auth: myAuthRequired(), });
})
);
if (saveRes.state === "success") { if (saveRes.state === "success") {
UserService.Instance.login(saveRes.data); UserService.Instance.login(saveRes.data);
location.reload(); location.reload();
@ -1174,15 +1158,12 @@ export class Settings extends Component<any, SettingsState> {
if (new_password && old_password && new_password_verify) { if (new_password && old_password && new_password_verify) {
i.setState({ changePasswordRes: { state: "loading" } }); i.setState({ changePasswordRes: { state: "loading" } });
const changePasswordRes = await apiWrapper( const changePasswordRes = await HttpService.wrappedClient.changePassword({
HttpService.client.changePassword({ new_password,
new_password, new_password_verify,
new_password_verify, old_password,
old_password, auth: myAuthRequired(),
auth: myAuthRequired(), });
})
);
if (changePasswordRes.state === "success") { if (changePasswordRes.state === "success") {
UserService.Instance.login(changePasswordRes.data); UserService.Instance.login(changePasswordRes.data);
window.scrollTo(0, 0); window.scrollTo(0, 0);
@ -1205,13 +1186,10 @@ export class Settings extends Component<any, SettingsState> {
const password = i.state.deleteAccountForm.password; const password = i.state.deleteAccountForm.password;
if (password) { if (password) {
i.setState({ deleteAccountRes: { state: "loading" } }); i.setState({ deleteAccountRes: { state: "loading" } });
const deleteAccountRes = await apiWrapper( const deleteAccountRes = await HttpService.wrappedClient.deleteAccount({
HttpService.client.deleteAccount({ password,
password, auth: myAuthRequired(),
auth: myAuthRequired(), });
})
);
if (deleteAccountRes.state === "success") { if (deleteAccountRes.state === "success") {
UserService.Instance.logout(); UserService.Instance.logout();
this.context.router.history.replace("/"); this.context.router.history.replace("/");

View file

@ -7,11 +7,7 @@ import {
wsUserOp, wsUserOp,
} from "lemmy-js-client"; } from "lemmy-js-client";
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { import { HttpService, RequestState } from "../../services/HttpService";
HttpService,
RequestState,
apiWrapper,
} from "../../services/HttpService";
import { setIsoData, toast } from "../../utils"; import { setIsoData, toast } from "../../utils";
import { HtmlTags } from "../common/html-tags"; import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon"; import { Spinner } from "../common/icon";
@ -39,11 +35,9 @@ export class VerifyEmail extends Component<any, State> {
}); });
this.setState({ this.setState({
verifyRes: await apiWrapper( verifyRes: await HttpService.wrappedClient.verifyEmail({
HttpService.client.verifyEmail({ token: this.props.match.params.token,
token: this.props.match.params.token, }),
})
),
}); });
if (this.state.verifyRes.state == "success") { if (this.state.verifyRes.state == "success") {

View file

@ -8,7 +8,7 @@ import {
} from "lemmy-js-client"; } from "lemmy-js-client";
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { InitialFetchRequest, PostFormParams } from "../../interfaces"; import { InitialFetchRequest, PostFormParams } from "../../interfaces";
import { HttpService, apiWrapper } from "../../services/HttpService"; import { HttpService } from "../../services/HttpService";
import { import {
Choice, Choice,
QueryParams, QueryParams,
@ -87,13 +87,10 @@ export class CreatePost extends Component<
const auth = myAuth(); const auth = myAuth();
if (communityId) { if (communityId) {
const res = await apiWrapper( const res = await HttpService.wrappedClient.getCommunity({
HttpService.client.getCommunity({ id: communityId,
id: communityId, auth,
auth, });
})
);
if (res.state == "success") { if (res.state == "success") {
this.setState({ this.setState({
selectedCommunityChoice: { selectedCommunityChoice: {
@ -197,7 +194,7 @@ export class CreatePost extends Component<
} }
async handlePostCreate(form: CreatePostI) { async handlePostCreate(form: CreatePostI) {
const res = await apiWrapper(HttpService.client.createPost(form)); const res = await HttpService.wrappedClient.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;

View file

@ -13,11 +13,7 @@ import {
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { PostFormParams } from "../../interfaces"; import { PostFormParams } from "../../interfaces";
import { UserService } from "../../services"; import { UserService } from "../../services";
import { import { HttpService, RequestState } from "../../services/HttpService";
HttpService,
RequestState,
apiWrapper,
} from "../../services/HttpService";
import { import {
Choice, Choice,
archiveTodayUrl, archiveTodayUrl,
@ -547,9 +543,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
if (url && validURL(url)) { if (url && validURL(url)) {
this.setState({ metadataRes: { state: "loading" } }); this.setState({ metadataRes: { state: "loading" } });
this.setState({ this.setState({
metadataRes: await apiWrapper( metadataRes: await HttpService.wrappedClient.getSiteMetadata({ url }),
HttpService.client.getSiteMetadata({ url })
),
}); });
} }
} }
@ -564,18 +558,16 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
if (q && q !== "") { if (q && q !== "") {
this.setState({ suggestedPostsRes: { state: "loading" } }); this.setState({ suggestedPostsRes: { state: "loading" } });
this.setState({ this.setState({
suggestedPostsRes: await apiWrapper( suggestedPostsRes: await HttpService.wrappedClient.search({
HttpService.client.search({ q,
q, type_: "Posts",
type_: "Posts", sort: "TopAll",
sort: "TopAll", listing_type: "All",
listing_type: "All", community_id: this.state.form.community_id,
community_id: this.state.form.community_id, page: 1,
page: 1, limit: trendingFetchLimit,
limit: trendingFetchLimit, auth: myAuth(),
auth: myAuth(), }),
})
),
}); });
} }
} }

View file

@ -27,7 +27,7 @@ import { getExternalHost, getHttpBase } from "../../env";
import { i18n } from "../../i18next"; import { i18n } from "../../i18next";
import { BanType, PostFormParams, PurgeType, VoteType } from "../../interfaces"; import { BanType, PostFormParams, PurgeType, VoteType } from "../../interfaces";
import { UserService } from "../../services"; import { UserService } from "../../services";
import { HttpService, apiWrapper } from "../../services/HttpService"; import { HttpService } from "../../services/HttpService";
import { import {
amAdmin, amAdmin,
amCommunityCreator, amCommunityCreator,
@ -1447,7 +1447,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
} }
async handleEditPost(form: EditPost) { async handleEditPost(form: EditPost) {
const res = await apiWrapper(HttpService.client.editPost(form)); const res = await HttpService.wrappedClient.editPost(form);
if (res.state === "success") { if (res.state === "success") {
this.setState({ showEdit: false }); this.setState({ showEdit: false });

View file

@ -58,7 +58,6 @@ import {
} from "../../interfaces"; } from "../../interfaces";
import { UserService } from "../../services"; import { UserService } from "../../services";
import { import {
apiWrapper,
apiWrapperIso, apiWrapperIso,
HttpService, HttpService,
RequestState, RequestState,
@ -195,24 +194,20 @@ export class Post extends Component<any, PostState> {
const auth = myAuth(); const auth = myAuth();
this.setState({ this.setState({
postRes: await apiWrapper( postRes: await HttpService.wrappedClient.getPost({
HttpService.client.getPost({ id: this.state.postId,
id: this.state.postId, comment_id: this.state.commentId,
comment_id: this.state.commentId, auth,
auth, }),
}) commentsRes: await HttpService.wrappedClient.getComments({
), post_id: this.state.postId,
commentsRes: await apiWrapper( parent_id: this.state.commentId,
HttpService.client.getComments({ max_depth: commentTreeMaxDepth,
post_id: this.state.postId, sort: this.state.commentSort,
parent_id: this.state.commentId, type_: "All",
max_depth: commentTreeMaxDepth, saved_only: false,
sort: this.state.commentSort, auth,
type_: "All", }),
saved_only: false,
auth,
})
),
}); });
setupTippy(); setupTippy();
@ -683,29 +678,25 @@ export class Post extends Component<any, PostState> {
} }
async handleDeleteCommunityClick(form: DeleteCommunity) { async handleDeleteCommunityClick(form: DeleteCommunity) {
const deleteCommunityRes = await apiWrapper( const deleteCommunityRes = await HttpService.wrappedClient.deleteCommunity(
HttpService.client.deleteCommunity(form) form
); );
this.updateCommunity(deleteCommunityRes); this.updateCommunity(deleteCommunityRes);
} }
async handleAddModToCommunity(form: AddModToCommunity) { async handleAddModToCommunity(form: AddModToCommunity) {
const addModRes = await apiWrapper( const addModRes = await HttpService.wrappedClient.addModToCommunity(form);
HttpService.client.addModToCommunity(form)
);
this.updateModerators(addModRes); this.updateModerators(addModRes);
} }
async handleFollow(form: FollowCommunity) { async handleFollow(form: FollowCommunity) {
const followCommunityRes = await apiWrapper( const followCommunityRes = await HttpService.wrappedClient.followCommunity(
HttpService.client.followCommunity(form) form
); );
this.updateCommunity(followCommunityRes); this.updateCommunity(followCommunityRes);
// Update myUserInfo // Update myUserInfo
if (followCommunityRes.state == "success") { if (followCommunityRes.state === "success") {
const communityId = followCommunityRes.data.community_view.community.id; const communityId = followCommunityRes.data.community_view.community.id;
const mui = UserService.Instance.myUserInfo; const mui = UserService.Instance.myUserInfo;
if (mui) { if (mui) {
@ -715,36 +706,31 @@ export class Post extends Component<any, PostState> {
} }
async handlePurgeCommunity(form: PurgeCommunity) { async handlePurgeCommunity(form: PurgeCommunity) {
const purgeCommunityRes = await apiWrapper( const purgeCommunityRes = await HttpService.wrappedClient.purgeCommunity(
HttpService.client.purgeCommunity(form) form
); );
this.purgeItem(purgeCommunityRes); this.purgeItem(purgeCommunityRes);
} }
async handlePurgePerson(form: PurgePerson) { async handlePurgePerson(form: PurgePerson) {
const purgePersonRes = await apiWrapper( const purgePersonRes = await HttpService.wrappedClient.purgePerson(form);
HttpService.client.purgePerson(form)
);
this.purgeItem(purgePersonRes); this.purgeItem(purgePersonRes);
} }
async handlePurgeComment(form: PurgeComment) { async handlePurgeComment(form: PurgeComment) {
const purgeCommentRes = await apiWrapper( const purgeCommentRes = await HttpService.wrappedClient.purgeComment(form);
HttpService.client.purgeComment(form)
);
this.purgeItem(purgeCommentRes); this.purgeItem(purgeCommentRes);
} }
async handlePurgePost(form: PurgePost) { async handlePurgePost(form: PurgePost) {
const purgeRes = await apiWrapper(HttpService.client.purgePost(form)); const purgeRes = await HttpService.wrappedClient.purgePost(form);
this.purgeItem(purgeRes); this.purgeItem(purgeRes);
} }
async handleBlockCommunity(form: BlockCommunity) { async handleBlockCommunity(form: BlockCommunity) {
const blockCommunityRes = await apiWrapper( const blockCommunityRes = await HttpService.wrappedClient.blockCommunity(
HttpService.client.blockCommunity(form) form
); );
// TODO Probably isn't necessary // TODO Probably isn't necessary
this.setState(s => { this.setState(s => {
if ( if (
@ -762,153 +748,133 @@ export class Post extends Component<any, PostState> {
} }
async handleBlockPerson(form: BlockPerson) { async handleBlockPerson(form: BlockPerson) {
const blockPersonRes = await apiWrapper( const blockPersonRes = await HttpService.wrappedClient.blockPerson(form);
HttpService.client.blockPerson(form)
);
if (blockPersonRes.state == "success") { if (blockPersonRes.state == "success") {
updatePersonBlock(blockPersonRes.data); updatePersonBlock(blockPersonRes.data);
} }
} }
async handleModRemoveCommunity(form: RemoveCommunity) { async handleModRemoveCommunity(form: RemoveCommunity) {
const removeCommunityRes = await apiWrapper( const removeCommunityRes = await HttpService.wrappedClient.removeCommunity(
HttpService.client.removeCommunity(form) form
); );
this.updateCommunity(removeCommunityRes); this.updateCommunity(removeCommunityRes);
} }
async handleEditCommunity(form: EditCommunity) { async handleEditCommunity(form: EditCommunity) {
const res = await apiWrapper(HttpService.client.editCommunity(form)); const res = await HttpService.wrappedClient.editCommunity(form);
this.updateCommunity(res); this.updateCommunity(res);
return res; return res;
} }
async handleCreateComment(form: CreateComment) { async handleCreateComment(form: CreateComment) {
const createCommentRes = await apiWrapper( const createCommentRes = await HttpService.wrappedClient.createComment(
HttpService.client.createComment(form) form
); );
this.createAndUpdateComments(createCommentRes); this.createAndUpdateComments(createCommentRes);
return createCommentRes; return createCommentRes;
} }
async handleEditComment(form: EditComment) { async handleEditComment(form: EditComment) {
const editCommentRes = await apiWrapper( const editCommentRes = await HttpService.wrappedClient.editComment(form);
HttpService.client.editComment(form)
);
this.findAndUpdateComment(editCommentRes); this.findAndUpdateComment(editCommentRes);
return editCommentRes; return editCommentRes;
} }
async handleDeleteComment(form: DeleteComment) { async handleDeleteComment(form: DeleteComment) {
const deleteCommentRes = await apiWrapper( const deleteCommentRes = await HttpService.wrappedClient.deleteComment(
HttpService.client.deleteComment(form) form
); );
this.findAndUpdateComment(deleteCommentRes); this.findAndUpdateComment(deleteCommentRes);
} }
async handleDeletePost(form: DeletePost) { async handleDeletePost(form: DeletePost) {
const deleteRes = await apiWrapper(HttpService.client.deletePost(form)); const deleteRes = await HttpService.wrappedClient.deletePost(form);
this.updatePost(deleteRes); this.updatePost(deleteRes);
} }
async handleRemovePost(form: RemovePost) { async handleRemovePost(form: RemovePost) {
const removeRes = await apiWrapper(HttpService.client.removePost(form)); const removeRes = await HttpService.wrappedClient.removePost(form);
this.updatePost(removeRes); this.updatePost(removeRes);
} }
async handleRemoveComment(form: RemoveComment) { async handleRemoveComment(form: RemoveComment) {
const removeCommentRes = await apiWrapper( const removeCommentRes = await HttpService.wrappedClient.removeComment(
HttpService.client.removeComment(form) form
); );
this.findAndUpdateComment(removeCommentRes); this.findAndUpdateComment(removeCommentRes);
} }
async handleSaveComment(form: SaveComment) { async handleSaveComment(form: SaveComment) {
const saveCommentRes = await apiWrapper( const saveCommentRes = await HttpService.wrappedClient.saveComment(form);
HttpService.client.saveComment(form)
);
this.findAndUpdateComment(saveCommentRes); this.findAndUpdateComment(saveCommentRes);
} }
async handleSavePost(form: SavePost) { async handleSavePost(form: SavePost) {
const saveRes = await apiWrapper(HttpService.client.savePost(form)); const saveRes = await HttpService.wrappedClient.savePost(form);
this.updatePost(saveRes); this.updatePost(saveRes);
} }
async handleFeaturePost(form: FeaturePost) { async handleFeaturePost(form: FeaturePost) {
const featureRes = await apiWrapper(HttpService.client.featurePost(form)); const featureRes = await HttpService.wrappedClient.featurePost(form);
this.updatePost(featureRes); this.updatePost(featureRes);
} }
async handleCommentVote(form: CreateCommentLike) { async handleCommentVote(form: CreateCommentLike) {
const voteRes = await apiWrapper(HttpService.client.likeComment(form)); const voteRes = await HttpService.wrappedClient.likeComment(form);
this.findAndUpdateComment(voteRes); this.findAndUpdateComment(voteRes);
} }
async handlePostVote(form: CreatePostLike) { async handlePostVote(form: CreatePostLike) {
const voteRes = await apiWrapper(HttpService.client.likePost(form)); const voteRes = await HttpService.wrappedClient.likePost(form);
this.updatePost(voteRes); this.updatePost(voteRes);
} }
async handleCommentReport(form: CreateCommentReport) { async handleCommentReport(form: CreateCommentReport) {
const reportRes = await apiWrapper( const reportRes = await HttpService.wrappedClient.createCommentReport(form);
HttpService.client.createCommentReport(form)
);
if (reportRes.state == "success") { if (reportRes.state == "success") {
toast(i18n.t("report_created")); toast(i18n.t("report_created"));
} }
} }
async handlePostReport(form: CreatePostReport) { async handlePostReport(form: CreatePostReport) {
const reportRes = await apiWrapper( const reportRes = await HttpService.wrappedClient.createPostReport(form);
HttpService.client.createPostReport(form)
);
if (reportRes.state == "success") { if (reportRes.state == "success") {
toast(i18n.t("report_created")); toast(i18n.t("report_created"));
} }
} }
async handleLockPost(form: LockPost) { async handleLockPost(form: LockPost) {
const lockRes = await apiWrapper(HttpService.client.lockPost(form)); const lockRes = await HttpService.wrappedClient.lockPost(form);
this.updatePost(lockRes); this.updatePost(lockRes);
} }
async handleDistinguishComment(form: DistinguishComment) { async handleDistinguishComment(form: DistinguishComment) {
const distinguishRes = await apiWrapper( const distinguishRes = await HttpService.wrappedClient.distinguishComment(
HttpService.client.distinguishComment(form) form
); );
this.findAndUpdateComment(distinguishRes); this.findAndUpdateComment(distinguishRes);
} }
async handleAddAdmin(form: AddAdmin) { async handleAddAdmin(form: AddAdmin) {
const addAdminRes = await apiWrapper(HttpService.client.addAdmin(form)); const addAdminRes = await HttpService.wrappedClient.addAdmin(form);
if (addAdminRes.state == "success") { if (addAdminRes.state === "success") {
this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s)); this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
} }
} }
async handleTransferCommunity(form: TransferCommunity) { async handleTransferCommunity(form: TransferCommunity) {
const transferCommunityRes = await apiWrapper( const transferCommunityRes =
HttpService.client.transferCommunity(form) await HttpService.wrappedClient.transferCommunity(form);
);
this.updateCommunityFull(transferCommunityRes); this.updateCommunityFull(transferCommunityRes);
} }
async handleFetchChildren(form: GetComments) { async handleFetchChildren(form: GetComments) {
const moreCommentsRes = await apiWrapper( const moreCommentsRes = await HttpService.wrappedClient.getComments(form);
HttpService.client.getComments(form)
);
if ( if (
this.state.commentsRes.state == "success" && this.state.commentsRes.state == "success" &&
moreCommentsRes.state == "success" moreCommentsRes.state == "success"
@ -923,24 +889,24 @@ export class Post extends Component<any, PostState> {
} }
async handleCommentReplyRead(form: MarkCommentReplyAsRead) { async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
const readRes = await apiWrapper( const readRes = await HttpService.wrappedClient.markCommentReplyAsRead(
HttpService.client.markCommentReplyAsRead(form) form
); );
this.findAndUpdateCommentReply(readRes); this.findAndUpdateCommentReply(readRes);
} }
async handlePersonMentionRead(form: MarkPersonMentionAsRead) { async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
// TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it. // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
await apiWrapper(HttpService.client.markPersonMentionAsRead(form)); await HttpService.wrappedClient.markPersonMentionAsRead(form);
} }
async handleBanFromCommunity(form: BanFromCommunity) { async handleBanFromCommunity(form: BanFromCommunity) {
const banRes = await apiWrapper(HttpService.client.banFromCommunity(form)); const banRes = await HttpService.wrappedClient.banFromCommunity(form);
this.updateBan(banRes); this.updateBan(banRes);
} }
async handleBanPerson(form: BanPerson) { async handleBanPerson(form: BanPerson) {
const banRes = await apiWrapper(HttpService.client.banPerson(form)); const banRes = await HttpService.wrappedClient.banPerson(form);
this.updateBan(banRes); this.updateBan(banRes);
} }

View file

@ -11,7 +11,6 @@ import { InitialFetchRequest } from "../../interfaces";
import { import {
HttpService, HttpService,
RequestState, RequestState,
apiWrapper,
apiWrapperIso, apiWrapperIso,
} from "../../services/HttpService"; } from "../../services/HttpService";
import { import {
@ -72,14 +71,12 @@ export class CreatePrivateMessage extends Component<
}); });
this.setState({ this.setState({
recipientRes: await apiWrapper( recipientRes: await HttpService.wrappedClient.getPersonDetails({
HttpService.client.getPersonDetails({ person_id: this.state.recipientId,
person_id: this.state.recipientId, sort: "New",
sort: "New", saved_only: false,
saved_only: false, auth: myAuth(),
auth: myAuth(), }),
})
),
}); });
} }
@ -147,7 +144,7 @@ export class CreatePrivateMessage extends Component<
} }
async handlePrivateMessageCreate(form: CreatePrivateMessageI) { async handlePrivateMessageCreate(form: CreatePrivateMessageI) {
const res = await apiWrapper(HttpService.client.createPrivateMessage(form)); const res = await HttpService.wrappedClient.createPrivateMessage(form);
if (res.state == "success") { if (res.state == "success") {
toast(i18n.t("message_sent")); toast(i18n.t("message_sent"));

View file

@ -25,7 +25,6 @@ import { CommentViewType, InitialFetchRequest } from "../interfaces";
import { import {
HttpService, HttpService,
RequestState, RequestState,
apiWrapper,
apiWrapperIso, apiWrapperIso,
} from "../services/HttpService"; } from "../services/HttpService";
import { import {
@ -322,14 +321,12 @@ export class Search extends Component<any, SearchState> {
async fetchCommunities() { async fetchCommunities() {
this.setState({ communitiesRes: { state: "loading" } }); this.setState({ communitiesRes: { state: "loading" } });
this.setState({ this.setState({
communitiesRes: await apiWrapper( communitiesRes: await HttpService.wrappedClient.listCommunities({
HttpService.client.listCommunities({ type_: defaultListingType,
type_: defaultListingType, sort: defaultSortType,
sort: defaultSortType, limit: fetchLimit,
limit: fetchLimit, auth: myAuth(),
auth: myAuth(), }),
})
),
}); });
} }
@ -879,19 +876,17 @@ export class Search extends Component<any, SearchState> {
if (q && q !== "") { if (q && q !== "") {
this.setState({ searchRes: { state: "loading" } }); this.setState({ searchRes: { state: "loading" } });
this.setState({ this.setState({
searchRes: await apiWrapper( searchRes: await HttpService.wrappedClient.search({
HttpService.client.search({ q,
q, community_id: communityId ?? undefined,
community_id: communityId ?? undefined, creator_id: creatorId ?? undefined,
creator_id: creatorId ?? undefined, type_: type,
type_: type, sort,
sort, listing_type: listingType,
listing_type: listingType, page,
page, limit: fetchLimit,
limit: fetchLimit, auth,
auth, }),
})
),
}); });
window.scrollTo(0, 0); window.scrollTo(0, 0);
restoreScrollPosition(this.context); restoreScrollPosition(this.context);
@ -899,12 +894,10 @@ export class Search extends Component<any, SearchState> {
if (auth) { if (auth) {
this.setState({ resolveObjectRes: { state: "loading" } }); this.setState({ resolveObjectRes: { state: "loading" } });
this.setState({ this.setState({
resolveObjectRes: await apiWrapper( resolveObjectRes: await HttpService.wrappedClient.resolveObject({
HttpService.client.resolveObject({ q,
q, auth,
auth, }),
})
),
}); });
} }
} }

View file

@ -32,6 +32,16 @@ export type RequestState<T> =
| FailedRequestState | FailedRequestState
| SuccessRequestState<T>; | SuccessRequestState<T>;
type WrappedLemmyHttp = {
[K in keyof LemmyHttp]: LemmyHttp[K] extends (...args: any[]) => any
? ReturnType<LemmyHttp[K]> extends Promise<infer U>
? (...args: Parameters<LemmyHttp[K]>) => Promise<RequestState<U>>
: (
...args: Parameters<LemmyHttp[K]>
) => Promise<RequestState<LemmyHttp[K]>>
: LemmyHttp[K];
};
export async function apiWrapper<ResponseType>( export async function apiWrapper<ResponseType>(
req: Promise<ResponseType> req: Promise<ResponseType>
): Promise<RequestState<ResponseType>> { ): Promise<RequestState<ResponseType>> {
@ -51,6 +61,40 @@ export async function apiWrapper<ResponseType>(
} }
} }
class WrappedLemmyHttpClient {
#client: LemmyHttp;
constructor(client: LemmyHttp) {
this.#client = client;
for (const key of Object.getOwnPropertyNames(
Object.getPrototypeOf(this.#client)
)) {
WrappedLemmyHttpClient.prototype[key] = async (...args) => {
try {
const res = await this.#client[key](...args);
return {
data: res,
state: "success",
};
} catch (error) {
console.error(`API error: ${error}`);
toast(i18n.t(error), "danger");
return {
state: "failed",
msg: error,
};
}
};
}
}
}
export function getWrappedClient(client: LemmyHttp) {
return new WrappedLemmyHttpClient(client) as unknown as WrappedLemmyHttp; // unfortunately, this verbose cast is necessary
}
/** /**
* A Special type of apiWrapper, used only for the iso routes. * A Special type of apiWrapper, used only for the iso routes.
* *
@ -75,18 +119,24 @@ export function apiWrapperIso<ResponseType>(
} }
export class HttpService { export class HttpService {
private static _instance: HttpService; static #_instance: HttpService;
private client: LemmyHttp; #client: LemmyHttp;
#wrappedClient: WrappedLemmyHttp;
private constructor() { private constructor() {
this.client = new LemmyHttp(getHttpBase()); this.#client = new LemmyHttp(getHttpBase());
this.#wrappedClient = getWrappedClient(this.#client);
} }
private static get Instance() { static get #Instance() {
return this._instance || (this._instance = new this()); return this.#_instance ?? (this.#_instance = new this());
} }
public static get client() { public static get client() {
return this.Instance.client; return this.#Instance.#client;
}
public static get wrappedClient() {
return this.#Instance.#wrappedClient;
} }
} }

View file

@ -94,7 +94,7 @@ const createClientConfig = (_env, mode) => {
plugins: [ plugins: [
...base.plugins, ...base.plugins,
new ServiceWorkerPlugin({ new ServiceWorkerPlugin({
enableInDevelopment: mode !== "development", enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct
workbox: { workbox: {
modifyURLPrefix: { modifyURLPrefix: {
"/": "/static/", "/": "/static/",

View file

@ -5341,10 +5341,10 @@ leac@^0.6.0:
resolved "https://registry.yarnpkg.com/leac/-/leac-0.6.0.tgz#dcf136e382e666bd2475f44a1096061b70dc0912" resolved "https://registry.yarnpkg.com/leac/-/leac-0.6.0.tgz#dcf136e382e666bd2475f44a1096061b70dc0912"
integrity sha512-y+SqErxb8h7nE/fiEX07jsbuhrpO9lL8eca7/Y1nuWV2moNlXhyd59iDGcRf6moVyDMbmTNzL40SUyrFU/yDpg== integrity sha512-y+SqErxb8h7nE/fiEX07jsbuhrpO9lL8eca7/Y1nuWV2moNlXhyd59iDGcRf6moVyDMbmTNzL40SUyrFU/yDpg==
lemmy-js-client@0.17.2-rc.20: lemmy-js-client@0.17.2-rc.23:
version "0.17.2-rc.20" version "0.17.2-rc.23"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.2-rc.20.tgz#64138cb48fa57f096ee50b33ae18feec10b5f9d7" resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.2-rc.23.tgz#7e3e2d2ba82b721a9dc092e687874eebeb080637"
integrity sha512-Y8jnGSCNNc65LaFOwE/YqQU+o2Q/PrEz2RW7ASo/zjPWxpoBT4tm1lPveXhTsHlhhvMaFvmK1LyzvIdq2AO5ZQ== integrity sha512-MXUg2s9CZbH5+G53RvfM9M/UnPtH+LnnMc05sWRNVkKDnpl1nHudIrbRTqOOJNikTf1sJLxGvUOi8TTDJ3OSdQ==
dependencies: dependencies:
cross-fetch "^3.1.5" cross-fetch "^3.1.5"
form-data "^4.0.0" form-data "^4.0.0"