Skip to content

Commit

Permalink
ページごとにモジュール化
Browse files Browse the repository at this point in the history
  • Loading branch information
hibohiboo committed Mar 17, 2019
1 parent 92ce0fc commit 909fc5a
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 36 deletions.
31 changes: 25 additions & 6 deletions src/assets/js/GitHub.elm
@@ -1,4 +1,4 @@
module GitHub exposing (Issue, Repo, getIssues, getRepos) module GitHub exposing (Issue, Repo, getIssues, getRepos, issueUrl)


import Http import Http
import Json.Decode as D exposing (Decoder) import Json.Decode as D exposing (Decoder)
Expand All @@ -12,7 +12,7 @@ import Url.Builder


type alias Repo = type alias Repo =
{ name : String { name : String
, description : String , description : Maybe String
, language : Maybe String , language : Maybe String
, owner : String , owner : String
, fork : Int , fork : Int
Expand All @@ -37,7 +37,7 @@ repoDecoder : Decoder Repo
repoDecoder = repoDecoder =
D.map7 Repo D.map7 Repo
(D.field "name" D.string) (D.field "name" D.string)
(D.field "description" D.string) (D.maybe (D.field "description" D.string))
(D.maybe (D.field "language" D.string)) (D.maybe (D.field "language" D.string))
(D.at [ "owner", "login" ] D.string) (D.at [ "owner", "login" ] D.string)
(D.field "forks" D.int) (D.field "forks" D.int)
Expand All @@ -61,15 +61,34 @@ issueDecoder =
getRepos : (Result Http.Error (List Repo) -> msg) -> String -> Cmd msg getRepos : (Result Http.Error (List Repo) -> msg) -> String -> Cmd msg
getRepos toMsg userName = getRepos toMsg userName =
Http.get Http.get
{ url = { url = repoUrl userName
Url.Builder.crossOrigin "https://api.github.com" [ "users", userName, "repos" ] []
, expect = Http.expectJson toMsg reposDecoder , expect = Http.expectJson toMsg reposDecoder
} }




getIssues : (Result Http.Error (List Issue) -> msg) -> String -> String -> Cmd msg getIssues : (Result Http.Error (List Issue) -> msg) -> String -> String -> Cmd msg
getIssues toMsg userName projectName = getIssues toMsg userName projectName =
Http.get Http.get
{ url = Url.Builder.crossOrigin "https://api.github.com" [ "repos", userName, projectName, "issues" ] [] { url = issuesUrl userName projectName
, expect = Http.expectJson toMsg issuesDecoder , expect = Http.expectJson toMsg issuesDecoder
} }


domain : String
domain =
"https://api.github.com"


repoUrl : String -> String
repoUrl userName =
Url.Builder.crossOrigin domain [ "users", userName, "repos" ] []


issuesUrl : String -> String -> String
issuesUrl userName projectName =
Url.Builder.crossOrigin domain [ "repos", userName, projectName, "issues" ] []


issueUrl : String -> String -> Int -> String
issueUrl userName projectName number =
Url.Builder.crossOrigin domain [ "repos", userName, projectName, "issues", String.fromInt number ] []
54 changes: 31 additions & 23 deletions src/assets/js/Main.elm
Expand Up @@ -6,6 +6,7 @@ import GitHub exposing (Issue, Repo)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
import Http import Http
import Page.Repo exposing (..)
import Route exposing (..) import Route exposing (..)
import Url import Url
import Url.Builder import Url.Builder
Expand Down Expand Up @@ -42,7 +43,7 @@ type Page
| ErrorPage Http.Error | ErrorPage Http.Error
| TopPage | TopPage
| UserPage (List Repo) | UserPage (List Repo)
| RepoPage (List Issue) | RepoPage Page.Repo.Model




init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg ) init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
Expand All @@ -61,6 +62,7 @@ type Msg
= UrlRequested Browser.UrlRequest = UrlRequested Browser.UrlRequest
| UrlChanged Url.Url | UrlChanged Url.Url
| Loaded (Result Http.Error Page) | Loaded (Result Http.Error Page)
| RepoMsg Page.Repo.Msg




update : Msg -> Model -> ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg )
Expand Down Expand Up @@ -92,6 +94,22 @@ update msg model =
, Cmd.none , Cmd.none
) )


-- Repoページのメッセージが来たとき
RepoMsg repoMsg ->
-- 現在表示中のページが
case model.page of
-- RepoPageであれば、
RepoPage repoModel ->
-- Repoページのupdate処理を行う
let
( newRepoModel, topCmd ) =
Page.Repo.update repoMsg repoModel
in
( { model | page = RepoPage newRepoModel }, Cmd.map RepoMsg topCmd )

_ ->
( model, Cmd.none )





{- パスに応じて各ページを初期化する -} {- パスに応じて各ページを初期化する -}
Expand All @@ -117,11 +135,13 @@ goTo maybeRoute model =
) )


Just (Route.Repo userName projectName) -> Just (Route.Repo userName projectName) ->
-- RepoPageを取得 -- Repo ページの初期化
( model let
, GitHub.getIssues (Result.map RepoPage >> Loaded) ( repoModel, repoCmd ) =
userName Page.Repo.init userName projectName
projectName in
( { model | page = RepoPage repoModel }
, Cmd.map RepoMsg repoCmd
) )




Expand Down Expand Up @@ -156,8 +176,10 @@ view model =
UserPage repos -> UserPage repos ->
viewUserPage repos viewUserPage repos


RepoPage issues -> RepoPage repoPageModel ->
viewRepoPage issues -- Repoページのview関数を呼ぶ
Page.Repo.view repoPageModel
|> Html.map RepoMsg
] ]
} }


Expand Down Expand Up @@ -185,7 +207,7 @@ viewTopPage : Html Msg
viewTopPage = viewTopPage =
ul [] ul []
[ viewLink (Url.Builder.absolute [ "elm" ] []) [ viewLink (Url.Builder.absolute [ "elm" ] [])
, viewLink (Url.Builder.absolute [ "evancz" ] []) , viewLink (Url.Builder.absolute [ "hibohiboo" ] [])
] ]




Expand All @@ -198,20 +220,6 @@ viewUserPage repos =
) )




viewRepoPage : List Issue -> Html msg
viewRepoPage issues =
ul [] (List.map viewIssue issues)


viewIssue : Issue -> Html msg
viewIssue issue =
li []
[ span [] [ text ("[" ++ issue.state ++ "]") ]
, span [] [ text ("#" ++ String.fromInt issue.number) ]
, span [] [ text issue.title ]
]


viewLink : String -> Html msg viewLink : String -> Html msg
viewLink path = viewLink path =
li [] [ a [ href path ] [ text path ] ] li [] [ a [ href path ] [ text path ] ]
67 changes: 67 additions & 0 deletions src/assets/js/Page/Repo.elm
@@ -0,0 +1,67 @@
module Page.Repo exposing (Model, Msg, init, update, view)

import GitHub exposing (Issue, Repo)
import Html exposing (..)
import Html.Attributes exposing (..)
import Http


type alias Model =
{ userName : String
, projectName : String
, state : State
}


type State
= Init
| Loaded (List Issue)
| Error Http.Error


init : String -> String -> ( Model, Cmd Msg )
init userName projectName =
-- ページの初期化
-- 最初のModelを作ると同時に、ページの表示に必要なデータをHttpで取得
( Model userName projectName Init
, GitHub.getIssues GotIssues userName projectName
)


type Msg
= GotIssues (Result Http.Error (List Issue))


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
--- init での Http リクエストの結果が得られたら Model を変更する
case msg of
GotIssues (Ok issues) ->
( { model | state = Loaded issues }, Cmd.none )

GotIssues (Err err) ->
( { model | state = Error err }, Cmd.none )


view : Model -> Html Msg
view model =
case model.state of
Init ->
text "Loading ..."

Loaded issues ->
ul [] (List.map (viewIssue model.userName model.projectName) issues)

Error e ->
text (Debug.toString e)


viewIssue : String -> String -> Issue -> Html Msg
viewIssue userName projectName issue =
li []
[ span [] [ text ("[" ++ issue.state ++ "]") ]
, a [ href (GitHub.issueUrl userName projectName issue.number), target "_blank" ]
[ text ("#" ++ String.fromInt issue.number)
, text issue.title
]
]
14 changes: 7 additions & 7 deletions src/assets/js/index.ts
Expand Up @@ -16,11 +16,11 @@ const app = Elm.Main.init({ node: mountNode, flags });


// app.ports.initialize.subscribe(() => { // app.ports.initialize.subscribe(() => {
// }); // });
app.ports.toJs.subscribe((data: string) => { // app.ports.toJs.subscribe((data: string) => {
// localStorage[STORAGE_KEY] = data; // // localStorage[STORAGE_KEY] = data;


// // 本文がなければ、ストレージから削除してしまう // // // 本文がなければ、ストレージから削除してしまう
// if(data.trim().length == 0){ // // if(data.trim().length == 0){
// localStorage.removeItem(STORAGE_KEY); // // localStorage.removeItem(STORAGE_KEY);
// } // // }
}); // });

0 comments on commit 909fc5a

Please sign in to comment.