@aarhus-university/au-lib-react-components 10.12.0 → 10.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/jest/context.test.ts +25 -0
- package/package.json +3 -3
- package/src/lib/context.tsx +38 -0
- package/src/lib/dates.ts +50 -0
- package/src/lib/portals.ts +23 -0
- package/stories/lib/helpers.tsx +9 -7
- package/src/lib/context.ts +0 -13
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { getPortalByName } from '../../src/lib/portals';
|
|
2
|
+
|
|
3
|
+
describe('Context', () => {
|
|
4
|
+
test('Mitstudie findes som portal', () => {
|
|
5
|
+
const portal = getPortalByName('da', 'mitstudie');
|
|
6
|
+
expect(portal?.name).toBe('mitstudie');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test('Mitstudie portalcontexten indeholder én impersonation option', () => {
|
|
10
|
+
const portal = getPortalByName('da', 'mitstudie');
|
|
11
|
+
expect(portal?.impersonationOptions.length).toBe(1);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('Mitstudie portalcontextens ene impersonation option hedder "AU-id" på dansk og "AU ID" på engelsk', () => {
|
|
15
|
+
const danish = getPortalByName('da', 'mitstudie');
|
|
16
|
+
expect(danish?.impersonationOptions[0].label).toBe('AU-id');
|
|
17
|
+
const english = getPortalByName('en', 'mitstudie');
|
|
18
|
+
expect(english?.impersonationOptions[0].label).toBe('AU ID');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('Portalcontexten "sfgsdfsdfsdf" findes ikke', () => {
|
|
22
|
+
const portal = getPortalByName('da', 'sfgsdfsdfsdf');
|
|
23
|
+
expect(portal).toBe(null);
|
|
24
|
+
});
|
|
25
|
+
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"sideEffects": false,
|
|
3
3
|
"name": "@aarhus-university/au-lib-react-components",
|
|
4
|
-
"version": "10.
|
|
4
|
+
"version": "10.15.0",
|
|
5
5
|
"description": "Library for shared React components for various applications on au.dk",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "jest",
|
|
@@ -71,14 +71,14 @@
|
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
73
|
"@aarhus-university/au-designsystem-delphinus": "0.29.0",
|
|
74
|
-
"@aarhus-university/types": "0.
|
|
74
|
+
"@aarhus-university/types": "0.10.0",
|
|
75
75
|
"@reduxjs/toolkit": "^1.8.3",
|
|
76
76
|
"@types/google.analytics": "^0.0.42",
|
|
77
77
|
"@types/history": "^5.0.0",
|
|
78
78
|
"@types/react": "^18.0.14",
|
|
79
79
|
"@types/react-dom": "^18.0.5",
|
|
80
80
|
"@types/react-router-dom": "^5.3.3",
|
|
81
|
-
"dayjs": "^1.
|
|
81
|
+
"dayjs": "^1.11.3",
|
|
82
82
|
"lodash.debounce": "^4.0.8",
|
|
83
83
|
"prop-types": "^15.8.1",
|
|
84
84
|
"query-string": "^7.1.0",
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import React, { createContext, useContext } from 'react';
|
|
2
|
+
import { setLocale } from './dates';
|
|
3
|
+
import { getPortalByName } from './portals';
|
|
4
|
+
|
|
5
|
+
export const LangContext = createContext<string>('da');
|
|
6
|
+
export const useLangContext = (): string => {
|
|
7
|
+
const lang = useContext(LangContext);
|
|
8
|
+
setLocale(lang);
|
|
9
|
+
return lang;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const UserContext = createContext<AU.IUserContext | null>(null);
|
|
13
|
+
export const useUserContext = (): AU.IUserContext | null => {
|
|
14
|
+
const user = useContext(UserContext);
|
|
15
|
+
return user;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const PortalContext = createContext<AU.IPortalContext | null>(null);
|
|
19
|
+
|
|
20
|
+
export const AUPortal = {
|
|
21
|
+
Provider: ({
|
|
22
|
+
name,
|
|
23
|
+
children,
|
|
24
|
+
}: PortalContextProviderProps) => {
|
|
25
|
+
const lang = useLangContext();
|
|
26
|
+
const portal = getPortalByName(lang || 'da', name);
|
|
27
|
+
return (
|
|
28
|
+
<PortalContext.Provider value={portal}>
|
|
29
|
+
{children}
|
|
30
|
+
</PortalContext.Provider>
|
|
31
|
+
);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const usePortalContext = (): AU.IPortalContext | null => {
|
|
36
|
+
const portal = useContext(PortalContext);
|
|
37
|
+
return portal;
|
|
38
|
+
};
|
package/src/lib/dates.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
import 'dayjs/locale/da';
|
|
3
|
+
import isoWeek from 'dayjs/plugin/isoWeek';
|
|
4
|
+
import weekday from 'dayjs/plugin/weekday';
|
|
5
|
+
import advancedFormat from 'dayjs/plugin/advancedFormat';
|
|
6
|
+
import updateLocale from 'dayjs/plugin/updateLocale';
|
|
7
|
+
|
|
8
|
+
dayjs.extend(isoWeek);
|
|
9
|
+
dayjs.extend(weekday);
|
|
10
|
+
dayjs.extend(advancedFormat);
|
|
11
|
+
dayjs.extend(updateLocale);
|
|
12
|
+
|
|
13
|
+
dayjs.updateLocale('da', {
|
|
14
|
+
weekdaysShort: 'søn_man_tir_ons_tor_fre_lør'.split('_'),
|
|
15
|
+
monthsShort: 'jan_feb_mar_apr_maj_jun_jul_aug_sep_okt_nov_dec'.split('_'),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const formatTime = (date: Date, format = 'HH:mm'): string => dayjs(date).format(format);
|
|
19
|
+
export const getLocale = (): string => dayjs.locale();
|
|
20
|
+
export const setLocale = (locale: string): string => dayjs.locale(locale);
|
|
21
|
+
export const getThisYear = (): number => dayjs().year();
|
|
22
|
+
export const getYearToString = (date: Date): string => formatTime(date, 'YYYY');
|
|
23
|
+
export const getNumberOfWeeksInYear = (year: number): number => dayjs(`${year}-12-31`).isoWeek();
|
|
24
|
+
export const skipWeeks = (date: Date, multiplier: number): Date => dayjs(date).clone().add(multiplier * 7, 'days').toDate();
|
|
25
|
+
export const getWeek = (date: Date): number => dayjs(date).isoWeek();
|
|
26
|
+
// eslint-disable-next-line max-len
|
|
27
|
+
export const getDateByWeekday = (date: Date, index: number, locale = 'da'): Date => {
|
|
28
|
+
const currentLocale = getLocale();
|
|
29
|
+
setLocale(locale);
|
|
30
|
+
const newDate = dayjs(date).weekday(index).toDate();
|
|
31
|
+
if (currentLocale !== locale) {
|
|
32
|
+
setLocale(currentLocale);
|
|
33
|
+
}
|
|
34
|
+
return newDate;
|
|
35
|
+
};
|
|
36
|
+
const dateIsSameMonthAsCurrent = (date: Date): boolean => formatTime(new Date(), 'M') === formatTime(date, 'M');
|
|
37
|
+
export const getLongDate = (date: Date): string => formatTime(date, 'dddd, Do MMMM YYYY');
|
|
38
|
+
export const getShortDate = (date: Date): string => formatTime(date, `ddd D${(dateIsSameMonthAsCurrent(date) ? '' : `${getLocale() === 'da' ? '.' : ''} MMM`)}`);
|
|
39
|
+
export const isToday = (date: Date): boolean => dayjs(date).isSame(dayjs());
|
|
40
|
+
export const multipleDaysFormatTime = (date: Date, index: number, format = 'ddd HH:mm', firstLetterToUpper = true): string => {
|
|
41
|
+
if (index > 0) {
|
|
42
|
+
const formatted = formatTime(date, format);
|
|
43
|
+
if (firstLetterToUpper) {
|
|
44
|
+
return formatted.charAt(0).toUpperCase() + formatted.slice(1);
|
|
45
|
+
}
|
|
46
|
+
return formatted;
|
|
47
|
+
}
|
|
48
|
+
return formatTime(date);
|
|
49
|
+
};
|
|
50
|
+
export const formatDayOfWeek = (date: Date): string => formatTime(date, 'dddd').charAt(0).toUpperCase() + formatTime(date, 'dddd').slice(1);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/* eslint-disable import/prefer-default-export */
|
|
2
|
+
const impersonationLabels = {
|
|
3
|
+
IMPERSONATION_SELECT_OPTION_CPR: { da: 'CPR-nr.', en: 'CPR no.' },
|
|
4
|
+
IMPERSONATION_SELECT_OPTION_EMAIL: { da: 'E-mail', en: 'Email' },
|
|
5
|
+
IMPERSONATION_SELECT_OPTION_AUID: { da: 'AU-id', en: 'AU ID' },
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const portals: (lang: string) => AU.IPortalContext[] = (lang: string) => [
|
|
9
|
+
{
|
|
10
|
+
name: 'mitstudie',
|
|
11
|
+
impersonationOptions: [
|
|
12
|
+
{
|
|
13
|
+
value: 'auid',
|
|
14
|
+
label: impersonationLabels.IMPERSONATION_SELECT_OPTION_AUID[lang],
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
},
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
export const getPortalByName = (
|
|
21
|
+
lang: string,
|
|
22
|
+
name: string,
|
|
23
|
+
): AU.IPortalContext | null => portals(lang).find((portal) => portal.name === name) ?? null;
|
package/stories/lib/helpers.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useEffect } from 'react';
|
|
1
|
+
import React, { useEffect, StrictMode } from 'react';
|
|
2
2
|
|
|
3
3
|
const globalTheme = {
|
|
4
4
|
theme: {
|
|
@@ -38,13 +38,15 @@ const ThemeWrapper = ({ theme, children }) => {
|
|
|
38
38
|
}
|
|
39
39
|
});
|
|
40
40
|
return (
|
|
41
|
-
<
|
|
42
|
-
<
|
|
43
|
-
<div className="
|
|
44
|
-
|
|
41
|
+
<StrictMode>
|
|
42
|
+
<main className={`theme--${theme}`}>
|
|
43
|
+
<div className="page">
|
|
44
|
+
<div className="page__content__block">
|
|
45
|
+
{children}
|
|
46
|
+
</div>
|
|
45
47
|
</div>
|
|
46
|
-
</
|
|
47
|
-
</
|
|
48
|
+
</main>
|
|
49
|
+
</StrictMode>
|
|
48
50
|
);
|
|
49
51
|
}
|
|
50
52
|
|
package/src/lib/context.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { createContext, useContext } from 'react';
|
|
2
|
-
|
|
3
|
-
export const LangContext = createContext<string>('da');
|
|
4
|
-
export const useLangContext = (): string => {
|
|
5
|
-
const lang = useContext(LangContext);
|
|
6
|
-
return lang;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export const UserContext = createContext<AU.IUserContext | null>(null);
|
|
10
|
-
export const useUserContext = (): AU.IUserContext | null => {
|
|
11
|
-
const user = useContext(UserContext);
|
|
12
|
-
return user;
|
|
13
|
-
};
|