]> Untitled Git - lemmy.git/blob - ui/src/components/setup.tsx
Merge pull request #10 from dessalines/dev
[lemmy.git] / ui / src / components / setup.tsx
1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import {
5   RegisterForm,
6   LoginResponse,
7   UserOperation,
8   WebSocketJsonResponse,
9 } from '../interfaces';
10 import { WebSocketService, UserService } from '../services';
11 import { wsJsonToRes, toast } from '../utils';
12 import { SiteForm } from './site-form';
13 import { i18n } from '../i18next';
14
15 interface State {
16   userForm: RegisterForm;
17   doneRegisteringUser: boolean;
18   userLoading: boolean;
19 }
20
21 export class Setup extends Component<any, State> {
22   private subscription: Subscription;
23
24   private emptyState: State = {
25     userForm: {
26       username: undefined,
27       password: undefined,
28       password_verify: undefined,
29       admin: true,
30       show_nsfw: true,
31     },
32     doneRegisteringUser: false,
33     userLoading: false,
34   };
35
36   constructor(props: any, context: any) {
37     super(props, context);
38
39     this.state = this.emptyState;
40
41     this.subscription = WebSocketService.Instance.subject
42       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
43       .subscribe(
44         msg => this.parseMessage(msg),
45         err => console.error(err),
46         () => console.log('complete')
47       );
48   }
49
50   componentWillUnmount() {
51     this.subscription.unsubscribe();
52   }
53
54   componentDidMount() {
55     document.title = `${i18n.t('setup')} - Lemmy`;
56   }
57
58   render() {
59     return (
60       <div class="container">
61         <div class="row">
62           <div class="col-12 offset-lg-3 col-lg-6">
63             <h3>{i18n.t('lemmy_instance_setup')}</h3>
64             {!this.state.doneRegisteringUser ? (
65               this.registerUser()
66             ) : (
67               <SiteForm />
68             )}
69           </div>
70         </div>
71       </div>
72     );
73   }
74
75   registerUser() {
76     return (
77       <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
78         <h5>{i18n.t('setup_admin')}</h5>
79         <div class="form-group row">
80           <label class="col-sm-2 col-form-label" htmlFor="username">
81             {i18n.t('username')}
82           </label>
83           <div class="col-sm-10">
84             <input
85               type="text"
86               class="form-control"
87               id="username"
88               value={this.state.userForm.username}
89               onInput={linkEvent(this, this.handleRegisterUsernameChange)}
90               required
91               minLength={3}
92               maxLength={20}
93               pattern="[a-zA-Z0-9_]+"
94             />
95           </div>
96         </div>
97         <div class="form-group row">
98           <label class="col-sm-2 col-form-label" htmlFor="email">
99             {i18n.t('email')}
100           </label>
101
102           <div class="col-sm-10">
103             <input
104               type="email"
105               id="email"
106               class="form-control"
107               placeholder={i18n.t('optional')}
108               value={this.state.userForm.email}
109               onInput={linkEvent(this, this.handleRegisterEmailChange)}
110               minLength={3}
111             />
112           </div>
113         </div>
114         <div class="form-group row">
115           <label class="col-sm-2 col-form-label" htmlFor="password">
116             {i18n.t('password')}
117           </label>
118           <div class="col-sm-10">
119             <input
120               type="password"
121               id="password"
122               value={this.state.userForm.password}
123               onInput={linkEvent(this, this.handleRegisterPasswordChange)}
124               class="form-control"
125               required
126             />
127           </div>
128         </div>
129         <div class="form-group row">
130           <label class="col-sm-2 col-form-label" htmlFor="verify-password">
131             {i18n.t('verify_password')}
132           </label>
133           <div class="col-sm-10">
134             <input
135               type="password"
136               id="verify-password"
137               value={this.state.userForm.password_verify}
138               onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
139               class="form-control"
140               required
141             />
142           </div>
143         </div>
144         <div class="form-group row">
145           <div class="col-sm-10">
146             <button type="submit" class="btn btn-secondary">
147               {this.state.userLoading ? (
148                 <svg class="icon icon-spinner spin">
149                   <use xlinkHref="#icon-spinner"></use>
150                 </svg>
151               ) : (
152                 i18n.t('sign_up')
153               )}
154             </button>
155           </div>
156         </div>
157       </form>
158     );
159   }
160
161   handleRegisterSubmit(i: Setup, event: any) {
162     event.preventDefault();
163     i.state.userLoading = true;
164     i.setState(i.state);
165     event.preventDefault();
166     WebSocketService.Instance.register(i.state.userForm);
167   }
168
169   handleRegisterUsernameChange(i: Setup, event: any) {
170     i.state.userForm.username = event.target.value;
171     i.setState(i.state);
172   }
173
174   handleRegisterEmailChange(i: Setup, event: any) {
175     i.state.userForm.email = event.target.value;
176     i.setState(i.state);
177   }
178
179   handleRegisterPasswordChange(i: Setup, event: any) {
180     i.state.userForm.password = event.target.value;
181     i.setState(i.state);
182   }
183
184   handleRegisterPasswordVerifyChange(i: Setup, event: any) {
185     i.state.userForm.password_verify = event.target.value;
186     i.setState(i.state);
187   }
188
189   parseMessage(msg: WebSocketJsonResponse) {
190     let res = wsJsonToRes(msg);
191     if (msg.error) {
192       toast(i18n.t(msg.error), 'danger');
193       this.state.userLoading = false;
194       this.setState(this.state);
195       return;
196     } else if (res.op == UserOperation.Register) {
197       let data = res.data as LoginResponse;
198       this.state.userLoading = false;
199       this.state.doneRegisteringUser = true;
200       UserService.Instance.login(data);
201       this.setState(this.state);
202     } else if (res.op == UserOperation.CreateSite) {
203       this.props.history.push('/');
204     }
205   }
206 }