fix: Enable toggling of insights while document is draft

This commit is contained in:
Tom Moor
2023-07-23 13:06:34 -04:00
parent 404f5ff871
commit 217e53d8b6
6 changed files with 32 additions and 23 deletions

View File

@@ -159,7 +159,7 @@ export const publishDocument = createAction({
}
if (document?.collectionId) {
await document.save({
await document.save(undefined, {
publish: true,
});
stores.toasts.showToast(t("Document published"), {

View File

@@ -84,7 +84,6 @@ export default class Document extends ParanoidModel {
/**
* Whether team members can see who has viewed this document.
*/
@Field
@observable
insightsEnabled: boolean;
@@ -352,12 +351,18 @@ export default class Document extends ParanoidModel {
templatize = () => this.store.templatize(this.id);
@action
save = async (options?: SaveOptions | undefined) => {
const params = this.toAPI();
save = async (
fields: Partial<Document> | undefined,
options?: SaveOptions | undefined
) => {
const params = fields ?? this.toAPI();
this.isSaving = true;
try {
const model = await this.store.save({ ...params, id: this.id }, options);
const model = await this.store.save(
{ ...params, ...fields, id: this.id },
options
);
// if saving is successful set the new values on the model itself
set(this, { ...params, ...model });

View File

@@ -305,7 +305,7 @@ class DocumentScene extends React.Component<Props> {
this.isPublishing = !!options.publish;
try {
const savedDocument = await document.save(options);
const savedDocument = await document.save(undefined, options);
this.isEditorDirty = false;
if (options.done) {

View File

@@ -160,8 +160,9 @@ function Insights() {
<Switch
checked={document.insightsEnabled}
onChange={async (ev) => {
document.insightsEnabled = ev.currentTarget.checked;
await document.save();
await document.save({
insightsEnabled: ev.currentTarget.checked,
});
}}
/>
</Manage>

View File

@@ -52,7 +52,7 @@ function DocumentPublish({ document }: Props) {
}
document.collectionId = collectionId;
await document.save({ publish: true });
await document.save(undefined, { publish: true });
showToast(t("Document published"), {
type: "success",

View File

@@ -129,6 +129,23 @@ allow(User, "update", Document, (user, document) => {
return user.teamId === document.teamId;
});
allow(User, "updateInsights", Document, (user, document) => {
if (!document || !document.isActive) {
return false;
}
if (document.collectionId) {
invariant(
document.collection,
"collection is missing, did you forget to include in the query scope?"
);
if (cannot(user, "update", document.collection)) {
return false;
}
}
return user.teamId === document.teamId;
});
allow(User, "createChildDocument", Document, (user, document) => {
if (!document || !document.isActive || document.isDraft) {
return false;
@@ -277,20 +294,6 @@ allow(User, "archive", Document, (user, document) => {
return user.teamId === document.teamId;
});
allow(User, "updateInsights", Document, (user, document) => {
if (!document || !document.isActive || document.isDraft) {
return false;
}
invariant(
document.collection,
"collection is missing, did you forget to include in the query scope?"
);
if (cannot(user, "update", document.collection)) {
return false;
}
return user.teamId === document.teamId;
});
allow(User, "unarchive", Document, (user, document) => {
if (!document) {
return false;