gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

settings.js (4588B)


      1 /*
      2 	GoToSocial
      3 	Copyright (C) GoToSocial Authors admin@gotosocial.org
      4 	SPDX-License-Identifier: AGPL-3.0-or-later
      5 
      6 	This program is free software: you can redistribute it and/or modify
      7 	it under the terms of the GNU Affero General Public License as published by
      8 	the Free Software Foundation, either version 3 of the License, or
      9 	(at your option) any later version.
     10 
     11 	This program is distributed in the hope that it will be useful,
     12 	but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 	GNU Affero General Public License for more details.
     15 
     16 	You should have received a copy of the GNU Affero General Public License
     17 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
     18 */
     19 
     20 "use strict";
     21 
     22 const React = require("react");
     23 
     24 const query = require("../lib/query");
     25 
     26 const {
     27 	useTextInput,
     28 	useBoolInput
     29 } = require("../lib/form");
     30 
     31 const useFormSubmit = require("../lib/form/submit");
     32 
     33 const {
     34 	Select,
     35 	TextInput,
     36 	Checkbox
     37 } = require("../components/form/inputs");
     38 
     39 const FormWithData = require("../lib/form/form-with-data");
     40 const Languages = require("../components/languages");
     41 const MutationButton = require("../components/form/mutation-button");
     42 
     43 module.exports = function UserSettings() {
     44 	return (
     45 		<FormWithData
     46 			dataQuery={query.useVerifyCredentialsQuery}
     47 			DataForm={UserSettingsForm}
     48 		/>
     49 	);
     50 };
     51 
     52 function UserSettingsForm({ data }) {
     53 	/* form keys
     54 		- string source[privacy]
     55 		- bool source[sensitive]
     56 		- string source[language]
     57 		- string source[status_content_type]
     58 	 */
     59 
     60 	const form = {
     61 		defaultPrivacy: useTextInput("source[privacy]", { source: data, defaultValue: "unlisted" }),
     62 		isSensitive: useBoolInput("source[sensitive]", { source: data }),
     63 		language: useTextInput("source[language]", { source: data, valueSelector: (s) => s.source.language?.toUpperCase() ?? "EN" }),
     64 		statusContentType: useTextInput("source[status_content_type]", { source: data, defaultValue: "text/plain" }),
     65 	};
     66 
     67 	const [submitForm, result] = useFormSubmit(form, query.useUpdateCredentialsMutation());
     68 
     69 	return (
     70 		<>
     71 			<form className="user-settings" onSubmit={submitForm}>
     72 				<h1>Post settings</h1>
     73 				<Select field={form.language} label="Default post language" options={
     74 					<Languages />
     75 				}>
     76 				</Select>
     77 				<Select field={form.defaultPrivacy} label="Default post privacy" options={
     78 					<>
     79 						<option value="private">Private / followers-only</option>
     80 						<option value="unlisted">Unlisted</option>
     81 						<option value="public">Public</option>
     82 					</>
     83 				}>
     84 					<a href="https://docs.gotosocial.org/en/latest/user_guide/posts/#privacy-settings" target="_blank" className="moreinfolink" rel="noreferrer">Learn more about post privacy settings (opens in a new tab)</a>
     85 				</Select>
     86 				<Select field={form.statusContentType} label="Default post (and bio) format" options={
     87 					<>
     88 						<option value="text/plain">Plain (default)</option>
     89 						<option value="text/markdown">Markdown</option>
     90 					</>
     91 				}>
     92 					<a href="https://docs.gotosocial.org/en/latest/user_guide/posts/#input-types" target="_blank" className="moreinfolink" rel="noreferrer">Learn more about post format settings (opens in a new tab)</a>
     93 				</Select>
     94 				<Checkbox
     95 					field={form.isSensitive}
     96 					label="Mark my posts as sensitive by default"
     97 				/>
     98 
     99 				<MutationButton label="Save settings" result={result} />
    100 			</form>
    101 			<div>
    102 				<PasswordChange />
    103 			</div>
    104 		</>
    105 	);
    106 }
    107 
    108 function PasswordChange() {
    109 	const form = {
    110 		oldPassword: useTextInput("old_password"),
    111 		newPassword: useTextInput("new_password", {
    112 			validator(val) {
    113 				if (val != "" && val == form.oldPassword.value) {
    114 					return "New password same as old password";
    115 				}
    116 				return "";
    117 			}
    118 		})
    119 	};
    120 
    121 	const verifyNewPassword = useTextInput("verifyNewPassword", {
    122 		validator(val) {
    123 			if (val != "" && val != form.newPassword.value) {
    124 				return "Passwords do not match";
    125 			}
    126 			return "";
    127 		}
    128 	});
    129 
    130 	const [submitForm, result] = useFormSubmit(form, query.usePasswordChangeMutation());
    131 
    132 	return (
    133 		<form className="change-password" onSubmit={submitForm}>
    134 			<h1>Change password</h1>
    135 			<TextInput
    136 				type="password"
    137 				name="password"
    138 				field={form.oldPassword}
    139 				label="Current password"
    140 			/>
    141 			<TextInput
    142 				type="password"
    143 				name="newPassword"
    144 				field={form.newPassword}
    145 				label="New password"
    146 			/>
    147 			<TextInput
    148 				type="password"
    149 				name="confirmNewPassword"
    150 				field={verifyNewPassword}
    151 				label="Confirm new password"
    152 			/>
    153 			<MutationButton label="Change password" result={result} />
    154 		</form>
    155 	);
    156 }