Skip to content

Commit

Permalink
fix: data persist on mails editor (#126)
Browse files Browse the repository at this point in the history
fix: data persist on mails editor
  • Loading branch information
nubsthead authored Jul 21, 2022
1 parent 4ed61ad commit 9ff8cca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
19 changes: 10 additions & 9 deletions src/store/editor-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ function createEditorReducer(
: ['', ''];

if (payload.action) {
const editorWithAction = { ...empty, action: payload.action };
switch (payload.action) {
case ActionsType.NEW:
state.editors[payload.editorId] = empty;
state.editors[payload.editorId] = editorWithAction;
break;
case ActionsType.MAIL_TO:
if (payload?.boardContext?.contacts && payload?.boardContext?.contacts?.length > 0) {
state.editors[payload.editorId] = {
...empty,
...editorWithAction,
to: [payload.boardContext.contacts[0]],
cc: drop(payload.boardContext.contacts, 1)
};
Expand All @@ -105,7 +106,7 @@ function createEditorReducer(
case ActionsType.EDIT_AS_DRAFT:
if (payload.original) {
state.editors[payload.editorId] = {
...empty,
...editorWithAction,
id: payload.id,
text: extractBody(payload.original),
to: retrieveTO(payload.original),
Expand All @@ -123,7 +124,7 @@ function createEditorReducer(
case ActionsType.EDIT_AS_NEW:
if (payload.original) {
state.editors[payload.editorId] = {
...empty,
...editorWithAction,
id: payload.id,
subject: payload.original.subject,
attach: { mp: retrieveAttachmentsType(payload.original, 'attachment') },
Expand All @@ -140,7 +141,7 @@ function createEditorReducer(
case ActionsType.REPLY:
if (payload.original) {
state.editors[payload.editorId] = {
...empty,
...editorWithAction,
id: payload.id,
text: textWithSignatureRepliesForwards,
to: retrieveReplyTo(payload.original),
Expand All @@ -158,7 +159,7 @@ function createEditorReducer(
case ActionsType.REPLY_ALL:
if (payload.original && payload.accounts) {
state.editors[payload.editorId] = {
...empty,
...editorWithAction,
text: textWithSignatureRepliesForwards,
to: retrieveALL(payload.original, payload.accounts),
cc: retrieveCC(payload.original, payload.accounts),
Expand All @@ -176,7 +177,7 @@ function createEditorReducer(
case ActionsType.FORWARD:
if (payload.original) {
state.editors[payload.editorId] = {
...empty,
...editorWithAction,
text: textWithSignatureRepliesForwards,
subject: `FWD: ${payload.original.subject.replace(FORWARD_REGEX, '')}`,
original: payload.original,
Expand All @@ -190,13 +191,13 @@ function createEditorReducer(
break;
case ActionsType.COMPOSE:
state.editors[payload.editorId] = {
...empty,
...editorWithAction,
...(payload.boardContext?.compositionData ?? {})
};
break;
case ActionsType.PREFILL_COMPOSE:
state.editors[payload.editorId] = {
...empty,
...editorWithAction,
...(payload.boardContext?.compositionData ?? {})
};
break;
Expand Down
34 changes: 13 additions & 21 deletions src/views/app/detail-panel/edit/edit-view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ export default function EditView({ mailId, folderId, setHeader, toggleAppBoard }
const [timer, setTimer] = useState(null);

const [loading, setLoading] = useState(false);
const [initialAction, setInitialAction] = useState(action);
const [actionChanged, setActionChanged] = useState(true);
const [isUploading, setIsUploading] = useState(false);

const [showRouteGuard, setShowRouteGuard] = useState(true);
Expand All @@ -99,11 +97,18 @@ export default function EditView({ mailId, folderId, setHeader, toggleAppBoard }

const editorId = useMemo(() => activeMailId ?? generateId(), [activeMailId]);

const isSameAction = useMemo(() => {
if (editors[editorId]) {
return editors[editorId].action === action;
}
return undefined;
}, [action, editorId, editors]);

useEffect(() => {
if (actionChanged && editors[editorId]) {
if (!isSameAction && editors[editorId]) {
dispatch(closeEditor(editorId));
}
}, [actionChanged, dispatch, editorId, editors]);
}, [isSameAction, dispatch, editorId, editors]);

const updateEditorCb = useCallback(
(data) => {
Expand Down Expand Up @@ -168,29 +173,15 @@ export default function EditView({ mailId, folderId, setHeader, toggleAppBoard }
}
}, [editor?.subject, setHeader, updateBoard, action, t]);

useEffect(() => {
if (action !== initialAction) {
setActionChanged(true);
setInitialAction(action);
}
}, [action, initialAction]);

useEffect(() => {
if (editors[editorId] && actionChanged) {
setActionChanged(false);
}
}, [actionChanged, editorId, editors]);

useEffect(() => {
if (
(activeMailId && messages?.[activeMailId]?.isComplete) ||
action === ActionsType.NEW ||
action === ActionsType.PREFILL_COMPOSE ||
action === ActionsType.COMPOSE ||
action === ActionsType.MAIL_TO ||
actionChanged
action === ActionsType.MAIL_TO
) {
if (!editors[editorId] || actionChanged) {
if (!editors[editorId] || isSameAction === false) {
setLoading(true);
dispatch(
createEditor({
Expand Down Expand Up @@ -221,7 +212,7 @@ export default function EditView({ mailId, folderId, setHeader, toggleAppBoard }
}, [
accounts,
action,
actionChanged,
isSameAction,
activeMailId,
boardContext,
change,
Expand Down Expand Up @@ -359,6 +350,7 @@ export default function EditView({ mailId, folderId, setHeader, toggleAppBoard }
createSnackbar,
folderId: FOLDERS.TRASH
}).click();
dispatch(closeEditor(editorId));
}}
/>
<EditViewContext.Provider value={context}>
Expand Down

0 comments on commit 9ff8cca

Please sign in to comment.