Skip to content

Commit 26c8cae

Browse files
authored
fix: image (#1069)
1 parent 6c1f9d9 commit 26c8cae

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

apps/studio/src/features/editing-experience/components/form-builder/hooks/useImage.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { env } from "~/env.mjs"
2+
13
const waitFor = (baseTimeoutMs = 500) => {
24
return new Promise((resolve) => setTimeout(resolve, baseTimeoutMs))
35
}
@@ -37,11 +39,12 @@ export const useImage = ({
3739
retries = 3,
3840
baseTimeoutMs = 500,
3941
}: UseImageProps) => {
42+
const assetsBaseUrl = `https://${env.NEXT_PUBLIC_S3_ASSETS_DOMAIN_NAME}`
4043
const handleImage = async (src: string) => {
4144
try {
4245
return await retry(
4346
async () => {
44-
const response = await fetch(src)
47+
const response = await fetch(`${assetsBaseUrl}${src}`)
4548
if (!response.ok) {
4649
throw new Error(`Unable to read from ${src}`)
4750
}

apps/studio/src/features/editing-experience/components/form-builder/renderers/controls/JsonFormsImageControl.tsx

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import 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"
33
import { and, isStringControl, rankWith, schemaMatches } from "@jsonforms/core"
44
import { withJsonFormsControlProps } from "@jsonforms/react"
55
import { FormErrorMessage, FormLabel } from "@opengovsg/design-system-react"
6+
import { BiTrash } from "react-icons/bi"
67
import { z } from "zod"
78

89
import { FileAttachment } from "~/components/PageEditor/FileAttachment"
910
import { JSON_FORMS_RANKING } from "~/constants/formBuilder"
10-
import { env } from "~/env.mjs"
1111
import { useQueryParse } from "~/hooks/useQueryParse"
1212
import { useImage } from "../../hooks/useImage"
1313
import {
@@ -16,7 +16,6 @@ import {
1616
} from "./constants"
1717
import { getCustomErrorMessage } from "./utils"
1818

19-
const assetsBaseUrl = `https://${env.NEXT_PUBLIC_S3_ASSETS_DOMAIN_NAME}`
2019
export 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)}

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)