Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
seanes committed Nov 26, 2024
1 parent 28d912c commit a212486
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { AccountMenuItem, AccountSearch } from '@altinn/altinn-components';
import type { Account } from '@altinn/altinn-components/dist/components/GlobalMenu/AccountButton';
import type { PartyFieldsFragment } from 'bff-types-generated';
import { type ChangeEvent, useState } from 'react';
import { useTranslation } from 'react-i18next';

interface UseAccountsProps {
parties: PartyFieldsFragment[];
selectedParties: PartyFieldsFragment[];
}

interface UseAccountsOutput {
accounts: AccountMenuItem[];
selectedAccount: Account;
accountSearch: AccountSearch | undefined;
}

type AccountType = 'company' | 'person';

export const useAccounts = ({ parties, selectedParties }: UseAccountsProps): UseAccountsOutput => {
const { t } = useTranslation();
const [searchString, setSearchString] = useState<string>('');
const accounts: AccountMenuItem[] = parties.map((party) => ({
id: party.party,
name: party.name,
type: party.partyType.toLowerCase() as AccountMenuItem['type'],
}));
const selectedAccount =
selectedParties?.length === 1
? selectedParties.map((party) => ({
id: party.party,
name: party.name,
type: party.partyType.toLowerCase() as AccountType,
}))[0]
: { id: 'ALL', name: t('parties.labels.all_organizations'), type: 'company' as AccountType };

const accountSearch =
parties.length > 10
? {
name: 'account-search',
value: searchString,
onChange: (event: ChangeEvent<HTMLInputElement>) => {
setSearchString(event.target.value);
},
placeholder: 'Søk etter konto',
getResultsLabel: (hits: number) => `Fant ${hits} kontoer`,
}
: undefined;

return {
accounts,
selectedAccount,
accountSearch,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { getSearchStringFromQueryParams } from '../../pages/Inbox/queryParams.ts
import { useSavedSearches } from '../../pages/SavedSearches/useSavedSearches.ts';
import { useProfile } from '../../profile';
import { BetaBanner } from '../BetaBanner/BetaBanner';
import { useFooter } from '../Footer/useFooter.tsx';
import { useSearchString } from '../Header';
import { useAuth } from '../Login/AuthContext.tsx';
import { useSidebarMenu } from '../Sidebar/useSidebarMenu.tsx';
import { useAccounts } from './Accounts/useAccounts.tsx';
import { useFooter } from './Footer/useFooter.tsx';
import { useSelectedDialogs } from './SelectedDialogs';
import { useSidebarMenu } from './Sidebar/useSidebarMenu.tsx';
import styles from './pageLayout.module.css';

export const useUpdateOnLocationChange = (fn: () => void) => {
Expand Down Expand Up @@ -53,6 +54,7 @@ export const PageLayoutArbeidsFlate: React.FC = () => {
const queryClient = useQueryClient();
const { setSearchString } = useSearchString();
const { selectedProfile, selectedParties, parties, selectedPartyIds } = useParties();
const { accounts, selectedAccount, accountSearch } = useAccounts({ parties, selectedParties });
const { currentPartySavedSearches } = useSavedSearches(selectedPartyIds);
const { dialogsByView } = useDialogs(selectedParties);
const itemsPerViewCount = {
Expand All @@ -74,31 +76,8 @@ export const PageLayoutArbeidsFlate: React.FC = () => {
queryClient.setQueryData(['search'], () => searchString || '');
});

interface AccountMenuItem {
type: 'person' | 'company';
name: string;
id: string;
groupId?: string;
selected?: boolean;
}

const currentEndUser = selectedParties.find((party) => party.isCurrentEndUser);
const accounts: AccountMenuItem[] = parties.map((party) => ({
id: party.party,
name: party.name,
type: party.partyType.toLowerCase() as AccountMenuItem['type'],
}));

const endUserAccount = currentEndUser
? { id: currentEndUser.party, name: currentEndUser.name, type: 'person' }
: {
id: '',
name: '',
type: 'person',
};

const headerProps: HeaderProps = {
currentAccount: endUserAccount as HeaderProps['currentAccount'],
currentAccount: selectedAccount,
search: {
expanded: false,
name: 'search',
Expand All @@ -114,6 +93,9 @@ export const PageLayoutArbeidsFlate: React.FC = () => {
accounts,
changeLabel: 'Endre konto',
backLabel: 'Tilbake',
...(accountSearch && {
accountSearch,
}),
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useSidebarMenu';
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { BadgeColor, BadgeProps, MenuProps } from '@altinn/altinn-components';
import { useTranslation } from 'react-i18next';
import { Link, useLocation } from 'react-router-dom';
import type { InboxViewType } from '../../api/useDialogs.tsx';
import { Routes } from '../../pages/Inbox/Inbox';
import type { InboxViewType } from '../../../api/useDialogs.tsx';
import { Routes } from '../../../pages/Inbox/Inbox.tsx';
export type SideBarView = InboxViewType | 'saved-searches' | 'archive' | 'bin';

export type ItemPerViewCount = {
Expand All @@ -28,7 +28,6 @@ export const useSidebarMenu = ({ itemsPerViewCount }: UseSidebarProps): MenuProp
const { pathname, search } = useLocation();

return {
groups: {},
items: [
{
id: '1',
Expand All @@ -39,8 +38,7 @@ export const useSidebarMenu = ({ itemsPerViewCount }: UseSidebarProps): MenuProp
color: 'strong',
badge: getBadgeProps(itemsPerViewCount.inbox, 'alert'),
selected: pathname === Routes.inbox,
as: Link,
to: Routes.inbox + search,
as: (props) => <Link {...props} to={Routes.inbox + search} />,
},
{
id: '2',
Expand All @@ -49,8 +47,7 @@ export const useSidebarMenu = ({ itemsPerViewCount }: UseSidebarProps): MenuProp
title: t('sidebar.drafts'),
badge: getBadgeProps(itemsPerViewCount.drafts),
selected: pathname === Routes.drafts,
as: Link,
to: Routes.drafts + search,
as: (props) => <Link {...props} to={Routes.drafts + search} />,
},
{
id: '3',
Expand All @@ -59,8 +56,7 @@ export const useSidebarMenu = ({ itemsPerViewCount }: UseSidebarProps): MenuProp
title: t('sidebar.sent'),
badge: getBadgeProps(itemsPerViewCount.sent),
selected: pathname === Routes.sent,
as: Link,
to: Routes.sent + search,
as: (props) => <Link {...props} to={Routes.sent + search} />,
},
{
id: '4',
Expand All @@ -69,8 +65,7 @@ export const useSidebarMenu = ({ itemsPerViewCount }: UseSidebarProps): MenuProp
title: t('sidebar.saved_searches'),
badge: getBadgeProps(itemsPerViewCount['saved-searches']),
selected: pathname === Routes.savedSearches,
as: Link,
to: Routes.savedSearches + search,
as: (props) => <Link {...props} to={Routes.savedSearches + search} />,
},
{
id: '5',
Expand All @@ -79,19 +74,16 @@ export const useSidebarMenu = ({ itemsPerViewCount }: UseSidebarProps): MenuProp
title: t('sidebar.archived'),
badge: getBadgeProps(itemsPerViewCount.archive),
selected: pathname === Routes.archive,
as: Link,
to: Routes.archive + search,
as: (props) => <Link {...props} to={Routes.archive + search} />,
},
{
id: '6',
groupId: '4',
disabled: true,
icon: 'trash',
title: t('sidebar.deleted'),
badge: getBadgeProps(itemsPerViewCount.bin),
selected: pathname === Routes.bin,
as: Link,
to: Routes.bin + search,
as: (props) => <Link {...props} to={Routes.bin + search} />,
},
],
};
Expand Down
2 changes: 2 additions & 0 deletions packages/frontend/src/components/PageLayout/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { PageLayoutArbeidsFlate } from './PageLayoutArbeidsFlate.tsx';
export * from './SelectedDialogs';
export * from './Footer';
export * from './Sidebar';
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useTranslation } from 'react-i18next';
import { useParties } from '../../api/useParties';
import { Backdrop } from '../Backdrop';
import { DropdownList, DropdownMobileHeader } from '../DropdownMenu';
import type { SideBarView } from '../PageLayout/Sidebar/useSidebarMenu';
import { ProfileButton } from '../ProfileButton';
import type { SideBarView } from '../Sidebar/useSidebarMenu';
import { PartyListContainer } from './PartyListContainer';

interface PartyDropdownRef {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useDialogs } from '../../api/useDialogs.tsx';
import { useParties } from '../../api/useParties.ts';
import { QUERY_KEYS } from '../../constants/queryKeys.ts';
import { useSavedSearches } from '../../pages/SavedSearches/useSavedSearches';
import type { SideBarView } from '../Sidebar/useSidebarMenu';
import type { SideBarView } from '../PageLayout/Sidebar/useSidebarMenu';
import { PartyList } from './PartyList.tsx';
import { type PartyOptionGroup, getOptionsGroups } from './mapToPartyOption';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { PartyFieldsFragment, SavedSearchesFieldsFragment } from 'bff-types
import { t } from 'i18next';
import type { InboxItemInput } from '../../pages/Inbox/Inbox.tsx';
import { filterSavedSearches } from '../../pages/SavedSearches/useSavedSearches.ts';
import type { SideBarView } from '../Sidebar/useSidebarMenu.tsx';
import type { SideBarView } from '../PageLayout/Sidebar/useSidebarMenu.tsx';

type Dialog = {
party: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './Header';
export * from './BackButton';
export * from './InboxItem';
export * from './Footer';
export * from './PageLayout/Footer';
export * from './ActionPanel';
export * from './FilterBar';
export * from './BottomDrawer';
Expand Down
9 changes: 9 additions & 0 deletions packages/frontend/src/mocks/data/base/parties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ export const parties: PartyFieldsFragment[] = [
isCurrentEndUser: false,
isDeleted: false,
},
{
party: 'urn:altinn:organization:identifier-sub:3',
partyType: 'Organization',
isAccessManager: true,
isMainAdministrator: true,
name: 'TESTBEDRIFT AS',
isCurrentEndUser: false,
isDeleted: false,
},
],
isAccessManager: true,
isMainAdministrator: true,
Expand Down
6 changes: 3 additions & 3 deletions packages/frontend/src/pages/Inbox/Inbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { useFormat } from '../../i18n/useDateFnsLocale.tsx';
import { InboxSkeleton } from './InboxSkeleton.tsx';
import { filterDialogs, getFilterBarSettings } from './filters.ts';
import styles from './inbox.module.css';
import { clearFiltersInQueryParams, getFiltersFromQueryParams } from './queryParams.ts';
import { getFiltersFromQueryParams, getQueryParamsWithoutFilters } from './queryParams.ts';

interface InboxProps {
viewType: InboxViewType;
Expand Down Expand Up @@ -76,7 +76,7 @@ export const Inbox = ({ viewType }: InboxProps) => {

const location = useLocation();
const { t } = useTranslation();
const [searchParams] = useSearchParams();
const [searchParams, setSearchParams] = useSearchParams();
const { selectedItems, setSelectedItems, selectedItemCount, inSelectionMode } = useSelectedDialogs();
const { openSnackbar } = useSnackbar();
const [isSavingSearch, setIsSavingSearch] = useState<boolean>(false);
Expand Down Expand Up @@ -105,7 +105,7 @@ export const Inbox = ({ viewType }: InboxProps) => {
// biome-ignore lint/correctness/useExhaustiveDependencies: Full control of what triggers this code is needed
useEffect(() => {
setActiveFilters([]);
clearFiltersInQueryParams();
setSearchParams(getQueryParamsWithoutFilters());
filterBarRef.current?.resetFilters();
}, [selectedParties]);

Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/src/pages/Inbox/queryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export const getFiltersFromQueryParams = (searchParams: URLSearchParams): Filter
return compressedData ? JSON.parse(compressedData) : ([] as Filter[]);
};

export const clearFiltersInQueryParams = (): void => {
export const getQueryParamsWithoutFilters = (): URLSearchParams => {
const searchParams = new URLSearchParams(window.location.search);
searchParams.delete('filters');
window.history.replaceState({}, '', `${window.location.pathname}?${searchParams}`);
return searchParams;
};

export const getSearchStringFromQueryParams = (searchParams: URLSearchParams): string => {
Expand Down

0 comments on commit a212486

Please sign in to comment.