Skip to content

Commit

Permalink
biome fix
Browse files Browse the repository at this point in the history
  • Loading branch information
magsyg committed Dec 7, 2024
1 parent f668437 commit 10c66bc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
62 changes: 28 additions & 34 deletions frontend/src/Components/InputFile/InputFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import classNames from 'classnames';
import { type ChangeEvent, type ReactNode, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { KEY } from '~/i18n/constants';
import { getFileNameFromUrl, isFileImage } from '~/utils';
import { Link } from '../Link';
import { TimeDisplay } from '../TimeDisplay';
import styles from './InputFile.module.scss';
import { Link } from '../Link';
import { getFileNameFromUrl, isFileImage } from '~/utils';

export type InputFileType = 'image' | 'pdf' | 'any';

Expand All @@ -18,13 +18,12 @@ export type InputFileProps = {
onSelected?: (file: File) => void;
};

export function InputFile({ fileType='any', label, existing_url, error = false, onSelected }: InputFileProps) {
export function InputFile({ fileType = 'any', label, existing_url, error = false, onSelected }: InputFileProps) {
const { t } = useTranslation();
const [selectedFile, setSelectedFile] = useState<File | undefined>(undefined);
const [preview, setPreview] = useState<string | undefined>(undefined);
const [isImage, setIsImage] = useState<boolean>(false);


function handleOnChange(e?: ChangeEvent<HTMLInputElement>) {
if (e === undefined || onSelected === undefined) return;
if (!e.target.files || e.target.files.length === 0) {
Expand Down Expand Up @@ -62,7 +61,7 @@ export function InputFile({ fileType='any', label, existing_url, error = false,
}
return '*';
}

const icons: Record<InputFileType, string> = {
image: 'mdi:image',
pdf: 'mdi:file',
Expand All @@ -88,37 +87,32 @@ export function InputFile({ fileType='any', label, existing_url, error = false,
<Icon icon={icons[fileType] ?? ''} />
{t(KEY.inputfile_choose_a_file)}
</div>
{
(existing_url && !selectedFile) ?
(
<Link className={styles.title} url={existing_url} target='external'>{getFileNameFromUrl(existing_url)}</Link>
):
(
<span className={styles.title}>{selectedFile?.name ?? t(KEY.inputfile_no_file_selected)}</span>
)


}
{existing_url && !selectedFile ? (
<Link className={styles.title} url={existing_url} target="external">
{getFileNameFromUrl(existing_url)}
</Link>
) : (
<span className={styles.title}>{selectedFile?.name ?? t(KEY.inputfile_no_file_selected)}</span>
)}
</div>

<div className={styles.selected_container}>
{preview &&
<>
<div className={styles.preview_meta}>
<p>
<TimeDisplay timestamp={new Date(selectedFile?.lastModified ?? 0)} />
</p>
<p>{fileSizeMb} MB</p>
</div>
{isImage && (
<div className={classNames(styles.preview_container, styles[typePreviewClass])}>
{preview && <img className={styles.preview} src={preview} alt="Preview" />}
</div>
)
}
</>
}
</div>
<div className={styles.selected_container}>
{preview && (
<>
<div className={styles.preview_meta}>
<p>
<TimeDisplay timestamp={new Date(selectedFile?.lastModified ?? 0)} />
</p>
<p>{fileSizeMb} MB</p>
</div>
{isImage && (
<div className={classNames(styles.preview_container, styles[typePreviewClass])}>
{preview && <img className={styles.preview} src={preview} alt="Preview" />}
</div>
)}
</>
)}
</div>
</label>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ export function RecruitmentApplicationFormPage() {
>
<h2 className={styles.label}>{t(KEY.recruitment_application)}:</h2>
<SamfFormField field="application_text" type="text_long" />{' '}
<SamfFormField field="file" type="file" props={{existing_url: 'http://localhost:3000/src/assets/logos/uka.png'}} />
<SamfFormField
field="file"
type="file"
props={{ existing_url: 'http://localhost:3000/src/assets/logos/uka.png' }}
/>
</SamfForm>
) : (
<div>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ export function getFullName(u: UserDto): string {
}

export function getFileNameFromUrl(url: string): string {
let url_split = url.split('/');
return url_split[url_split.length-1];
const url_split = url.split('/');
return url_split[url_split.length - 1];
}

export function isFileImage(file_name: string): boolean {
Expand Down

0 comments on commit 10c66bc

Please sign in to comment.