Skip to content

Commit

Permalink
Fix bug where creation of new regelverk would not redirect correctly …
Browse files Browse the repository at this point in the history
…+ remove trash from query params when creating new maltekstseksjon
  • Loading branch information
eriksson-daniel committed Sep 27, 2024
1 parent ef30d52 commit b6ddc18
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 43 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/maltekstseksjoner/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const CreateMaltekstseksjon = ({ query }: Props) => {
};
const { id, versionId } = await createMaltekstseksjon({ maltekstseksjon, query }).unwrap();

setPath({ maltekstseksjonId: id, maltekstseksjonVersionId: versionId });
setPath({ maltekstseksjonId: id, maltekstseksjonVersionId: versionId, trash: false });
}, [createMaltekstseksjon, query, setPath]);

return (
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ import {
isRegelverkType,
isRichTextType,
} from '@app/functions/is-rich-plain-text';
import { useNavigateToStandaloneTextVersion } from '@app/hooks/use-navigate-to-standalone-text-version';
import { useRedaktoerLanguage } from '@app/hooks/use-redaktoer-language';
import { useAddTextMutation } from '@app/redux-api/texts/mutations';
import { TextTypes } from '@app/types/common-text-types';
import { REGELVERK_TYPE, TextTypes } from '@app/types/common-text-types';
import { Language } from '@app/types/texts/language';
import { LoadText } from './edit/load-text';
import { FilteredTextList } from './filtered-text-list';
import { getNewGodFormulering, getNewPlainText, getNewRegelverk, getNewRichText } from './functions/new-text';
import { useTextNavigate } from './hooks/use-text-navigate';

interface Props {
textType: TextTypes;
}

export const SmartEditorTexts = ({ textType }: Props) => {
const query = useTextQuery();
const navigate = useTextNavigate();
const navigate = useNavigateToStandaloneTextVersion(textType !== REGELVERK_TYPE);
const [addText, { isLoading }] = useAddTextMutation();
const lang = useRedaktoerLanguage();

const onClick = useCallback(async () => {
const text = getNewText(textType, lang);
const { id } = await addText({ text, query }).unwrap();
navigate(id, false);
const { id, versionId } = await addText({ text, query }).unwrap();
navigate({ id, versionId, trash: false });
}, [addText, lang, navigate, query, textType]);

return (
Expand Down
22 changes: 18 additions & 4 deletions frontend/src/hooks/use-navigate-to-standalone-text-version.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
import { useCallback, useEffect } from 'react';
import { useLocation, useNavigate, useParams } from 'react-router';
import { useSearchParams } from 'react-router-dom';
import { Language } from '@app/types/texts/language';

interface Params {
id?: string | null;
versionId?: string | null;
lang?: string;
trash?: boolean;
}

export const useNavigateToStandaloneTextVersion = (hasLanguage: boolean) => {
const navigate = useNavigate();
const { pathname, search } = useLocation();
const { pathname } = useLocation();
const oldParams = useParams();
const { lang } = useParams();
const [searchParams] = useSearchParams();

const [, rootPath] = pathname.split('/');

const navigateToText = useCallback(
(newParams: Params, replace = false) =>
navigate(`${calculatePath(rootPath, oldParams, newParams, hasLanguage)}${search}`, { replace }),
[hasLanguage, navigate, oldParams, rootPath, search],
(newParams: Params, replace = false) => {
const path = calculatePath(rootPath, oldParams, newParams, hasLanguage);

if (newParams.trash === true) {
searchParams.set('trash', 'true');
} else if (newParams.trash === false) {
searchParams.delete('trash');
}

return navigate(`${path}${searchParams.toString()}`, {
replace,
});
},
[hasLanguage, navigate, oldParams, rootPath, searchParams],
);

useEffect(() => {
Expand Down

0 comments on commit b6ddc18

Please sign in to comment.