Skip to content

Commit

Permalink
ui/routes: fix SearchRoute
Browse files Browse the repository at this point in the history
- inherit from UserIndexRoute
- fix/rewrite model hooks
- reformat template
  • Loading branch information
bastimeyer committed Jan 24, 2022
1 parent 715bd0e commit e45a695
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 91 deletions.
2 changes: 0 additions & 2 deletions src/app/locales/de/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ search:
all: Alle Suchergebnisse
games: "Spiel-Suchergebnisse: "
channels: "Kanal-Suchergebnisse: "
streams: "Stream-Suchergebnisse: "
subheader:
games: Spiele
channels: Kanäle
streams: Streams
empty: Deine Suchanfrage lieferte keine Ergebnisse...
watching:
header: Du schaust
Expand Down
2 changes: 0 additions & 2 deletions src/app/locales/en/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ search:
all: All search results
games: "Game search results: "
channels: "Stream search results: "
streams: "Stream search results: "
subheader:
games: Games
channels: Channels
streams: Streams
empty: Your query didn't match anything...
watching:
header: You're watching
Expand Down
2 changes: 0 additions & 2 deletions src/app/locales/es/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ search:
all: Todos los resultados de la búsqueda
games: "Resultados de la búsqueda de juegos: "
channels: "Resultados de la búsqueda de canales: "
streams: "Resultados de la búsqueda de streams: "
subheader:
games: Juegos
channels: Canales
streams: Streams
empty: Tu consulta no coincidió con nada...
watching:
header: Estás viendo
Expand Down
2 changes: 0 additions & 2 deletions src/app/locales/fr/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ search:
all: Tous les résultats de la recherche
games: "Résultat de la recherche de jeux : "
channels: "Résultat de la recherche de chaînes : "
streams: "Résultat de la recherche de streams : "
subheader:
games: Jeux
channels: Chaînes
streams: Streams
empty: Votre requête n'a renvoyé aucun résultat...
watching:
header: Vous regardez
Expand Down
2 changes: 0 additions & 2 deletions src/app/locales/it/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ search:
all: Tutti i risultati della ricerca
games: "Risultati dei giochi cercati: "
channels: "Risultati dei canali cercati: "
streams: "Risultati dei flussi cercati: "
subheader:
games: Giochi
channels: Canali
streams: Flussi
empty: La tua richiesta non corrisponde a nulla...
watching:
header: Stai guardando
Expand Down
2 changes: 0 additions & 2 deletions src/app/locales/pt-br/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ search:
all: Todas os resultados buscados
games: "Resultado de jogos buscados: "
channels: "Resultado de canais buscados: "
streams: "Resultado de stream buscados: "
subheader:
games: Jogos
channels: Canais
streams: Streams
empty: Sua requisição não encontrou nada...
watching:
header: Você está assistindo
Expand Down
2 changes: 0 additions & 2 deletions src/app/locales/ru/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,9 @@ search:
all: "Все результаты поиска:"
games: "Результат поиска игр:"
channels: "Результат поиска каналов:"
streams: "Результат поиска стримов:"
subheader:
games: "Игры"
channels: "Каналы"
streams: "Стримы"
empty: "Нет результатов по Вашему запросу..."
watching:
header: "Вы смотрите"
Expand Down
2 changes: 0 additions & 2 deletions src/app/locales/zh-tw/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ search:
all: "搜尋結果: "
games: "遊戲搜尋結果: "
channels: "頻道搜尋結果: "
streams: "實況搜尋結果: "
subheader:
games: 遊戲
channels: 頻道
streams: 實況
empty: 查無資訊
watching:
header: 正在收看
Expand Down
10 changes: 3 additions & 7 deletions src/app/ui/routes/search/controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Controller from "@ember/controller";
import { get, computed } from "@ember/object";
import { computed } from "@ember/object";
import { alias, empty, equal } from "@ember/object/computed";
import "./styles.less";

Expand All @@ -8,18 +8,14 @@ export default Controller.extend({
queryParams: [ "filter", "query" ],

games : alias( "model.games" ),
streams : alias( "model.streams" ),
channels: alias( "model.channels" ),

notFiltered: equal( "filter", "all" ),

emptyGames : empty( "games" ),
emptyStreams : empty( "streams" ),
emptyChannels: empty( "channels" ),

noResults: computed( "emptyGames", "emptyStreams", "emptyChannels", function() {
return get( this, "emptyGames" )
&& get( this, "emptyStreams" )
&& get( this, "emptyChannels" );
noResults: computed( "emptyGames", "emptyChannels", function() {
return this.emptyGames && this.emptyChannels;
})
});
96 changes: 51 additions & 45 deletions src/app/ui/routes/search/route.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { get, setProperties } from "@ember/object";
import Route from "@ember/routing/route";
import UserIndexRoute from "ui/routes/user/index/route";
import PaginationMixin from "ui/routes/-mixins/routes/infinite-scroll/pagination";
import { map, toArray } from "ui/routes/-mixins/routes/infinite-scroll/record-array";
import RefreshRouteMixin from "ui/routes/-mixins/routes/refresh";
import { toArray, mapBy } from "utils/ember/recordArrayMethods";
import preload from "utils/preload";


Expand All @@ -11,28 +11,25 @@ function filterMatches( filter, value ) {
}

const contentPaths = {
all: "controller.model.streams",
all: "controller.model.channels",
games: "controller.model.games",
channels: "controller.model.channels",
streams: "controller.model.streams"
channels: "controller.model.channels"
};

const itemSelectors = {
all: ".stream-item-component",
all: ".channel-item-component",
games: ".game-item-component",
channels: ".channel-item-component",
streams: ".stream-item-component"
channels: ".channel-item-component"
};

const fetchMethods = {
all: "fetchStreams",
all: "fetchChannels",
games: "fetchGames",
channels: "fetchChannels",
streams: "fetchStreams"
channels: "fetchChannels"
};


export default Route.extend( PaginationMixin, RefreshRouteMixin, {
export default UserIndexRoute.extend( PaginationMixin, RefreshRouteMixin, {
queryParams: {
filter: {
refreshModel: true,
Expand All @@ -59,66 +56,75 @@ export default Route.extend( PaginationMixin, RefreshRouteMixin, {
},

async model( params ) {
const [ games, channels, streams ] = await Promise.all([
const [ games, channels ] = await Promise.all([
this.fetchGames( params ),
this.fetchChannels( params ),
this.fetchStreams( params )
this.fetchChannels( params )
]);

return { games, channels, streams };
return { games, channels };
},

fetchContent() {
const fetchMethod = get( this, "fetchMethod" );
const filter = get( this, "controller.filter" );
const query = get( this, "controller.query" );
const { fetchMethod, controller: { filter, query } } = this;

return this[ fetchMethod ]({ filter, query });
},


/**
* @return {Promise<TwitchSearchGame[]>}
*/
async fetchGames({ filter, query }) {
if ( !filterMatches( filter, "games" ) ) {
return [];
}

const store = get( this, "store" );
const records = await store.query( "twitchSearchGame", {
type: "suggest",
live: true,
query
});
const first = this.calcFetchSize( itemSelectors[ "games" ], 2 );
const queryData = { query, first };
if ( filter === "games" ) {
queryData[ "first" ] = this.limit;
if ( this.paginationMethod && this.paginationCursor ) {
queryData[ this.paginationMethod ] = this.paginationCursor;
}
}

/** @type {TwitchSearchGame[]} */
const records = await this.store.query( "twitch-search-game", queryData );

if ( filter === "games" ) {
this.paginationCursor = get( records, "meta.pagination.cursor" );
}

return await preload( toArray( records ), "game.box.largeLatest" );
return await preload( await toArray( records ), "game.box_art_url.latest" );
},

/**
* @return {Promise<TwitchSearchChannel[]>}
*/
async fetchChannels({ filter, query }) {
if ( !filterMatches( filter, "channels" ) ) {
return [];
}

const store = get( this, "store" );
const records = await store.query( "twitchSearchChannel", {
offset: get( this, "offset" ),
limit: get( this, "limit" ),
query
});

return await preload( mapBy( records, "channel" ), "logo" );
},

async fetchStreams({ filter, query }) {
if ( !filterMatches( filter, "streams" ) ) {
return [];
const queryData = { query, first: this.limit };
if ( this.paginationMethod && this.paginationCursor ) {
queryData[ this.paginationMethod ] = this.paginationCursor;
}

const store = get( this, "store" );
const records = await store.query( "twitchSearchStream", {
offset: get( this, "offset" ),
limit: get( this, "limit" ),
query
/** @type {TwitchSearchChannel[]} */
let records = await this.store.query( "twitch-search-channel", queryData );
this.paginationCursor = get( records, "meta.pagination.cursor" );

/** @type {TwitchUser[]} */
records = await map( records, async record => {
try {
await record.user.promise;
return record.user.content;
} catch ( e ) {
return false;
}
});

return await preload( mapBy( records, "stream" ), "preview.mediumLatest" );
return await preload( records, "profile_image_url" );
}
});
56 changes: 33 additions & 23 deletions src/app/ui/routes/search/template.hbs
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
<main class="content content-search">
<h2>{{t (concat "routes.search.header." filter)}}<span>{{query}}</span></h2>
{{#if noResults}}
<section class="noresults">{{t "routes.search.empty"}}</section>
{{/if}}
<h2>{{t (concat "routes.search.header." filter)}}<span>{{query}}</span></h2>
{{#if noResults}}
<section class="noresults">{{t "routes.search.empty"}}</section>
{{/if}}

{{#if games.length}}
{{#if notFiltered}}<h3>{{t "routes.search.subheader.games"}}</h3>{{/if}}
{{#content-list games infiniteScroll=(bool-not notFiltered) classNames="search-games" as |item isNewItem isDuplicate|}}
{{game-item content=item isNewItem=(if notFiltered false isNewItem) isDuplicate=isDuplicate}}
{{/content-list}}
{{/if}}
{{#if games.length}}
{{#if notFiltered}}<h3>{{t "routes.search.subheader.games"}}</h3>{{/if}}
{{#content-list
games
infiniteScroll=(bool-not notFiltered)
classNames="search-games"
as |item isNewItem isDuplicate|
}}
{{game-item
content=item.game
isNewItem=(if notFiltered false isNewItem)
isDuplicate=isDuplicate
}}
{{/content-list}}
{{/if}}

{{#if channels.length}}
{{#if notFiltered}}<h3>{{t "routes.search.subheader.channels"}}</h3>{{/if}}
{{#content-list channels infiniteScroll=(bool-not notFiltered) classNames="search-channels" as |item isNewItem isDuplicate|}}
{{channel-item content=item isNewItem=(if notFiltered false isNewItem) isDuplicate=isDuplicate}}
{{/content-list}}
{{/if}}

{{#if streams.length}}
{{#if notFiltered}}<h3>{{t "routes.search.subheader.streams"}}</h3>{{/if}}
{{#content-list streams classNames="search-streams" as |item isNewItem isDuplicate|}}
{{stream-item content=item isNewItem=isNewItem isDuplicate=isDuplicate}}
{{/content-list}}
{{/if}}
{{#if channels.length}}
{{#if notFiltered}}<h3>{{t "routes.search.subheader.channels"}}</h3>{{/if}}
{{#content-list
channels
classNames="search-channels"
as |item isNewItem isDuplicate|
}}
{{channel-item
content=item
isNewItem=isNewItem
isDuplicate=isDuplicate
}}
{{/content-list}}
{{/if}}
</main>

0 comments on commit e45a695

Please sign in to comment.