All files / lib/components/MapTag/Modal/CreateMapTagModal index.tsx

0% Statements 0/18
0% Branches 0/6
0% Functions 0/6
0% Lines 0/18

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148                                                                                                                                                                                                                                                                                                       
import { usePostCreateMapTagMutation } from '@lib/api/mutations';
import { useGetAdventuresQuery } from '@lib/api/queries';
import GoogleMapComponent from '@lib/components/GoogleMap';
import {
  CreateMapTagRequestDto,
  CreateMapTagRequestDtoMapTypeEnum
} from '@uniquegood/realworld-adventure-interface';
import { Form, Input, InputNumber, Modal, ModalProps, Select, Switch, message } from 'antd';
import React from 'react';
import { styled } from 'styled-components';
 
type CreateMapTagModalProps = {
  modalData: ModalProps;
  closeModal: () => unknown;
};
 
interface FormData extends Omit<CreateMapTagRequestDto, 'iconImageUrl'> {
  iconImageUrl: { file: File; fileList: (File & { originFileObj: File })[] };
}
 
export default function CreateMapTagModal({ modalData, closeModal }: CreateMapTagModalProps) {
  const { data: adventures, isLoading } = useGetAdventuresQuery();
  const { mutateAsync: createMapTag } = usePostCreateMapTagMutation();
 
  const [currentType, setCurrentType] = React.useState<CreateMapTagRequestDtoMapTypeEnum>();
 
  const [form] = Form.useForm();
 
  const handleSubmit = async (values: FormData) => {
    try {
      const data = await createMapTag(values);
 
      if (data.success) {
        message.success('맵 태그가 생성되었습니다.');
 
        closeModal();
      }
    } catch (e) {
      console.error(e);
 
      message.error('맵 태그 생성에 실패했습니다.');
    }
  };
 
  return (
    <Modal
      {...modalData}
      title="맵 태그 생성"
      onOk={form.submit}
      okText="확인"
      cancelText="닫기"
      afterClose={() => {
        setCurrentType(undefined);
      }}
    >
      <Form
        form={form}
        onFinish={handleSubmit}
        labelCol={{ span: 24 }}
        wrapperCol={{ span: 24 }}
        preserve={false}
        validateMessages={{ required: '필수 입력 항목입니다.' }}
      >
        <Form.Item name="name" label="이름" rules={[{ required: true }]}>
          <Input placeholder="태그 이름을 입력해주세요." />
        </Form.Item>
        <Form.Item
          name="latitude"
          label="위도"
          rules={[{ required: true }]}
          style={{ display: 'none' }}
        >
          <StyledInputNumber placeholder="위도를 입력해주세요." />
        </Form.Item>
        <Form.Item
          name="longitude"
          label="경도"
          rules={[{ required: true }]}
          style={{ display: 'none' }}
        >
          <StyledInputNumber placeholder="경도를 입력해주세요." />
        </Form.Item>
        <Form.Item name="position" label="위치">
          <GoogleMapComponent
            onCenterChange={({ lat, lng }) => {
              form.setFieldsValue({
                latitude: lat,
                longitude: lng
              });
            }}
          />
        </Form.Item>
        <Form.Item name="mapType" label="맵 타입" rules={[{ required: true }]}>
          <Select
            placeholder="맵 타입을 선택해주세요."
            onChange={setCurrentType}
            options={[
              {
                label: '일반',
                value: 'Common'
              },
              {
                label: '어드벤처',
                value: 'Adventure'
              },
              {
                label: '트레저',
                value: 'Treasure'
              }
            ]}
          />
        </Form.Item>
        <Form.Item name="zoomLevel" label="줌 레벨" rules={[{ required: true }]}>
          <StyledInputNumber placeholder="줌 레벨을 입력해주세요." />
        </Form.Item>
        {currentType === 'Common' && (
          <Form.Item name="modeId" label="연결할 캠페인 또는 축제">
            <Select
              placeholder="연결할 캠페인 또는 축제를 선택해주세요."
              options={adventures?.data?.map((adventure) => ({
                label: adventure.title,
                value: adventure.id
              }))}
              loading={isLoading}
              showSearch
              filterOption={(input, option) => {
                return option?.label?.includes(input) || false;
              }}
            />
          </Form.Item>
        )}
        <Form.Item
          name="isActive"
          label="활성화 여부"
          initialValue={false}
          rules={[{ required: true }]}
        >
          <Switch />
        </Form.Item>
      </Form>
    </Modal>
  );
}
 
const StyledInputNumber = styled(InputNumber)`
  width: 100%;
`;