Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
MM-39046 Don't modify all drafts on page load (#9039)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmhealey committed Oct 5, 2021
1 parent 6aa94c1 commit 98c5b82
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 110 deletions.
21 changes: 3 additions & 18 deletions actions/storage.js
Expand Up @@ -62,24 +62,9 @@ export function clear(options = {exclude: []}) {
}

export function actionOnGlobalItemsWithPrefix(prefix, action) {
return (dispatch) => {
dispatch({
type: StorageTypes.ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX,
data: {prefix, action},
});
return {data: true};
};
}

export function actionOnItemsWithPrefix(prefix, action) {
return (dispatch, getState) => {
const state = getState();
const globalPrefix = getPrefix(state);
dispatch({
type: StorageTypes.ACTION_ON_ITEMS_WITH_PREFIX,
data: {globalPrefix, prefix, action},
});
return {data: true};
return {
type: StorageTypes.ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX,
data: {prefix, action},
};
}

Expand Down
17 changes: 0 additions & 17 deletions actions/storage.test.js
Expand Up @@ -70,23 +70,6 @@ describe('Actions.Storage', () => {
);
});

it('actionOnItemsWithPrefix', async () => {
store.dispatch(Actions.setItem('prefix_test1', 1));
store.dispatch(Actions.setItem('prefix_test2', 2));
store.dispatch(Actions.setItem('not_prefix_test', 3));

const touchedPairs = [];
store.dispatch(Actions.actionOnItemsWithPrefix(
'prefix',
(key, value) => touchedPairs.push([key, value]),
));

assert.deepEqual(
touchedPairs,
[['prefix_test1', 1], ['prefix_test2', 2]],
);
});

it('clear', async () => {
store.dispatch(Actions.setGlobalItem('key', 'value'));
store.dispatch(Actions.setGlobalItem('excluded', 'not-cleared'));
Expand Down
9 changes: 5 additions & 4 deletions actions/views/create_comment.tsx
Expand Up @@ -34,11 +34,12 @@ import {GlobalState} from 'types/store';
import {DispatchFunc, GetStateFunc} from 'mattermost-redux/types/actions';

export function clearCommentDraftUploads() {
return actionOnGlobalItemsWithPrefix(StoragePrefixes.COMMENT_DRAFT, (_key: string, value: PostDraft) => {
if (value) {
return {...value, uploadsInProgress: []};
return actionOnGlobalItemsWithPrefix(StoragePrefixes.COMMENT_DRAFT, (_key: string, draft: PostDraft) => {
if (!draft || draft.uploadsInProgress.length === 0) {
return draft;
}
return value;

return {...draft, uploadsInProgress: []};
});
}

Expand Down
9 changes: 2 additions & 7 deletions components/create_post/create_post.tsx
Expand Up @@ -256,7 +256,7 @@ type Props = {
/**
* func called on load of component to clear drafts
*/
clearDraftUploads: (prefix: string, action: (key: string, value?: PostDraft) => PostDraft | undefined) => void;
clearDraftUploads: () => void;

/**
* hooks called before a message is sent to the server
Expand Down Expand Up @@ -389,12 +389,7 @@ class CreatePost extends React.PureComponent<Props, State> {
const {useGroupMentions, currentChannel, isTimezoneEnabled, actions} = this.props;
this.onOrientationChange();
actions.setShowPreview(false);
actions.clearDraftUploads(StoragePrefixes.DRAFT, (key, value) => {
if (value) {
return {...value, uploadsInProgress: []};
}
return value;
});
actions.clearDraftUploads();
this.focusTextbox();
document.addEventListener('paste', this.pasteHandler);
document.addEventListener('keydown', this.documentKeyHandler);
Expand Down
14 changes: 12 additions & 2 deletions components/create_post/index.ts
Expand Up @@ -146,7 +146,7 @@ type Actions = {
addReaction: (postId: string, emojiName: string) => void;
onSubmitPost: (post: Post, fileInfos: FileInfo[]) => void;
removeReaction: (postId: string, emojiName: string) => void;
clearDraftUploads: (prefix: string, action: (key: string, value?: PostDraft) => PostDraft | undefined) => void;
clearDraftUploads: () => void;
runMessageWillBePostedHooks: (originalPost: Post) => ActionResult;
runSlashCommandWillBePostedHooks: (originalMessage: string, originalArgs: CommandArgs) => ActionResult;
setDraft: (name: string, value: PostDraft | null) => void;
Expand All @@ -173,6 +173,16 @@ function setDraft(key: string, value: PostDraft) {
return setGlobalItem(key, value);
}

function clearDraftUploads() {
return actionOnGlobalItemsWithPrefix(StoragePrefixes.DRAFT, (_key: string, draft: PostDraft) => {
if (!draft || draft.uploadsInProgress.length === 0) {
return draft;
}

return {...draft, uploadsInProgress: []};
});
}

function mapDispatchToProps(dispatch: Dispatch<GenericAction>) {
return {
actions: bindActionCreators<ActionCreatorsMapObject<any>, Actions>({
Expand All @@ -183,7 +193,7 @@ function mapDispatchToProps(dispatch: Dispatch<GenericAction>) {
addReaction,
removeReaction,
setDraft,
clearDraftUploads: actionOnGlobalItemsWithPrefix,
clearDraftUploads,
selectPostFromRightHandSideSearchByPostId,
setEditingPost,
emitShortcutReactToLastPostFrom,
Expand Down
41 changes: 18 additions & 23 deletions reducers/storage.js
Expand Up @@ -8,8 +8,6 @@ import {General} from 'mattermost-redux/constants';
import {StorageTypes} from 'utils/constants';

function storage(state = {}, action) {
var key;

switch (action.type) {
case StorageTypes.SET_ITEM: {
if (!state[action.data.prefix + action.data.name] ||
Expand Down Expand Up @@ -62,30 +60,27 @@ function storage(state = {}, action) {
}
case StorageTypes.ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX: {
const nextState = {...state};
for (key in state) {
if (key.lastIndexOf(action.data.prefix, 0) === 0) {
nextState[key] = {
timestamp: new Date(),
value: action.data.action(key, state[key].value),
};
let changed = false;

for (const key of Object.keys(nextState)) {
if (!key.startsWith(action.data.prefix)) {
continue;
}
}
return nextState;
}
case StorageTypes.ACTION_ON_ITEMS_WITH_PREFIX: {
const nextState = {...state};
var globalPrefix = action.data.globalPrefix;
var globalPrefixLen = action.data.globalPrefix.length;
for (key in state) {
if (key.lastIndexOf(globalPrefix + action.data.prefix, 0) === 0) {
var userkey = key.substring(globalPrefixLen);
nextState[key] = {
timestamp: new Date(),
value: action.data.action(userkey, state[key].value),
};

const value = nextState[key].value;
const nextValue = action.data.action(key, value);
if (value === nextValue) {
continue;
}

nextState[key] = {
timestamp: new Date(),
value: action.data.action(key, state[key].value),
};
changed = true;
}
return nextState;

return changed ? nextState : state;
}
case StorageTypes.STORAGE_REHYDRATE: {
return {...state, ...action.data};
Expand Down
74 changes: 36 additions & 38 deletions reducers/storage.test.js
Expand Up @@ -9,7 +9,7 @@ import {StorageTypes} from 'utils/constants';
describe('Reducers.Storage', () => {
const now = new Date();

it('Storage.SET_ITEM', async () => {
it('Storage.SET_ITEM', () => {
const nextState = storageReducer(
{
storage: {},
Expand All @@ -32,7 +32,7 @@ describe('Reducers.Storage', () => {
);
});

it('Storage.SET_GLOBAL_ITEM', async () => {
it('Storage.SET_GLOBAL_ITEM', () => {
const nextState = storageReducer(
{
storage: {},
Expand All @@ -54,7 +54,7 @@ describe('Reducers.Storage', () => {
);
});

it('Storage.REMOVE_ITEM', async () => {
it('Storage.REMOVE_ITEM', () => {
var nextState = storageReducer(
{
storage: {
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('Reducers.Storage', () => {
);
});

it('Storage.REMOVE_GLOBAL_ITEM', async () => {
it('Storage.REMOVE_GLOBAL_ITEM', () => {
var nextState = storageReducer(
{
storage: {
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('Reducers.Storage', () => {
);
});

it('Storage.CLEAR', async () => {
it('Storage.CLEAR', () => {
const nextState = storageReducer(
{
storage: {
Expand All @@ -149,56 +149,54 @@ describe('Reducers.Storage', () => {
);
});

it('Storage.ACTION_ON_ITEMS_WITH_PREFIX', async () => {
var touchedPairs = [];
storageReducer(
{
describe('Storage.ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX', () => {
it('should call the provided action on the given objects', () => {
const state = storageReducer({
storage: {
user_id_prefix_key1: {value: 1, timestamp: now},
user_id_prefix_key2: {value: 2, timestamp: now},
user_id_not_prefix_key: {value: 3, timestamp: now},
prefix_key1: {value: 1, timestamp: now},
prefix_key2: {value: 2, timestamp: now},
not_prefix_key: {value: 3, timestamp: now},
},
},
{
type: StorageTypes.ACTION_ON_ITEMS_WITH_PREFIX,
}, {});

const nextState = storageReducer(state, {
type: StorageTypes.ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX,
data: {
globalPrefix: 'user_id_',
prefix: 'prefix',
action: (key, value) => touchedPairs.push([key, value]),
action: (key, value) => value + 5,
},
},
);
assert.deepEqual(
touchedPairs,
[['prefix_key1', 1], ['prefix_key2', 2]],
);
});
});

it('Storage.ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX', async () => {
var touchedPairs = [];
storageReducer(
{
expect(nextState).not.toBe(state);
expect(nextState.storage.prefix_key1.value).toBe(6);
expect(nextState.storage.prefix_key1.timestamp).not.toBe(now);
expect(nextState.storage.prefix_key2.value).toBe(7);
expect(nextState.storage.prefix_key2.timestamp).not.toBe(now);
expect(nextState.storage.prefix_key3).toBe(state.storage.prefix_key3);
});

it('should return the original state if no results change', () => {
const state = storageReducer({
storage: {
prefix_key1: {value: 1, timestamp: now},
prefix_key2: {value: 2, timestamp: now},
not_prefix_key: {value: 3, timestamp: now},
},
},
{
}, {});

const nextState = storageReducer(state, {
type: StorageTypes.ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX,
data: {
prefix: 'prefix',
action: (key, value) => touchedPairs.push([key, value]),
action: (key, value) => value,
},
},
);
assert.deepEqual(
touchedPairs,
[['prefix_key1', 1], ['prefix_key2', 2]],
);
});

expect(nextState).toBe(state);
});
});

it('Storage.STORAGE_REHYDRATE', async () => {
it('Storage.STORAGE_REHYDRATE', () => {
var nextState = storageReducer(
{
storage: {},
Expand Down
1 change: 0 additions & 1 deletion utils/constants.jsx
Expand Up @@ -594,7 +594,6 @@ export const StorageTypes = keyMirror({
REMOVE_GLOBAL_ITEM: null,
CLEAR: null,
ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX: null,
ACTION_ON_ITEMS_WITH_PREFIX: null,
STORAGE_REHYDRATE: null,
});

Expand Down

0 comments on commit 98c5b82

Please sign in to comment.