Files
astuto/app/javascript/components/SiteSettings/General/GeneralSiteSettingsP.tsx

252 lines
8.7 KiB
TypeScript
Raw Normal View History

2022-07-18 10:47:54 +02:00
import * as React from 'react';
2022-07-22 16:50:36 +02:00
import { useForm, SubmitHandler } from 'react-hook-form';
2022-07-18 10:47:54 +02:00
import I18n from 'i18n-js';
import Box from '../../common/Box';
import SiteSettingsInfoBox from '../../common/SiteSettingsInfoBox';
import Button from '../../common/Button';
import HttpStatus from '../../../constants/http_status';
import {
TENANT_SETTING_BRAND_DISPLAY_NAME_AND_LOGO,
TENANT_SETTING_BRAND_DISPLAY_NAME_ONLY,
TENANT_SETTING_BRAND_DISPLAY_LOGO_ONLY,
TENANT_SETTING_BRAND_DISPLAY_NONE,
TENANT_SETTING_COLLAPSE_BOARDS_IN_HEADER_NO_COLLAPSE,
TENANT_SETTING_COLLAPSE_BOARDS_IN_HEADER_ALWAYS_COLLAPSE,
} from '../../../interfaces/ITenantSetting';
import { DangerText, SmallMutedText } from '../../common/CustomTexts';
import { getLabel, getValidationMessage } from '../../../helpers/formUtils';
import IBoardJSON from '../../../interfaces/json/IBoard';
2022-07-22 16:50:36 +02:00
export interface ISiteSettingsGeneralForm {
siteName: string;
siteLogo: string;
brandDisplaySetting: string;
locale: string;
showVoteCount: boolean;
showVoteButtonInBoard: boolean;
rootBoardId?: string;
showRoadmapInHeader: boolean;
collapseBoardsInHeader: string;
2022-07-22 16:50:36 +02:00
}
2022-07-18 10:47:54 +02:00
interface Props {
originForm: ISiteSettingsGeneralForm;
boards: IBoardJSON[];
2022-07-18 10:47:54 +02:00
authenticityToken: string;
areUpdating: boolean;
error: string;
updateTenant(
siteName: string,
siteLogo: string,
brandDisplaySetting: string,
locale: string,
rootBoardId: number,
showRoadmapInHeader: boolean,
collapseBoardsInHeader: string,
showVoteCount: boolean,
showVoteButtonInBoard: boolean,
2022-07-18 10:47:54 +02:00
authenticityToken: string
): Promise<any>;
}
2022-07-22 16:50:36 +02:00
const GeneralSiteSettingsP = ({
originForm,
boards,
2022-07-22 16:50:36 +02:00
authenticityToken,
areUpdating,
error,
updateTenant,
}: Props) => {
const {
register,
handleSubmit,
formState: { isDirty, isSubmitSuccessful, errors }
} = useForm<ISiteSettingsGeneralForm>({
defaultValues: {
siteName: originForm.siteName,
siteLogo: originForm.siteLogo,
brandDisplaySetting: originForm.brandDisplaySetting,
locale: originForm.locale,
showVoteCount: originForm.showVoteCount,
showVoteButtonInBoard: originForm.showVoteButtonInBoard,
rootBoardId: originForm.rootBoardId,
showRoadmapInHeader: originForm.showRoadmapInHeader,
collapseBoardsInHeader: originForm.collapseBoardsInHeader,
2022-07-22 16:50:36 +02:00
},
});
const onSubmit: SubmitHandler<ISiteSettingsGeneralForm> = data => {
updateTenant(
data.siteName,
data.siteLogo,
data.brandDisplaySetting,
data.locale,
Number(data.rootBoardId),
data.showRoadmapInHeader,
data.collapseBoardsInHeader,
data.showVoteCount,
data.showVoteButtonInBoard,
authenticityToken
2022-07-18 10:47:54 +02:00
).then(res => {
if (res?.status !== HttpStatus.OK) return;
window.location.reload();
});
2022-07-22 16:50:36 +02:00
};
return (
<>
<Box>
<h2>{ I18n.t('site_settings.general.title') }</h2>
<form onSubmit={handleSubmit(onSubmit)}>
<div className="formRow">
<div className="formGroup col-4">
<label htmlFor="siteName">{ getLabel('tenant', 'site_name') }</label>
2022-07-22 16:50:36 +02:00
<input
{...register('siteName', { required: true })}
id="siteName"
className="formControl"
/>
<DangerText>{errors.siteName && getValidationMessage(errors.siteName.type, 'tenant', 'site_name')}</DangerText>
2022-07-18 10:47:54 +02:00
</div>
2022-07-22 16:50:36 +02:00
<div className="formGroup col-4">
<label htmlFor="siteLogo">{ getLabel('tenant', 'site_logo') }</label>
2022-07-22 16:50:36 +02:00
<input
{...register('siteLogo')}
id="siteLogo"
className="formControl"
/>
</div>
<div className="formGroup col-4">
<label htmlFor="brandSetting">{ getLabel('tenant_setting', 'brand_display') }</label>
2022-07-18 10:47:54 +02:00
<select
2022-07-22 16:50:36 +02:00
{...register('brandDisplaySetting')}
id="brandSetting"
2022-07-18 10:47:54 +02:00
className="selectPicker"
>
<option value={TENANT_SETTING_BRAND_DISPLAY_NAME_AND_LOGO}>
2022-07-22 16:50:36 +02:00
{ I18n.t('site_settings.general.brand_setting_both') }
</option>
<option value={TENANT_SETTING_BRAND_DISPLAY_NAME_ONLY}>
2022-07-22 16:50:36 +02:00
{ I18n.t('site_settings.general.brand_setting_name') }
</option>
<option value={TENANT_SETTING_BRAND_DISPLAY_LOGO_ONLY}>
2022-07-22 16:50:36 +02:00
{ I18n.t('site_settings.general.brand_setting_logo') }
</option>
<option value={TENANT_SETTING_BRAND_DISPLAY_NONE}>
2022-07-22 16:50:36 +02:00
{ I18n.t('site_settings.general.brand_setting_none') }
</option>
2022-07-18 10:47:54 +02:00
</select>
</div>
2022-07-22 16:50:36 +02:00
</div>
<div className="formGroup">
<label htmlFor="locale">{ getLabel('tenant', 'locale') }</label>
2022-07-22 16:50:36 +02:00
<select
{...register('locale')}
id="locale"
className="selectPicker"
>
<option value="en">🇬🇧 English</option>
<option value="it">🇮🇹 Italiano</option>
<option value="de">🇩🇪 Deutsch</option>
<option value="fr">🇫🇷 Français</option>
2024-01-06 17:13:56 +01:00
<option value="es">🇪🇸 Español</option>
<option value="ru">🇷🇺 Русский</option>
2022-07-22 16:50:36 +02:00
</select>
</div>
2022-07-18 10:47:54 +02:00
<div className="formGroup">
<label htmlFor="rootBoardId">{ getLabel('tenant_setting', 'root_board_id') }</label>
<select
{...register('rootBoardId')}
id="rootBoardId"
className="selectPicker"
>
<option value="0">
{I18n.t('roadmap.title')}
</option>
<optgroup label={getLabel('board')}>
{boards.map((board, i) => (
<option value={board.id} key={i}>{board.name}</option>
))}
</optgroup>
</select>
</div>
<br />
<h4>{ I18n.t('site_settings.general.subtitle_header') }</h4>
<div className="formGroup">
<div className="checkboxSwitch">
<input {...register('showRoadmapInHeader')} type="checkbox" id="show_roadmap_in_header" />
<label htmlFor="show_roadmap_in_header">{ getLabel('tenant_setting', 'show_roadmap_in_header') }</label>
</div>
</div>
<br />
<div className="formGroup">
<label htmlFor="collapseBoardsInHeader">{ getLabel('tenant_setting', 'collapse_boards_in_header') }</label>
<select
{...register('collapseBoardsInHeader')}
id="collapseBoardsInHeader"
className="selectPicker"
>
<option value={TENANT_SETTING_COLLAPSE_BOARDS_IN_HEADER_NO_COLLAPSE}>
{ I18n.t('site_settings.general.collapse_boards_in_header_no_collapse') }
</option>
<option value={TENANT_SETTING_COLLAPSE_BOARDS_IN_HEADER_ALWAYS_COLLAPSE}>
{ I18n.t('site_settings.general.collapse_boards_in_header_always_collapse') }
</option>
</select>
</div>
<br />
<h4>{ I18n.t('site_settings.general.subtitle_visibility') }</h4>
<div className="formGroup">
<div className="checkboxSwitch">
<input {...register('showVoteCount')} type="checkbox" id="show_vote_count_checkbox" />
<label htmlFor="show_vote_count_checkbox">{ getLabel('tenant_setting', 'show_vote_count') }</label>
<SmallMutedText>
{ I18n.t('site_settings.general.show_vote_count_help') }
</SmallMutedText>
</div>
</div>
<br />
<div className="formGroup">
<div className="checkboxSwitch">
<input {...register('showVoteButtonInBoard')} type="checkbox" id="show_vote_button_in_board_checkbox" />
<label htmlFor="show_vote_button_in_board_checkbox">{ getLabel('tenant_setting', 'show_vote_button_in_board') }</label>
<SmallMutedText>
{ I18n.t('site_settings.general.show_vote_button_in_board_help') }
</SmallMutedText>
</div>
</div>
2022-07-18 10:47:54 +02:00
<br />
2022-07-22 16:50:36 +02:00
<Button onClick={() => null} disabled={!isDirty}>
{I18n.t('common.buttons.update')}
2022-07-18 10:47:54 +02:00
</Button>
2022-07-22 16:50:36 +02:00
</form>
</Box>
<SiteSettingsInfoBox
areUpdating={areUpdating}
error={error}
areDirty={isDirty && !isSubmitSuccessful}
/>
</>
);
2022-07-18 10:47:54 +02:00
}
export default GeneralSiteSettingsP;