Refactor cockpit user loading
This commit is contained in:
parent
47770bde90
commit
1b87aa5bac
|
|
@ -2,6 +2,7 @@ import { itGetCached } from "@/fetchHelpers";
|
|||
import type { CourseSessionUser, ExpertSessionUser } from "@/types";
|
||||
import log from "loglevel";
|
||||
|
||||
import { useUserStore } from "@/stores/user";
|
||||
import { defineStore } from "pinia";
|
||||
|
||||
export type CockpitStoreState = {
|
||||
|
|
@ -29,15 +30,18 @@ export const useCockpitStore = defineStore({
|
|||
actions: {
|
||||
async loadCourseSessionUsers(courseSlug: string, reload = false) {
|
||||
log.debug("loadCockpitData called");
|
||||
const { users, cockpit_user: cockpitUser } = await itGetCached(
|
||||
`/api/course/sessions/${courseSlug}/users/`,
|
||||
{
|
||||
const users = (await itGetCached(`/api/course/sessions/${courseSlug}/users/`, {
|
||||
reload: reload,
|
||||
}
|
||||
);
|
||||
})) as CourseSessionUser[];
|
||||
|
||||
this.courseSessionUsers = users;
|
||||
this.cockpitSessionUser = cockpitUser;
|
||||
this.courseSessionUsers = users.filter((user) => user.role === "MEMBER");
|
||||
|
||||
const userStore = useUserStore();
|
||||
const currentUser = users.find((user) => user.user_id === userStore.id);
|
||||
|
||||
if (currentUser && currentUser.role === "EXPERT") {
|
||||
this.cockpitSessionUser = currentUser as ExpertSessionUser;
|
||||
}
|
||||
|
||||
if (this.cockpitSessionUser && this.cockpitSessionUser.circles?.length > 0) {
|
||||
this.selectedCircles = [this.cockpitSessionUser.circles[0].translation_key];
|
||||
|
|
|
|||
|
|
@ -80,9 +80,6 @@ export const useUserStore = defineStore({
|
|||
this.$state = data;
|
||||
this.loggedIn = true;
|
||||
appStore.userLoaded = true;
|
||||
// todo: why?
|
||||
// const courseSessionsStore = useCourseSessionsStore();
|
||||
// await courseSessionsStore.loadCourseSessionsData();
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -236,6 +236,9 @@ class CourseSessionUser(models.Model):
|
|||
"email": self.user.email,
|
||||
"avatar_url": self.user.avatar_url,
|
||||
"role": self.role,
|
||||
"circles": self.expert.all().values(
|
||||
"id", "title", "slug", "translation_key"
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -144,24 +144,9 @@ def get_course_session_users(request, course_slug):
|
|||
course__slug=course_slug
|
||||
)
|
||||
qs = CourseSessionUser.objects.filter(course_session__in=course_sessions)
|
||||
cockpit_user_csu = qs.filter(user_id=request.user.id)
|
||||
|
||||
if len(cockpit_user_csu) == 0:
|
||||
return Response({"error": "User not found"}, status=404)
|
||||
|
||||
user_data = [csu.to_dict() for csu in qs.exclude(user_id=request.user.id)]
|
||||
|
||||
data = {
|
||||
"cockpit_user": cockpit_user_csu[0].to_dict()
|
||||
| {
|
||||
"circles": cockpit_user_csu[0]
|
||||
.expert.all()
|
||||
.values("id", "title", "slug", "translation_key")
|
||||
},
|
||||
"users": user_data,
|
||||
}
|
||||
|
||||
return Response(status=200, data=data)
|
||||
user_data = [csu.to_dict() for csu in qs]
|
||||
return Response(status=200, data=user_data)
|
||||
except PermissionDenied as e:
|
||||
raise e
|
||||
except Exception as e:
|
||||
|
|
|
|||
Loading…
Reference in New Issue