feat: add better duration selection to ban modal
This commit is contained in:
1 parent
5651ad475d
commit
98cb1992d2
6 files changed
+110
-41
No files matched your search
@@ -0,0 +1,70 @@
|
||||
<script lang="ts">
|
||||
import { t } from '$lib/translations'
|
||||
import { TextInput, Select, Option } from 'mono-svelte'
|
||||
import type { Snippet } from 'svelte'
|
||||
|
||||
const multipliers = {
|
||||
minute: 60,
|
||||
hour: 60 * 60,
|
||||
day: 60 * 60 * 24,
|
||||
month: 60 * 60 * 24 * 30,
|
||||
year: 60 * 60 * 24 * 365,
|
||||
permanent: -1,
|
||||
}
|
||||
|
||||
type Duration = keyof typeof multipliers
|
||||
|
||||
function calculateValue(number: number, duration: Duration): number {
|
||||
return number * multipliers[duration]
|
||||
}
|
||||
|
||||
interface Props {
|
||||
value: number
|
||||
max?: number
|
||||
min?: number
|
||||
number?: Snippet<[number]>
|
||||
duration?: Snippet<[Duration]>
|
||||
}
|
||||
|
||||
let {
|
||||
value = $bindable(),
|
||||
max,
|
||||
min = 0,
|
||||
number: numberSnippet,
|
||||
duration: durationSnippet,
|
||||
}: Props = $props()
|
||||
|
||||
let number = $state<number>(0)
|
||||
let duration = $state<Duration>(value == -1 ? 'permanent' : 'day')
|
||||
|
||||
$effect(() => {
|
||||
value = calculateValue(number, duration)
|
||||
console.log(value)
|
||||
})
|
||||
</script>
|
||||
|
||||
<div class="flex flex-row items-end gap-2">
|
||||
{#if duration != 'permanent'}
|
||||
<TextInput
|
||||
{min}
|
||||
{max}
|
||||
type="number"
|
||||
label="Number"
|
||||
class="w-24"
|
||||
bind:value={number}
|
||||
></TextInput>
|
||||
{/if}
|
||||
<Select
|
||||
selectClass="h-[38px]"
|
||||
baseClass="flex-1"
|
||||
label={$t('form.time.duration')}
|
||||
bind:value={duration}
|
||||
>
|
||||
<Option value="minute">{$t('form.time.minutes')}</Option>
|
||||
<Option value="hour">{$t('form.time.hours')}</Option>
|
||||
<Option value="day">{$t('form.time.days')}</Option>
|
||||
<Option value="month">{$t('form.time.months')}</Option>
|
||||
<Option value="year">{$t('form.time.years')}</Option>
|
||||
<Option value="permanent">{$t('form.time.permanent')}</Option>
|
||||
</Select>
|
||||
</div>
|
||||
@@ -10,6 +10,8 @@
|
||||
import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte'
|
||||
import { t } from '$lib/translations'
|
||||
import CommunityLink from '../community/CommunityLink.svelte'
|
||||
import Duration from '$lib/components/form/Duration.svelte'
|
||||
import SectionTitle from '$lib/components/ui/SectionTitle.svelte'
|
||||
|
||||
interface Props {
|
||||
open?: boolean
|
||||
@@ -27,18 +29,17 @@
|
||||
|
||||
let reason = $state('')
|
||||
let deleteData = $state(false)
|
||||
let expires = $state('')
|
||||
|
||||
let expires = $state(-1)
|
||||
let loading = $state(false)
|
||||
|
||||
// hack due to svelte's reactive declarations
|
||||
const resetReason = () => {
|
||||
reason = ''
|
||||
deleteData = false
|
||||
expires = ''
|
||||
expires = -1
|
||||
}
|
||||
|
||||
run(() => {
|
||||
$effect(() => {
|
||||
if (item) resetReason()
|
||||
})
|
||||
|
||||
@@ -50,28 +51,8 @@
|
||||
try {
|
||||
let date: number | undefined
|
||||
|
||||
if (expires != '') {
|
||||
date = Date.parse(expires)
|
||||
if (Number.isNaN(date)) {
|
||||
toast({
|
||||
content: $t('toast.invalidDateAbsolute'),
|
||||
type: 'error',
|
||||
})
|
||||
|
||||
loading = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (date < Date.now()) {
|
||||
toast({
|
||||
content: $t('toast.invalidDateBeforeCurrent'),
|
||||
type: 'error',
|
||||
})
|
||||
|
||||
loading = false
|
||||
return
|
||||
}
|
||||
if (expires > 0) {
|
||||
date = Math.floor(Date.now() / 1000) + expires
|
||||
}
|
||||
|
||||
if (community) {
|
||||
@@ -81,7 +62,7 @@
|
||||
person_id: item.id,
|
||||
reason: reason || undefined,
|
||||
remove_data: deleteData,
|
||||
expires: date ? Math.floor(date / 1000) : undefined,
|
||||
expires: date,
|
||||
})
|
||||
} else {
|
||||
await getClient().banPerson({
|
||||
@@ -89,7 +70,7 @@
|
||||
person_id: item.id,
|
||||
reason: reason || undefined,
|
||||
remove_data: deleteData,
|
||||
expires: date ? Math.floor(date / 1000) : undefined,
|
||||
expires: date,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -137,11 +118,10 @@
|
||||
{$t('moderation.ban.warning')}
|
||||
{/snippet}
|
||||
</Checkbox>
|
||||
<TextInput
|
||||
bind:value={expires}
|
||||
label={$t('moderation.ban.expires')}
|
||||
placeholder="2024 August 5"
|
||||
/>
|
||||
<div>
|
||||
<SectionTitle>{$t('moderation.ban.expires')}</SectionTitle>
|
||||
<Duration bind:value={expires}></Duration>
|
||||
</div>
|
||||
{/if}
|
||||
<Button submit color="primary" {loading} disabled={loading} size="lg">
|
||||
{$t('form.submit')}
|
||||
|
||||
Reference in new issue
Block a user