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

feat: remove delete button #608

Merged
merged 2 commits into from
Nov 1, 2023
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
193 changes: 35 additions & 158 deletions src/components/form-control/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { memo, useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import { Variant } from '@fellesdatakatalog/button';

import { ConceptStatus, TimeFormat } from '../../types/enums';
import { formatTime } from '../../utils/date';
Expand All @@ -10,17 +9,13 @@ import {
patchConceptFromForm,
postConceptFromForm
} from '../../lib/patchConceptForm';
import { publishConceptFromForm } from '../../lib/publishConceptFromForm';
import { deleteConcept } from '../../api/concept-catalog-api';
import ConfirmDialog from '../confirm-dialog';

import SC from './styled';

interface Props<V> {
isFormDirty: boolean;
onNewConceptRevision: () => void;
onSave: (conceptId) => void;
onDelete: () => void;
isInitialInValidForm: boolean;
lastPatchedResponse: any;
values: V;
Expand All @@ -30,12 +25,9 @@ const FormControl = <V,>({
isFormDirty,
onNewConceptRevision,
onSave,
onDelete,
isInitialInValidForm,
lastPatchedResponse,
values
}: Props<V>) => {
const [showConfirmDelete, setShowConfirmDelete] = useState<boolean>(false);
const [isSticky, setSticky] = useState(false);
const [saveCalled, setSaveCalled] = useState(false);
const [newConceptId, setNewConceptId] = useState(null);
Expand All @@ -61,9 +53,6 @@ const FormControl = <V,>({
};
}, [debounce, handleScroll]);

const toggleShowConfirmDelete = (): void =>
setShowConfirmDelete(!showConfirmDelete);

const { conceptId } = useParams<{
conceptId: string;
}>();
Expand All @@ -72,7 +61,6 @@ const FormControl = <V,>({
const { concept } = conceptForm;
const erSistPublisert = concept?.erSistPublisert ?? false;
const published = concept?.erPublisert ?? false;
const validationError = conceptForm.isValidationError || isInitialInValidForm;
const isSaving = conceptForm.isSaving ?? false;
const justChangedStatus = conceptForm.justChangedStatus ?? false;
const errorSaving = conceptForm.error ?? false;
Expand Down Expand Up @@ -111,11 +99,6 @@ const FormControl = <V,>({
: '';
};

const confirmDelete = async (): Promise<void> => {
await deleteConcept(conceptId);
onDelete();
};

useEffect(() => {
const id = conceptId === 'new' ? newConceptId : conceptId;
if (saveCalled && id && !(isSaving || errorSaving)) {
Expand All @@ -124,151 +107,45 @@ const FormControl = <V,>({
}, [isSaving, errorSaving, saveCalled, newConceptId, onSave]);

return concept ? (
<>
<SC.FormControl $isSticky={isSticky}>
<SC.FormControlContent>
{isFormDirty && published && erSistPublisert && (
<SC.Button onClick={onNewConceptRevision}>
<SC.StatusDraftIcon />
{localization.saveDraft}
</SC.Button>
)}
{!published && (
<SC.Button
disabled={isSaving || !isFormDirty}
onClick={() => {
setSaveCalled(true);
if (concept?.id) {
patchConceptFromForm(values, {
concept,
dispatch,
lastPatchedResponse,
isSaving
});
} else {
postConceptFromForm(values, {
concept,
dispatch,
isSaving
})?.then(action => {
setNewConceptId(action.payload);
});
}
}}
>
{localization.save}
</SC.Button>
)}
{!published && (
<SC.StatusButton
$active={concept?.status === ConceptStatus.HOERING}
disabled={
validationError ||
(!!concept.revisjonAv && !concept.revisjonAvSistPublisert) ||
isFormDirty
}
onClick={() => {
setSaveCalled(true);
patchConceptFromForm(
{
status: ConceptStatus.HOERING,
...(concept.versjonsnr?.major === 0 &&
concept.versjonsnr?.minor === 0 &&
concept.versjonsnr?.patch === 1 && {
versjonsnr: { major: 1, minor: 0, patch: 0 }
})
},
{
concept,
dispatch,
lastPatchedResponse: concept,
isSaving
}
);
}}
>
<SC.StatusHearingIcon />
{localization.setToHoering}
</SC.StatusButton>
)}
{!published && (
<SC.StatusButton
$active={concept?.status === ConceptStatus.GODKJENT}
disabled={
validationError ||
(!!concept.revisjonAv && !concept.revisjonAvSistPublisert) ||
isFormDirty
}
onClick={() => {
setSaveCalled(true);
patchConceptFromForm(
{
status: ConceptStatus.GODKJENT,
...(concept.versjonsnr?.major === 0 &&
concept.versjonsnr?.minor === 0 &&
concept.versjonsnr?.patch === 1 && {
versjonsnr: { major: 1, minor: 0, patch: 0 }
})
},
{
concept,
dispatch,
lastPatchedResponse: concept,
isSaving
}
);
}}
>
<SC.StatusApprovedIcon />
{localization.setToApproval}
</SC.StatusButton>
)}
{!published && (
<SC.StatusButton
disabled={
validationError ||
(!!concept.revisjonAv && !concept.revisjonAvSistPublisert) ||
isFormDirty
}
onClick={() => {
setSaveCalled(true);
publishConceptFromForm({
<SC.FormControl $isSticky={isSticky}>
<SC.FormControlContent>
{isFormDirty && published && erSistPublisert && (
<SC.Button onClick={onNewConceptRevision}>
<SC.StatusDraftIcon />
{localization.saveDraft}
</SC.Button>
)}
{!published && (
<SC.Button
disabled={isSaving || !isFormDirty}
onClick={() => {
setSaveCalled(true);
if (concept?.id) {
patchConceptFromForm(values, {
concept,
dispatch,
lastPatchedResponse: concept,
lastPatchedResponse,
isSaving
});
}}
>
<SC.StatusPublishedIcon />
{localization.publish}
</SC.StatusButton>
)}
<div>
<span>{createMessage()}</span>
</div>

{!published && concept?.id && (
<SC.DeleteButton
variant={Variant.TERTIARY}
disabled={isSaving}
onClick={toggleShowConfirmDelete}
>
<SC.RemoveIcon />
{localization.deleteDraft}
</SC.DeleteButton>
)}
</SC.FormControlContent>
</SC.FormControl>
{showConfirmDelete && (
<ConfirmDialog
title={localization.confirmDeleteTitle}
text={localization.confirmDeleteMessage}
onConfirm={confirmDelete}
onCancel={toggleShowConfirmDelete}
/>
)}
</>
} else {
postConceptFromForm(values, {
concept,
dispatch,
isSaving
})?.then(action => {
setNewConceptId(action.payload);
});
}
}}
>
{localization.save}
</SC.Button>
)}
<div>
<span>{createMessage()}</span>
</div>
</SC.FormControlContent>
</SC.FormControl>
) : null;
};

Expand Down
4 changes: 0 additions & 4 deletions src/l10n/nb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export const nb = {
help: 'Hjelp',
moreRecomendations: 'Flere anbefalinger',
lessRecomendations: 'Færre anbefalinger',
confirmDeleteTitle: 'Bekreft sletting',
confirmDeleteMessage: 'Bekreft at du vil slette dette begrepet.',
cancelDelete: 'Avbryt',
requiredFieldsMissing: 'Obligatoriske felt mangler',
concept: 'Begrep',
conceptCatalog: 'Begrepskatalog',
Expand Down Expand Up @@ -89,7 +86,6 @@ export const nb = {
saveDraft: 'Lagre som nytt utkast',
version: 'Versjon',
exPublished: 'Ekspublisert',
deleteDraft: 'Slett utkast',
confirm: 'Bekreft',
cancel: 'Avbryt',
unsavedPrompt: 'Endringene er ikke lagret, vil du forkaste endringene?',
Expand Down
17 changes: 3 additions & 14 deletions src/pages/concept-registration-page/form-concept/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import { schema as validationSchema } from './form-concept.schema';

import SC from './styled';
import { InternalInfo } from './internal-info';
import { getConfig } from '../../../config';
import { setValidationError } from '../../../features/conceptForm';

export const validateWithPreProcess = (
Expand Down Expand Up @@ -123,11 +122,8 @@ export const FormConceptPure: FC<Props> = ({
} = errors;
const [showUserPrompt, setShowUserPrompt] = useState<boolean>(true);
const [saveCalled, setSaveCalled] = useState<boolean>(false);
const [deleteCalled, setDeleteCalled] = useState<boolean>(false);
const [newConceptId, setNewConceptId] = useState(null);

const config = getConfig();

const languageEntities = useAppSelector(state => state.languages.entities);

const appDispatch = useAppDispatch();
Expand Down Expand Up @@ -178,20 +174,16 @@ export const FormConceptPure: FC<Props> = ({
event.returnValue = '';
};
// if the form is NOT unchanged, then set the onbeforeunload
if (dirty && showUserPrompt && !(saveCalled || deleteCalled)) {
if (dirty && showUserPrompt && !saveCalled) {
window.addEventListener('beforeunload', handler);
// clean it up, if the dirty state changes
return () => {
window.removeEventListener('beforeunload', handler);
};
}

if (deleteCalled) {
window.location.href = `${config.conceptCatalogFrontendBaseUri}/${catalogId}`;
}
// since this is not dirty, don't do anything
return () => {};
}, [dirty, showUserPrompt, saveCalled, deleteCalled]);
}, [dirty, showUserPrompt, saveCalled]);

useEffect(() => {
if (saveCalled) {
Expand All @@ -206,7 +198,7 @@ export const FormConceptPure: FC<Props> = ({
return (
<SC.Page>
<Prompt
when={dirty && showUserPrompt && !(saveCalled || deleteCalled)}
when={dirty && showUserPrompt && !saveCalled}
message={localization.unsavedPrompt}
/>
<Can
Expand All @@ -220,9 +212,6 @@ export const FormConceptPure: FC<Props> = ({
setSaveCalled(true);
setNewConceptId(id);
}}
onDelete={() => {
setDeleteCalled(true);
}}
isInitialInValidForm={!isValid}
lastPatchedResponse={lastPatchedResponse}
values={values}
Expand Down
Loading