5757import { getSidebarLinks } from ' @/utils'
5858import { useRouter } from ' vue-router'
5959import { call } from ' frappe-ui'
60- import { watch , ref , onMounted } from ' vue'
60+ import { ref , watch } from ' vue'
6161import { sessionStore } from ' @/stores/session'
6262import { useSettings } from ' @/stores/settings'
6363import { usersStore } from ' @/stores/user'
@@ -68,26 +68,13 @@ let { isLoggedIn } = sessionStore()
6868const { sidebarSettings } = useSettings ()
6969const router = useRouter ()
7070let { userResource } = usersStore ()
71- const sidebarLinks = ref (getSidebarLinks () )
71+ const sidebarLinks = ref ([] )
7272const otherLinks = ref ([])
7373const showMenu = ref (false )
7474const menu = ref (null )
7575const isModerator = ref (false )
7676const isInstructor = ref (false )
7777
78- onMounted (() => {
79- sidebarSettings .reload (
80- {},
81- {
82- onSuccess (data ) {
83- destructureSidebarLinks ()
84- filterLinksToShow (data)
85- addOtherLinks ()
86- },
87- }
88- )
89- })
90-
9178const handleOutsideClick = (e ) => {
9279 if (menu .value && ! menu .value .contains (e .target )) {
9380 showMenu .value = false
@@ -126,65 +113,57 @@ const filterLinksToShow = (data) => {
126113
127114const addOtherLinks = () => {
128115 if (user) {
129- otherLinks .value .push ({
130- label: ' Notifications' ,
131- icon: ' Bell' ,
132- to: ' Notifications' ,
133- })
134- otherLinks .value .push ({
135- label: ' Profile' ,
136- icon: ' UserRound' ,
137- })
138- otherLinks .value .push ({
139- label: ' Log out' ,
140- icon: ' LogOut' ,
141- })
116+ addLink (' Notifications' , ' Bell' , ' Notifications' )
117+ addLink (' Profile' , ' UserRound' )
118+ addLink (' Log out' , ' LogOut' )
142119 } else {
143- otherLinks .value .push ({
144- label: ' Log in' ,
145- icon: ' LogIn' ,
146- })
120+ addLink (' Log in' , ' LogIn' )
147121 }
148122}
149123
150- watch (userResource, () => {
151- if (userResource .data ) {
152- isModerator .value = userResource .data .is_moderator
153- isInstructor .value = userResource .data .is_instructor
154- addPrograms ()
155- if (isModerator .value || isInstructor .value ) {
156- addProgrammingExercises ()
157- addQuizzes ()
158- addAssignments ()
124+ const addLink = (label , icon , to = ' ' ) => {
125+ if (otherLinks .value .some ((link ) => link .label === label)) return
126+ otherLinks .value .push ({
127+ label: label,
128+ icon: icon,
129+ to: to,
130+ })
131+ }
132+
133+ const updateSidebarLinks = () => {
134+ sidebarLinks .value = getSidebarLinks (true )
135+ destructureSidebarLinks ()
136+ sidebarSettings .reload (
137+ {},
138+ {
139+ onSuccess: async (data ) => {
140+ filterLinksToShow (data)
141+ await addPrograms ()
142+ if (isModerator .value || isInstructor .value ) {
143+ addQuizzes ()
144+ addAssignments ()
145+ addProgrammingExercises ()
146+ }
147+ addOtherLinks ()
148+ },
159149 }
160- }
161- })
150+ )
151+ }
162152
163153const addQuizzes = () => {
164- otherLinks .value .push ({
165- label: ' Quizzes' ,
166- icon: ' CircleHelp' ,
167- to: ' Quizzes' ,
168- })
154+ addLink (' Quizzes' , ' CircleHelp' , ' Quizzes' )
169155}
170156
171157const addAssignments = () => {
172- otherLinks .value .push ({
173- label: ' Assignments' ,
174- icon: ' Pencil' ,
175- to: ' Assignments' ,
176- })
158+ addLink (' Assignments' , ' Pencil' , ' Assignments' )
177159}
178160
179161const addProgrammingExercises = () => {
180- otherLinks .value .push ({
181- label: ' Programming Exercises' ,
182- icon: ' Code' ,
183- to: ' ProgrammingExercises' ,
184- })
162+ addLink (' Programming Exercises' , ' Code' , ' ProgrammingExercises' )
185163}
186164
187165const addPrograms = async () => {
166+ if (sidebarLinks .value .some ((link ) => link .label === ' Programs' )) return
188167 let canAddProgram = await checkIfCanAddProgram ()
189168 if (! canAddProgram) return
190169 let activeFor = [' Programs' , ' ProgramDetail' ]
@@ -198,7 +177,21 @@ const addPrograms = async () => {
198177 })
199178}
200179
180+ watch (
181+ userResource,
182+ async () => {
183+ await userResource .promise
184+ if (userResource .data ) {
185+ isModerator .value = userResource .data .is_moderator
186+ isInstructor .value = userResource .data .is_instructor
187+ }
188+ updateSidebarLinks ()
189+ },
190+ { immediate: true }
191+ )
192+
201193const checkIfCanAddProgram = async () => {
194+ if (! userResource .data ) return false
202195 if (isModerator .value || isInstructor .value ) {
203196 return true
204197 }
0 commit comments