Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Repeated calls to group self #3321

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions frontend/composables/use-groups.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
import { useAsync, ref } from "@nuxtjs/composition-api";
import { useAsyncKey } from "./use-utils";
import { useUserApi } from "~/composables/api";
import { GroupBase } from "~/lib/api/types/user";
import { GroupBase, GroupSummary } from "~/lib/api/types/user";

const groupSelfRef = ref<GroupSummary | null>(null);
const loading = ref(false);

export const useGroupSelf = function () {
const api = useUserApi();
async function refreshGroupSelf() {
loading.value = true;
const { data } = await api.groups.getCurrentUserGroup();
groupSelfRef.value = data;
loading.value = false;
}

const actions = {
get() {
const group = useAsync(async () => {
const { data } = await api.groups.getCurrentUserGroup();

return data;
}, useAsyncKey());
if (!(groupSelfRef.value || loading.value)) {
refreshGroupSelf();
}

return group;
return groupSelfRef;
},
async updatePreferences() {
if (!group.value?.preferences) {
if (!groupSelfRef.value) {
await refreshGroupSelf();
}
if (!groupSelfRef.value?.preferences) {
return;
}

const { data } = await api.groups.setPreferences(group.value.preferences);
const { data } = await api.groups.setPreferences(groupSelfRef.value.preferences);

if (data) {
group.value.preferences = data;
groupSelfRef.value.preferences = data;
}
},
};
Expand Down
7 changes: 7 additions & 0 deletions frontend/lib/api/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ export interface GroupInDB {
users?: UserOut[];
preferences?: ReadGroupPreferences;
}
export interface GroupSummary {
name: string;
id: string;
slug: string;
preferences?: ReadGroupPreferences;

}
export interface CategoryBase {
name: string;
id: string;
Expand Down
4 changes: 2 additions & 2 deletions frontend/lib/api/user/groups.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseCRUDAPI } from "../base/base-clients";
import { CategoryBase, GroupBase, GroupInDB, UserOut } from "~/lib/api/types/user";
import { CategoryBase, GroupBase, GroupInDB, GroupSummary, UserOut } from "~/lib/api/types/user";
import {
CreateInviteToken,
GroupAdminUpdate,
Expand Down Expand Up @@ -35,7 +35,7 @@ export class GroupAPI extends BaseCRUDAPI<GroupBase, GroupInDB, GroupAdminUpdate
/** Returns the Group Data for the Current User
*/
async getCurrentUserGroup() {
return await this.requests.get<GroupInDB>(routes.groupsSelf);
return await this.requests.get<GroupSummary>(routes.groupsSelf);
}

async getCategories() {
Expand Down
6 changes: 3 additions & 3 deletions mealie/routes/groups/controller_group_self_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mealie.schema.group.group_permissions import SetPermissions
from mealie.schema.group.group_preferences import ReadGroupPreferences, UpdateGroupPreferences
from mealie.schema.group.group_statistics import GroupStatistics, GroupStorage
from mealie.schema.user.user import GroupInDB, UserOut
from mealie.schema.user.user import GroupSummary, UserOut
from mealie.services.group_services.group_service import GroupService

router = UserAPIRouter(prefix="/groups", tags=["Groups: Self Service"])
Expand All @@ -20,10 +20,10 @@ class GroupSelfServiceController(BaseUserController):
def service(self) -> GroupService:
return GroupService(self.group_id, self.repos)

@router.get("/self", response_model=GroupInDB)
@router.get("/self", response_model=GroupSummary)
def get_logged_in_user_group(self):
"""Returns the Group Data for the Current User"""
return self.group
return self.group.cast(GroupSummary)

@router.get("/members", response_model=list[UserOut])
def get_group_members(self):
Expand Down
13 changes: 13 additions & 0 deletions mealie/schema/user/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,19 @@ def loader_options(cls) -> list[LoaderOption]:
]


class GroupSummary(GroupBase):
id: UUID4
name: str
slug: str
preferences: ReadGroupPreferences | None = None

@classmethod
def loader_options(cls) -> list[LoaderOption]:
return [
joinedload(Group.preferences),
]


class GroupPagination(PaginationBase):
items: list[GroupInDB]

Expand Down
Loading