|
| 1 | +import { useStore } from "../store"; |
| 2 | +import ListErrors from "../components/ListErrors"; |
| 3 | +import { createState } from "solid-js"; |
| 4 | + |
1 | 5 | export default () => { |
| 6 | + const [store, { logout, updateUser }] = useStore(), |
| 7 | + [state, setState] = createState({ |
| 8 | + image: store.currentUser.image || "", |
| 9 | + username: store.currentUser.username, |
| 10 | + bio: store.currentUser.bio || "", |
| 11 | + email: store.currentUser.email, |
| 12 | + password: "" |
| 13 | + }), |
| 14 | + updateState = field => ev => setState(field, ev.target.value), |
| 15 | + submitForn = ev => { |
| 16 | + ev.preventDefault(); |
| 17 | + const user = Object.assign({}, state); |
| 18 | + if (!user.password) { |
| 19 | + delete user.password; |
| 20 | + } |
| 21 | + setState({ updatingUser: true }); |
| 22 | + updateUser(user) |
| 23 | + .then(() => location.hash = `/@${user.username}`) |
| 24 | + .catch(errors => setState({ errors })) |
| 25 | + .finally(() => setState({ updatingUser: false })); |
| 26 | + }; |
| 27 | + |
2 | 28 | return ( |
3 | 29 | <div class="settings-page"> |
4 | 30 | <div class="container page"> |
5 | 31 | <div class="row"> |
6 | 32 | <div class="col-md-6 offset-md-3 col-xs-12"> |
7 | 33 | <h1 class="text-xs-center">Your Settings</h1> |
8 | | - <form> |
| 34 | + <ListErrors errors={state.errors} /> |
| 35 | + <form onSubmit={submitForn}> |
9 | 36 | <fieldset> |
10 | 37 | <fieldset class="form-group"> |
11 | 38 | <input |
12 | 39 | class="form-control" |
13 | 40 | type="text" |
14 | 41 | placeholder="URL of profile picture" |
| 42 | + value={state.image} |
| 43 | + onChange={updateState("image")} |
15 | 44 | /> |
16 | 45 | </fieldset> |
17 | 46 | <fieldset class="form-group"> |
18 | 47 | <input |
19 | 48 | class="form-control form-control-lg" |
20 | 49 | type="text" |
21 | 50 | placeholder="Your Name" |
| 51 | + value={state.username} |
| 52 | + onChange={updateState("username")} |
22 | 53 | /> |
23 | 54 | </fieldset> |
24 | 55 | <fieldset class="form-group"> |
25 | 56 | <textarea |
26 | 57 | class="form-control form-control-lg" |
27 | 58 | rows="8" |
28 | 59 | placeholder="Short bio about you" |
| 60 | + value={state.bio} |
| 61 | + onChange={updateState("bio")} |
29 | 62 | ></textarea> |
30 | 63 | </fieldset> |
31 | 64 | <fieldset class="form-group"> |
32 | 65 | <input |
33 | 66 | class="form-control form-control-lg" |
34 | 67 | type="text" |
35 | 68 | placeholder="Email" |
| 69 | + value={state.email} |
| 70 | + onChange={updateState("email")} |
36 | 71 | /> |
37 | 72 | </fieldset> |
38 | 73 | <fieldset class="form-group"> |
39 | 74 | <input |
40 | 75 | class="form-control form-control-lg" |
41 | 76 | type="password" |
42 | 77 | placeholder="Password" |
| 78 | + value={state.password} |
| 79 | + onChange={updateState("password")} |
43 | 80 | /> |
44 | 81 | </fieldset> |
45 | | - <button class="btn btn-lg btn-primary pull-xs-right"> |
| 82 | + <button |
| 83 | + class="btn btn-lg btn-primary pull-xs-right" |
| 84 | + type="submit" |
| 85 | + disabled={state.updatingUser} |
| 86 | + > |
46 | 87 | Update Settings |
47 | 88 | </button> |
48 | 89 | </fieldset> |
49 | 90 | </form> |
| 91 | + <hr/> |
| 92 | + <button |
| 93 | + class="btn btn-outline-danger" |
| 94 | + onClick={() => (logout(), location.hash = "/")} |
| 95 | + > |
| 96 | + Or click here to logout. |
| 97 | + </button> |
50 | 98 | </div> |
51 | 99 | </div> |
52 | 100 | </div> |
|
0 commit comments