Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Обновление форм с настройками серверов #178

Merged
merged 13 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/renderer/src/components/Sidebar/Loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ export const Loader: React.FC<FlasherProps> = ({ compilerData }) => {

<FlasherSelectModal
isOpen={isFlasherModalOpen}
isLocal={flasherIsLocal}
handleLocal={handleLocalFlasher}
handleRemote={handleRemoteFlasher}
onClose={closeFlasherModal}
Expand Down
32 changes: 15 additions & 17 deletions src/renderer/src/components/serverSelect/DocSelectModal.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// TODO: нужно как-то объединить файлы FlasherSelectModal.tsx, ServerSelectModal.tsx, DocSelectModal.tsx, чтобы уменьшить повторения кода

import { useForm } from 'react-hook-form';
import { twMerge } from 'tailwind-merge';

import { Modal, TextInput } from '@renderer/components/UI';

import { Settings } from '../Modules/Settings';

interface DocSelectModalProps {
Expand All @@ -19,30 +21,19 @@ interface DocSelectModalProps {

interface formValues {
// текущее значение поля ввода для адреса
inputHost: string;
host: string;
}

export const DocSelectModal: React.FC<DocSelectModalProps> = ({
onClose,
handleCustom: handleCustom,
...props
}) => {
const {
register,
handleSubmit: hookHandleSubmit,
setValue,
} = useForm<formValues>({
defaultValues: async () => {
return Settings.get(props.electronSettingsKey).then((server) => {
return {
inputHost: server.host ?? '',
};
});
},
});
const { register, handleSubmit: hookHandleSubmit, setValue, reset } = useForm<formValues>({});

// текущий адрес к которому подключен клиент
const handleSubmit = hookHandleSubmit((data) => {
handleCustom(String(data.inputHost));
handleCustom(String(data.host));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleSubmit

onRequestClose();
});

Expand All @@ -51,7 +42,13 @@ export const DocSelectModal: React.FC<DocSelectModalProps> = ({
};

const handleReturnOriginalValues = () => {
setValue('inputHost', props.originaltHostValue);
setValue('host', props.originaltHostValue);
};

const onAfterOpen = () => {
Settings.get(props.electronSettingsKey).then((server) => {
reset({ host: server.host ?? '' });
});
};

return (
Expand All @@ -61,11 +58,12 @@ export const DocSelectModal: React.FC<DocSelectModalProps> = ({
title={props.topTitle}
submitLabel="Подключиться"
onSubmit={handleSubmit}
onAfterOpen={onAfterOpen}
>
<div className={twMerge('flex')}>
<TextInput
maxLength={80}
{...register('inputHost')}
{...register('host')}
label="Адрес:"
placeholder="Напишите адрес"
isHidden={false}
Expand Down
31 changes: 17 additions & 14 deletions src/renderer/src/components/serverSelect/FlasherSelectModal.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Controller, useForm } from 'react-hook-form';

import { Select } from '@renderer/components/UI';
import { Select, Modal, TextInput } from '@renderer/components/UI';

import { Settings } from '../Modules/Settings';
import { Modal, TextInput } from '@renderer/components/UI';

const SELECT_LOCAL = 'local';
const SELECT_REMOTE = 'remote';
Expand All @@ -15,6 +14,7 @@ const options = [

interface FlasherSelectModalProps {
isOpen: boolean;
isLocal: boolean;
onClose: () => void;
handleLocal: () => void;
handleRemote: (host: string, port: number) => void;
Expand All @@ -30,25 +30,16 @@ export const FlasherSelectModal: React.FC<FlasherSelectModalProps> = ({
onClose,
handleLocal,
handleRemote,
isLocal,
...props
}) => {
const {
register,
control,
handleSubmit: hookHandleSubmit,
watch,
} = useForm<formValues>({
defaultValues: async () => {
return Settings.getFlasherSettings().then((server) => {
return {
host: String(server.host),
port: Number(server.port),
flasherType: SELECT_REMOTE,
};
});
},
});

reset,
} = useForm<formValues>({});
// октрыта ли опция выбора локального загрузчика
const showSecondaryField = watch('flasherType') === SELECT_REMOTE;

Expand All @@ -65,13 +56,24 @@ export const FlasherSelectModal: React.FC<FlasherSelectModalProps> = ({
onClose();
};

const currentServer = () => {
return `Текущий тип сервера: ${isLocal ? 'локальный' : 'удалённый'}`;
};

const onAfterOpen = () => {
Settings.getFlasherSettings().then((server) => {
reset({ host: String(server.host), port: Number(server.port), flasherType: SELECT_REMOTE });
});
};

return (
<Modal
{...props}
onRequestClose={onRequestClose}
title={'Выберите загрузчик'}
submitLabel="Подключиться"
onSubmit={handleSubmit}
onAfterOpen={onAfterOpen}
>
<div className="flex items-center">
<Controller
Expand Down Expand Up @@ -121,6 +123,7 @@ export const FlasherSelectModal: React.FC<FlasherSelectModalProps> = ({
disabled={!showSecondaryField}
/>
</div>
<div> {currentServer()}</div>
</Modal>
);
};
37 changes: 16 additions & 21 deletions src/renderer/src/components/serverSelect/ServerSelectModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useForm } from 'react-hook-form';

import { Modal, TextInput } from '@renderer/components/UI';

import { Settings } from '../Modules/Settings';

interface ServerSelectModalProps {
Expand All @@ -21,33 +22,20 @@ interface ServerSelectModalProps {

interface formValues {
// текущее значение поля ввода для хоста
inputHost: string;
host: string;
// текущее значение поля ввода для порта
inputPort: string;
port: string;
}

export const ServerSelectModal: React.FC<ServerSelectModalProps> = ({
onClose,
handleCustom: handleCustom,
...props
}) => {
const {
handleSubmit: hookHandleSubmit,
setValue,
register,
} = useForm<formValues>({
defaultValues: async () => {
return Settings.get(props.electronSettingsKey).then((server) => {
return {
inputHost: server.host,
inputPort: server.port,
};
});
},
});
const { handleSubmit: hookHandleSubmit, setValue, register, reset } = useForm<formValues>({});

const handleSubmit = hookHandleSubmit((data) => {
handleCustom(data.inputHost, Number(data.inputPort));
handleCustom(data.host, Number(data.port));
onRequestClose();
});

Expand All @@ -56,8 +44,14 @@ export const ServerSelectModal: React.FC<ServerSelectModalProps> = ({
};

const handleReturnOriginalValues = () => {
setValue('inputHost', props.originaltHostValue);
setValue('inputPort', props.originaltPortValue);
setValue('host', props.originaltHostValue);
setValue('port', props.originaltPortValue);
};

const onAfterOpen = () => {
Settings.get(props.electronSettingsKey).then((server) => {
reset({ host: server.host, port: server.port });
});
};

return (
Expand All @@ -67,19 +61,20 @@ export const ServerSelectModal: React.FC<ServerSelectModalProps> = ({
title={props.topTitle}
submitLabel="Подключиться"
onSubmit={handleSubmit}
onAfterOpen={onAfterOpen}
>
<div className={'flex'}>
<TextInput
maxLength={80}
{...register('inputHost')}
{...register('host')}
label="Хост:"
placeholder="Напишите адрес хоста"
isHidden={false}
error={false}
errorMessage={''}
/>
<TextInput
{...register('inputPort')}
{...register('port')}
label="Порт:"
placeholder="Напишите порт"
isHidden={false}
Expand Down