Manage cache

This commit is contained in:
Chi Vinh Le
2018-06-04 22:30:12 +02:00
parent f142150b3e
commit 636bc9efa4
4 changed files with 90 additions and 59 deletions
+12 -58
View File
@@ -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: {
@@ -98,9 +98,15 @@ const fields = `
status {
username {
status
history {
status
}
}
}
}
action_summaries {
count
}
`;
const USERNAME_FLAGGED_SUBSCRIPTION = gql`
@@ -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:
}
}
@@ -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,
});
},
});
},
}),