Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: file size reactive to unit change #166

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import { Dependencies } from '$lib/constants';
import Delete from '../deleteBucket.svelte';
import { trackEvent } from '$lib/actions/analytics';
import { writable } from 'svelte/store';

let showDelete = false;

Expand All @@ -35,13 +36,14 @@
encryption: boolean = null,
antivirus: boolean = null,
maxSize: number;
let byteUnit: 'Bytes' | 'KB' | 'MB' | 'GB' = 'MB',
options = [
{ label: 'Bytes', value: 'Bytes' },
{ label: 'Kilobytes', value: 'KB' },
{ label: 'Megabytes', value: 'MB' },
{ label: 'Gigabytes', value: 'GB' }
];
let byteUnit = writable('KB');
let sizeInBytes: number = null;
const options = [
{ label: 'Bytes', value: 'Bytes' },
{ label: 'Kilobytes', value: 'KB' },
{ label: 'Megabytes', value: 'MB' },
{ label: 'Gigabytes', value: 'GB' }
];
let suggestedExtensions = ['jpg', 'png', 'svg', 'gif', 'html', 'pdf', 'mp4'];
let extensions = $bucket.allowedFileExtensions;
let isExtensionsDisabled = true;
Expand All @@ -54,6 +56,7 @@
bucketPermissions ??= $bucket.$permissions;
encryption ??= $bucket.encryption;
antivirus ??= $bucket.antivirus;
maxSize = $bucket.maximumFileSize / 1024;
});
$: if (bucketPermissions) {
if (symmetricDifference(bucketPermissions, $bucket.$permissions).length) {
Expand Down Expand Up @@ -174,7 +177,7 @@
}
}
async function updateMaxSize() {
let size = sizeToBytes(maxSize, byteUnit);
let size = sizeToBytes(maxSize, $byteUnit);
try {
await sdkForProject.storage.updateBucket(
$bucket.$id,
Expand Down Expand Up @@ -222,6 +225,22 @@
});
}
}

byteUnit.subscribe((b) => {
if (b === 'Bytes') {
maxSize = sizeInBytes;
} else if (b === 'KB') {
maxSize = sizeInBytes / 1024;
} else if (b === 'MB') {
maxSize = sizeInBytes / 1024 / 1024;
} else if (b === 'GB') {
maxSize = sizeInBytes / 1024 / 1024 / 1024;
}
});

$: if (maxSize) {
sizeInBytes = sizeToBytes(maxSize, $byteUnit);
}
</script>

<Container>
Expand Down Expand Up @@ -398,14 +417,15 @@
<InputNumber
id="size"
label="Size"
placeholder="256"
placeholder={$bucket.maximumFileSize.toString()}
bind:value={maxSize} />
<InputSelect id="bytes" label="Bytes" {options} bind:value={byteUnit} />
<InputSelect id="bytes" label="Bytes" {options} bind:value={$byteUnit} />
</ul>
</svelte:fragment>

<svelte:fragment slot="actions">
<Button disabled={!maxSize} submit>Update</Button>
<Button disabled={sizeInBytes === $bucket.maximumFileSize} submit
>Update</Button>
</svelte:fragment>
</CardGrid>
</Form>
Expand Down