-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecurrings_pages.cpp
More file actions
115 lines (87 loc) · 3.35 KB
/
recurrings_pages.cpp
File metadata and controls
115 lines (87 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//=======================================================================
// Copyright (c) 2013-2020 Baptiste Wicht.
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
// http://opensource.org/licenses/MIT)
//=======================================================================
#include "recurring.hpp"
#include "pages/html_writer.hpp"
#include "pages/recurrings_pages.hpp"
#include "http.hpp"
using namespace budget;
namespace {
void add_frequencypicker(budget::writer& w, std::string_view default_value = "") {
w << R"=====(
<div class="form-group">
<label for="input_recurs">Recurrence</label>
<select class="form-control" id="input_recurs" name="input_recurs">
)=====";
if ("monthly" == default_value) {
w << "<option selected value=\"monthly\">Monthly</option>";
} else {
w << "<option value=\"monthly\">Monthly</option>";
}
if ("weekly" == default_value) {
w << "<option selected value=\"weekly\">Weekly</option>";
} else {
w << "<option value=\"weekly\">Weekly</option>";
}
w << R"=====(
</select>
</div>
)=====";
}
void add_type_picker(budget::writer& w, std::string_view default_value = "") {
w << R"=====(
<div class="form-group">
<label for="input_type">Type</label>
<select class="form-control" id="input_type" name="input_type">
)=====";
if ("expense" == default_value) {
w << "<option selected value=\"expense\">Expense</option>";
} else {
w << "<option value=\"expense\">Expense</option>";
}
if ("earning" == default_value) {
w << "<option selected value=\"earning\">Earning</option>";
} else {
w << "<option value=\"earning\">Earning</option>";
}
w << R"=====(
</select>
</div>
)=====";
}
} // namespace
void budget::recurrings_list_page(html_writer& w) {
budget::show_recurrings(w);
make_tables_sortable(w);
}
void budget::add_recurrings_page(html_writer& w) {
w << title_begin << "New Recurring Expense" << title_end;
form_begin(w, "/api/recurrings/add/", "/recurrings/add/");
add_name_picker(w);
add_amount_picker(w);
add_account_picker(w, budget::local_day());
add_frequencypicker(w);
add_type_picker(w);
form_end(w);
}
void budget::edit_recurrings_page(html_writer& w, const httplib::Request& req) {
if (!req.has_param("input_id") || !req.has_param("back_page")) {
return display_error_message(w, "Invalid parameter for the request");
}
auto input_id = req.get_param_value("input_id");
if (!recurring_exists(budget::to_number<size_t>(input_id))) {
return display_error_message(w, "The recurring expense {} does not exist", input_id);
}
auto back_page = req.get_param_value("back_page");
w << title_begin << "Edit Recurring Expense " << input_id << title_end;
form_begin_edit(w, "/api/recurrings/edit/", back_page, input_id);
auto recurring = recurring_get(budget::to_number<size_t>(input_id));
add_name_picker(w, recurring.name);
add_amount_picker(w, budget::money_to_string(recurring.amount));
add_account_picker(w, budget::local_day(), budget::to_string(recurring.account));
add_frequencypicker(w, recurring.recurs);
form_end(w);
}