Skip to content

Commit

Permalink
One time share is only for users with a plan (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk authored Mar 2, 2024
1 parent e05b365 commit 7b9c0d8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
10 changes: 10 additions & 0 deletions shared/access/can-advanced-share.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { PlansForUser } from "../../functions/db/plan/get-active-plans-for-user.ts";
import type { UserDbType } from "../../functions/db/user/upsert-user-db.ts";

export const canAdvancedShare = (user: UserDbType, plans?: PlansForUser) => {
if (user.is_admin) {
return true;
}

return plans?.some((plan) => plan.advanced_sharing);
};
2 changes: 1 addition & 1 deletion shared/access/can-duplicate-deck-or-folder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type PlansForUser } from "../../functions/db/plan/get-plans-for-user";
import { type PlansForUser } from "../../functions/db/plan/get-active-plans-for-user.ts";
import { type UserDbType } from "../../functions/db/user/upsert-user-db";
import { type DeckOrFolderDbType } from "../../functions/db/plan/can-duplicate-deck-or-folder-db.ts";

Expand Down
20 changes: 12 additions & 8 deletions src/screens/deck-review/deck-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { t } from "../../translations/t.ts";
import { ButtonGrid } from "../../ui/button-grid.tsx";
import { Button } from "../../ui/button.tsx";
import { DeckFolderDescription } from "../shared/deck-folder-description.tsx";
import {
useScrollToTopOnMount
} from "../../lib/react/use-scroll-to-top-mount.ts";
import { useScrollToTopOnMount } from "../../lib/react/use-scroll-to-top-mount.ts";
import { userStore } from "../../store/user-store.ts";
import { redirectUserToDeckOrFolderLink } from "../share-deck/redirect-user-to-deck-or-folder-link.tsx";

export const DeckPreview = observer(() => {
const reviewStore = useReviewStore();
Expand Down Expand Up @@ -170,11 +170,15 @@ export const DeckPreview = observer(() => {
icon={"mdi-share-circle mdi-24px"}
outline
onClick={() => {
screenStore.go({
type: "shareDeck",
deckId: deck.id,
shareId: deck.share_id,
});
if (userStore.canAdvancedShare) {
screenStore.go({
type: "shareDeck",
deckId: deck.id,
shareId: deck.share_id,
});
} else {
redirectUserToDeckOrFolderLink(deck.share_id);
}
}}
>
{t("share")}
Expand Down
16 changes: 11 additions & 5 deletions src/screens/folder-review/folder-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { DeckRowWithCardsToReview } from "../shared/deck-row-with-cards-to-revie
import { ButtonGrid } from "../../ui/button-grid.tsx";
import { DeckFolderDescription } from "../shared/deck-folder-description.tsx";
import { useScrollToTopOnMount } from "../../lib/react/use-scroll-to-top-mount.ts";
import { redirectUserToDeckOrFolderLink } from "../share-deck/redirect-user-to-deck-or-folder-link.tsx";
import { userStore } from "../../store/user-store.ts";

export const FolderPreview = observer(() => {
const reviewStore = useReviewStore();
Expand Down Expand Up @@ -171,11 +173,15 @@ export const FolderPreview = observer(() => {
icon={"mdi-share-circle mdi-24px"}
outline
onClick={() => {
screenStore.go({
type: "shareFolder",
folderId: folder.id,
shareId: folder.shareId,
});
if (userStore.canAdvancedShare) {
screenStore.go({
type: "shareFolder",
folderId: folder.id,
shareId: folder.shareId,
});
} else {
redirectUserToDeckOrFolderLink(folder.shareId);
}
}}
>
{t("share")}
Expand Down
10 changes: 9 additions & 1 deletion src/store/user-store.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { makeAutoObservable } from "mobx";
import { type UserDbType } from "../../functions/db/user/upsert-user-db.ts";
import { assert } from "../lib/typescript/assert.ts";
import { type PlansForUser } from "../../functions/db/plan/get-plans-for-user.ts";
import { type PlansForUser } from "../../functions/db/plan/get-active-plans-for-user.ts";
import { makePersistable } from "mobx-persist-store";
import { storageAdapter } from "../lib/telegram/storage-adapter.ts";
import { BooleanToggle } from "mobx-form-lite";
import { CardAnswerType } from "../../functions/db/custom-types.ts";
import { persistableField } from "../lib/mobx-form-lite-persistable/persistable-field.ts";
import { canAdvancedShare } from "../../shared/access/can-advanced-share.ts";

export class UserStore {
userInfo?: UserDbType;
Expand Down Expand Up @@ -56,6 +57,13 @@ export class UserStore {
return this.user?.is_speaking_card_enabled ?? false;
}

get canAdvancedShare() {
if (!this.user || !this.plans) {
return false;
}
return canAdvancedShare(this.user, this.plans);
}

updateSettings(body: Partial<UserDbType>) {
assert(this.userInfo, "myInfo is not loaded in optimisticUpdateSettings");
Object.assign(this.userInfo, body);
Expand Down

0 comments on commit 7b9c0d8

Please sign in to comment.