Skip to content

Commit 28ee656

Browse files
committed
feat: handle deleted post
1 parent d1d075b commit 28ee656

File tree

4 files changed

+190
-93
lines changed

4 files changed

+190
-93
lines changed

web-marketplace/src/components/organisms/PostFlow/PostFlow.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
DRAFT_SUBMIT_LABEL,
4949
PUBLISH_SUBMIT_LABEL,
5050
} from './PostFlow.constants';
51+
import { DeletedDraftModal } from './PostFlow.DeletedDraftModal';
5152
import { EditedDraftModal } from './PostFlow.EditedDraftModal';
5253
import { SignModal } from './PostFlow.SignModal';
5354

@@ -92,6 +93,9 @@ export const PostFlow = ({
9293
const [isDraftEditedModalLabel, setIsDraftEditedModalLabel] = useState<
9394
string | undefined
9495
>(undefined);
96+
const [isDraftDeletedModalLabel, setIsDraftDeletedModalLabel] = useState<
97+
string | undefined
98+
>(undefined);
9599
const [editedData, setEditedData] = useState<PostFormSchemaType | undefined>(
96100
undefined,
97101
);
@@ -194,6 +198,9 @@ export const PostFlow = ({
194198
async (data: PostFormSchemaType) => {
195199
// Check if draft post has been updated in the meantime
196200
if (draftPostId && initialValues?.updatedAt) {
201+
const modalLabel = data.published
202+
? PUBLISH_SUBMIT_LABEL
203+
: DRAFT_SUBMIT_LABEL;
197204
try {
198205
const resp = await fetch(
199206
`${apiUri}/marketplace/v1/posts/by-id/${draftPostId}`,
@@ -203,6 +210,12 @@ export const PostFlow = ({
203210
},
204211
);
205212
if (resp.status !== 200) {
213+
if (resp.status === 404) {
214+
// We set iri to undefined to re-create the post
215+
setEditedData({ ...data, iri: undefined });
216+
setIsDraftDeletedModalLabel(modalLabel);
217+
return;
218+
}
206219
throw new Error(
207220
_(msg`Cannot get existing post: ${resp.statusText}`),
208221
);
@@ -215,9 +228,7 @@ export const PostFlow = ({
215228
) {
216229
// We need to overwrite iri in case it has changed
217230
setEditedData({ ...data, iri: existingPost.iri });
218-
setIsDraftEditedModalLabel(
219-
data.published ? PUBLISH_SUBMIT_LABEL : DRAFT_SUBMIT_LABEL,
220-
);
231+
setIsDraftEditedModalLabel(modalLabel);
221232
return;
222233
}
223234
} catch (e) {
@@ -378,7 +389,7 @@ export const PostFlow = ({
378389
bodyText={_(DISCARD_CHANGES_BODY)}
379390
buttonText={_(DISCARD_CHANGES_BUTTON)}
380391
/>
381-
{editedData && (
392+
{editedData && isDraftEditedModalLabel && (
382393
<EditedDraftModal
383394
open={!!isDraftEditedModalLabel}
384395
shouldSaveDraft={isDraftEditedModalLabel === DRAFT_SUBMIT_LABEL}
@@ -390,6 +401,18 @@ export const PostFlow = ({
390401
onClose={() => setIsDraftEditedModalLabel(undefined)}
391402
/>
392403
)}
404+
{editedData && isDraftDeletedModalLabel && (
405+
<DeletedDraftModal
406+
open={!!isDraftDeletedModalLabel}
407+
shouldSaveDraft={isDraftDeletedModalLabel === DRAFT_SUBMIT_LABEL}
408+
onCancel={() => setIsDraftDeletedModalLabel(undefined)}
409+
onSubmit={async () => {
410+
setIsDraftDeletedModalLabel(undefined);
411+
await saveDataPost(editedData);
412+
}}
413+
onClose={() => setIsDraftDeletedModalLabel(undefined)}
414+
/>
415+
)}
393416
</>
394417
);
395418
};

web-marketplace/src/components/organisms/PostFlow/hooks/useSign.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ export const useSign = ({
152152
// Validate that organization option is available when user selects org
153153
if (signAs === ORG && !withOrganization) {
154154
throw new Error(
155-
'Cannot sign as organization: missing authorization. You may not have the required role or permissions to sign on behalf of the organization.',
155+
_(
156+
msg`Cannot sign as organization: missing authorization. You may not have the required role or permissions to sign on behalf of the organization.`,
157+
),
156158
);
157159
}
158160

0 commit comments

Comments
 (0)