|
| 1 | +import { |
| 2 | + useCallback, |
| 3 | + useId, |
| 4 | +} from 'react'; |
| 5 | +import { |
| 6 | + isDefined, |
| 7 | + isNotDefined, |
| 8 | +} from '@togglecorp/fujs'; |
| 9 | +import { ulid } from 'ulid'; |
| 10 | +import { gql } from 'urql'; |
| 11 | + |
| 12 | +import TutorialAssetPreview from '#components/domain/TutorialAssetPreview'; |
| 13 | +import FileInput from '#components/FileInput'; |
| 14 | +import InputContainerLayout, { Props as InputContainerLayoutProps } from '#components/InputContainerLayout'; |
| 15 | +import { |
| 16 | + AssetMimetypeEnum, |
| 17 | + useCreateTutorialAssetMutation, |
| 18 | +} from '#generated/types/graphql'; |
| 19 | +import { OPERATION_INFO_FRAGMENT } from '#utils/query'; |
| 20 | + |
| 21 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 22 | +const CREATE_tutorial_ASSET_MUTATION = gql` |
| 23 | +${OPERATION_INFO_FRAGMENT} |
| 24 | +mutation CreateTutorialAsset($data: TutorialAssetCreateInput!) { |
| 25 | + createTutorialAsset(data: $data) { |
| 26 | + ... on TutorialAssetTypeMutationResponseType { |
| 27 | + __typename |
| 28 | + errors |
| 29 | + ok |
| 30 | + result { |
| 31 | + id |
| 32 | + } |
| 33 | + } |
| 34 | + ... on OperationInfo { |
| 35 | + ...OperationInfoFields |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | +`; |
| 40 | + |
| 41 | +interface Props<NAME> extends Omit<InputContainerLayoutProps, 'children' | 'inputId'> { |
| 42 | + name: NAME, |
| 43 | + tutorialId: string; |
| 44 | + value: string | undefined | null; |
| 45 | + onChange: (newValue: string | undefined, name: NAME) => void; |
| 46 | + selectFileButtonLabel?: React.ReactNode; |
| 47 | + className?: string; |
| 48 | + disabled?: boolean; |
| 49 | + inputType?: 'geojson' | 'image'; |
| 50 | + withoutPreview?: boolean; |
| 51 | +} |
| 52 | + |
| 53 | +function TutorialAssetInput<const NAME>(props: Props<NAME>) { |
| 54 | + const { |
| 55 | + className, |
| 56 | + name, |
| 57 | + tutorialId, |
| 58 | + value, |
| 59 | + onChange, |
| 60 | + disabled, |
| 61 | + inputType = 'geojson', |
| 62 | + selectFileButtonLabel = inputType === 'image' |
| 63 | + ? 'Select an image' |
| 64 | + : 'Select geojson', |
| 65 | + withoutPreview, |
| 66 | + ...inputLayoutContainerProps |
| 67 | + } = props; |
| 68 | + |
| 69 | + const inputId = useId(); |
| 70 | + const [ |
| 71 | + { fetching: createTutorialAssetPending }, |
| 72 | + createTutorialAsset, |
| 73 | + ] = useCreateTutorialAssetMutation(); |
| 74 | + |
| 75 | + const handleFileInputChange = useCallback(async (file: File | undefined) => { |
| 76 | + if (file) { |
| 77 | + const { type } = file; |
| 78 | + const mimetypeEnumMap: Record<string, AssetMimetypeEnum> = { |
| 79 | + 'image/jpeg': AssetMimetypeEnum.ImageJpeg, |
| 80 | + 'image/png': AssetMimetypeEnum.ImagePng, |
| 81 | + 'image/gif': AssetMimetypeEnum.ImageGif, |
| 82 | + 'application/geo+json': AssetMimetypeEnum.Geojson, |
| 83 | + }; |
| 84 | + |
| 85 | + const selectedEnum = mimetypeEnumMap[type]; |
| 86 | + |
| 87 | + if (isNotDefined(selectedEnum)) { |
| 88 | + // eslint-disable-next-line no-console |
| 89 | + console.error('Invalid file selected!'); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + const result = await createTutorialAsset({ |
| 94 | + data: { |
| 95 | + clientId: ulid(), |
| 96 | + file, |
| 97 | + mimetype: selectedEnum, |
| 98 | + tutorial: tutorialId, |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + if ( |
| 103 | + // eslint-disable-next-line no-underscore-dangle |
| 104 | + result.data?.createTutorialAsset.__typename === 'TutorialAssetTypeMutationResponseType' |
| 105 | + && result.data.createTutorialAsset.ok |
| 106 | + && result.data.createTutorialAsset.result |
| 107 | + ) { |
| 108 | + onChange(result.data.createTutorialAsset.result.id, name); |
| 109 | + } else { |
| 110 | + // eslint-disable-next-line no-underscore-dangle |
| 111 | + const errorMessage = result.data |
| 112 | + ?.createTutorialAsset?.__typename === 'OperationInfo' |
| 113 | + ? result.data.createTutorialAsset.messages |
| 114 | + : result.data?.createTutorialAsset?.errors?.[0]?.message |
| 115 | + || 'Failed to upload file. Please try again.'; |
| 116 | + // eslint-disable-next-line no-console |
| 117 | + console.error(errorMessage); |
| 118 | + } |
| 119 | + } |
| 120 | + }, [createTutorialAsset, tutorialId, onChange, name]); |
| 121 | + |
| 122 | + return ( |
| 123 | + <InputContainerLayout |
| 124 | + inputId={inputId} |
| 125 | + className={className} |
| 126 | + // eslint-disable-next-line react/jsx-props-no-spreading |
| 127 | + {...inputLayoutContainerProps} |
| 128 | + > |
| 129 | + <FileInput |
| 130 | + name={undefined} |
| 131 | + inputId={inputId} |
| 132 | + type="file" |
| 133 | + value={undefined} |
| 134 | + onChange={handleFileInputChange} |
| 135 | + accept={inputType === 'geojson' ? '.geojson' : 'image/png, image/gif, image/jpeg'} |
| 136 | + disabled={disabled || createTutorialAssetPending} |
| 137 | + selectButtonLabel={selectFileButtonLabel} |
| 138 | + status={isDefined(value) ? '1 file selected' : 'No file selected'} |
| 139 | + > |
| 140 | + {!withoutPreview && isDefined(value) && ( |
| 141 | + <TutorialAssetPreview |
| 142 | + assetId={value} |
| 143 | + /> |
| 144 | + )} |
| 145 | + </FileInput> |
| 146 | + </InputContainerLayout> |
| 147 | + ); |
| 148 | +} |
| 149 | + |
| 150 | +export default TutorialAssetInput; |
0 commit comments