Merge pull request 'Poder editar la direccion de un proyecto' (#17) from feature/editar-direccion-proyecto into develop
Reviewed-on: #17
This commit is contained in:
@ -9,3 +9,7 @@ $app->group('/direcciones', function($app) {
|
||||
$app->post('/find[/]', [Direcciones::class, 'findComunas']);
|
||||
});
|
||||
});
|
||||
$app->group('/direccion/{direccion_id:[0-9]+}', function($app) {
|
||||
$app->post('/edit[/]', [Direcciones::class, 'edit']);
|
||||
$app->get('[/]', [Direcciones::class, 'get']);
|
||||
});
|
||||
|
@ -2,7 +2,10 @@
|
||||
|
||||
use Incoviba\Controller\API\Regiones;
|
||||
|
||||
//$app->group('/regiones', function($app) {});
|
||||
$app->group('/regiones', function($app) {
|
||||
$app->get('[/]', Regiones::class);
|
||||
});
|
||||
$app->group('/region/{region_id}', function($app) {
|
||||
$app->get('/provincias[/]', [Regiones::class, 'provincias']);
|
||||
$app->get('/comunas[/]', [Regiones::class, 'comunas']);
|
||||
});
|
||||
|
@ -15,7 +15,7 @@
|
||||
<table class="ui striped table">
|
||||
<tr>
|
||||
<td>Dirección</td>
|
||||
<td>{{$proyecto->direccion()}}</td>
|
||||
<td>{{$proyecto->direccion()}} <a href="#" id="edit_direccion_button"><i class="small edit icon"></i></a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Inmobiliaria</td>
|
||||
@ -137,6 +137,7 @@
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@include('proyectos.show.edit_direccion')
|
||||
@include('layout.body.scripts.chartjs')
|
||||
|
||||
@push('page_scripts')
|
||||
@ -485,6 +486,11 @@
|
||||
ventas.setup({id_ventas: '#ventas', id_stock: '#stock', id_proyeccion: '#proyeccion',
|
||||
id_chart_general: '#chart_venta_general', id_chart_tipologias: '#chart_venta_tipologia',
|
||||
id_chart_velocidad: '#chart_venta_velocidad'})
|
||||
|
||||
document.getElementById('edit_direccion_button').addEventListener('click', event => {
|
||||
event.preventDefault()
|
||||
$('#edit_direccion_modal').modal('show')
|
||||
})
|
||||
})
|
||||
</script>
|
||||
@endpush
|
||||
|
136
app/resources/views/proyectos/show/edit_direccion.blade.php
Normal file
136
app/resources/views/proyectos/show/edit_direccion.blade.php
Normal file
@ -0,0 +1,136 @@
|
||||
<div class="ui modal" id="edit_direccion_modal">
|
||||
<div class="header">
|
||||
Editar Dirección
|
||||
</div>
|
||||
<div class="content">
|
||||
<form class="ui form" id="edit_direccion_form">
|
||||
<input type="hidden" name="id" />
|
||||
<div class="fields">
|
||||
<div class="field">
|
||||
<label>Calle</label>
|
||||
<input type="text" name="calle" value="{{ $proyecto->direccion()->calle }}" />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Número</label>
|
||||
<input type="text" name="numero" value="{{ $proyecto->direccion()->numero }}" />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Extra</label>
|
||||
<input type="text" name="extra" value="{{ $proyecto->direccion()->extra }}" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="fields">
|
||||
<div class="field">
|
||||
<label>Comuna</label>
|
||||
<div class="ui search selection dropdown" id="comuna">
|
||||
<input type="hidden" name="comuna" />
|
||||
<i class="dropdown icon"></i>
|
||||
<div class="default text">Comuna</div>
|
||||
<div class="menu"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Región</label>
|
||||
<div class="ui search selection dropdown" id="region">
|
||||
<input type="hidden" name="region" />
|
||||
<i class="dropdown icon"></i>
|
||||
<div class="default text">Región</div>
|
||||
<div class="menu"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<div class="ui black deny button">
|
||||
Cancelar
|
||||
</div>
|
||||
<div class="ui green ok button">
|
||||
Guardar
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@push('page_scripts')
|
||||
<script>
|
||||
function submitForm() {
|
||||
const form = document.getElementById('edit_direccion_form')
|
||||
const body = new FormData(form)
|
||||
const url = '{{$urls->api}}/direccion/{{ $proyecto->direccion()->id }}/edit'
|
||||
const method = 'post'
|
||||
APIClient.fetch(url, {method, body}).then(response => {
|
||||
if (response.ok) {
|
||||
return response.json().then(json => {
|
||||
if (json.success) {
|
||||
window.location.reload()
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
function fetchComunas(region_id) {
|
||||
const url = `{{ $urls->api }}/region/${region_id}/comunas`
|
||||
APIClient.fetch(url).then(response => {
|
||||
if (!response.ok) {
|
||||
return;
|
||||
}
|
||||
return response.json().then(json => {
|
||||
const dropdown = $('#comuna')
|
||||
const values = []
|
||||
json.comunas.forEach(comuna => {
|
||||
values.push({
|
||||
name: comuna.descripcion,
|
||||
value: comuna.id,
|
||||
text: comuna.descripcion
|
||||
})
|
||||
})
|
||||
dropdown.dropdown('change values', values)
|
||||
|
||||
if (json.region_id === {{ $proyecto->direccion()->comuna->provincia->region->id }}) {
|
||||
dropdown.dropdown('set selected', {{ $proyecto->direccion()->comuna->id }})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
function fetchRegiones() {
|
||||
const url = '{{ $urls->api }}/regiones'
|
||||
APIClient.fetch(url).then(response => {
|
||||
if (!response.ok) {
|
||||
return;
|
||||
}
|
||||
return response.json().then(json => {
|
||||
const dropdown = $('#region')
|
||||
const values = []
|
||||
json.regiones.forEach(region => {
|
||||
values.push({
|
||||
name: region.descripcion,
|
||||
value: region.id,
|
||||
text: region.descripcion
|
||||
})
|
||||
})
|
||||
dropdown.dropdown({
|
||||
values,
|
||||
onChange: (value, text, $choice) => {
|
||||
fetchComunas(value)
|
||||
}
|
||||
})
|
||||
$('#comuna').dropdown()
|
||||
dropdown.dropdown('set selected', {{ $proyecto->direccion()->comuna->provincia->region->id }})
|
||||
})
|
||||
})
|
||||
}
|
||||
$(document).ready(function() {
|
||||
fetchRegiones()
|
||||
$('#edit_direccion_modal').modal({
|
||||
onApprove: function() {
|
||||
submitForm()
|
||||
}
|
||||
})
|
||||
document.getElementById('edit_direccion_form').addEventListener('submit', event => {
|
||||
event.preventDefault()
|
||||
submitForm()
|
||||
return false
|
||||
})
|
||||
})
|
||||
</script>
|
||||
@endpush
|
@ -14,6 +14,24 @@ class Direcciones
|
||||
{
|
||||
use withRedis, withJson;
|
||||
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Repository\Direccion $direccionRepository, int $direccion_id): ResponseInterface
|
||||
{
|
||||
$output = ['direccion_id' => $direccion_id, 'direccion' => null];
|
||||
$redisKey = "direcciones:{$direccion_id}";
|
||||
try {
|
||||
$direccion = $this->fetchRedis($redisService, $redisKey);
|
||||
$output['direccion'] = $direccion;
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$direccion = $direccionRepository->fetchById($direccion_id);
|
||||
$output['direccion'] = $direccion;
|
||||
$this->saveRedis($redisService, $redisKey, $direccion, 60 * 60 * 24 * 30);
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function comunas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Repository\Region $regionRepository, Repository\Comuna $comunaRepository,
|
||||
int $region_id) : ResponseInterface
|
||||
@ -64,7 +82,7 @@ class Direcciones
|
||||
$body = $request->getBody();
|
||||
$json = json_decode($body->getContents());
|
||||
$output = ['input' => $json, 'total' => 0, 'comunas' => []];
|
||||
$redisKey = "comunas:direccion:{$json->direccion}";
|
||||
$redisKey = "direcciones:{$json->direccion}:comuna";
|
||||
try {
|
||||
$output['comunas'] = $this->fetchRedis($redisService, $redisKey);
|
||||
} catch (EmptyRedis) {
|
||||
@ -77,4 +95,21 @@ class Direcciones
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
|
||||
public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Repository\Direccion $direccionRepository, int $direccion_id): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = ['direccion_id' => $direccion_id, 'input' => $body, 'direccion' => [], 'success' => false];
|
||||
$redisKey = "direcciones:{$direccion_id}";
|
||||
try {
|
||||
$direccion = $direccionRepository->fetchById($direccion_id);
|
||||
$filteredData = $direccionRepository->filterData($body);
|
||||
$output['direccion'] = $direccionRepository->edit($direccion, $filteredData);
|
||||
$output['success'] = true;
|
||||
$this->saveRedis($redisService, $redisKey, $output['direccion'], 60 * 60 * 24 * 30);
|
||||
} catch (EmptyResult) {}
|
||||
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,21 @@ class Regiones
|
||||
{
|
||||
use withRedis, withJson;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Repository\Region $regionRepository): ResponseInterface
|
||||
{
|
||||
$regiones = [];
|
||||
try {
|
||||
$regiones = $this->fetchRedis($redisService, 'regiones');
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$regiones = $regionRepository->fetchAll();
|
||||
$this->saveRedis($redisService, 'regiones', $regiones, 60 * 60 * 24 * 30);
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
return $this->withJson($response, compact('regiones'));
|
||||
}
|
||||
|
||||
public function provincias(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Repository\Provincia $provinciaRepository, int $region_id): ResponseInterface
|
||||
{
|
||||
@ -36,4 +51,54 @@ class Regiones
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function comunas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Repository\Provincia $provinciaRepository,
|
||||
Repository\Comuna $comunaRepository, int $region_id): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'region_id' => $region_id,
|
||||
'comunas' => []
|
||||
];
|
||||
$comunas = [];
|
||||
try {
|
||||
$redisKey = "provincias:region:{$region_id}";
|
||||
$provincias = $this->fetchRedis($redisService, $redisKey);
|
||||
foreach ($provincias as $provincia) {
|
||||
$comunas = array_merge($comunas, $this->fetchComunasByProvincia($redisService, $comunaRepository, $provincia->id));
|
||||
}
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$provincias = $provinciaRepository->fetchByRegion($region_id);
|
||||
usort($provincias, function (Model\Provincia $a, Model\Provincia $b) {
|
||||
return strcmp($a->descripcion, $b->descripcion);
|
||||
});
|
||||
$this->saveRedis($redisService, $redisKey, $provincias, 60 * 60 * 24 * 30);
|
||||
foreach ($provincias as $provincia) {
|
||||
$comunas = array_merge($comunas, $this->fetchComunasByProvincia($redisService, $comunaRepository, $provincia->id));
|
||||
}
|
||||
} catch (EmptyResult) {}
|
||||
} finally {
|
||||
usort($comunas, function ($a, $b) {
|
||||
return strcmp($a->descripcion, $b->descripcion);
|
||||
});
|
||||
$output['comunas'] = $comunas;
|
||||
}
|
||||
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
|
||||
protected function fetchComunasByProvincia(Service\Redis $redisService, Repository\Comuna $comunaRepository, int $provincia_id): array
|
||||
{
|
||||
$redisKey = "comunas:provincia:{$provincia_id}";
|
||||
try {
|
||||
$comunas = $this->fetchRedis($redisService, $redisKey);
|
||||
} catch (EmptyRedis) {
|
||||
$comunas = $comunaRepository->fetchByProvincia($provincia_id);
|
||||
usort($comunas, function (Model\Comuna $a, Model\Comuna $b) {
|
||||
return strcmp($a->descripcion, $b->descripcion);
|
||||
});
|
||||
$this->saveRedis($redisService, $redisKey, $comunas, 60 * 60 * 24 * 30);
|
||||
}
|
||||
return $comunas;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user