import React, { useState, useReducer } from 'react'; import { UseFormRegister, UseFormResetField } from 'react-hook-form'; import { FieldValues } from 'react-hook-form/dist/types/fields'; import { User } from '../../models/user'; import { SocialNetwork } from '../../models/social-network'; import Icons from '../../../../images/social-icons.svg'; import { FormInput } from '../form/form-input'; import { Trash } from 'phosphor-react'; import { useTranslation } from 'react-i18next'; interface EditSocialsProps { register: UseFormRegister, resetField: UseFormResetField, networks: SocialNetwork[], } export const EditSocials = ({ register, resetField, networks }: EditSocialsProps) => { const { t } = useTranslation('shared'); const initSelectedNetworks = networks.filter(el => el.url !== ''); const [selectedNetworks, setSelectedNetworks] = useState(initSelectedNetworks); const selectNetwork = (network) => { setSelectedNetworks([...selectedNetworks, network]); }; const reducer = (state, action) => { switch (action.type) { case 'delete': setSelectedNetworks(selectedNetworks.filter(el => el !== action.payload.network)); resetField(action.payload.field); return state.map(el => el === action.payload.network ? { ...el, url: '' } : el); case 'update': return state.map(el => el === action.payload ? { ...el, url: action.payload.url } : el); default: return state; } }; const [userNetworks, dispatch] = useReducer(reducer, networks); return ( <>
{userNetworks.map((network, index) => !selectedNetworks.includes(network) && selectNetwork(network)}> )}
{userNetworks.map((network, index) => selectedNetworks.includes(network) && } addOn={} addOnAction={() => dispatch({ type: 'delete', payload: { network, field: `profile.${network.name}` } })} /> )}
); };