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

93.5% Statements 72/77
80% Branches 4/5
80% Functions 4/5
93.5% Lines 72/77

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 781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x     1x 9x 9x 9x 9x 9x 9x 9x       9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 2x 2x 2x 9x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x  
import { useState } from 'react';
 
import { useForm } from '@tanstack/react-form';
 
import { Icon } from '@/components/icon';
import { Button, Input, ModalContent, ModalTitle, Toast, useModal } from '@/components/ui';
import { useToast } from '@/components/ui/toast/core';
import { useAddFollowers } from '@/hooks/use-follower';
 
export const FollowingModal = ({ userId }: { userId: number }) => {
  const { close } = useModal();
  const [errorMessage, setErrorMessage] = useState<string | null>(null);
  const { mutateAsync } = useAddFollowers({ userId });
  const { run } = useToast();
  const form = useForm({
    defaultValues: { nickname: '' },
    onSubmit: async ({ value }) => {
      const { nickname } = value;
      setErrorMessage(null);
      try {
        await mutateAsync({ followNickname: nickname });
        close();
        run(<Toast type='success'>{nickname}님을 팔로우 했어요!</Toast>);
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
      } catch (error: any) {
        setErrorMessage(error.detail.slice(4));
      }
    },
  });
 
  return (
    <ModalContent className='max-w-77.75'>
      <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>
  );
};