Skip to content

Commit

Permalink
Update user
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Ito committed Nov 23, 2021
1 parent 5f1b312 commit 1f263ec
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,4 @@ ASALocalRun/

packages/
.ionide/
localAccount.txt
3 changes: 3 additions & 0 deletions src/App/Pages/Settings/Settings.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ let SettingsPage (model: State) dispatch =
//let updateProfile
let view =
let user = model.User.Value
let token = user.Token

//let state, dispatch = Store.makeElmish (init model) update ignore ()
let url = Store.make user.Image
let username = Store.make user.Username
Expand Down Expand Up @@ -187,6 +189,7 @@ let SettingsPage (model: State) dispatch =
UpdateUser { UpsertUser.Username = username.Value
Image = url.Value
Bio = bio.Value
Token = Some token
Email = email.Value
Password = password.Value }))
[]
Expand Down
1 change: 1 addition & 0 deletions src/App/Pages/SignUp/SignUp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ let SignUpPage dispatch =
SignUp { UpsertUser.Username = username.Value
Image = String.Empty
Bio = String.Empty
Token = None
Email = email.Value
Password = password.Value }))
[]
Expand Down
6 changes: 3 additions & 3 deletions src/App/Services/Api/ArticleApi.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ArticleFilter =

let getTags () =
let url =
"https://cirosantilli-realworld-next.herokuapp.com/api/tags"
"https://conduit.productionready.io/api/tags"

promise {
let! response = Fetch.fetch url []
Expand All @@ -22,7 +22,7 @@ let getTags () =

let getArticles limit offset (filter: ArticleFilter option) =
let url =
"https://cirosantilli-realworld-next.herokuapp.com/api/articles"
"https://conduit.productionready.io/api/articles"

let url =
$"{url}?limit={limit}&offset={offset}"
Expand All @@ -41,7 +41,7 @@ let getArticles limit offset (filter: ArticleFilter option) =

let getArticle slug =
let url =
$"https://cirosantilli-realworld-next.herokuapp.com/api/articles/{slug}"
$"https://conduit.productionready.io/api/articles/{slug}"

promise {
let! response = Fetch.fetch url []
Expand Down
26 changes: 24 additions & 2 deletions src/App/Services/Api/ProfileApi.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open Fable.Core.JsInterop

let signUp (upsertUser: UpsertUser) =
let url =
"https://cirosantilli-realworld-next.herokuapp.com/api/users"
"https://conduit.productionready.io/api/users"

promise {
let json = upsertUser.toJson ()
Expand All @@ -24,10 +24,32 @@ let signUp (upsertUser: UpsertUser) =
return User.fromJson response
}

let updateUser (upsertUser: UpsertUser) =
let url =
"https://conduit.productionready.io/api/user"

promise {
let json = upsertUser.toJson ()

let! response =
Fetch.fetch
url
[ Method HttpMethod.PUT
Fetch.requestHeaders [
Accept "application/json"
Authorization $"Token {upsertUser.Token.Value}"
ContentType "application/json"
]
Body !^json ]

let! response = response.text ()
return User.fromJson response
}

let getProfile username =

let url =
$"https://cirosantilli-realworld-next.herokuapp.com/api/profiles/{username}"
$"https://conduit.productionready.io/api/profiles/{username}"

promise {
let! response = Fetch.fetch url []
Expand Down
2 changes: 1 addition & 1 deletion src/App/Services/Elmish.fs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ let private update (msg: Message) (state: State) =

let errorFn error = UnsuccessfulLogin error

state, Cmd.OfPromise.either ProfileApi.signUp upsertUser successFn (fun e -> UnsuccessfulLogin e)
state, Cmd.OfPromise.either ProfileApi.updateUser upsertUser successFn (fun e -> UnsuccessfulLogin e)
| Logout ->
LocalStorage.removeItem LocalStorageKeys.User

Expand Down
6 changes: 5 additions & 1 deletion src/App/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ type UpsertUser =
{ Username: string
Image: string
Bio: string
Token: string option
Email: string
Password: string }

Expand All @@ -126,7 +127,10 @@ type UpsertUser =
"image", Encode.option Encode.string (Option.ofString user.Image)
"bio", Encode.option Encode.string (Option.ofString user.Bio)
"email", Encode.string user.Email
"password", Encode.option Encode.string (Option.ofString user.Password)
if not (String.IsNullOrWhiteSpace user.Password)
then "password", Encode.string user.Password
if user.Token.IsSome
then "token", Encode.option Encode.string user.Token
]
Encode.object [ "user", userEncoder ]
static member Decoder = decoder<UpsertUser> Extra.empty
Expand Down

0 comments on commit 1f263ec

Please sign in to comment.