import {
Choice,
QueryParams,
+ WithPromiseKeys,
enableDownvotes,
enableNsfw,
getIdFromString,
getQueryParams,
- getQueryString,
isBrowser,
myAuth,
setIsoData,
communityId?: number;
}
+interface CreatePostData {
+ communityResponse?: GetCommunityResponse;
+}
+
function getCreatePostQueryParams() {
return getQueryParams<CreatePostProps>({
communityId: getIdFromString,
RouteComponentProps<Record<string, never>>,
CreatePostState
> {
- private isoData = setIsoData(this.context);
+ private isoData = setIsoData<CreatePostData>(this.context);
private subscription?: Subscription;
state: CreatePostState = {
siteRes: this.isoData.site_res,
// Only fetch the data if coming from another route
if (this.isoData.path === this.context.router.route.match.url) {
- const communityRes = this.isoData.routeData[0] as
- | GetCommunityResponse
- | undefined;
+ const { communityResponse } = this.isoData.routeData;
- if (communityRes) {
+ if (communityResponse) {
const communityChoice: Choice = {
- label: communityResponse.community_view.community.name,
- label: communityRes.community_view.community.title,
- value: communityRes.community_view.community.id.toString(),
++ label: communityResponse.community_view.community.title,
+ value: communityResponse.community_view.community.id.toString(),
};
this.state = {
updateUrl({ communityId }: Partial<CreatePostProps>) {
const { communityId: urlCommunityId } = getCreatePostQueryParams();
- const queryParams: QueryParams<CreatePostProps> = {
- communityId: (communityId ?? urlCommunityId)?.toString(),
- };
-
const locationState = this.props.history.location.state as
| PostFormParams
| undefined;
- this.props.history.replace(
- `/create_post${getQueryString(queryParams)}`,
- locationState
- );
+ const url = new URL(location.href);
+
+ const newId = (communityId ?? urlCommunityId)?.toString();
+
+ if (newId !== undefined) {
+ url.searchParams.set("communityId", newId);
+ } else {
+ url.searchParams.delete("communityId");
+ }
+
+ history.replaceState(locationState, "", url);
this.fetchCommunity();
}
client,
query: { communityId },
auth,
- }: InitialFetchRequest<QueryParams<CreatePostProps>>): Promise<any>[] {
- const promises: Promise<any>[] = [];
+ }: InitialFetchRequest<
+ QueryParams<CreatePostProps>
+ >): WithPromiseKeys<CreatePostData> {
+ const data: WithPromiseKeys<CreatePostData> = {};
if (communityId) {
const form: GetCommunity = {
id: getIdFromString(communityId),
};
- promises.push(client.getCommunity(form));
- } else {
- promises.push(Promise.resolve());
+ data.communityResponse = client.getCommunity(form);
}
- return promises;
+ return data;
}
parseMessage(msg: any) {
if (op === UserOperation.GetCommunity) {
const {
community_view: {
- community: { name, id },
+ community: { title, id },
},
} = wsJsonToRes<GetCommunityResponse>(msg);
this.setState({
- selectedCommunityChoice: { label: name, value: id.toString() },
+ selectedCommunityChoice: { label: title, value: id.toString() },
loading: false,
});
}