Showing 2 changed files with 63 additions and 5 deletions
+1 -1
lib/Gitprep.pm
... ...
@@ -444,7 +444,7 @@ sub startup {
444 444
             $r->any('/issues/:number' => sub { shift->render_maybe('/issue') })->to(tab => 'issues');
445 445
 
446 446
             # Labels
447
-            $r->get('/labels' => sub { shift->render_maybe('/labels') })->to(tab => 'issues');
447
+            $r->any('/labels' => sub { shift->render_maybe('/labels') })->to(tab => 'issues');
448 448
             
449 449
             # Pull requests
450 450
             $r->get('/pulls' => sub { shift->render_maybe('/pulls') })->to(tab => 'pulls');
+62 -4
templates/labels.html.ep
... ...
@@ -6,6 +6,64 @@
6 6
   my $user_id = param('user');
7 7
   my $project_id = param('project');
8 8
   
9
+  my $errors;
10
+  if (lc $self->req->method eq 'post') {
11
+    my $op = param('op') // '';
12
+
13
+    my $project_row_id = app->dbi->model('project')->select(
14
+      'project.row_id',
15
+      where => {'user.id' => $user_id, 'project.id' => $project_id}
16
+    )->value;
17
+    
18
+    if ($op eq 'create') {
19
+      my $id = param('label-id');
20
+      my $color = param('color');
21
+      
22
+      my $vc = app->vc;
23
+      my $validation = $vc->validation;
24
+      
25
+      if (!length $id) {
26
+        $validation->add_failed(id => "Name can't be blank");
27
+      }
28
+      elsif (length $id > 100) {
29
+        $validation->add_failed(id => "Name is too long");
30
+      }
31
+      else {
32
+        my $label = app->dbi->model('label')->select(
33
+          where => {
34
+            project => $project_row_id,
35
+            id => $id
36
+          }
37
+        )->one;
38
+        
39
+        if ($label) {
40
+          $validation->add_failed(id => "Name has already been taken");
41
+        }
42
+      }
43
+      
44
+      if (!length $color) {
45
+        $validation->add_failed(color => "Color can't be blank");
46
+      }
47
+      
48
+      if ($validation->is_valid) {
49
+        
50
+        my $new_label = {
51
+          id => $id,
52
+          color => $color,
53
+          project => $project_row_id
54
+        };
55
+        
56
+        app->dbi->model('label')->insert($new_label);
57
+        
58
+        $self->redirect_to;
59
+        return;
60
+      }
61
+      else {
62
+        $errors = $validation->messages;
63
+      }
64
+    }
65
+  }
66
+  
9 67
   my $labels = app->dbi->model('label')->select(
10 68
     {__MY__ => '*'},
11 69
     where => {'project__user.id' => $user_id, 'project.id' => $project_id},
... ...
@@ -38,33 +96,33 @@
38 96
       $(this).closest('li').find('.labels-edit-area').hide();
39 97
     });
40 98
     
41
-    // labels-edit-save-btn
42 99
   });
43 100
 % end
44 101
 
45 102
 %= include '/include/header';
46 103
 
47 104
 <div class="container">
105
+  %= include '/include/errors', errors => $errors;
48 106
   <div class="labels-new-panel">
49 107
     <div class="labels-new-btn btn btn-success">
50 108
       New label
51 109
     </div>
52 110
   </div>
53 111
   <form class="labels-create-panel" action="<%= url_for %>" method="post" style="display:nonea">
112
+    <%= hidden_field op => 'create' %>
54 113
     <div class="labels-create-left">
55 114
       <%= input_tag 'label-id', class => 'labels-create-label-id' %>
56 115
       <div class="labels-create-label-color-area">
57 116
         <%= input_tag 'color' => $default_color, class => 'labels-create-label-color' %>
58 117
         <div class ="labels-create-label-color-palette" style="background:<%= $default_color %>;"></div>
118
+        <div class="error"></div>
59 119
       </div>
60 120
     </div>
61 121
     <div class="labels-create-right">
62 122
       <div class="labels-create-cancel-btn btn">
63 123
         Cancel
64 124
       </div>
65
-      <div class="labels-create-create-btn btn btn-success">
66
-        Create label
67
-      </div>
125
+      <input type="submit" value="Create label" class="labels-create-create-btn btn btn-success">
68 126
     </div>
69 127
   </form>
70 128
   <ul class="labels">