Skip to content

Commit

Permalink
Removing save and read config hjson. Fixes #695 (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines authored Jun 23, 2022
1 parent 46c6109 commit 75d52f1
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 114 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.4",
"import-sort-style-module": "^6.0.0",
"lemmy-js-client": "0.17.0-rc.31",
"lemmy-js-client": "0.17.0-rc.32",
"lint-staged": "^12.4.1",
"mini-css-extract-plugin": "^2.6.0",
"node-fetch": "^2.6.1",
Expand Down
1 change: 0 additions & 1 deletion src/shared/components/community/community.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ export class Community extends Component<any, State> {
componentWillUnmount() {
saveScrollPosition(this.context);
this.subscription.unsubscribe();
window.isoData.path = undefined;
}

static getDerivedStateFromProps(props: any): CommunityProps {
Expand Down
110 changes: 5 additions & 105 deletions src/shared/components/home/admin-settings.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { None, Option, Some } from "@sniptt/monads";
import { None, Some } from "@sniptt/monads";
import autosize from "autosize";
import { Component, linkEvent } from "inferno";
import {
BannedPersonsResponse,
GetBannedPersons,
GetSiteConfig,
GetSiteConfigResponse,
GetSiteResponse,
PersonViewSafe,
SaveSiteConfig,
SiteResponse,
toUndefined,
UserOperation,
wsJsonToRes,
wsUserOp,
Expand All @@ -37,29 +33,19 @@ import { SiteForm } from "./site-form";

interface AdminSettingsState {
siteRes: GetSiteResponse;
siteConfigRes: Option<GetSiteConfigResponse>;
siteConfigHjson: Option<string>;
banned: PersonViewSafe[];
loading: boolean;
siteConfigLoading: boolean;
leaveAdminTeamLoading: boolean;
}

export class AdminSettings extends Component<any, AdminSettingsState> {
private siteConfigTextAreaId = `site-config-${randomStr()}`;
private isoData = setIsoData(
this.context,
GetSiteConfigResponse,
BannedPersonsResponse
);
private isoData = setIsoData(this.context, BannedPersonsResponse);
private subscription: Subscription;
private emptyState: AdminSettingsState = {
siteRes: this.isoData.site_res,
siteConfigHjson: None,
siteConfigRes: None,
banned: [],
loading: true,
siteConfigLoading: null,
leaveAdminTeamLoading: null,
};

Expand All @@ -73,23 +59,11 @@ export class AdminSettings extends Component<any, AdminSettingsState> {

// Only fetch the data if coming from another route
if (this.isoData.path == this.context.router.route.match.url) {
this.state.siteConfigRes = Some(
this.isoData.routeData[0] as GetSiteConfigResponse
);
this.state.siteConfigHjson = this.state.siteConfigRes.map(
s => s.config_hjson
);
this.state.banned = (
this.isoData.routeData[1] as BannedPersonsResponse
this.isoData.routeData[0] as BannedPersonsResponse
).banned;
this.state.siteConfigLoading = false;
this.state.loading = false;
} else {
WebSocketService.Instance.send(
wsClient.getSiteConfig({
auth: auth().unwrap(),
})
);
WebSocketService.Instance.send(
wsClient.getBannedPersons({
auth: auth().unwrap(),
Expand All @@ -101,9 +75,6 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
let promises: Promise<any>[] = [];

let siteConfigForm = new GetSiteConfig({ auth: req.auth.unwrap() });
promises.push(req.client.getSiteConfig(siteConfigForm));

let bannedPersonsForm = new GetBannedPersons({ auth: req.auth.unwrap() });
promises.push(req.client.getBannedPersons(bannedPersonsForm));

Expand Down Expand Up @@ -155,10 +126,11 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
),
none: <></>,
})}
</div>
<div class="col-12 col-md-6">
{this.admins()}
{this.bannedUsers()}
</div>
<div class="col-12 col-md-6">{this.adminSettings()}</div>
</div>
)}
</div>
Expand Down Expand Up @@ -211,60 +183,6 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
);
}

adminSettings() {
return (
<div>
<h5>{i18n.t("admin_settings")}</h5>
<form onSubmit={linkEvent(this, this.handleSiteConfigSubmit)}>
<div class="form-group row">
<label
class="col-12 col-form-label"
htmlFor={this.siteConfigTextAreaId}
>
{i18n.t("site_config")}
</label>
<div class="col-12">
<textarea
id={this.siteConfigTextAreaId}
value={toUndefined(this.state.siteConfigHjson)}
onInput={linkEvent(this, this.handleSiteConfigHjsonChange)}
class="form-control text-monospace"
rows={3}
/>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<button type="submit" class="btn btn-secondary mr-2">
{this.state.siteConfigLoading ? (
<Spinner />
) : (
capitalizeFirstLetter(i18n.t("save"))
)}
</button>
</div>
</div>
</form>
</div>
);
}

handleSiteConfigSubmit(i: AdminSettings, event: any) {
event.preventDefault();
i.state.siteConfigLoading = true;
let form = new SaveSiteConfig({
config_hjson: toUndefined(i.state.siteConfigHjson),
auth: auth().unwrap(),
});
WebSocketService.Instance.send(wsClient.saveSiteConfig(form));
i.setState(i.state);
}

handleSiteConfigHjsonChange(i: AdminSettings, event: any) {
i.state.siteConfigHjson = event.target.value;
i.setState(i.state);
}

handleLeaveAdminTeam(i: AdminSettings) {
i.state.leaveAdminTeamLoading = true;
WebSocketService.Instance.send(
Expand All @@ -290,17 +208,8 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
} else if (op == UserOperation.GetBannedPersons) {
let data = wsJsonToRes<BannedPersonsResponse>(msg, BannedPersonsResponse);
this.state.banned = data.banned;
this.setState(this.state);
} else if (op == UserOperation.GetSiteConfig) {
let data = wsJsonToRes<GetSiteConfigResponse>(msg, GetSiteConfigResponse);
this.state.siteConfigRes = Some(data);
this.state.loading = false;
this.state.siteConfigHjson = this.state.siteConfigRes.map(
s => s.config_hjson
);
this.setState(this.state);
var textarea: any = document.getElementById(this.siteConfigTextAreaId);
autosize(textarea);
} else if (op == UserOperation.LeaveAdmin) {
let data = wsJsonToRes<GetSiteResponse>(msg, GetSiteResponse);
this.state.siteRes.site_view = data.site_view;
Expand All @@ -309,15 +218,6 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
toast(i18n.t("left_admin_team"));
this.setState(this.state);
this.context.router.history.push("/");
} else if (op == UserOperation.SaveSiteConfig) {
let data = wsJsonToRes<GetSiteConfigResponse>(msg, GetSiteConfigResponse);
this.state.siteConfigRes = Some(data);
this.state.siteConfigHjson = this.state.siteConfigRes.map(
s => s.config_hjson
);
this.state.siteConfigLoading = false;
toast(i18n.t("site_saved"));
this.setState(this.state);
}
}
}
1 change: 0 additions & 1 deletion src/shared/components/home/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ export class Home extends Component<any, HomeState> {
componentWillUnmount() {
saveScrollPosition(this.context);
this.subscription.unsubscribe();
window.isoData.path = undefined;
}

static getDerivedStateFromProps(props: any): HomeProps {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/components/home/site-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class SiteForm extends Component<SiteFormProps, SiteFormState> {
default_theme: Some(site.default_theme),
default_post_listing_type: Some(site.default_post_listing_type),
legal_information: site.legal_information,
auth: auth(false).unwrap(),
auth: undefined,
});
},
none: void 0,
Expand Down
1 change: 0 additions & 1 deletion src/shared/components/post/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ export class Post extends Component<any, PostState> {
this.subscription.unsubscribe();
document.removeEventListener("scroll", this.commentScrollDebounced);

window.isoData.path = undefined;
saveScrollPosition(this.context);
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4948,10 +4948,10 @@ lcid@^1.0.0:
dependencies:
invert-kv "^1.0.0"

[email protected].31:
version "0.17.0-rc.31"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.0-rc.31.tgz#373ad2dcbb1305bd82e7fb13704fbdb8d2f1c438"
integrity sha512-hcjFcOxgplffQullf9HuAGv2ko9wWySrnv+s8FWPPpg4EsixuBjXI+Dh7y0GR/KVs6fRmeXn4YBhR2YdJsBc7A==
[email protected].32:
version "0.17.0-rc.32"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.0-rc.32.tgz#d67f432f1fffc54c267f915278fe260ec554b018"
integrity sha512-qPLybaesu3GVr1DMStsyCYanW4maxHrqX71UHadFMeuh+aUK8taC3zfsLRK9dlIlSDRS283xd8IZkI6ZlcOVEQ==

levn@^0.4.1:
version "0.4.1"
Expand Down

0 comments on commit 75d52f1

Please sign in to comment.