11import type { ControlProps , RankedTester } from "@jsonforms/core"
2- import { Box , FormControl } from "@chakra-ui/react"
2+ import { Box , Flex , FormControl , IconButton , Text } from "@chakra-ui/react"
33import { and , isStringControl , rankWith , schemaMatches } from "@jsonforms/core"
44import { withJsonFormsControlProps } from "@jsonforms/react"
55import { FormErrorMessage , FormLabel } from "@opengovsg/design-system-react"
6+ import { BiTrash } from "react-icons/bi"
67import { z } from "zod"
78
89import { FileAttachment } from "~/components/PageEditor/FileAttachment"
910import { JSON_FORMS_RANKING } from "~/constants/formBuilder"
10- import { env } from "~/env.mjs"
1111import { useQueryParse } from "~/hooks/useQueryParse"
1212import { useImage } from "../../hooks/useImage"
1313import {
@@ -16,7 +16,6 @@ import {
1616} from "./constants"
1717import { getCustomErrorMessage } from "./utils"
1818
19- const assetsBaseUrl = `https://${ env . NEXT_PUBLIC_S3_ASSETS_DOMAIN_NAME } `
2019export const jsonFormsImageControlTester : RankedTester = rankWith (
2120 JSON_FORMS_RANKING . ImageControl ,
2221 and (
@@ -39,24 +38,43 @@ export function JsonFormsImageControl({
3938 required,
4039 errors,
4140 description,
41+ data,
4242} : JsonFormsImageControlProps ) {
4343 const { siteId } = useQueryParse ( editSiteSchema )
4444 const { handleImage } = useImage ( { } )
4545
4646 return (
4747 < Box as = { FormControl } isRequired = { required } isInvalid = { ! ! errors } >
4848 < FormLabel description = { description } > { label } </ FormLabel >
49- < FileAttachment
50- maxSizeInBytes = { MAX_IMG_FILE_SIZE_BYTES }
51- acceptedFileTypes = { IMAGE_UPLOAD_ACCEPTED_MIME_TYPE_MAPPING }
52- siteId = { siteId }
53- setHref = { ( src ) =>
54- src &&
55- handleImage ( `${ assetsBaseUrl } ${ src } ` ) . then ( ( src ) =>
56- handleChange ( path , src ) ,
57- )
58- }
59- />
49+ { data ? (
50+ < Flex
51+ px = "1rem"
52+ py = "0.75rem"
53+ flexDir = "row"
54+ background = "brand.primary.100"
55+ justifyContent = "space-between"
56+ alignItems = "center"
57+ >
58+ < Text overflow = "auto" > { data . split ( "/" ) . pop ( ) } </ Text >
59+ < IconButton
60+ size = "xs"
61+ variant = "clear"
62+ colorScheme = "critical"
63+ aria-label = "Remove file"
64+ icon = { < BiTrash /> }
65+ onClick = { ( ) => handleChange ( path , undefined ) }
66+ />
67+ </ Flex >
68+ ) : (
69+ < FileAttachment
70+ maxSizeInBytes = { MAX_IMG_FILE_SIZE_BYTES }
71+ acceptedFileTypes = { IMAGE_UPLOAD_ACCEPTED_MIME_TYPE_MAPPING }
72+ siteId = { siteId }
73+ setHref = { ( src ) =>
74+ src && handleImage ( src ) . then ( ( src ) => handleChange ( path , src ) )
75+ }
76+ />
77+ ) }
6078 { ! ! errors && (
6179 < FormErrorMessage >
6280 { label } { getCustomErrorMessage ( errors ) }
0 commit comments