feat(travel): Refactor route handling to use direction instead of destination
This commit is contained in:
@@ -256,8 +256,8 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>目的地</label>
|
||||
<select id="dest_select">
|
||||
<label>方向</label>
|
||||
<select id="direction_select">
|
||||
<option value="">請先選擇路線</option>
|
||||
</select>
|
||||
</div>
|
||||
@@ -327,11 +327,11 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
|
||||
function updateStations() {
|
||||
const lineCode = document.getElementById('line_select').value;
|
||||
const stationSelect = document.getElementById('station_select');
|
||||
const destSelect = document.getElementById('dest_select');
|
||||
const directionSelect = document.getElementById('direction_select');
|
||||
|
||||
if (!lineCode) {
|
||||
stationSelect.innerHTML = '<option value="">請先選擇路線</option>';
|
||||
destSelect.innerHTML = '<option value="">請先選擇路線</option>';
|
||||
directionSelect.innerHTML = '<option value="">請先選擇路線</option>';
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -343,7 +343,18 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
|
||||
).join('');
|
||||
|
||||
stationSelect.innerHTML = '<option value="">請選擇車站</option>' + stationsHtml;
|
||||
destSelect.innerHTML = '<option value="">請選擇目的地</option>' + stationsHtml;
|
||||
|
||||
const stations = line.stations;
|
||||
if (stations.length >= 2) {
|
||||
const firstStation = stations[0];
|
||||
const lastStation = stations[stations.length - 1];
|
||||
const directionsHtml =
|
||||
`<option value="${firstStation.code}">${firstStation.name}方向</option>` +
|
||||
`<option value="${lastStation.code}">${lastStation.name}方向</option>`;
|
||||
directionSelect.innerHTML = '<option value="">請選擇方向</option>' + directionsHtml;
|
||||
} else {
|
||||
directionSelect.innerHTML = '<option value="">路線車站不足</option>';
|
||||
}
|
||||
}
|
||||
|
||||
function renderRoutes() {
|
||||
@@ -357,7 +368,7 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
|
||||
<div class="route-item">
|
||||
<div class="route-info">
|
||||
<span class="route-line" style="background: ${route.line_color}">${route.line_name}</span>
|
||||
${route.station_name} → ${route.dest_name}
|
||||
${route.station_name} → ${route.direction_name}
|
||||
</div>
|
||||
<button class="danger" style="width: auto; padding: 5px 10px;"
|
||||
onclick="removeRoute(${index})">刪除</button>
|
||||
@@ -368,21 +379,21 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
|
||||
async function addRoute() {
|
||||
const lineCode = document.getElementById('line_select').value;
|
||||
const stationCode = document.getElementById('station_select').value;
|
||||
const destCode = document.getElementById('dest_select').value;
|
||||
const directionCode = document.getElementById('direction_select').value;
|
||||
|
||||
if (!lineCode || !stationCode || !destCode) {
|
||||
showStatus('請選擇路線、出發站和目的地', 'error');
|
||||
if (!lineCode || !stationCode || !directionCode) {
|
||||
showStatus('請選擇路線、出發站和方向', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
if (stationCode === destCode) {
|
||||
showStatus('出發站和目的地不能相同', 'error');
|
||||
if (stationCode === directionCode) {
|
||||
showStatus('出發站和方向不能相同', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const line = linesData.find(l => l.code === lineCode);
|
||||
const station = line.stations.find(s => s.code === stationCode);
|
||||
const dest = line.stations.find(s => s.code === destCode);
|
||||
const direction = line.stations.find(s => s.code === directionCode);
|
||||
|
||||
const route = {
|
||||
line_code: lineCode,
|
||||
@@ -390,8 +401,8 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
|
||||
line_color: line.color,
|
||||
station_code: stationCode,
|
||||
station_name: station.name,
|
||||
dest_code: destCode,
|
||||
dest_name: dest.name
|
||||
direction: directionCode,
|
||||
direction_name: direction.name + '方向'
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -536,8 +547,8 @@ esp_err_t WebHandler::get_routes_handler_(httpd_req_t* req) {
|
||||
cJSON_AddStringToObject(route_obj, "line_color", route.line_color.c_str());
|
||||
cJSON_AddStringToObject(route_obj, "station_code", route.station_code.c_str());
|
||||
cJSON_AddStringToObject(route_obj, "station_name", route.station_name.c_str());
|
||||
cJSON_AddStringToObject(route_obj, "dest_code", route.dest_code.c_str());
|
||||
cJSON_AddStringToObject(route_obj, "dest_name", route.dest_name.c_str());
|
||||
cJSON_AddStringToObject(route_obj, "direction", route.direction.c_str());
|
||||
cJSON_AddStringToObject(route_obj, "direction_name", route.direction_name.c_str());
|
||||
cJSON_AddItemToArray(routes_arr, route_obj);
|
||||
}
|
||||
cJSON_AddItemToObject(root, "routes", routes_arr);
|
||||
@@ -603,15 +614,15 @@ esp_err_t WebHandler::add_route_handler_(httpd_req_t* req) {
|
||||
item = cJSON_GetObjectItem(root, "station_name");
|
||||
if (item && cJSON_IsString(item)) route.station_name = item->valuestring;
|
||||
|
||||
item = cJSON_GetObjectItem(root, "dest_code");
|
||||
if (item && cJSON_IsString(item)) route.dest_code = item->valuestring;
|
||||
item = cJSON_GetObjectItem(root, "direction");
|
||||
if (item && cJSON_IsString(item)) route.direction = item->valuestring;
|
||||
|
||||
item = cJSON_GetObjectItem(root, "dest_name");
|
||||
if (item && cJSON_IsString(item)) route.dest_name = item->valuestring;
|
||||
item = cJSON_GetObjectItem(root, "direction_name");
|
||||
if (item && cJSON_IsString(item)) route.direction_name = item->valuestring;
|
||||
|
||||
cJSON_Delete(root);
|
||||
|
||||
if (route.line_code.empty() || route.station_code.empty() || route.dest_code.empty()) {
|
||||
if (route.line_code.empty() || route.station_code.empty() || route.direction.empty()) {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing required fields");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user