From 1e1a638011d552034da94fd8b4e14786651b2743 Mon Sep 17 00:00:00 2001 From: Waldemar Reusch Date: Tue, 14 Mar 2023 17:29:14 +0100 Subject: [PATCH] feat(storybook): progress on many more pages --- .storybook/data.ts | 27 --- .storybook/main.js | 30 ++- .storybook/preview.js | 16 +- .storybook/util.tsx | 41 ++++ package.json | 4 +- src/keycloak-theme/Template.stories.tsx | 32 +++ src/keycloak-theme/pages/Error.stories.tsx | 16 ++ src/keycloak-theme/pages/Error.tsx | 34 ++++ .../pages/IdpReviewUserProfile.stories.tsx | 17 ++ .../pages/IdpReviewUserProfile.tsx | 47 +++++ src/keycloak-theme/pages/Info.stories.tsx | 37 ++++ src/keycloak-theme/pages/Info.tsx | 49 +++++ src/keycloak-theme/pages/Login.stories.tsx | 112 ++--------- .../pages/LoginConfigTotp.stories.tsx | 27 +++ src/keycloak-theme/pages/LoginConfigTotp.tsx | 186 ++++++++++++++++++ .../pages/LoginIdpLinkConfirm.stories.tsx | 17 ++ .../pages/LoginIdpLinkConfirm.tsx | 54 +++++ .../pages/LoginIdpLinkEmail.stories.tsx | 17 ++ .../pages/LoginIdpLinkEmail.tsx | 32 +++ src/keycloak-theme/pages/LoginOtp.stories.tsx | 16 ++ src/keycloak-theme/pages/LoginOtp.tsx | 114 +++++++++++ .../pages/LoginPageExpired.stories.tsx | 16 ++ src/keycloak-theme/pages/LoginPageExpired.tsx | 36 ++++ .../pages/MyExtraPage1.stories.tsx | 28 +-- .../pages/MyExtraPage2.stories.tsx | 33 ++-- src/keycloak-theme/pages/Register.stories.tsx | 93 ++++----- src/keycloak-theme/pages/Register.tsx | 1 + .../pages/RegisterUserProfile.stories.tsx | 79 ++++++++ src/keycloak-theme/pages/Terms.stories.tsx | 15 ++ src/keycloak-theme/pages/Terms.tsx | 135 +++++++------ 30 files changed, 1061 insertions(+), 300 deletions(-) delete mode 100644 .storybook/data.ts create mode 100644 .storybook/util.tsx create mode 100644 src/keycloak-theme/Template.stories.tsx create mode 100644 src/keycloak-theme/pages/Error.stories.tsx create mode 100644 src/keycloak-theme/pages/Error.tsx create mode 100644 src/keycloak-theme/pages/IdpReviewUserProfile.stories.tsx create mode 100644 src/keycloak-theme/pages/IdpReviewUserProfile.tsx create mode 100644 src/keycloak-theme/pages/Info.stories.tsx create mode 100644 src/keycloak-theme/pages/Info.tsx create mode 100644 src/keycloak-theme/pages/LoginConfigTotp.stories.tsx create mode 100644 src/keycloak-theme/pages/LoginConfigTotp.tsx create mode 100644 src/keycloak-theme/pages/LoginIdpLinkConfirm.stories.tsx create mode 100644 src/keycloak-theme/pages/LoginIdpLinkConfirm.tsx create mode 100644 src/keycloak-theme/pages/LoginIdpLinkEmail.stories.tsx create mode 100644 src/keycloak-theme/pages/LoginIdpLinkEmail.tsx create mode 100644 src/keycloak-theme/pages/LoginOtp.stories.tsx create mode 100644 src/keycloak-theme/pages/LoginOtp.tsx create mode 100644 src/keycloak-theme/pages/LoginPageExpired.stories.tsx create mode 100644 src/keycloak-theme/pages/LoginPageExpired.tsx create mode 100644 src/keycloak-theme/pages/RegisterUserProfile.stories.tsx create mode 100644 src/keycloak-theme/pages/Terms.stories.tsx diff --git a/.storybook/data.ts b/.storybook/data.ts deleted file mode 100644 index 337e207..0000000 --- a/.storybook/data.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { KcContextBase, getKcContext } from "keycloakify/lib/getKcContext"; -import type { DeepPartial } from "keycloakify/lib/tools/DeepPartial"; -import type { ExtendsKcContextBase } from "keycloakify/lib/getKcContext/getKcContextFromWindow"; -import type { KcContextExtension } from "keycloak-theme/kcContext"; - - -export const useKcStoryData = (mockData: ( - { pageId: KcContextBase['pageId'] | KcContextExtension['pageId'] } & DeepPartial> -)) => { - const { kcContext } = getKcContext({ mockPageId: mockData.pageId, mockData: [mockData] }) - return { kcContext: kcContext as NonNullable } -} - -export const socialProviders = [ - { loginUrl: 'google', alias: 'google', providerId: 'google', displayName: 'Google' }, - { loginUrl: 'microsoft', alias: 'microsoft', providerId: 'microsoft', displayName: 'Microsoft' }, - { loginUrl: 'facebook', alias: 'facebook', providerId: 'facebook', displayName: 'Facebook' }, - { loginUrl: 'instagram', alias: 'instagram', providerId: 'instagram', displayName: 'Instagram' }, - { loginUrl: 'twitter', alias: 'twitter', providerId: 'twitter', displayName: 'Twitter' }, - { loginUrl: 'linkedin', alias: 'linkedin', providerId: 'linkedin', displayName: 'LinkedIn' }, - { loginUrl: 'stackoverflow', alias: 'stackoverflow', providerId: 'stackoverflow', displayName: 'Stackoverflow' }, - { loginUrl: 'github', alias: 'github', providerId: 'github', displayName: 'Github' }, - { loginUrl: 'gitlab', alias: 'gitlab', providerId: 'gitlab', displayName: 'Gitlab' }, - { loginUrl: 'bitbucket', alias: 'bitbucket', providerId: 'bitbucket', displayName: 'Bitbucket' }, - { loginUrl: 'paypal', alias: 'paypal', providerId: 'paypal', displayName: 'PayPal' }, - { loginUrl: 'openshift', alias: 'openshift', providerId: 'openshift', displayName: 'OpenShift' }, -] \ No newline at end of file diff --git a/.storybook/main.js b/.storybook/main.js index ae1673f..bf619ea 100644 --- a/.storybook/main.js +++ b/.storybook/main.js @@ -1,18 +1,16 @@ module.exports = { - "stories": [ - "../src/keycloak-theme/pages/Login.stories.tsx", - "../src/keycloak-theme/pages/Register.stories.tsx", - "../src/keycloak-theme/pages/MyExtraPage1.stories.tsx", - "../src/keycloak-theme/pages/MyExtraPage2.stories.tsx", - ], - "addons": [ - "@storybook/addon-links", - "@storybook/addon-essentials", - "@storybook/addon-interactions", - "@storybook/preset-create-react-app" - ], - "framework": "@storybook/react", - "core": { - "builder": "@storybook/builder-webpack5" - } + "stories": [ + "../src/keycloak-theme/**/*.stories.tsx", + ], + "addons": [ + "@storybook/addon-links", + "@storybook/addon-essentials", + "@storybook/addon-interactions", + "@storybook/preset-create-react-app" + ], + "framework": "@storybook/react", + "core": { + "builder": "@storybook/builder-webpack5" + }, + "staticDirs": ['../public'] } \ No newline at end of file diff --git a/.storybook/preview.js b/.storybook/preview.js index 48afd56..b6de5f6 100644 --- a/.storybook/preview.js +++ b/.storybook/preview.js @@ -1,9 +1,13 @@ export const parameters = { - actions: { argTypesRegex: "^on[A-Z].*" }, - controls: { - matchers: { - color: /(background|color)$/i, - date: /Date$/, + actions: {argTypesRegex: "^on[A-Z].*"}, + controls: { + matchers: { + color: /(background|color)$/i, + date: /Date$/, + }, + }, + options: { + storySort: (a, b) => + a[1].kind === b[1].kind ? 0 : a[1].id.localeCompare(b[1].id, undefined, {numeric: true}), }, - }, } \ No newline at end of file diff --git a/.storybook/util.tsx b/.storybook/util.tsx new file mode 100644 index 0000000..4180dee --- /dev/null +++ b/.storybook/util.tsx @@ -0,0 +1,41 @@ +import type {KcContextExtension} from "keycloak-theme/kcContext"; +import KcApp from "../src/keycloak-theme/KcApp"; +import {KcContextBase} from "keycloakify"; +import {getKcContext} from "keycloakify/lib/getKcContext"; +import {ExtendsKcContextBase} from "keycloakify/src/lib/getKcContext/getKcContextFromWindow"; +import {DeepPartial} from "keycloakify/src/lib/tools/DeepPartial"; + + +export const socialProviders = [ + {loginUrl: 'google', alias: 'google', providerId: 'google', displayName: 'Google'}, + {loginUrl: 'microsoft', alias: 'microsoft', providerId: 'microsoft', displayName: 'Microsoft'}, + {loginUrl: 'facebook', alias: 'facebook', providerId: 'facebook', displayName: 'Facebook'}, + {loginUrl: 'instagram', alias: 'instagram', providerId: 'instagram', displayName: 'Instagram'}, + {loginUrl: 'twitter', alias: 'twitter', providerId: 'twitter', displayName: 'Twitter'}, + {loginUrl: 'linkedin', alias: 'linkedin', providerId: 'linkedin', displayName: 'LinkedIn'}, + {loginUrl: 'stackoverflow', alias: 'stackoverflow', providerId: 'stackoverflow', displayName: 'Stackoverflow'}, + {loginUrl: 'github', alias: 'github', providerId: 'github', displayName: 'Github'}, + {loginUrl: 'gitlab', alias: 'gitlab', providerId: 'gitlab', displayName: 'Gitlab'}, + {loginUrl: 'bitbucket', alias: 'bitbucket', providerId: 'bitbucket', displayName: 'Bitbucket'}, + {loginUrl: 'paypal', alias: 'paypal', providerId: 'paypal', displayName: 'PayPal'}, + {loginUrl: 'openshift', alias: 'openshift', providerId: 'openshift', displayName: 'OpenShift'}, +] + +type PageId = (KcContextExtension | KcContextBase)['pageId'] +export const template = (pageId: PageId) => { + type MockData = DeepPartial>; + + const Template = (mockData: MockData) => { + const finalMockData = { + message: undefined, + pageId, + ...mockData + } as MockData + if (!("message" in mockData)) mockData["message"] = undefined + const {kcContext} = getKcContext({mockPageId: pageId, mockData: [finalMockData]}) + return }/> + } + + return (args: MockData) => Object.assign(Template.bind({}), {args}) +} + diff --git a/package.json b/package.json index eb6772b..11a0e3c 100755 --- a/package.json +++ b/package.json @@ -12,8 +12,8 @@ "build": "react-scripts build", "build-keycloak-theme": "yarn build && keycloakify", "download-builtin-keycloak-theme": "download-builtin-keycloak-theme 15.0.2", - "storybook": "start-storybook -p 6006 -s public", - "build-storybook": "build-storybook -s public" + "storybook": "start-storybook -p 6006", + "build-storybook": "build-storybook" }, "keycloakify": { "extraPages": [ diff --git a/src/keycloak-theme/Template.stories.tsx b/src/keycloak-theme/Template.stories.tsx new file mode 100644 index 0000000..d8660ed --- /dev/null +++ b/src/keycloak-theme/Template.stories.tsx @@ -0,0 +1,32 @@ +import type {ComponentMeta} from '@storybook/react'; +import KcApp from './KcApp'; +import {template} from '../../.storybook/util' + +const bind = template('my-extra-page-1.ftl'); + +export default { + title: 'Theme/Template', + component: KcApp, + parameters: { + layout: 'fullscreen', + }, +} as ComponentMeta; + + +export const Default = bind({}) +export const InFrench = bind({locale: {currentLanguageTag: 'fr'}}) +export const RealmDisplayNameIsHtml = bind({ + realm: { + displayNameHtml: 'my realm' + } +}) + +export const NoInternationalization = bind({ + realm: { + internationalizationEnabled: false, + } +}) + +export const WithGlobalError = bind({ + message: {type: "error", summary: "This is an error"} +}) diff --git a/src/keycloak-theme/pages/Error.stories.tsx b/src/keycloak-theme/pages/Error.stories.tsx new file mode 100644 index 0000000..ec176d8 --- /dev/null +++ b/src/keycloak-theme/pages/Error.stories.tsx @@ -0,0 +1,16 @@ +import {ComponentMeta} from '@storybook/react'; +import KcApp from '../KcApp'; +import {template} from '../../../.storybook/util' + +const bind = template('error.ftl'); + +export default { + kind: 'Page', + title: 'Theme/Pages/Error', + component: KcApp, + parameters: { + layout: 'fullscreen', + }, +} as ComponentMeta; + +export const Default = bind({message: {type: 'error', summary: 'Something went wrong'}}) diff --git a/src/keycloak-theme/pages/Error.tsx b/src/keycloak-theme/pages/Error.tsx new file mode 100644 index 0000000..b28ff06 --- /dev/null +++ b/src/keycloak-theme/pages/Error.tsx @@ -0,0 +1,34 @@ +// copied and adapted from https://github.com/InseeFrLab/keycloakify/blob/main/src/lib/pages/Error.tsx +import React from "react"; +import type { PageProps } from "keycloakify" +import type { KcContext } from "../kcContext"; +import type { I18n } from "../i18n"; + + +export default function Error(props: PageProps, I18n>) { + const { kcContext, i18n, doFetchDefaultThemeResources = true, Template, ...kcProps } = props; + + const { message, client } = kcContext; + + const { msg } = i18n; + + return ( +