Add rejected files list

This commit is contained in:
Leonardo Murça 2022-11-18 13:53:32 -03:00
parent e0319aa75d
commit 6dc87e3354
4 changed files with 58 additions and 22 deletions

View file

@ -1,6 +1,6 @@
<script> <script>
export let files; export let files;
export let acceptedFiles; export let supportedFormats;
export let onKeydown; export let onKeydown;
export let onClick; export let onClick;
export let onDrop; export let onDrop;
@ -23,7 +23,7 @@
id="file-input" id="file-input"
type="file" type="file"
name="files[]" name="files[]"
accept={acceptedFiles.join(",")} accept={supportedFormats.join(",")}
multiple multiple
on:change={onChange} on:change={onChange}
bind:files bind:files

View file

@ -1,12 +1,14 @@
<script> <script>
export let title;
export let files = []; export let files = [];
export let isError = false;
</script> </script>
{#if files.length !== 0} {#if files.length !== 0}
<div id="selected-files-container"> <div id="selected-files-container">
<h2>Selected files:</h2> <h2>{title}:</h2>
{#each Array.from(files) as file} {#each Array.from(files) as file}
<div id="selected-file-card"> <div id={isError ? "selected-file-card-error" : "selected-file-card"}>
<p>{file.name} ({file.size / 1000} kb)</p> <p>{file.name} ({file.size / 1000} kb)</p>
</div> </div>
{/each} {/each}
@ -21,6 +23,14 @@
margin-top: 10px; margin-top: 10px;
} }
#selected-file-card-error {
border: 1px solid red;
width: 500px;
padding-left: 15px;
margin-top: 10px;
color: red;
}
@media only screen and (max-device-width: 812px) { @media only screen and (max-device-width: 812px) {
#selected-files-container { #selected-files-container {
width: 100%; width: 100%;

View file

@ -3,10 +3,15 @@
import Dropzone from "./Dropzone.svelte"; import Dropzone from "./Dropzone.svelte";
import FileList from "./FileList.svelte"; import FileList from "./FileList.svelte";
import { filterFileRequirements } from "../utils/filterFileRequirements";
let files; let files;
let filesRendered = false; let filesRendered = false;
const acceptedFiles = [".pes"]; let filesRejected;
const maxFileSize = 700000; const fileRequirements = {
supportedFormats: [".pes"],
maxSize: 700000,
};
const onSubmit = () => { const onSubmit = () => {
filesRendered = true; filesRendered = true;
@ -23,18 +28,10 @@
const changedFiles = evt.dataTransfer const changedFiles = evt.dataTransfer
? evt.dataTransfer.files ? evt.dataTransfer.files
: evt.target.files; : evt.target.files;
let filesToUpload = [];
for (var i = 0, file; (file = changedFiles[i]); i++) { const results = filterFileRequirements(changedFiles, fileRequirements);
if (file) { files = results.accepted;
if (file.size <= maxFileSize) { filesRejected = results.rejected;
filesToUpload.push(file);
} else {
console.log("File too large!");
console.log(file);
}
}
}
files = filesToUpload;
}; };
const onClick = () => { const onClick = () => {
@ -55,11 +52,19 @@
> >
<h2>Upload files</h2> <h2>Upload files</h2>
<p> <p>
Max file size is <strong>{maxFileSize / 1000}kb</strong>. Accepted formats: Max file size is <strong>{fileRequirements.maxSize / 1000}kb</strong>.
<strong>{acceptedFiles.join(",")}</strong>. Accepted formats:
<strong>{fileRequirements.supportedFormats.join(",")}</strong>.
</p> </p>
<Dropzone {files} {acceptedFiles} {onKeydown} {onClick} {onDrop} {onChange} /> <Dropzone
{files}
supportedFormats={fileRequirements.supportedFormats}
{onKeydown}
{onClick}
{onDrop}
{onChange}
/>
<input type="submit" value="Render files" /> <input type="submit" value="Render files" />
</form> </form>
@ -67,7 +72,8 @@
{#if filesRendered} {#if filesRendered}
<CardList {files} /> <CardList {files} />
{:else} {:else}
<FileList {files} /> <FileList title="Rejected Files" files={filesRejected} isError />
<FileList title="Selected Files" {files} />
{/if} {/if}
<style> <style>

View file

@ -0,0 +1,20 @@
export function filterFileRequirements(files, requirements) {
let accepted = [];
let rejected = [];
for (var i = 0, file; (file = files[i]); i++) {
if (file) {
if (
requirements.maxSize >= file.size &&
requirements.supportedFormats.includes(
`.${file.name.split(".").pop().toLowerCase()}`
)
) {
accepted.push(file);
} else {
rejected.push(file);
}
}
}
return { accepted, rejected };
}