╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Settings.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
310 .enter => if (on_submit) return self.submitForm(allocator), 311 .mouse => |mouse| if (on_submit) { 312 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 313 }, 314 else => {}, 315 } 316 try child.widget.input(allocator, key, root_focus); 317 } 318 319 fn formRadio(form: *wgt.Box(ui.Widget), name: []const u8) !*ui.widget.Radio { 320 for (form.children.values()) |*child| switch (child.widget) { 321 .radio => |*radio| if (std.mem.eql(u8, radio.name, name)) return radio, 322 else => {}, 323 }; 324 return error.MissingFormField; 325 } 326 327 fn formField(form: *wgt.Box(ui.Widget), name: []const u8) !*wgt.TextInput { 328 for (form.children.values()) |*child| switch (child.widget) { 329 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 330 else => {}, 331 }; 332 return error.MissingFormField; 333 } 334 335 // update the repo and navigate to its new url. this is the terminal path; 336 // the web posts the form to the settings route. 337 fn submitForm(self: *Form, allocator: std.mem.Allocator) !void { 338 if (comptime wasm) return; 339 const io = self.session.io orelse return; 340 const users_dir = self.session.users_dir orelse return; 341 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 342 343 const name = try (try formField(self.formBox(), "name")).text(allocator); 344 defer allocator.free(name); 345 const description = try (try formField(self.formBox(), "description")).text(allocator); 346 defer allocator.free(description); 347 const access = std.meta.stringToEnum(evt.Repo.Access, (try formRadio(self.formBox(), "access")).selected()) orelse unreachable; 348 const discuss_role = labelRole((try formRadio(self.formBox(), "discuss_role")).selected()) catch unreachable; 349 const issue_role = labelRole((try formRadio(self.formBox(), "issue_role")).selected()) catch unreachable; 350 const patch_role = labelRole((try formRadio(self.formBox(), "patch_role")).selected()) catch unreachable; 351 352 const route = update(io, allocator, users_dir, actor, self.data.identity, name, description, access, discuss_role, issue_role, patch_role) catch |err| { 353 const failure = ui.Session.FormFeedback.RepoFailure.fromError(err) orelse return err; 354 const aa = self.session.arena.allocator(); 355 self.session.data.form_feedback = .{ .repo_settings = .{ .failure = failure, .fields = .{ 356 .name = try aa.dupe(u8, name), 357 .description = try aa.dupe(u8, description), 358 .access = access, 359 .discuss_role = discuss_role, 360 .issue_role = issue_role, 361 .patch_role = patch_role, 362 } } }; 363 return; 364 }; 365 try self.session.navigate(route); 366 } 367 368 fn formBox(self: *Form) *wgt.Box(ui.Widget) { 369 return &self.scroll.child.center.child.box; 370 } 371 372 pub fn clearGrid(self: *Form) void { 373 self.scroll.clearGrid(); 374 } 375 376 pub fn getGrid(self: Form) ?Grid { 377 return self.scroll.getGrid(); 378 } 379 380 pub fn getFocus(self: *Form) *Focus { 381 return self.scroll.getFocus(); 382 } 383 384 // up leaves the form from its first control 385 pub fn atTop(self: Form) bool { 386 const box = &self.scroll.child.center.child.box; 387 return box.focus.child_id == box.children.keys()[0]; 388 } 389 }; 390 391 // change the repo `identity` names as `actor`, its owner, returning its 392 // settings route under its new name 393 pub fn update( 394 io: std.Io, 395 allocator: std.mem.Allocator, 396 users_dir: []const u8, 397 actor: ui.Actor, 398 identity: []const u8, 399 name: []const u8, 400 description: []const u8, 401 access: evt.Repo.Access, 402 discuss_role: ?evt.Repo.Role, 403 issue_role: ?evt.Repo.Role, 404 patch_role: ?evt.Repo.Role, 405 ) !ui.RoutablePage { 406 const parsed = ui.RoutablePage.RepoIdentity.parse(identity) orelse return error.NotFound; 407 const owner_id = actor.repo_user_id orelse return error.NotFound; 408 try evt.updateRepo(io, allocator, users_dir, &owner_id, actor.author, parsed.name, name, description, access, discuss_role, issue_role, patch_role); 409 var buf: [ui.RoutablePage.repo_route_max_len]u8 = undefined; 410 const new_identity = std.fmt.bufPrint(&buf, "{s}:{s}", .{ parsed.owner, name }) catch return error.RouteTooLong; 411 return ui.RoutablePage.repoSettingsRoute(new_identity) orelse error.RouteTooLong; 412 } 413