Skip to content

Commit

Permalink
Redaktør delete draft button: Show correct label
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksson-daniel committed Nov 7, 2024
1 parent 105a501 commit fa789f2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDepublished, isPublished } from '@app/components/smart-editor-texts/functions/status-helpers';
import { useDeleteDraftVersionMutation } from '@app/redux-api/maltekstseksjoner/mutations';
import { useGetMaltekstseksjonVersionsQuery } from '@app/redux-api/maltekstseksjoner/queries';
import type { IGetMaltekstseksjonParams } from '@app/types/common-text-types';
Expand All @@ -17,9 +18,12 @@ export const DeleteMaltekstseksjonDraftButton = ({ id, title, onDraftDeleted, qu
const [isOpen, setIsOpen] = useState<boolean>(false);
const [, { isLoading }] = useDeleteDraftVersionMutation({ fixedCacheKey: id });
const { data: versions = [] } = useGetMaltekstseksjonVersionsQuery(id);
const lastPublishedVersion = useMemo(() => versions.find((version) => version.published), [versions]);
const willBeMovedToDepublished = useMemo(
() => versions.some(isDepublished) && !versions.some(isPublished),
[versions],
);

const text = lastPublishedVersion !== undefined ? 'Slett utkast og flytt til avpubliserte' : 'Slett utkast';
const text = willBeMovedToDepublished ? 'Slett utkast og sett maltekstseksjon som avpublisert' : 'Slett utkast';

if (isOpen) {
return (
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/components/smart-editor-texts/edit/footer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AllMaltekstseksjonReferences } from '@app/components/malteksteksjon-references/maltekstseksjon-references';
import { SavedStatus, type SavedStatusProps } from '@app/components/saved-status/saved-status';
import { isDepublished, isPublished } from '@app/components/smart-editor-texts/functions/status-helpers';
import { isoDateTimeToPretty } from '@app/domain/date';
import { isGodFormuleringType, isRegelverkType, isRichTextType } from '@app/functions/is-rich-plain-text';
import { useRedaktoerLanguage } from '@app/hooks/use-redaktoer-language';
Expand Down Expand Up @@ -27,7 +28,10 @@ interface Props {
export const Footer = ({ text, onDraftDeleted, status, onPublish, deleteTranslation, error }: Props) => {
const [, { isLoading: publishIsLoading }] = usePublishMutation({ fixedCacheKey: text.id });
const { data: versions = [] } = useGetTextVersionsQuery(text.id);
const lastPublishedVersion = useMemo(() => versions.find((version) => version.published), [versions]);
const willBeMovedToDepublished = useMemo(
() => versions.some(isDepublished) && !versions.some(isPublished),
[versions],
);

const { id, edits, publishedMaltekstseksjonIdList, draftMaltekstseksjonIdList, textType } = text;

Expand All @@ -43,7 +47,7 @@ export const Footer = ({ text, onDraftDeleted, status, onPublish, deleteTranslat
<DeleteLanguageVersion deleteTranslation={deleteTranslation} />
{isDraft ? (
<DeleteDraftButton id={id} title={text.title} onDraftDeleted={onDraftDeleted}>
{lastPublishedVersion !== undefined ? 'Slett utkast og flytt til avpubliserte' : 'Slett utkast'}
{willBeMovedToDepublished ? 'Slett utkast og sett tekst som avpublisert' : 'Slett utkast'}
</DeleteDraftButton>
) : null}
</Row>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Filterable {
published: boolean;
publishedDateTime: string | null;
}

export const isDepublished = ({ published, publishedDateTime }: Filterable) => !published && publishedDateTime !== null;
export const isDraft = ({ published, publishedDateTime }: Filterable) => !published && publishedDateTime === null;
export const isPublished = ({ published, publishedDateTime }: Filterable) => published && publishedDateTime !== null;
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { FilterDropdown } from '@app/components/filter-dropdown/filter-dropdown';
import {
type Filterable,
isDepublished,
isDraft,
isPublished,
} from '@app/components/smart-editor-texts/functions/status-helpers';
import { useSearchParams } from 'react-router-dom';

export const StatusFilter = () => {
Expand Down Expand Up @@ -38,15 +44,6 @@ const DEFAULT_STATUSES = [Status.PUBLISHED, Status.DRAFT];

export const DEFAULT_STATUS_FILTER = `${PARAM_KEY}=${DEFAULT_STATUSES.join(encodeURIComponent(','))}`;

interface Filterable {
published: boolean;
publishedDateTime: string | null;
}

const isDepublished = ({ published, publishedDateTime }: Filterable) => !published && publishedDateTime !== null;
const isDraft = ({ published, publishedDateTime }: Filterable) => !published && publishedDateTime === null;
const isPublished = ({ published, publishedDateTime }: Filterable) => published && publishedDateTime !== null;

export const filterByStatus = (filteredStatuses: Status[], text: Filterable) => {
if (filteredStatuses.length === 0) {
return true;
Expand Down

0 comments on commit fa789f2

Please sign in to comment.