Skip to content

Commit

Permalink
patch new time entries into existing store when stores are refreshed …
Browse files Browse the repository at this point in the history
…on focus
  • Loading branch information
Onatcer committed Nov 12, 2024
1 parent 4b0cb2e commit df0f3b2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion resources/js/utils/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { canViewClients, canViewMembers } from '@/utils/permissions';

export function initializeStores() {
refreshStores();
useTimeEntriesStore().fetchTimeEntries();
}

export function refreshStores() {
useProjectsStore().fetchProjects();
useTasksStore().fetchTasks();
useTagsStore().fetchTags();
useCurrentTimeEntryStore().fetchCurrentTimeEntry();
useTimeEntriesStore().patchTimeEntries();
if (canViewMembers()) {
useMembersStore().fetchMembers();
}
Expand Down
34 changes: 34 additions & 0 deletions resources/js/utils/useTimeEntries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,39 @@ export const useTimeEntriesStore = defineStore('timeEntries', () => {
const allTimeEntriesLoaded = ref(false);
const { handleApiRequestNotifications } = useNotificationsStore();

async function patchTimeEntries(
queryParams: TimeEntriesQueryParams = {
only_full_dates: 'true',
member_id: getCurrentMembershipId(),
}
) {
const organizationId = getCurrentOrganizationId();

if (organizationId) {
const timeEntriesResponse = await handleApiRequestNotifications(
() =>
api.getTimeEntries({
params: {
organization: organizationId,
},
queries: queryParams,
}),
undefined,
'Failed to fetch time entries'
);
if (timeEntriesResponse?.data) {
// insert missing time entries
const missingTimeEntries = timeEntriesResponse.data.filter(
(entry) => !timeEntries.value.find((e) => e.id === entry.id)
);
timeEntries.value = [
...missingTimeEntries,
...timeEntries.value,
];
}
}
}

async function fetchTimeEntries(
queryParams: TimeEntriesQueryParams = {
only_full_dates: 'true',
Expand Down Expand Up @@ -200,5 +233,6 @@ export const useTimeEntriesStore = defineStore('timeEntries', () => {
allTimeEntriesLoaded,
updateTimeEntries,
deleteTimeEntries,
patchTimeEntries,
};
});

0 comments on commit df0f3b2

Please sign in to comment.