Skip to content

Commit

Permalink
randomness implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
zazedd committed May 21, 2023
1 parent a1f0f83 commit dfe3248
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"elm/html": "1.0.0",
"elm/http": "2.0.0",
"elm/json": "1.1.3",
"elm/random": "1.0.0",
"elm/url": "1.0.0",
"fapian/elm-html-aria": "1.4.0",
"folkertdev/elm-flate": "2.0.5",
Expand All @@ -22,7 +23,6 @@
"myrho/elm-round": "1.0.5"
},
"indirect": {
"elm/random": "1.0.0",
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.3",
"elm-community/list-extra": "8.7.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Dashboard.elm
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ playlists =
td [ class "table-dark" ]
[ text playlist.name ]
, td [ class "table-dark" ]
[ a [ href playlist.url ] [ text "Link" ]
[ a [ href ("https://open.spotify.com/playlist/" ++ playlist.id) ] [ text "Link" ]
]

-- , th [ class "table-dark" ]
Expand Down
11 changes: 8 additions & 3 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Register exposing (submitRegister)
import Types exposing (..)
import Url exposing (Protocol(..))
import Url.Parser exposing (s, top)
import Random exposing (..)


type alias Flags =
Expand Down Expand Up @@ -58,6 +59,7 @@ init flags url key =
, token = flags.token
, mood = 5
, genre = "Rock"
, randomInt = 0
, divvis = visibleController ()
, playlist = Nothing
, tracks = Nothing
Expand Down Expand Up @@ -215,7 +217,7 @@ update msg model =
( model, Cmd.none )

ToggleDiv ->
( { model | divvis = { visible1 = not model.divvis.visible1, visible2 = not model.divvis.visible2 } }, Cmd.none )
( { model | divvis = { visible1 = not model.divvis.visible1, visible2 = not model.divvis.visible2 } }, Random.generate RandomInt (Random.int 1 30))

MoodUpdate m ->
( { model
Expand All @@ -231,8 +233,11 @@ update msg model =
, Cmd.none
)

RandomInt x ->
( { model | randomInt = x }, Cmd.none )

PlaylistSubmit ->
( model, playlistRequest model.access_token model.mood model.genre )
( model, playlistRequest model model.access_token model.mood model.genre )

PlaylistRequest (Ok playlist_response) ->
let
Expand Down Expand Up @@ -273,7 +278,7 @@ update msg model =
in
case repeat of
PlayListSpotifyRequest ->
( m, playlistRequest model.access_token model.mood model.genre )
( m, playlistRequest model model.access_token model.mood model.genre )

TracksSpotifyRequest ->
case model.playlist of
Expand Down
2 changes: 1 addition & 1 deletion src/Mood.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Mood exposing (mood, storePlaylist)

import Common exposing (nbsp, sidebar)
import Html exposing (Html, button, div, h1, img, input, label, option, p, select, source, span, text, video)
import Html.Attributes exposing (autoplay, class, classList, disabled, for, height, href, id, loop, selected, src, step, style, type_, value, width)
import Html.Attributes exposing (autoplay, class, classList, disabled, for, height, href, id, loop, src, step, style, type_, value, width)
import Html.Attributes.Aria exposing (ariaLabel)
import Html.Events exposing (onClick, onInput)
import Http
Expand Down
13 changes: 7 additions & 6 deletions src/PlaylistApi.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Html exposing (..)
import Http
import Json.Decode as Json
import Types exposing (..)
import Random


moodSwitch : Int -> String
Expand All @@ -28,8 +29,8 @@ moodSwitch mood =
""


playlistRequest : String -> Int -> String -> Cmd Msg
playlistRequest access_token mood genre =
playlistRequest : Model -> String -> Int -> String -> Cmd Msg
playlistRequest model access_token mood genre =
Http.request
{ method = "GET"
, headers =
Expand All @@ -40,15 +41,15 @@ playlistRequest access_token mood genre =
]
, url = "https://api.spotify.com/v1/search?q=" ++ (mood |> moodSwitch) ++ ("+" ++ genre) ++ "&type=playlist&market=PT&limit=30"
, body = Http.emptyBody
, expect = Http.expectJson PlaylistRequest playlistDecoder
, expect = Http.expectJson PlaylistRequest (playlistDecoder model)
, timeout = Nothing
, tracker = Nothing
}


playlistDecoder : Json.Decoder Playlist
playlistDecoder =
Json.at [ "playlists", "items", "0" ]
playlistDecoder : Model -> Json.Decoder Playlist
playlistDecoder model =
Json.at [ "playlists", "items", (model.randomInt |> String.fromInt) ]
(Json.map5 Playlist
(Json.field "name" Json.string)
(Json.field "id" Json.string)
Expand Down
4 changes: 3 additions & 1 deletion src/Types.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Http
import Json.Decode
import Json.Encode
import Url
import Random


type Route
Expand Down Expand Up @@ -209,6 +210,7 @@ type alias Model =
, token : String
, mood : Int
, genre : String
, randomInt : Int
, divvis : DivVisibility
, playlist : Maybe Playlist
, tracks : Maybe (List Tracks)
Expand All @@ -219,7 +221,6 @@ type alias Model =
, playlistsStored : List PlaylistStored
}


type SpotifyRequest
= PlayListSpotifyRequest
| TracksSpotifyRequest
Expand All @@ -243,6 +244,7 @@ type Msg
| ToggleDiv
| MoodUpdate Int
| GenreUpdate String
| RandomInt Int
| PlaylistSubmit
| PlaylistRequest (Result Http.Error Playlist)
| RefreshTokenRequest ( SpotifyRequest, Result Http.Error SpotifyAuth )
Expand Down

0 comments on commit dfe3248

Please sign in to comment.