1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00

Fix types and reset input function

This commit is contained in:
vincent 2022-05-04 08:55:51 +02:00
parent e56a68221e
commit 07cb248187
3 changed files with 7 additions and 8 deletions

View File

@ -1,5 +1,5 @@
import React, { useState, useReducer } from 'react';
import { UseFormRegister, UseFormResetField } from 'react-hook-form';
import { UseFormRegister, UseFormSetValue } from 'react-hook-form';
import { FieldValues } from 'react-hook-form/dist/types/fields';
import { User } from '../../models/user';
import { SocialNetwork } from '../../models/social-network';
@ -10,11 +10,11 @@ import { useTranslation } from 'react-i18next';
interface EditSocialsProps<TFieldValues> {
register: UseFormRegister<TFieldValues>,
resetField: UseFormResetField<User>,
setValue: UseFormSetValue<User>,
networks: SocialNetwork[],
}
export const EditSocials = <TFieldValues extends FieldValues>({ register, resetField, networks }: EditSocialsProps<TFieldValues>) => {
export const EditSocials = <TFieldValues extends FieldValues>({ register, setValue, networks }: EditSocialsProps<TFieldValues>) => {
const { t } = useTranslation('shared');
const initSelectedNetworks = networks.filter(el => el.url !== '');
@ -27,7 +27,7 @@ export const EditSocials = <TFieldValues extends FieldValues>({ register, resetF
switch (action.type) {
case 'delete':
setSelectedNetworks(selectedNetworks.filter(el => el !== action.payload.network));
resetField(action.payload.field);
setValue(action.payload.field, '');
return state.map(el => el === action.payload.network
? { ...el, url: '' }
: el);

View File

@ -27,7 +27,7 @@ interface UserProfileFormProps {
export const UserProfileForm: React.FC<UserProfileFormProps> = ({ action, size, user, className, onError }) => {
const { t } = useTranslation('shared');
const { handleSubmit, register, resetField, control, formState } = useForm<User>({ defaultValues: { ...user } });
const { handleSubmit, register, setValue, control, formState } = useForm<User>({ defaultValues: { ...user } });
const output = useWatch<User>({ control });
const [isOrganization, setIsOrganization] = React.useState<boolean>(user.invoicing_profile.organization !== null);
@ -112,7 +112,7 @@ export const UserProfileForm: React.FC<UserProfileFormProps> = ({ action, size,
<h4>{t('app.shared.user_profile_form.account_networks')}</h4>
<EditSocials register={register}
networks={userNetworks}
resetField={resetField} />
setValue={setValue} />
</div>
<div className="organization-data">
<h4>{t('app.shared.user_profile_form.organization_data')}</h4>
@ -139,7 +139,6 @@ export const UserProfileForm: React.FC<UserProfileFormProps> = ({ action, size,
label={t('app.shared.user_profile_form.organization_address')} />
</div>}
</div>
<button type="submit">go</button>
</div>
</form>
);

View File

@ -1,4 +1,5 @@
import { User, UserRole } from '../models/user';
import { supportedNetworks } from '../models/social-network';
export default class UserLib {
private user: User;
@ -25,7 +26,6 @@ export default class UserLib {
*/
getUserSocialNetworks = (customer: User): {name: string, url: string}[] => {
const userNetworks = [];
const supportedNetworks = ['facebook', 'twitter', 'viadeo', 'linkedin', 'instagram', 'youtube', 'vimeo', 'dailymotion', 'github', 'echosciences', 'pinterest', 'lastfm', 'flickr'];
for (const [name, url] of Object.entries(customer.profile)) {
supportedNetworks.includes(name) && userNetworks.push({ name, url });