handle all configuration cases

This commit is contained in:
Kire committed 2021-03-22 10:37:10 -04:00
1 parent 8000cc3d3f
commit 97f5c99cc0
12 files changed
+289 -157

No files matched your search

@@ -11,8 +11,10 @@ import { useProfile } from '../../hooks/useProfile';
import ProfileUpdateButton from '../buttons/ProfileUpdateButton';
interface IProps {
handleUpdate: () => void;
profileName: string;
setProfileName: (name: string) => void;
defaultProfileNames: string[];
updateEvent: string;
}
const useStyles = makeStyles((theme) => ({
@@ -28,25 +30,17 @@ const useStyles = makeStyles((theme) => ({
},
}));
export function DefaultProfilePrompt({ defaultProfileNames, updateEvent }: IProps): JSX.Element {
export function DefaultProfilePrompt({
profileName,
setProfileName,
handleUpdate,
defaultProfileNames,
}: IProps): JSX.Element {
const classes = useStyles();
const { t } = useTranslation();
const { profile } = useProfile();
const hasDefaults = defaultProfileNames.length > 0;
const defaultProfileName = hasDefaults ? defaultProfileNames[0] : '';
const [profileName, setProfileName] = useState(defaultProfileName);
const handleUpdate = () => {
const data = {
...profile,
profile_name: profileName,
};
Nui.send(updateEvent, data);
};
const handleChange = (event: React.ChangeEvent<any>): void => {
event.preventDefault();
setProfileName(event.target.value);
};
@@ -65,8 +59,10 @@ export function DefaultProfilePrompt({ defaultProfileNames, updateEvent }: IProp
}}
>
<option value="" />
{defaultProfileNames.map((profileName) => (
<option value={profileName}>{profileName}</option>
{defaultProfileNames.map((name) => (
<option key={name} value={name}>
{name}
</option>
))}
</Select>
</FormControl>
@@ -5,7 +5,12 @@ import { useTranslation } from 'react-i18next';
import Nui from '../../../../os/nui-events/utils/Nui';
import { useProfile } from '../../hooks/useProfile';
import ProfileField from './ProfileField';
import DefaultProfilePrompt from './DefaultProfilePrompt';
import ProfileUpdateButton from '../buttons/ProfileUpdateButton';
import { useRecoilValue } from 'recoil';
import { twitterState } from '../../hooks/state';
import { useSettings } from '../../../settings/hooks/useSettings';
import { usePhone } from '../../../../os/phone/hooks/usePhone';
const useStyles = makeStyles((theme) => ({
root: {
@@ -23,23 +28,43 @@ export function ProfilePrompt() {
const classes = useStyles();
const { t } = useTranslation();
const { profile } = useProfile();
const defaultProfileNames = useRecoilValue(twitterState.defaultProfileNames);
const [profileName, setProfileName] = useState(profile?.profile_name || '');
const { config } = usePhone();
const [name, handleNameChange] = useState(profile.profile_name || '');
const showDefaultProfileNames = !config.twitter.allowEdtiableProfileName && !profile;
const eventName = showDefaultProfileNames
? 'phone:createTwitterProfile'
: 'phone:updateTwitterProfile';
const handleUpdate = () => {
const data = {
Nui.send(eventName, {
...profile,
profile_name: name,
};
Nui.send('phone:updateTwitterProfile', data);
profile_name: profileName,
});
};
// case where profile doesn't exist, couldn't be created automatically
// and the player is not allowed to edit their own profile name
if (showDefaultProfileNames) {
return (
<DefaultProfilePrompt
profileName={profileName}
defaultProfileNames={defaultProfileNames}
setProfileName={setProfileName}
handleUpdate={handleUpdate}
/>
);
}
// case where profile doesn't exist, couldn't be created automatically
// but the player IS allowed to edit their own profile name
return (
<div className={classes.root}>
<ProfileField
label={t('APPS_TWITTER_EDIT_PROFILE_NAME')}
value={name}
handleChange={handleNameChange}
value={profileName}
handleChange={setProfileName}
allowChange
/>
<ProfileUpdateButton handleClick={handleUpdate} />