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')}
|
||||
|
||||
+10
-1
@@ -190,7 +190,16 @@
|
||||
"anotherInstance": "Find another instance"
|
||||
}
|
||||
},
|
||||
"description": "Description"
|
||||
"description": "Description",
|
||||
"time": {
|
||||
"duration": "Duration",
|
||||
"minutes": "Minutes",
|
||||
"hours": "Hours",
|
||||
"days": "Days",
|
||||
"months": "Months",
|
||||
"years": "Years",
|
||||
"permanent": "Permanent"
|
||||
}
|
||||
},
|
||||
"routes": {
|
||||
"frontpage": {
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
|
||||
interface Props extends Omit<HTMLInputAttributes, 'size' | 'prefix'> {
|
||||
label?: string | undefined
|
||||
value?: string
|
||||
placeholder?: string
|
||||
disabled?: boolean
|
||||
required?: boolean
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
Icon,
|
||||
type IconSource,
|
||||
} from 'svelte-hero-icons'
|
||||
import type { HTMLSelectAttributes } from 'svelte/elements'
|
||||
import type { ClassValue, HTMLSelectAttributes } from 'svelte/elements'
|
||||
|
||||
let open = $state(false)
|
||||
let element: HTMLSelectElement | undefined = $state()
|
||||
@@ -47,7 +47,9 @@
|
||||
size?: ButtonSize
|
||||
shadow?: ButtonShadow
|
||||
id?: string
|
||||
class?: string
|
||||
class?: ClassValue
|
||||
baseClass?: ClassValue
|
||||
selectClass?: ClassValue
|
||||
customLabel?: import('svelte').Snippet
|
||||
children?: import('svelte').Snippet
|
||||
customOption?: import('svelte').Snippet<[any]>
|
||||
@@ -63,6 +65,8 @@
|
||||
shadow = 'none',
|
||||
id = generateID(),
|
||||
class: clazz = '',
|
||||
baseClass = '',
|
||||
selectClass = '',
|
||||
customLabel,
|
||||
children,
|
||||
customOption,
|
||||
@@ -72,7 +76,7 @@
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-1 {clazz}">
|
||||
<div class="flex flex-col gap-1 {clazz} {baseClass}">
|
||||
{#if customLabel || label}
|
||||
<Label for={id} text={label}>
|
||||
{@render customLabel?.()}
|
||||
@@ -90,7 +94,7 @@
|
||||
shadow
|
||||
]} {buttonColor.secondary} appearance-none transition-colors rounded-lg text-sm
|
||||
w-full min-w-full cursor-pointer pr-6 {buttonColor.secondary}
|
||||
{clazz}"
|
||||
{clazz} {selectClass}"
|
||||
bind:value
|
||||
onmousedown={(e) => {
|
||||
e.preventDefault()
|
||||
|
||||
Reference in new issue
Block a user