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