feat: Pin to home (#2880)
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { addDays, differenceInDays } from "date-fns";
|
||||
import invariant from "invariant";
|
||||
import { floor } from "lodash";
|
||||
import { action, computed, observable } from "mobx";
|
||||
import parseTitle from "@shared/utils/parseTitle";
|
||||
@@ -72,8 +71,6 @@ export default class Document extends BaseModel {
|
||||
|
||||
updatedBy: User;
|
||||
|
||||
pinned: boolean;
|
||||
|
||||
publishedAt: string | undefined;
|
||||
|
||||
archivedAt: string;
|
||||
@@ -240,31 +237,23 @@ export default class Document extends BaseModel {
|
||||
};
|
||||
|
||||
@action
|
||||
pin = async () => {
|
||||
this.pinned = true;
|
||||
|
||||
try {
|
||||
const res = await this.store.pin(this);
|
||||
invariant(res && res.data, "Data should be available");
|
||||
this.updateFromJson(res.data);
|
||||
} catch (err) {
|
||||
this.pinned = false;
|
||||
throw err;
|
||||
}
|
||||
pin = async (collectionId?: string) => {
|
||||
await this.store.rootStore.pins.create({
|
||||
documentId: this.id,
|
||||
...(collectionId ? { collectionId } : {}),
|
||||
});
|
||||
};
|
||||
|
||||
@action
|
||||
unpin = async () => {
|
||||
this.pinned = false;
|
||||
unpin = async (collectionId?: string) => {
|
||||
const pin = this.store.rootStore.pins.orderedData.find(
|
||||
(pin) =>
|
||||
pin.documentId === this.id &&
|
||||
(pin.collectionId === collectionId ||
|
||||
(!collectionId && !pin.collectionId))
|
||||
);
|
||||
|
||||
try {
|
||||
const res = await this.store.unpin(this);
|
||||
invariant(res && res.data, "Data should be available");
|
||||
this.updateFromJson(res.data);
|
||||
} catch (err) {
|
||||
this.pinned = true;
|
||||
throw err;
|
||||
}
|
||||
await pin?.delete();
|
||||
};
|
||||
|
||||
@action
|
||||
@@ -387,6 +376,21 @@ export default class Document extends BaseModel {
|
||||
return result;
|
||||
};
|
||||
|
||||
@computed
|
||||
get pinned(): boolean {
|
||||
return !!this.store.rootStore.pins.orderedData.find(
|
||||
(pin) =>
|
||||
pin.documentId === this.id && pin.collectionId === this.collectionId
|
||||
);
|
||||
}
|
||||
|
||||
@computed
|
||||
get pinnedToHome(): boolean {
|
||||
return !!this.store.rootStore.pins.orderedData.find(
|
||||
(pin) => pin.documentId === this.id && !pin.collectionId
|
||||
);
|
||||
}
|
||||
|
||||
@computed
|
||||
get isActive(): boolean {
|
||||
return !this.isDeleted && !this.isTemplate && !this.isArchived;
|
||||
|
||||
Reference in New Issue
Block a user