Merge pull request #1294 from coralproject/close-asset-now

closeAsset Mutation
This commit is contained in:
Kim Gardner
2018-01-18 12:07:47 -05:00
committed by GitHub
6 changed files with 104 additions and 21 deletions
@@ -3,15 +3,15 @@ import { gql, compose } from 'react-apollo';
import { withFragments } from 'coral-framework/hocs';
import AssetStatusInfo from '../components/AssetStatusInfo';
import PropTypes from 'prop-types';
import { withUpdateAssetStatus } from 'coral-framework/graphql/mutations';
import {
withUpdateAssetStatus,
withCloseAsset,
} from 'coral-framework/graphql/mutations';
class AssetStatusInfoContainer extends React.Component {
openAsset = () =>
this.props.updateAssetStatus(this.props.asset.id, { closedAt: null });
closeAsset = () =>
this.props.updateAssetStatus(this.props.asset.id, {
closedAt: new Date().toISOString(),
});
closeAsset = () => this.props.closeAsset(this.props.asset.id);
render() {
return (
@@ -29,6 +29,7 @@ class AssetStatusInfoContainer extends React.Component {
AssetStatusInfoContainer.propTypes = {
asset: PropTypes.object.isRequired,
updateAssetStatus: PropTypes.func.isRequired,
closeAsset: PropTypes.func.isRequired,
};
const withAssetStatusInfoFragments = withFragments({
@@ -41,6 +42,10 @@ const withAssetStatusInfoFragments = withFragments({
`,
});
const enhance = compose(withAssetStatusInfoFragments, withUpdateAssetStatus);
const enhance = compose(
withAssetStatusInfoFragments,
withUpdateAssetStatus,
withCloseAsset
);
export default enhance(AssetStatusInfoContainer);
+16 -15
View File
@@ -3,27 +3,28 @@ import { createDefaultResponseFragments } from '../utils';
// fragments defined here are automatically registered.
export default {
...createDefaultResponseFragments(
'SetUserRoleResponse',
'ChangeUsernameResponse',
'SetUsernameResponse',
'BanUsersResponse',
'UnbanUserResponse',
'SetUserSuspensionStatusResponse',
'SetCommentStatusResponse',
'SetUsernameStatusResponse',
'UnsuspendUserResponse',
'SuspendUserResponse',
'ChangeUsernameResponse',
'CloseAssetResponse',
'CreateCommentResponse',
'CreateFlagResponse',
'EditCommentResponse',
'PostFlagResponse',
'CreateDontAgreeResponse',
'CreateFlagResponse',
'DeleteActionResponse',
'ModifyTagResponse',
'EditCommentResponse',
'IgnoreUserResponse',
'ModifyTagResponse',
'PostFlagResponse',
'SetCommentStatusResponse',
'SetUsernameResponse',
'SetUsernameStatusResponse',
'SetUserRoleResponse',
'SetUserSuspensionStatusResponse',
'StopIgnoringUserResponse',
'UpdateSettingsResponse',
'SuspendUserResponse',
'UnbanUserResponse',
'UnsuspendUserResponse',
'UpdateAssetSettingsResponse',
'UpdateAssetStatusResponse'
'UpdateAssetStatusResponse',
'UpdateSettingsResponse'
),
};
@@ -665,3 +665,46 @@ export const withUpdateAssetStatus = withMutation(
}),
}
);
export const withCloseAsset = withMutation(
gql`
mutation CloseAsset($id: ID!) {
closeAsset(id: $id) {
...CloseAssetResponse
}
}
`,
{
props: ({ mutate }) => ({
closeAsset: id => {
return mutate({
variables: {
id,
},
optimisticResponse: {
closeAsset: {
__typename: 'CloseAssetResponse',
errors: null,
},
},
update: proxy => {
const fragment = gql`
fragment Talk_CloseAssetResponse on Asset {
closedAt
isClosed
}
`;
const fragmentId = `Asset_${id}`;
const data = {
__typename: 'Asset',
closedAt: new Date(),
isClosed: true,
};
proxy.writeFragment({ fragment, id: fragmentId, data });
},
});
},
}),
}
);
+20
View File
@@ -38,11 +38,30 @@ const updateStatus = async (ctx, id, { closedAt, closedMessage }) =>
}
);
/**
* closeNow will close an asset for commenting.
*
* @param {Object} ctx graphql context
* @param {String} id the asset's id to close
*/
const closeNow = async (ctx, id) =>
AssetModel.update(
{
id,
},
{
$set: {
closedAt: new Date(),
},
}
);
module.exports = ctx => {
let mutators = {
Asset: {
updateSettings: () => Promise.reject(errors.ErrNotAuthorized),
updateStatus: () => Promise.reject(errors.ErrNotAuthorized),
closeNow: () => Promise.reject(errors.ErrNotAuthorized),
},
};
@@ -55,6 +74,7 @@ module.exports = ctx => {
if (ctx.user.can(UPDATE_ASSET_STATUS)) {
mutators.Asset.updateStatus = (id, status) =>
updateStatus(ctx, id, status);
mutators.Asset.closeNow = id => closeNow(ctx, id);
}
}
+3
View File
@@ -94,6 +94,9 @@ const RootMutation = {
) => {
await Asset.updateStatus(id, status);
},
closeAsset: async (_, { id }, { mutators: { Asset } }) => {
await Asset.closeNow(id);
},
setUserRole: async (_, { id, role }, { mutators: { User } }) => {
await User.setRole(id, role);
},
+11
View File
@@ -1111,6 +1111,14 @@ type UpdateAssetStatusResponse implements Response {
errors: [UserError!]
}
# CloseAssetResponse is the response returned with possibly some errors
# relating to the update status attempt.
type CloseAssetResponse implements Response {
# An array of errors relating to the mutation that occurred.
errors: [UserError!]
}
# UpdateAssetSettingsResponse is the response returned with possibly some errors
# relating to the update settings attempt.
type UpdateAssetSettingsResponse implements Response {
@@ -1446,6 +1454,9 @@ type RootMutation {
# Mutation is restricted.
updateAssetStatus(id: ID!, input: UpdateAssetStatusInput!): UpdateAssetStatusResponse
# closeAsset will close the asset for commenting based on server time.
closeAsset(id: ID!): CloseAssetResponse
# updateSettings will update the global settings.
# Mutation is restricted.
updateSettings(input: UpdateSettingsInput!): UpdateSettingsResponse