Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JNG-6159 extend principal hook api #516

Merged
merged 2 commits into from
Feb 10, 2025
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
13 changes: 13 additions & 0 deletions docs/pages/01_ui_react.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ public/18n/application_default_extra.fragment.hbs

== Hooks

=== usePrincipal

The `usePrincipal()` hook can be used to obtain information and/or manipulate Principal data.

*API:*

- `principal: JudoStored<unknown>`: The current data of the logged in Principal
- `getPrincipal: () => Promise<JudoRestResponse<unknown>>` / `setPrincipal(data: JudoStored<unknown>)`: Can be used to fetch or update the data on the UI
- `refreshPrincipal: () => Promise<void>`: refreshes and updates the Principal data

> Setting / Refreshing Principal data automatically refreshes most of the UI tree including e.g. the menu, and the active
screen since it's Context is in a very high position in the component tree.

=== useViewData

The `useViewData()` hook can be used to obtain the data of page/dialog's.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,17 @@ public static List<Action> getAccessTableOperationActions(PageDefinition pageDef
.toList();
}

public static List<PageDefinition> getAccessTableOperationActionPages(Application application) {
Set<String> store = new HashSet<>();
List<PageDefinition> pages = getAccessPages(application).stream().filter(p -> p.getContainer().isTable()).toList();
Set<Action> collected = pages.stream()
.flatMap(p -> getAccessTableOperationActions(p).stream()).collect(Collectors.toSet());
return collected.stream().map(Action::getTargetPageDefinition)
.filter(targetPageDefinition -> store.add(pageName(targetPageDefinition)))
.sorted(Comparator.comparing(NamedElement::getFQName))
.toList();
}

public static Action getAccessCreateActionForFormPage(PageDefinition pageDefinition) {
return pageDefinition.getActions().stream().filter(Action::getIsCreateAction).findFirst().orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{> fragment.header.hbs }}

{{# if application.authentication }}
import { createContext, useContext, useEffect, useMemo, useState } from 'react';
import { createContext, useContext, useEffect, useMemo, useState, useCallback } from 'react';
import type { ReactNode } from 'react';
import { useAuth } from 'react-oidc-context';
import { type {{ classDataName application.principal 'Stored' }} } from '~/services/data-api/model/{{ classDataName application.principal '' }}';
Expand All @@ -14,6 +14,7 @@
principal: {{ classDataName application.principal 'Stored' }};
setPrincipal: (principal: {{ classDataName application.principal 'Stored' }}) => void;
getPrincipal: () => Promise<JudoRestResponse<{{ classDataName application.principal 'Stored' }}>>;
refreshPrincipal: () => Promise<void>;
errorCode?: string | null;
}

Expand All @@ -31,7 +32,7 @@
const [errorCode, setErrorCode] = useState<string | null>(null);
const { isAuthenticated } = useAuth();

const fetchData = async () => {
const refreshPrincipal = useCallback(async () => {
try {
const { data } = await accessServiceImpl.getPrincipal();
setPrincipal({ ...data });
Expand All @@ -45,27 +46,27 @@
console.error(e);
}
}
};
}, [principal, errorCode]);

const getPrincipal = (): Promise<JudoRestResponse<{{ classDataName application.principal 'Stored' }}>> => accessServiceImpl.getPrincipal();

useEffect(() => {
if (isAuthenticated) {
fetchData();
refreshPrincipal();
} else {
setPrincipal({} as unknown as {{ classDataName application.principal 'Stored' }});
}
}, [isAuthenticated]);

return (
<PrincipalContext.Provider value={ { principal, setPrincipal, getPrincipal, errorCode } }>{children}</PrincipalContext.Provider>
<PrincipalContext.Provider value={ { principal, setPrincipal, getPrincipal, refreshPrincipal, errorCode } }>{children}</PrincipalContext.Provider>
);
};

export const usePrincipal = () => {
const { principal, setPrincipal, getPrincipal, errorCode } = useContext(PrincipalContext);
const { principal, setPrincipal, getPrincipal, refreshPrincipal, errorCode } = useContext(PrincipalContext);

return { principal, setPrincipal, getPrincipal, errorCode };
return { principal, setPrincipal, getPrincipal, refreshPrincipal, errorCode };
};
{{ else }}
// nothing to export, application has no Principal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import { judoAxiosProvider } from '~/services/data-axios/JudoAxiosProvider';
{{# if page.container.form }}
import { use{{ pageName page }} } from '~/dialogs/{{ pagePath page }}/hooks';
{{/ if }}
{{# if page.container.table }}
{{# each (getAccessTableOperationActions page) as |action| }}
import { use{{ pageName action.targetPageDefinition }} } from '~/dialogs/{{ pagePath action.targetPageDefinition }}/hooks';
{{/ each }}
{{/ if }}
{{/ each }}
{{# each (getAccessTableOperationActionPages application) as |page| }}
import { use{{ pageName page }} } from '~/dialogs/{{ pagePath page }}/hooks';
{{/ each }}

export type AccessName = {{# each (getAccessPages application) as |page| }}{{# if page.container.view }}'{{ page.dataElement.name }}' | {{/ if }}{{/ each }}unknown;
Expand Down Expand Up @@ -63,7 +61,8 @@ export function useOperationUtils() {
{{/ if }}
{{# if page.container.table }}
{{# each (getAccessTableOperationActions page) as |action| }}
const open{{ pageName action.targetPageDefinition }} = use{{ pageName action.targetPageDefinition }}();
// action name: {{ action.name }}
const open{{ firstToUpper page.relationType.name }}{{ pageName action.targetPageDefinition }} = use{{ pageName action.targetPageDefinition }}();
{{/ each }}
{{/ if }}
{{/ each }}
Expand Down Expand Up @@ -127,7 +126,7 @@ export function useOperationUtils() {
if (access === '{{ page.dataElement.name }}') {
{{# each (getAccessTableOperationActions page) as |action| }}
if (operation === '{{ action.targetDataElement.name }}') {
const { result, data: returnedData } = await open{{ pageName action.targetPageDefinition }}({
const { result, data: returnedData } = await open{{ firstToUpper page.relationType.name }}{{ pageName action.targetPageDefinition }}({
ownerData: null,
});
if (result === 'submit' && returnedData) {
Expand Down
Loading