This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
73,956 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\ReportRequest; | ||
use App\Report; | ||
|
||
/** | ||
* Reports API controller. | ||
* | ||
* @version 2.0.0 | ||
* @author Gustavo Straube <[email protected]> | ||
*/ | ||
class ReportsController extends Controller | ||
{ | ||
/** | ||
* Create a new controller instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->middleware('auth'); | ||
} | ||
|
||
public function store(ReportRequest $request): Report | ||
{ | ||
$report = Report::create($request->validated()); | ||
$report->url = route('reports.index', [ 'report_id' => $report->id ]); | ||
|
||
return $report; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* Report request. | ||
* | ||
* @version 2.0.0 | ||
* @author Gustavo Straube <[email protected]> | ||
*/ | ||
class ReportRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'name' => [ | ||
'required', | ||
'string', | ||
'max:100', | ||
], | ||
'filter' => [ | ||
'required', | ||
'array', | ||
'min:1', | ||
], | ||
'filter.started' => [ | ||
'nullable', | ||
'string', | ||
'date_format:Y-m-d', | ||
], | ||
'filter.end' => [ | ||
'nullable', | ||
'string', | ||
'date_format:Y-m-d', | ||
'after:filter.started', | ||
], | ||
'filter.user_id' => [ | ||
'nullable', | ||
'integer', | ||
'exists:users,id', | ||
], | ||
'filter.project_id' => [ | ||
'nullable', | ||
'integer', | ||
'exists:projects,id', | ||
], | ||
'filter.activity_id' => [ | ||
'nullable', | ||
'integer', | ||
'exists:activities,id', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Get the validated data from the request. | ||
* | ||
* @return array | ||
*/ | ||
public function validated() | ||
{ | ||
$data = parent::validated(); | ||
$data['code'] = Str::random(20); | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<template> | ||
<div class="modal fade" ref="modal" id="create-project" tabindex="-1" role="dialog" aria-labelledby="create-project-label" aria-hidden="true"> | ||
<div class="modal-dialog"> | ||
<form class="modal-content" @submit.prevent="onSubmit"> | ||
<div class="modal-header"> | ||
<h5 class="modal-title" id="create-project-label">New Report</h5> | ||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> | ||
<span aria-hidden="true">×</span> | ||
</button> | ||
</div> | ||
<div class="modal-body"> | ||
<div class="form-row"> | ||
<div class="form-group col"> | ||
<label>Name</label> | ||
<input :class="{ 'form-control': true, 'is-invalid': error('name') }" type="text" v-model="name" /> | ||
<span v-if="error('name')" class="invalid-feedback" role="alert"> | ||
{{ error('name') }} | ||
</span> | ||
</div> | ||
</div> | ||
<span v-if="error('filter')" class="invalid-feedback d-block" role="alert"> | ||
{{ error('filter') }} | ||
</span> | ||
<span v-for="error in filterErrors()" class="invalid-feedback d-block" role="alert"> | ||
{{ error }} | ||
</span> | ||
</div> | ||
<div class="modal-footer"> | ||
<button type="button" class="btn btn-link" data-dismiss="modal">Cancel</button> | ||
<button type="submit" class="btn btn-primary" :disabled="isSubmitting">Save</button> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data() { | ||
return { | ||
isSubmitting: false, | ||
errors: {}, | ||
// Form | ||
name: '', | ||
filters: {}, | ||
} | ||
}, | ||
methods: { | ||
reset() { | ||
this.isSubmitting = false; | ||
this.errors = {}; | ||
this.name = ''; | ||
this.filters = {}; | ||
}, | ||
error(name) { | ||
if (!this.errors || !this.errors[name]) { | ||
return null; | ||
} | ||
return this.errors[name].join(' '); | ||
}, | ||
filterErrors() { | ||
const errors = []; | ||
Object.keys(this.errors).filter((name) => !!name.match(/^filter\./)).forEach((name) => { | ||
errors.push(this.error(name)); | ||
}); | ||
return errors; | ||
}, | ||
fieldsToObject(form) { | ||
return Array.from(form.elements).reduce((data, element) => { | ||
if (!element.value) { | ||
return data; | ||
} | ||
const name = element.name.replace(/^.*\[(.+)\]$/, '$1'); | ||
return Object.assign(data, { [name] : element.value }); | ||
}, {}); | ||
}, | ||
onCreate(form) { | ||
this.reset(); | ||
this.filters = this.fieldsToObject(form); | ||
jQuery(this.$refs.modal).modal('show'); | ||
}, | ||
onModalHide(event) { | ||
if (event.target !== this.$refs.modal) { | ||
return; | ||
} | ||
this.reset(); | ||
}, | ||
onSubmit() { | ||
this.isSubmitting = true; | ||
this.errors = {}; | ||
(async () => { | ||
try { | ||
const data = { | ||
name: this.name, | ||
filter: this.filters, | ||
}; | ||
const report = await axios.post(this.$root.route('api.reports.store'), data); | ||
this.isSubmitting = false; | ||
jQuery(this.$refs.modal).modal('hide'); | ||
location = report.data.url; | ||
} catch (e) { | ||
console.error(e); | ||
this.isSubmitting = false; | ||
if (e.response && e.response.data && e.response.data.errors) { | ||
this.errors = e.response.data.errors; | ||
return; | ||
} | ||
this.$root.alert('Something went wrong while saving the report. Please check the info you provided and try again.'); | ||
} | ||
})(); | ||
}, | ||
}, | ||
created() { | ||
jQuery(document).on('hidden.bs.modal', this.reset); | ||
this.$root.$on('create-report', this.onCreate); | ||
}, | ||
beforeDestroyed() { | ||
jQuery(document).off('hidden.bs.modal', this.reset); | ||
}, | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters