All files / src/components/pages/message/message-following-modal index.tsx

93.9% Statements 77/82
100% Branches 5/5
71.42% Functions 5/7
93.9% Lines 77/82

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 831x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 7x 7x 7x 7x 7x 7x       7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 2x 7x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x  
import { useState } from 'react';
 
import { useForm } from '@tanstack/react-form';
 
import { Icon } from '@/components/icon';
import { Button, Input, ModalContent, ModalTitle, useModal } from '@/components/ui';
import { useAddFollowers } from '@/hooks/use-follower';
 
export const FollowingModal = ({ userId }: { userId: number }) => {
  const { close } = useModal();
  const [errorMessage, setErrorMessage] = useState<string | null>(null);
  const { mutate: addFollower } = useAddFollowers({ userId });
  const form = useForm({
    defaultValues: {
      nickname: '',
    },
    onSubmit: ({ value }) => {
      const { nickname } = value;
      setErrorMessage(null);
 
      addFollower(
        {
          followNickname: nickname,
        },
        {
          onSuccess: () => {
            close();
          },
          onError: () => {
            setErrorMessage('존재하지 않는 유저입니다.');
          },
        },
      );
    },
  });
  return (
    <ModalContent className='mx-8'>
      <ModalTitle className='mb-3'>팔로우 할 닉네임을 입력하세요</ModalTitle>
      <form
        onSubmit={(e) => {
          e.preventDefault();
          form.handleSubmit();
        }}
      >
        <div className='mb-3'>
          <form.Field
            children={(field) => (
              <Input
                className='text-text-sm-medium w-full rounded-3xl bg-gray-100 px-4 py-2.5 text-gray-800'
                iconButton={
                  <Icon id='search' className='absolute top-2.5 right-3 size-5 text-gray-500' />
                }
                placeholder='nickname'
                value={field.state.value}
                onChange={(e) => {
                  field.handleChange(e.target.value);
                  setErrorMessage(null);
                }}
                onKeyDown={(e) => {
                  if (e.key === 'Enter') {
                    e.preventDefault();
                    form.handleSubmit();
                  }
                }}
              />
            )}
            name='nickname'
          ></form.Field>
        </div>
        {errorMessage && <p className='text-error-500 mb-2 ml-2 text-sm'>{errorMessage}</p>}
        <div className='flex w-full flex-row gap-2'>
          <Button size='sm' type='button' variant='tertiary' onClick={close}>
            취소
          </Button>
          <Button size='sm' type='submit'>
            팔로우
          </Button>
        </div>
      </form>
    </ModalContent>
  );
};