Skip to content

Commit

Permalink
fix: 819 upload component improvements (#820)
Browse files Browse the repository at this point in the history
* refactor(upload): rename Error component to ErrorComponent

* refactor(upload): rename Error component to ErrorComponent

* refactor(upload): rename Error component to ErrorComponent
  • Loading branch information
AdrianBielecAriane authored Oct 28, 2024
1 parent f579af3 commit 323e876
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
6 changes: 5 additions & 1 deletion packages/react-kit/src/components/form/Field.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export type FileUploadWrapperTheme = Partial<{
error: Partial<{
borderColor: CSSProperties["borderColor"];
}>;
overrides: Partial<CSSProperties>;
}>;
export const FileUploadWrapper = styled.div<{
$error: unknown;
Expand Down Expand Up @@ -188,13 +189,16 @@ export const FileUploadWrapper = styled.div<{
cursor: not-allowed;
opacity: 0.5;
}
${({ theme: { overrides } }) => overrides}
`;

export const FieldFileUpload = styled(FieldInput)`
display: none;
`;

export const FieldFileUploadWrapper = styled.div<{ $disabled: boolean }>`
export const FieldFileUploadWrapper = styled.div<{
$disabled: boolean;
}>`
position: relative;
display: inline-block;
${({ $disabled }) =>
Expand Down
35 changes: 24 additions & 11 deletions packages/react-kit/src/components/form/Upload/BaseUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { theme } from "../../../theme";
import Loading from "../../ui/loading/LoadingWrapper";
import ThemedButton from "../../ui/ThemedButton";
import { Typography } from "../../ui/Typography";
import Error from "../Error";
import ErrorComponent from "../Error";
import {
FieldFileUploadWrapper,
FieldInput,
Expand Down Expand Up @@ -52,6 +52,7 @@ function BaseUpload({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
removeFile,
saveButtonTheme,
errorComponent,
theme,
...props
}: UploadPropsWithNoIpfs & WithUploadToIpfsProps) {
Expand All @@ -60,6 +61,7 @@ function BaseUpload({
const [isLoading, setIsLoading] = useState<boolean>(false);
const [preview, setPreview] = useState<string | null>();
const [field, meta, helpers] = useField(name);
const [errorMesage, setErrorMessage] = useState<string>();

const handleLoading = useCallback(
(loadingValue: boolean) => {
Expand Down Expand Up @@ -216,16 +218,26 @@ function BaseUpload({
helpers.setTouched(true);
}
handleLoading(true);
const files = await saveToIpfs(efiles);
if (files) {
setFiles(files);
} else {
setFiles([]);
console.warn(
`There has been an error because 'files' ${files} is falsy in handleSave`
);
try {
const files = await saveToIpfs(efiles, { throwOnError: true });
setErrorMessage(undefined);
if (files) {
setFiles(files);
} else {
setFiles([]);
console.warn(
`There has been an error because 'files' ${files} is falsy in handleSave`
);
}
} catch (err) {
if (err instanceof Error) {
setErrorMessage(err.message);
} else {
setErrorMessage("Something went wrong");
}
} finally {
handleLoading(false);
}
handleLoading(false);
},
[meta.touched, handleLoading, saveToIpfs, helpers, setFiles]
);
Expand Down Expand Up @@ -263,6 +275,7 @@ function BaseUpload({
}}
/>
)}
{errorMesage && errorComponent?.(errorMesage)}
<FieldFileUploadWrapper {...wrapperProps} $disabled={!!disabled}>
<FieldInput
{...props}
Expand Down Expand Up @@ -364,7 +377,7 @@ function BaseUpload({
<UploadedFiles files={files} handleRemoveFile={handleRemoveFile} />
)}
</FieldFileUploadWrapper>
<Error display={displayError} message={errorMessage} />
<ErrorComponent display={displayError} message={errorMessage} />
</>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const SUPPORTED_FORMATS = [
];
export interface WithUploadToIpfsProps {
saveToIpfs: (
files: File[] | null
files: File[] | null,
options?: { throwOnError?: boolean }
) => Promise<false | FileProps[] | undefined>;
loadMedia: (src: string) => Promise<string | undefined>;
removeFile: (src: string) => Promise<void>;
Expand All @@ -39,7 +40,10 @@ export function WithUploadToIpfs<P extends WithUploadToIpfsProps>(
const { saveFile, loadMedia, removeFile } = useSaveImageToIpfs();

const saveToIpfs: WithUploadToIpfsProps["saveToIpfs"] = useCallback(
async (filesArray: File[] | null) => {
async (
filesArray: File[] | null,
options: { throwOnError?: boolean } = {}
) => {
if (!filesArray) {
return;
}
Expand All @@ -53,10 +57,12 @@ export function WithUploadToIpfs<P extends WithUploadToIpfsProps>(
const err = `File ${
file?.name
} size cannot exceed more than ${bytesToSize(sizeValidation)}!`;
if (options?.throwOnError) throw new Error(err);
filesErrors.push(err);
}
if (!formatValidation.includes(file?.type)) {
const err = `Uploaded file has unsupported format of ${file?.type}!`;
if (options?.throwOnError) throw new Error(err);
filesErrors.push(err);
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/react-kit/src/components/form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ export type UploadProps = BaseProps & {
borderRadiusUnit?: "px" | "%";
width?: number;
height?: number;
errorComponent?: (errorMessage: string) => React.ReactNode;
imgPreviewStyle?: Pick<CSSProperties, "objectFit">;
theme?: Partial<{ triggerTheme: FileUploadWrapperTheme }>;
theme?: Partial<{
triggerTheme: FileUploadWrapperTheme;
overrides: React.CSSProperties;
}>;
} & (
| {
withEditor: true;
Expand Down
3 changes: 2 additions & 1 deletion packages/react-kit/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const theme = {
cyan: "#00FFFF",
lightArrowColor: "#dedfe3",
darkGreyTimeStamp: "#E8EAF1",
lightGrey2: "#eff0f7"
lightGrey2: "#eff0f7",
greyText: "rgba(28, 13, 24, 0.75)"
}
},
mobile: "768px",
Expand Down

0 comments on commit 323e876

Please sign in to comment.