|
1 | 1 | <script lang="ts"> |
2 | | - import Button from 'onyx-ui/components/buttons/Button.svelte'; |
3 | 2 | import Card from 'onyx-ui/components/card/Card.svelte'; |
4 | 3 | import CardContent from 'onyx-ui/components/card/CardContent.svelte'; |
5 | 4 | import CardHeader from 'onyx-ui/components/card/CardHeader.svelte'; |
6 | | - import Divider from 'onyx-ui/components/divider/Divider.svelte'; |
7 | | - import InputRow from 'onyx-ui/components/form/InputRow.svelte'; |
8 | | - import SelectRow from 'onyx-ui/components/form/SelectRow.svelte'; |
9 | | - import ListItem from 'onyx-ui/components/list/ListItem.svelte'; |
10 | | - import Typography from 'onyx-ui/components/Typography.svelte'; |
11 | 5 | import View from 'onyx-ui/components/view/View.svelte'; |
12 | 6 | import ViewContent from 'onyx-ui/components/view/ViewContent.svelte'; |
13 | | - import { DataStatus } from 'onyx-ui/enums'; |
14 | | - import { registerView, updateView } from 'onyx-ui/stores/view'; |
15 | | - import { getShortcutFromIndex } from 'onyx-ui/utils/getShortcutFromIndex'; |
16 | | - import { onMount } from 'svelte'; |
17 | | - import { push, querystring, replace } from 'svelte-spa-router'; |
18 | | - import { SoundCloud } from '../lib/soundcloud'; |
19 | | - import { getImage } from '../utils/getImage'; |
| 7 | + import { registerView, view } from 'onyx-ui/stores/view'; |
| 8 | + import { replace } from 'svelte-spa-router'; |
| 9 | + import SearchTracks from '../components/SearchTracks.svelte'; |
| 10 | + import SearchUsers from '../components/SearchUsers.svelte'; |
20 | 11 |
|
21 | | - let getData = Promise.resolve([]); |
| 12 | + export let params: { cardId: string }; |
22 | 13 |
|
23 | | - registerView({}); |
24 | | -
|
25 | | - function search() { |
26 | | - getData = new SoundCloud().user.search(query); |
27 | | - } |
28 | | -
|
29 | | - onMount(async () => { |
30 | | - if (query?.length >= 3) { |
31 | | - search(); |
32 | | - await getData; |
33 | | - updateView({ dataStatus: DataStatus.Loaded }); |
34 | | - } |
| 14 | + registerView({ |
| 15 | + cards: [ |
| 16 | + { |
| 17 | + id: 'users', |
| 18 | + title: 'Search Users', |
| 19 | + onSelect: () => replace(`/search/users`), |
| 20 | + }, |
| 21 | + { |
| 22 | + id: 'tracks', |
| 23 | + title: 'Search Tracks', |
| 24 | + onSelect: () => replace(`/search/tracks`), |
| 25 | + }, |
| 26 | + { |
| 27 | + id: 'playlists', |
| 28 | + title: 'Search Playlists', |
| 29 | + onSelect: () => replace(`/search/playlists`), |
| 30 | + }, |
| 31 | + ], |
| 32 | + activeCardId: params.cardId ?? 'users', |
35 | 33 | }); |
36 | | -
|
37 | | - $: query = new URLSearchParams($querystring).get('q') || ''; |
38 | | - $: type = new URLSearchParams($querystring).get('type') || 'users'; |
39 | 34 | </script> |
40 | 35 |
|
41 | 36 | <View> |
42 | 37 | <ViewContent> |
43 | | - <Card> |
44 | | - <CardHeader title="Search" /> |
45 | | - <CardContent> |
46 | | - <InputRow |
47 | | - label="Query" |
48 | | - value={query} |
49 | | - placeholder="Enter text..." |
50 | | - onChange={(val) => replace(`/search?q=${val}&type=${type}`)} |
51 | | - /> |
52 | | - <SelectRow |
53 | | - label="Result Type" |
54 | | - value={type} |
55 | | - disabled |
56 | | - options={[ |
57 | | - { id: 'tracks', label: 'Tracks' }, |
58 | | - { id: 'playlists', label: 'Playlists' }, |
59 | | - { id: 'users', label: 'Users' }, |
60 | | - ]} |
61 | | - onChange={(val) => replace(`/search?q=${query}&type=${val}`)} |
62 | | - /> |
63 | | - <Button |
64 | | - title="Search" |
65 | | - disabled={query.length < 3} |
66 | | - navi={{ |
67 | | - itemId: `signinCode`, |
68 | | - onSelect: async () => search(), |
69 | | - }} |
70 | | - /> |
71 | | - <Divider title="Results" /> |
72 | | - {#await getData} |
73 | | - <Typography align="center">Loading...</Typography> |
74 | | - {:then data} |
75 | | - {#each data as user, i (user.id)} |
76 | | - <ListItem |
77 | | - imageUrl={getImage(user.avatar_url, 60)} |
78 | | - primaryText={user.full_name || user.username} |
79 | | - secondaryText={`${user.followers_count.toLocaleString()} followers`} |
80 | | - navi={{ |
81 | | - itemId: `${i + 1}`, |
82 | | - shortcutKey: getShortcutFromIndex(i), |
83 | | - onSelect: () => push(`/user/${user.id}`), |
84 | | - }} |
85 | | - /> |
86 | | - {:else} |
87 | | - <Typography align="center">No results</Typography> |
88 | | - {/each} |
89 | | - {:catch error} |
90 | | - <Typography align="center">Failed to load data</Typography> |
91 | | - {/await} |
92 | | - </CardContent> |
93 | | - </Card> |
| 38 | + {#if params.cardId === $view.cards[0].id} |
| 39 | + <Card cardId={$view.cards[0].id}> |
| 40 | + <CardHeader /> |
| 41 | + <CardContent> |
| 42 | + <SearchUsers /> |
| 43 | + </CardContent> |
| 44 | + </Card> |
| 45 | + {:else if params.cardId === $view.cards[1].id} |
| 46 | + <Card cardId={$view.cards[1].id}> |
| 47 | + <CardHeader /> |
| 48 | + <CardContent> |
| 49 | + <SearchTracks /> |
| 50 | + </CardContent> |
| 51 | + </Card> |
| 52 | + {:else if params.cardId === $view.cards[2].id} |
| 53 | + <Card cardId={$view.cards[2].id}> |
| 54 | + <CardHeader /> |
| 55 | + <CardContent> |
| 56 | + <div>search playlists</div> |
| 57 | + </CardContent> |
| 58 | + </Card> |
| 59 | + {/if} |
94 | 60 | </ViewContent> |
95 | 61 | </View> |
0 commit comments