2021-01-28 12:08:57 +01:00
|
|
|
import React, { useState, useContext, useEffect } from 'react';
|
2021-01-31 10:38:20 +01:00
|
|
|
import TextFieldWithSubmit, {
|
|
|
|
TEXTFIELD_TYPE_TEXTAREA,
|
|
|
|
TEXTFIELD_TYPE_URL,
|
|
|
|
} from './form-textfield-with-submit';
|
2021-01-28 12:08:57 +01:00
|
|
|
|
|
|
|
import { ServerStatusContext } from '../../../utils/server-status-context';
|
2021-01-31 10:38:20 +01:00
|
|
|
import {
|
|
|
|
postConfigUpdateToAPI,
|
|
|
|
TEXTFIELD_PROPS_INSTANCE_URL,
|
2021-02-04 08:51:38 +01:00
|
|
|
TEXTFIELD_PROPS_SERVER_NAME,
|
2021-01-31 10:38:20 +01:00
|
|
|
TEXTFIELD_PROPS_SERVER_SUMMARY,
|
|
|
|
TEXTFIELD_PROPS_LOGO,
|
|
|
|
API_YP_SWITCH,
|
|
|
|
} from './constants';
|
2021-01-28 12:08:57 +01:00
|
|
|
|
2021-01-29 19:26:55 +01:00
|
|
|
import { UpdateArgs } from '../../../types/config-section';
|
2021-01-28 12:08:57 +01:00
|
|
|
|
|
|
|
export default function EditInstanceDetails() {
|
|
|
|
const [formDataValues, setFormDataValues] = useState(null);
|
|
|
|
const serverStatusData = useContext(ServerStatusContext);
|
|
|
|
const { serverConfig } = serverStatusData || {};
|
|
|
|
|
|
|
|
const { instanceDetails, yp } = serverConfig;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setFormDataValues({
|
|
|
|
...instanceDetails,
|
|
|
|
...yp,
|
|
|
|
});
|
|
|
|
}, [instanceDetails, yp]);
|
|
|
|
|
|
|
|
if (!formDataValues) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if instanceUrl is empty, we should also turn OFF the `enabled` field of directory.
|
|
|
|
const handleSubmitInstanceUrl = () => {
|
|
|
|
if (formDataValues.instanceUrl === '') {
|
|
|
|
if (yp.enabled === true) {
|
|
|
|
postConfigUpdateToAPI({
|
|
|
|
apiPath: API_YP_SWITCH,
|
|
|
|
data: { value: false },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 10:38:20 +01:00
|
|
|
};
|
2021-01-28 12:08:57 +01:00
|
|
|
|
2021-01-29 19:26:55 +01:00
|
|
|
const handleFieldChange = ({ fieldName, value }: UpdateArgs) => {
|
2021-01-28 12:08:57 +01:00
|
|
|
setFormDataValues({
|
|
|
|
...formDataValues,
|
|
|
|
[fieldName]: value,
|
|
|
|
});
|
2021-01-31 10:38:20 +01:00
|
|
|
};
|
2021-01-28 12:08:57 +01:00
|
|
|
|
2021-01-31 10:38:20 +01:00
|
|
|
return (
|
2021-02-01 06:36:14 +01:00
|
|
|
<div className={`publicDetailsContainer`}>
|
|
|
|
<div className={`textFieldsSection`}>
|
2021-01-31 09:48:39 +01:00
|
|
|
<TextFieldWithSubmit
|
2021-01-28 12:08:57 +01:00
|
|
|
fieldName="instanceUrl"
|
|
|
|
{...TEXTFIELD_PROPS_INSTANCE_URL}
|
|
|
|
value={formDataValues.instanceUrl}
|
|
|
|
initialValue={yp.instanceUrl}
|
|
|
|
type={TEXTFIELD_TYPE_URL}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
onSubmit={handleSubmitInstanceUrl}
|
|
|
|
/>
|
2021-01-31 10:38:20 +01:00
|
|
|
|
2021-01-31 09:48:39 +01:00
|
|
|
<TextFieldWithSubmit
|
2021-02-04 08:51:38 +01:00
|
|
|
fieldName="name"
|
|
|
|
{...TEXTFIELD_PROPS_SERVER_NAME}
|
|
|
|
value={formDataValues.name}
|
|
|
|
initialValue={instanceDetails.name}
|
2021-01-28 12:08:57 +01:00
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
2021-01-31 09:48:39 +01:00
|
|
|
<TextFieldWithSubmit
|
2021-01-28 12:08:57 +01:00
|
|
|
fieldName="summary"
|
|
|
|
{...TEXTFIELD_PROPS_SERVER_SUMMARY}
|
|
|
|
type={TEXTFIELD_TYPE_TEXTAREA}
|
|
|
|
value={formDataValues.summary}
|
|
|
|
initialValue={instanceDetails.summary}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
2021-01-31 09:48:39 +01:00
|
|
|
<TextFieldWithSubmit
|
2021-01-28 12:08:57 +01:00
|
|
|
fieldName="logo"
|
|
|
|
{...TEXTFIELD_PROPS_LOGO}
|
|
|
|
value={formDataValues.logo}
|
|
|
|
initialValue={instanceDetails.logo}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-01-31 10:38:20 +01:00
|
|
|
</div>
|
|
|
|
);
|
2021-01-28 12:08:57 +01:00
|
|
|
}
|