╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ NewUser.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
5 const inp = @import("./input.zig"); 6 const xit = @import("xit"); 7 const xitui = xit.xitui; 8 const wgt = xitui.widget; 9 const layout = xitui.layout; 10 const Key = xitui.input.Key; 11 const Grid = xitui.grid.Grid; 12 const Focus = xitui.focus.Focus; 13 14 const wasm = builtin.target.cpu.arch == .wasm32; 15 16 // the signup form every page's new user tab shows while logged out 17 pub const View = struct { 18 center: ui.widget.Center, 19 session: *ui.Session, 20 21 // `route` is the page's own new-user route, which the web form posts to 22 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, route: ui.RoutablePage) !View { 23 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 24 errdefer box.deinit(allocator); 25 box.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 26 27 const saved_fields = if (session.formFeedback(.user)) |saved| saved.fields else null; 28 29 { 30 var name = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " name " }, .name = "name", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 31 errdefer name.deinit(allocator); 32 name.getFocus().mode = .all; 33 if (saved_fields) |saved| try name.setContent(allocator, saved.name); 34 try box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_input = name }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 35 box.getFocus().child_id = name.getFocus().id; 36 } 37 38 { 39 var email = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " email " }, .name = "email", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 40 errdefer email.deinit(allocator); 41 email.getFocus().mode = .all; 42 if (saved_fields) |saved| try email.setContent(allocator, saved.email); 43 try box.children.put(allocator, email.getFocus().id, .{ .widget = .{ .text_input = email }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 44 } 45 46 { 47 var password = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " password " }, .password = true, .name = "password", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 48 errdefer password.deinit(allocator); 49 password.getFocus().mode = .all; 50 try box.children.put(allocator, password.getFocus().id, .{ .widget = .{ .text_input = password }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 51 } 52 53 { 54 var password_again = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " password again " }, .password = true, .name = "password_again", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 55 errdefer password_again.deinit(allocator); 56 password_again.getFocus().mode = .all; 57 try box.children.put(allocator, password_again.getFocus().id, .{ .widget = .{ .text_input = password_again }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 58 } 59 60 { 61 var submit = try ui.widget.SubmitButton.initLabeled(allocator, "submit"); 62 errdefer submit.deinit(allocator); 63 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 64 } 65 66 return .{ .center = try ui.widget.Center.init(allocator, .{ .box = box }), .session = session }; 67 } 68 69 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 70 self.center.deinit(allocator); 71 } 72 73 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 74 self.clearGrid(); 75 const failure = if (self.session.formFeedback(.user)) |saved| saved.failure else null; 76 (try formField(self.formBox(), "name")).options.top_label.text = if (failure) |value| switch (value) { 77 .required_name => " name (required) ", 78 .invalid_name => " name (invalid) ", 79 .name_taken => " name (taken) ", 80 else => " name ", 81 } else " name "; 82 (try formField(self.formBox(), "email")).options.top_label.text = if (failure) |value| switch (value) { 83 .required_email => " email (required) ", 84 .email_taken => " email (taken) ", 85 else => " email ", 86 } else " email "; 87 (try formField(self.formBox(), "password")).options.top_label.text = if (failure == .required_password) " password (required) " else " password "; 88 (try formField(self.formBox(), "password_again")).options.top_label.text = if (failure == .password_mismatch) " password again (doesn't match) " else " password again "; 89 // the web form handling finds the inputs by focus id 90 const inputs_arena = self.session.arena.allocator(); 91 for (self.formBox().children.values()) |*child| switch (child.widget) { 92 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 93 else => {}, 94 }; 95 try self.center.build(allocator, constraint, root_focus); 96 } 97 98 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 99 const cid = self.formBox().getFocus().child_id orelse return; 100 const cur = self.formBox().children.getIndex(cid) orelse return; 101 const child = &self.formBox().children.values()[cur]; 102 103 const on_submit = child.widget == .submit_button; 104 const direction = inp.vertDirection(key); 105 if (direction == .up or key == .back_tab) return if (cur > 0) root_focus.setFocus(self.formBox().children.keys()[cur - 1]); 106 if (direction == .down or key == .tab) return if (cur + 1 < self.formBox().children.count()) root_focus.setFocus(self.formBox().children.keys()[cur + 1]); 107 switch (key) { 108 .enter => if (on_submit) return self.submitForm(allocator), 109 .mouse => |mouse| if (on_submit) { 110 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 111 }, 112 else => {}, 113 } 114 try child.widget.input(allocator, key, root_focus); 115 } 116 117 fn formField(form: *wgt.Box(ui.Widget), name: []const u8) !*wgt.TextInput { 118 for (form.children.values()) |*child| switch (child.widget) { 119 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 120 else => {}, 121 }; 122 return error.MissingFormField; 123 } 124 125 // create the user, log in, and navigate to their page. this is the 126 // terminal path; the web posts the form to the new-user route. 127 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 128 if (comptime wasm) return; 129 const io = self.session.io orelse return; 130 const users_dir = self.session.users_dir orelse return; 131 const admin_repo = self.session.admin_repo orelse return; 132 133 const name_input = try formField(self.formBox(), "name"); 134 const email_input = try formField(self.formBox(), "email"); 135 const password_input = try formField(self.formBox(), "password"); 136 const password_again_input = try formField(self.formBox(), "password_again"); 137 const name = try name_input.text(allocator); 138 defer allocator.free(name); 139 const email = try email_input.text(allocator); 140 defer allocator.free(email); 141 const password = try password_input.text(allocator); 142 defer allocator.free(password); 143 const password_again = try password_again_input.text(allocator); 144 defer allocator.free(password_again); 145 146 const id = evt.createUser(io, allocator, users_dir, admin_repo, name, email, password, password_again) catch |err| { 147 const aa = self.session.arena.allocator(); 148 self.session.data.form_feedback = .{ .user = .{ 149 .failure = ui.Session.FormFeedback.UserFailure.fromError(err) orelse return err, 150 .fields = .{ .name = try aa.dupe(u8, name), .email = try aa.dupe(u8, email) }, 151 } }; 152 return; 153 }; 154 155 // log in as the new user, reading them from a moment that has them 156 self.session.data.user_id = try self.session.arena.allocator().dupe(u8, &id); 157 try self.session.rememberSshKey(); 158 self.session.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 159 try self.session.loadUser(); 160 161 // wipe the form so a return visit starts fresh 162 name_input.clear(allocator); 163 email_input.clear(allocator); 164 password_input.clear(allocator); 165 password_again_input.clear(allocator); 166 167 try self.session.navigate(.{ .user_repos = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong } }); 168 } 169 170 fn formBox(self: *View) *wgt.Box(ui.Widget) { 171 return &self.center.child.box; 172 } 173 174 pub fn clearGrid(self: *View) void { 175 self.center.clearGrid(); 176 } 177 178 pub fn getGrid(self: View) ?Grid { 179 return self.center.getGrid(); 180 } 181 182 pub fn getFocus(self: *View) *Focus { 183 return self.center.getFocus(); 184 } 185 186 // up leaves the form from its first control 187 pub fn atTop(self: View) bool { 188 const box = &self.center.child.box; 189 return box.focus.child_id == box.children.keys()[0]; 190 } 191 }; 192