(isRefresh = false, query = {}, ranking = "cs_rank", {priority = FOREGROUND_PRIORITY} = {})
| 11 | */ |
| 12 | |
| 13 | export async function getPrograms(isRefresh = false, query = {}, ranking = "cs_rank", {priority = FOREGROUND_PRIORITY} = {}) { |
| 14 | /* |
| 15 | * Get the list of programs (without description) from the server or local storage |
| 16 | * @param isRefresh [Boolean]: whether to refresh the data |
| 17 | * @param query [Object]: { |
| 18 | * 'u': [String]: university name, |
| 19 | * (NO PROGRAM) [Delete] 'p': [String]: program name [Delete], |
| 20 | * 'd': [String]: degree, |
| 21 | * 'm': [String]: major, |
| 22 | * 'r': [String]: region, |
| 23 | * } |
| 24 | * @return: list of programs (without description) |
| 25 | */ |
| 26 | const filters = { |
| 27 | ...query, |
| 28 | r: normalizeQueryValues(query.r), |
| 29 | d: normalizeQueryValues(query.d), |
| 30 | m: normalizeQueryValues(query.m), |
| 31 | }; |
| 32 | let programs = await loadCachedValue({ |
| 33 | key: "programs", |
| 34 | legacyFields: ["data"], |
| 35 | forceRefresh: isRefresh, |
| 36 | priority, |
| 37 | load: async () => { |
| 38 | const response = await apiJson(PROGRAM_LIST, { |
| 39 | fetchPriority: priority, |
| 40 | }); |
| 41 | return response.data; |
| 42 | }, |
| 43 | }); |
| 44 | if (!programs) { |
| 45 | return {}; |
| 46 | } |
| 47 | const univAbbrOrder = univListOrder.reduce((acc, univ) => { |
| 48 | acc[univ.abbr] = univ[ranking || 'cs_rank']; |
| 49 | return acc; |
| 50 | }, {}); |
| 51 | programs = Object.entries(programs).sort(([univ1], [univ2]) => { |
| 52 | return univAbbrOrder[univ1] - univAbbrOrder[univ2]; |
| 53 | }).reduce((acc, [univ, programs]) => { |
| 54 | acc[univ] = programs; |
| 55 | return acc; |
| 56 | }, {}); |
| 57 | |
| 58 | const searchTerm = filters.u?.trim().toLowerCase() ?? ''; |
| 59 | const matchesUniversity = (univ) => ( |
| 60 | searchTerm === '' || |
| 61 | univ.abbr.toLowerCase().includes(searchTerm) || |
| 62 | univ.fullName.toLowerCase().includes(searchTerm) |
| 63 | ); |
| 64 | const matchesRegion = (univ) => ( |
| 65 | !filters.r || filters.r.some(region => univ.region.includes(region)) |
| 66 | ); |
| 67 | const emptyUniversityMatches = new Set(); |
| 68 | const searchPrograms = Object.keys(programs).reduce((matchingPrograms, univName) => { |
| 69 | const fullNameResults = (univAbbrFullNameMapping[univName] ?? '').toLowerCase().includes(searchTerm); |
| 70 | const abbrResults = univName.toLowerCase().includes(searchTerm); |
no test coverage detected