From b772b958c46cb7afa7fed2fd313cc0776626885c Mon Sep 17 00:00:00 2001 From: Daniel Saewitz Date: Wed, 13 Dec 2023 14:02:43 -0500 Subject: [PATCH] Fix sorting on RSC --- src/app/queries.tsx | 5 ++++- src/redux/modules/tapes.js | 46 +++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/app/queries.tsx b/src/app/queries.tsx index 3829302..dc4ee72 100644 --- a/src/app/queries.tsx +++ b/src/app/queries.tsx @@ -1,6 +1,7 @@ +import { sortSources } from '@/redux/modules/tapes'; +import { Artist, Tape } from '@/types'; import ky from 'ky-universal'; import { API_DOMAIN } from '../lib/constants'; -import { Artist, Tape } from '@/types'; export const fetchArtists = async (): Promise => { const parsed = await ky(`${API_DOMAIN}/api/v2/artists`, { @@ -25,5 +26,7 @@ export const fetchShow = async ( cache: 'no-cache', }).json()) as Tape; + parsed.sources = sortSources(parsed.sources); + return parsed; }; diff --git a/src/redux/modules/tapes.js b/src/redux/modules/tapes.js index aa4b4cc..ef63f07 100644 --- a/src/redux/modules/tapes.js +++ b/src/redux/modules/tapes.js @@ -60,30 +60,27 @@ const getEtreeId = (s = '') => // tapes: TODO: GD sort (charlie miller, sbd + etree id, weighted average), sbd + etree id, weighted avg, asc, desc // for now, hardcode sort: sbd, charlie miller, etree id, weighted average -const sortTapes = (data = {}) => { - const sortedTapes = - data && data.sources - ? [...data.sources].sort( - firstBy((t) => t.is_soundboard, 'desc') - // Charlie for GD, Pete for JRAD - .thenBy((t) => +export const sortSources = (sources) => { + const sortedSources = sources + ? [...sources].sort( + firstBy((t) => t.is_soundboard, 'desc') + // Charlie for GD, Pete for JRAD + .thenBy( + (t) => /(charlie miller)|(peter costello)/i.test( - [t.taper, t.transferrer, t.source].join(''), - 'desc' - ) - ) - .thenBy( - (t1, t2) => getEtreeId(t1.upstream_identifier) - getEtreeId(t2.upstream_identifier), - 'desc' - ) - .thenBy((t) => t.avg_rating_weighted, 'desc') - ) - : []; + [t.taper, t.transferrer, t.source].join('') + ), + 'desc' + ) + .thenBy( + (t1, t2) => getEtreeId(t1.upstream_identifier) - getEtreeId(t2.upstream_identifier), + 'desc' + ) + .thenBy((t) => t.avg_rating_weighted, 'desc') + ) + : []; - return { - ...(data || {}), - sources: sortedTapes.reverse(), - }; + return sortedSources; }; export function requestTapes(artistSlug, year, showDate) { @@ -101,7 +98,10 @@ export function receiveTapes(artistSlug, year, showDate, data) { artistSlug, year, showDate, - data: sortTapes(data), + data: { + ...data, + sources: sortSources(data.sources), + }, }; }