Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion SPECIFICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,11 @@ days; a country does not get its regions' days.
- **Out again:** the page downloads the shown list in the same format, and
`occ absence:export-holidays [--year] [--region]` prints it.

Everyone can read the list. HR always edits it, and HR or an admin can let every
Everyone can read the list. The page opens on the viewer's own days (their code, its
country and `*`) when they have a region code, and says which those are; without one it
links to the profile to set it. HR, who keep the list for every region, open on all
regions with the rule itself. Days already past in the shown year are muted.
HR always edits the list, and HR or an admin can let every
employee (§2.2) add, edit and delete single holidays, so HR does not become a bottleneck. That switch
lives in the *Public holidays* section of the Settings page (§12); the *Public
holidays* page only tells HR who may edit and links there. Importing a whole list
Expand Down
35 changes: 33 additions & 2 deletions src/utils/regions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { describe, expect, it, vi } from 'vitest'
import { regionCountryName, regionFlag } from './regions.ts'
import { holidayAppliesTo, holidaySources, regionCountryName, regionFlag } from './regions.ts'

vi.mock('@nextcloud/l10n', () => ({ getLanguage: () => 'en' }))
vi.mock('@nextcloud/l10n', () => ({
getLanguage: () => 'en',
t: (_app: string, text: string, vars: Record<string, string>) => text.replace(/\{(\w+)\}/g, (_match: string, key: string) => vars[key] ?? ''),
}))

describe('regionFlag', () => {
it('shows the country of a code', () => {
Expand All @@ -31,3 +34,31 @@ describe('regionCountryName', () => {
expect(regionCountryName('QQ')).toBe('')
})
})

describe('holidayAppliesTo', () => {
it('takes the days of the code, of its country and of everywhere', () => {
expect(holidayAppliesTo('DE-BY', 'DE-BY')).toBe(true)
expect(holidayAppliesTo('DE', 'DE-BY')).toBe(true)
expect(holidayAppliesTo('*', 'DE-BY')).toBe(true)
})

it('leaves out other regions, even of the same country', () => {
expect(holidayAppliesTo('DE-BE', 'DE-BY')).toBe(false)
expect(holidayAppliesTo('FR', 'DE-BY')).toBe(false)
expect(holidayAppliesTo('DE-BY', 'DE')).toBe(false)
})
})

describe('holidaySources', () => {
it('names the region and its country', () => {
expect(holidaySources('DE-BY')).toBe('You get the holidays of DE-BY, of Germany and those for everyone.')
})

it('names only the country for a code without a region', () => {
expect(holidaySources('FR')).toBe('You get the holidays of France and those for everyone.')
})

it('says nothing without a code', () => {
expect(holidaySources('')).toBe('')
})
})
30 changes: 29 additions & 1 deletion src/utils/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { getLanguage } from '@nextcloud/l10n'
import { getLanguage, t } from '@nextcloud/l10n'

/** Holidays entered for this code apply to everyone (§7.2) */
export const EVERYWHERE = '*'
Expand Down Expand Up @@ -48,3 +48,31 @@ export function regionCountryName(code: string | null | undefined): string {
const name = new Intl.DisplayNames([getLanguage().replace('_', '-'), 'en'], { type: 'region', fallback: 'none' }).of(country)
return name ?? ''
}

/**
* Whether a holiday entered for `region` applies to someone with `code` (§7.2):
* the days of the code, of its country and of everywhere.
*
* @param region the region code of the holiday
* @param code the person's region code, uppercase such as `DE-BY`
*/
export function holidayAppliesTo(region: string, code: string): boolean {
return region === EVERYWHERE || region === code || region === countryOf(code)
}

/**
* Where the holidays of someone with this region code come from, in one sentence.
*
* @param code their region code, '' when they set none
* @return '' without a code
*/
export function holidaySources(code: string): string {
const country = countryOf(code)
if (!country) {
return ''
}
const countryName = regionCountryName(code) || country
return code === country
? t('absence', 'You get the holidays of {country} and those for everyone.', { country: countryName }, undefined, { escape: false, sanitize: false })
: t('absence', 'You get the holidays of {region}, of {country} and those for everyone.', { region: code, country: countryName }, undefined, { escape: false, sanitize: false })
}
76 changes: 57 additions & 19 deletions src/views/HolidaysView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,23 @@
</div>
</header>

<p class="holidays__intro">
{{ t('absence', 'People get the days of their region code, of its country, and the days entered for * (everywhere). DE-BY gets DE-BY, DE and * days.') }}
<template v-if="!canEdit">
<div class="holidays__intro">
<p v-if="session.isHr">
{{ t('absence', 'People get the days of their region code, its country and * (everywhere): DE-BY gets DE-BY, DE and *.') }}
</p>
<p v-else-if="ownCode">
{{ holidaySources(ownCode) }}
</p>
<p v-else>
{{ t('absence', 'You have no region code yet, so only the holidays for everyone apply.') }}
<RouterLink :to="{ name: 'profile' }">
{{ t('absence', 'Set it on your profile') }}
</RouterLink>
</p>
<p v-if="!canEdit">
{{ t('absence', 'Only HR edits public holidays.') }}
</template>
</p>
</p>
</div>
<p v-if="session.isHr" class="holidays__who">
{{ session.holidaysEveryoneEdits ? t('absence', 'Who may edit: everyone.') : t('absence', 'Who may edit: HR only.') }}
<RouterLink :to="{ name: 'hr-settings' }">
Expand All @@ -73,7 +84,7 @@
</tr>
</thead>
<tbody>
<tr v-for="holiday in shown" :key="holiday.id + '-' + holiday.date">
<tr v-for="holiday in shown" :key="holiday.id + '-' + holiday.date" :class="{ holidays__past: holiday.date < today }">
<td>{{ formatDate(holiday.date) }}</td>
<td>
<span class="holidays__region"><span aria-hidden="true">{{ regionFlag(holiday.region) }}</span> {{ regionName(holiday.region) }}</span>
Expand Down Expand Up @@ -267,16 +278,22 @@ import api from '../services/api.ts'
import { useSessionStore } from '../stores/session.ts'
import { formatDate, toIso } from '../utils/dates.ts'
import { serverMessage } from '../utils/errors.ts'
import { formatRegionCode } from '../utils/holidays.ts'
import { latestOnly } from '../utils/latest.ts'
import { locale } from '../utils/locale.ts'
import { EVERYWHERE, regionFlag } from '../utils/regions.ts'
import { EVERYWHERE, holidayAppliesTo, holidaySources, regionFlag } from '../utils/regions.ts'

const sessionStore = useSessionStore()

/** An entry of the region filter: `id` is the region code, '' for all of them */
/**
* An entry of the region filter. `code` is a region code, '' for all of them;
* `mine` shows every day that applies to the viewer's code, not only its own.
*/
interface RegionOption {
id: string
label: string
code: string
mine: boolean
}

/** The add and edit form: `id` is null for a new holiday */
Expand All @@ -289,13 +306,24 @@ interface HolidayForm {
}

const currentYear = new Date().getFullYear()
const all: RegionOption = { id: '', label: t('absence', 'All regions') }
const all: RegionOption = { id: '', label: t('absence', 'All regions'), code: '', mine: false }
const today = toIso(new Date())

const loading = ref(true)
const holidays = shallowRef<Holiday[]>([])
const year = ref(currentYear)
const years = [currentYear - 1, currentYear, currentYear + 1, currentYear + 2]
const region = shallowRef<RegionOption>(all)
const session = computed(() => sessionStore.session)

/** The viewer's region code such as `DE-BY`, '' when they set none */
const ownCode = computed(() => formatRegionCode(session.value.holidayCountry ?? '', session.value.holidayRegion ?? ''))

const mine = computed<RegionOption | null>(() => (ownCode.value
? { id: 'mine', label: t('absence', 'Your holidays ({code})', { code: ownCode.value }), code: ownCode.value, mine: true }
: null))

// HR keeps the list for every region; everyone else opens on their own days
const region = shallowRef<RegionOption>((!session.value.isHr && mine.value) || all)
const form = ref<HolidayForm | null>(null)
const saving = ref(false)
// The holiday whose deletion waits for a confirmation
Expand All @@ -314,16 +342,21 @@ const example = JSON.stringify([
// Changing the year twice quickly: only the year picked last may land.
const startLoad = latestOnly()

const session = computed(() => sessionStore.session)

const canEdit = computed(() => sessionStore.session.canEditHolidays)

const regionOptions = computed<RegionOption[]>(() => {
const codes = [...new Set(holidays.value.map((h) => h.region))].sort()
return [all, ...codes.map((code) => ({ id: code, label: `${regionFlag(code)} ${regionName(code)}`.trim() }))]
const own = mine.value ? [mine.value] : []
return [all, ...own, ...codes.map((code) => ({ id: code, label: `${regionFlag(code)} ${regionName(code)}`.trim(), code, mine: false }))]
})

const shown = computed(() => (region.value.id === '' ? holidays.value : holidays.value.filter((h) => h.region === region.value.id)))
const shown = computed(() => {
const { code, mine: applying } = region.value
if (code === '') {
return holidays.value
}
return holidays.value.filter((h) => (applying ? holidayAppliesTo(h.region, code) : h.region === code))
})

watch(year, () => {
load()
Expand Down Expand Up @@ -372,7 +405,7 @@ async function load(): Promise<void> {
function edit(holiday: Holiday | null): void {
form.value = holiday
? { id: holiday.id, date: new Date(holiday.date + 'T00:00:00'), region: holiday.region, label: holiday.label, recurring: holiday.recurring }
: { id: null, date: new Date(`${year.value}-01-01T00:00:00`), region: region.value.id, label: '', recurring: false }
: { id: null, date: new Date(`${year.value}-01-01T00:00:00`), region: region.value.code, label: '', recurring: false }
}

/**
Expand Down Expand Up @@ -532,7 +565,7 @@ function download(): void {
const list = shown.value.map(({ date, region, label, recurring }) => ({ date, region, label, recurring }))
const link = document.createElement('a')
link.href = URL.createObjectURL(new Blob([JSON.stringify(list, null, '\t')], { type: 'application/json' }))
link.download = `holidays-${year.value}${region.value.id ? '-' + region.value.id : ''}.json`
link.download = `holidays-${year.value}${region.value.code ? '-' + region.value.code : ''}.json`
link.click()
URL.revokeObjectURL(link.href)
}
Expand All @@ -550,12 +583,17 @@ function download(): void {
font-weight: 600;
}

&__intro a,
&__who a {
text-decoration: underline;
}

&__who {
margin-block-end: calc(2 * var(--default-grid-baseline));
}

a {
text-decoration: underline;
}
&__past td {
color: var(--color-text-maxcontrast);
}

&__byline,
Expand Down
Loading