Created
September 10, 2024 17:06
-
-
Save decagondev/99e307411af06e326dc54fb08c793e19 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
// Canvas API settings | |
const canvasApiUrl = 'https://canvas.instructure.com/api/v1'; | |
const apiKey = 'The API Key'; | |
const headers = { | |
'Authorization': `Bearer ${apiKey}` | |
}; | |
// Define interfaces for expected API responses (optional) | |
interface Course { | |
id: number; | |
name: string; | |
} | |
interface Tab { | |
id: string; | |
label: string; | |
hidden: boolean; | |
visibility: string; | |
} | |
const getCourses = async (): Promise<Course[]> => { | |
try { | |
const response = await axios.get(`${canvasApiUrl}/courses`, { headers }); | |
return response.data; | |
} catch (error) { | |
console.error('Error fetching courses:', error); | |
return []; | |
} | |
}; | |
const getTabs = async (courseId: number): Promise<Tab[]> => { | |
try { | |
const response = await axios.get(`${canvasApiUrl}/courses/${courseId}/tabs`, { headers }); | |
return response.data; | |
} catch (error) { | |
console.error(`Error fetching tabs for course ${courseId}:`, error); | |
return []; | |
} | |
}; | |
const disableTab = async (courseId: number, tabId: string): Promise<void> => { | |
try { | |
const url = `${canvasApiUrl}/courses/${courseId}/tabs/${tabId}`; | |
const data = { | |
hidden: true // Set the tab to hidden | |
}; | |
await axios.put(url, data, { headers }); | |
console.log(`Tab ${tabId} in course ${courseId} disabled successfully.`); | |
} catch (error) { | |
console.error(`Error disabling tab ${tabId} in course ${courseId}:`, error); | |
} | |
}; | |
const disableTabGlobally = async (tabToDisable: string): Promise<void> => { | |
const courses = await getCourses(); | |
for (const course of courses) { | |
const courseId = course.id; | |
const tabs = await getTabs(courseId); | |
const specificTab = tabs.find(tab => tab.label.toLowerCase() === tabToDisable); // Adjust label if necessary | |
if (specificTab && !specificTab.hidden) { | |
await disableTab(courseId, specificTab.id); | |
} | |
} | |
}; | |
disableTabGlobally("Help Hub").then(() => { | |
console.log('Tab disabling process complete.'); | |
}).catch(error => { | |
console.error('Error in disabling Tab globally:', error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment