mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-15 01:03:16 +01:00
Always show the create goal button and open the upgrade modal when the goal limit is reached
This commit is contained in:
@@ -22,6 +22,8 @@ import {
|
||||
canCreateInvitations,
|
||||
} from '@/utils/permissions';
|
||||
import { isBillingActivated, isInvoicingActivated } from '@/utils/billing';
|
||||
import { useQueryClient } from '@tanstack/vue-query';
|
||||
import { fetchCanCreateMoreGoals } from '@/utils/useGoalsQuery';
|
||||
import { useTimeEntriesInfiniteQuery } from '@/utils/useTimeEntriesInfiniteQuery';
|
||||
import { useProjectsQuery } from '@/utils/useProjectsQuery';
|
||||
import { useClientsQuery } from '@/utils/useClientsQuery';
|
||||
@@ -66,6 +68,7 @@ const showCreateClientModal = ref(false);
|
||||
const showCreateTaskModal = ref(false);
|
||||
const showCreateTagModal = ref(false);
|
||||
const showCreateGoalModal = ref(false);
|
||||
const showGoalUpgradeModal = ref(false);
|
||||
const showInviteMemberModal = ref(false);
|
||||
const showCreateTimeEntryModal = ref(false);
|
||||
|
||||
@@ -103,6 +106,7 @@ const ENTITY_ICONS: Record<string, typeof FolderIcon> = {
|
||||
};
|
||||
|
||||
export function useCommandPalette() {
|
||||
const queryClient = useQueryClient();
|
||||
const currentTimeEntryStore = useCurrentTimeEntryStore();
|
||||
const { currentTimeEntry, isActive } = storeToRefs(currentTimeEntryStore);
|
||||
const { setActiveState, updateTimer } = currentTimeEntryStore;
|
||||
@@ -240,9 +244,13 @@ export function useCommandPalette() {
|
||||
showCreateTagModal.value = true;
|
||||
}
|
||||
|
||||
function openCreateGoalModal() {
|
||||
async function openCreateGoalModal() {
|
||||
closePaletteAfterAction();
|
||||
showCreateGoalModal.value = true;
|
||||
if (await fetchCanCreateMoreGoals(queryClient)) {
|
||||
showCreateGoalModal.value = true;
|
||||
} else {
|
||||
showGoalUpgradeModal.value = true;
|
||||
}
|
||||
}
|
||||
|
||||
function openInviteMemberModal() {
|
||||
@@ -525,6 +533,7 @@ export function useCommandPalette() {
|
||||
showCreateTaskModal,
|
||||
showCreateTagModal,
|
||||
showCreateGoalModal,
|
||||
showGoalUpgradeModal,
|
||||
showInviteMemberModal,
|
||||
showCreateTimeEntryModal,
|
||||
showProjectSelector,
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { useQuery, useQueryClient } from '@tanstack/vue-query';
|
||||
import { useQuery, useQueryClient, type QueryClient } from '@tanstack/vue-query';
|
||||
import { api } from '@/packages/api/src';
|
||||
import { getCurrentOrganizationId } from '@/utils/useUser';
|
||||
import type { Goal } from '@/packages/api/src';
|
||||
import { computed } from 'vue';
|
||||
import { fetchAllPages } from '@/utils/fetchAllPages';
|
||||
import { canCreateMoreGoals, goalLimit } from '@/utils/billing';
|
||||
|
||||
export type GoalArchivedFilter = 'true' | 'false' | 'all';
|
||||
|
||||
@@ -19,19 +20,38 @@ export async function fetchAllGoals(
|
||||
);
|
||||
}
|
||||
|
||||
export function useGoalsQuery(archived: GoalArchivedFilter = 'false') {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: computed(() => ['goals', getCurrentOrganizationId(), archived]),
|
||||
export function goalsQueryOptions(archived: GoalArchivedFilter = 'false') {
|
||||
return {
|
||||
queryKey: ['goals', getCurrentOrganizationId(), archived],
|
||||
queryFn: async () => {
|
||||
const organizationId = getCurrentOrganizationId();
|
||||
if (!organizationId) throw new Error('No organization');
|
||||
const data = await fetchAllGoals(organizationId, archived);
|
||||
return { data };
|
||||
},
|
||||
enabled: () => !!getCurrentOrganizationId(),
|
||||
staleTime: 1000 * 30, // 30 seconds
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the goal limit outside of the goals page, where the goals are not loaded yet.
|
||||
* Archived goals count towards the limit, so all goals are counted.
|
||||
*/
|
||||
export async function fetchCanCreateMoreGoals(queryClient: QueryClient): Promise<boolean> {
|
||||
if (goalLimit() === null) {
|
||||
return true;
|
||||
}
|
||||
const { data } = await queryClient.fetchQuery(goalsQueryOptions('all'));
|
||||
return canCreateMoreGoals(data.length);
|
||||
}
|
||||
|
||||
export function useGoalsQuery(archived: GoalArchivedFilter = 'false') {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const query = useQuery({
|
||||
...goalsQueryOptions(archived),
|
||||
queryKey: computed(() => goalsQueryOptions(archived).queryKey),
|
||||
enabled: () => !!getCurrentOrganizationId(),
|
||||
// Progress is computed server side, keep it fresh while the page is open
|
||||
refetchInterval: 1000 * 60,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user