diff --git a/client/coral-admin/src/graphql/index.js b/client/coral-admin/src/graphql/index.js index 6a1b06a6e..b51897a4b 100644 --- a/client/coral-admin/src/graphql/index.js +++ b/client/coral-admin/src/graphql/index.js @@ -194,6 +194,12 @@ export default { }, updateQueries: { TalkAdmin_Community_FlaggedAccounts: (prev, { mutationResult }) => { + // No need to update, when user was not in the flagged users queue. + // TODO: this should be more generic, e.g. looking at the history. + if (!prev.flaggedUsers.nodes.find(node => node.id === id)) { + return prev; + } + const decrement = { flaggedUsernamesCount: { $apply: count => count - 1 }, }; @@ -214,35 +220,6 @@ export default { return updated; }, }, - update: proxy => { - proxy.writeFragment({ - fragment: gql` - fragment Talk_ApproveUsername on User { - state { - status { - username { - status - } - } - } - } - `, - id: `User_${id}`, - data: { - __typename: 'User', - state: { - __typename: 'UserState', - status: { - __typename: 'UserStatus', - username: { - __typename: 'UsernameStatus', - status: 'APPROVED', - }, - }, - }, - }, - }); - }, }), RejectUsername: ({ variables: { id } }) => ({ optimisticResponse: { @@ -254,6 +231,12 @@ export default { }, updateQueries: { TalkAdmin_Community_FlaggedAccounts: (prev, { mutationResult }) => { + // No need to update, when user was not in the flagged users queue. + // TODO: this should be more generic, e.g. looking at the history. + if (!prev.flaggedUsers.nodes.find(node => node.id === id)) { + return prev; + } + const decrement = { flaggedUsernamesCount: { $apply: count => count - 1 }, }; @@ -274,35 +257,6 @@ export default { return updated; }, }, - update: proxy => { - proxy.writeFragment({ - fragment: gql` - fragment Talk_RejectUsername on User { - state { - status { - username { - status - } - } - } - } - `, - id: `User_${id}`, - data: { - __typename: 'User', - state: { - __typename: 'UserState', - status: { - __typename: 'UserStatus', - username: { - __typename: 'UsernameStatus', - status: 'REJECTED', - }, - }, - }, - }, - }); - }, }), UpdateSettings: ({ variables: { input } }) => ({ updateQueries: { diff --git a/client/coral-admin/src/routes/Community/containers/Indicator.js b/client/coral-admin/src/routes/Community/containers/Indicator.js index 9e5be831a..3c078a8d7 100644 --- a/client/coral-admin/src/routes/Community/containers/Indicator.js +++ b/client/coral-admin/src/routes/Community/containers/Indicator.js @@ -98,9 +98,15 @@ const fields = ` status { username { status + history { + status + } } } } + action_summaries { + count + } `; const USERNAME_FLAGGED_SUBSCRIPTION = gql` diff --git a/client/coral-admin/src/routes/Community/graphql.js b/client/coral-admin/src/routes/Community/graphql.js index 8d6941c41..c106fdd61 100644 --- a/client/coral-admin/src/routes/Community/graphql.js +++ b/client/coral-admin/src/routes/Community/graphql.js @@ -106,6 +106,23 @@ export function handleFlaggedAccountsChange(root, user, notify) { } } +export const wasUsernameReported = user => { + const previousStatus = + user.state.status.username.history[ + user.state.status.username.history.length - 2 + ]; + + // Check for correct previous status + if (!['SET', 'CHANGES'].includes(previousStatus)) { + return false; + } + // Check for flags + if (user.action_summaries.every(as => as.count === 0)) { + return false; + } + return true; +}; + /** * Track indicator status * @param {Object} root current state of the store @@ -119,7 +136,9 @@ export function handleIndicatorChange(root, user) { return incrementFlaggedUserCount(root); case 'APPROVED': case 'REJECTED': - return decrementFlaggedUserCount(root); + if (wasUsernameReported(user)) { + return decrementFlaggedUserCount(root); + } default: } } diff --git a/client/coral-framework/graphql/mutations.js b/client/coral-framework/graphql/mutations.js index 722d8ea98..245fe2b2f 100644 --- a/client/coral-framework/graphql/mutations.js +++ b/client/coral-framework/graphql/mutations.js @@ -242,6 +242,16 @@ export const withUnsuspendUser = withMutation( } ); +const SetUsernameStatusFragment = gql` + fragment Talk_SetUsernameStatus on User { + state { + status { + username + } + } + } +`; + export const withApproveUsername = withMutation( gql` mutation ApproveUsername($id: ID!) { @@ -257,6 +267,27 @@ export const withApproveUsername = withMutation( variables: { id, }, + update: proxy => { + const fragmentId = `User_${id}`; + const data = { + __typename: 'User', + state: { + __typename: 'UserState', + status: { + __typename: 'UserStatus', + username: { + __typename: 'UsernameStatus', + status: 'APPROVED', + }, + }, + }, + }; + proxy.writeFragment({ + fragment: SetUsernameStatusFragment, + id: fragmentId, + data, + }); + }, }); }, }), @@ -278,6 +309,27 @@ export const withRejectUsername = withMutation( variables: { id, }, + update: proxy => { + const fragmentId = `User_${id}`; + const data = { + __typename: 'User', + state: { + __typename: 'UserState', + status: { + __typename: 'UserStatus', + username: { + __typename: 'UsernameStatus', + status: 'REJECTED', + }, + }, + }, + }; + proxy.writeFragment({ + fragment: SetUsernameStatusFragment, + id: fragmentId, + data, + }); + }, }); }, }),