Skip to content

Commit bb9b302

Browse files
committed
refact vue
1 parent 387407c commit bb9b302

File tree

5 files changed

+85
-53
lines changed

5 files changed

+85
-53
lines changed

app/Http/Controllers/Admin/TransactionController.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
class TransactionController extends Controller
1111
{
12+
private $typeService;
13+
14+
public function __construct(?TransactionTypeService $typeService)
15+
{
16+
$this->typeService = $typeService;
17+
}
1218

1319
public function index()
1420
{
@@ -49,13 +55,12 @@ public function update($key, TransactionService $service)
4955

5056
public function getTransactionListQuery()
5157
{
52-
$typeService = new TransactionTypeService();
5358
$query =
5459
\DB::table('transactions as tr')
5560
->select('tr.amount', 'tr.created_at', 'u.name as debit_user_name', 'user.name as crebit_user_name', 'tr.transaction_key')
56-
->where('tr.transaction_type_id', $typeService->getDebitId())
61+
->where('tr.transaction_type_id', $this->typeService->getDebitId())
5762
->join('transactions as trans', 'tr.transaction_key', '=', 'trans.transaction_key')
58-
->where('trans.transaction_type_id', $typeService->getCreditId())
63+
->where('trans.transaction_type_id', $this->typeService->getCreditId())
5964
->join('users as user', 'trans.user_id', '=', 'user.id')
6065
->join('users as u', 'tr.user_id', '=', 'u.id')
6166
;

app/UseCases/TransactionService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class TransactionService
99
{
1010
private $typeService;
1111

12-
public function __construct()
12+
public function __construct(TransactionTypeService $typeService)
1313
{
14-
$this->typeService = new TransactionTypeService();
14+
$this->typeService = $typeService;
1515
}
1616

1717
public function create($debit_user, $credit_user, $amount)

database/seeds/DatabaseSeeder.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\UseCases\TransactionService;
34
use Illuminate\Database\Seeder;
45

56
class DatabaseSeeder extends Seeder
@@ -9,7 +10,7 @@ class DatabaseSeeder extends Seeder
910
*
1011
* @return void
1112
*/
12-
public function run()
13+
public function run(TransactionService $service)
1314
{
1415
$this->createTransactionTypes();
1516

@@ -22,16 +23,16 @@ public function run()
2223
'email' => 'brath1@example.org',
2324
'password' => '$2y$10$SHOpPjq2SAlrZ.Uv1ljZjuYY1eSt2Df57E8tBTYh8fby65tCflnX.',
2425
]);
25-
$service = new \App\UseCases\TransactionService();
2626

2727
foreach ($users as $user) {
2828
$amount = rand(100, 400);
29-
29+
3030
$service->create($user, $myUser, $amount);
3131
}
32+
3233
foreach ($users as $user) {
3334
$amount = rand(100, 300);
34-
35+
3536
$service->create($myUser, $user, $amount);
3637
}
3738

@@ -40,10 +41,10 @@ public function run()
4041
public function createTransactionTypes()
4142
{
4243
factory(\App\TransactionType::class)->create([
43-
'name'=> \App\TransactionType::DEBIT
44-
]);
44+
'name' => \App\TransactionType::DEBIT,
45+
]);
4546
factory(\App\TransactionType::class)->create([
46-
'name'=> \App\TransactionType::CREDIT
47-
]);
47+
'name' => \App\TransactionType::CREDIT,
48+
]);
4849
}
4950
}

public/js/app.js

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48724,26 +48724,34 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
4872448724

4872548725
/* harmony default export */ __webpack_exports__["default"] = ({
4872648726
props: ["fields"],
48727+
data: function data() {
48728+
return {
48729+
data: []
48730+
};
48731+
},
4872748732
created: function created() {
4872848733
this.change = __WEBPACK_IMPORTED_MODULE_0_debounce___default()(this.change, 500);
48734+
48735+
for (var i = 0; i < this.fields.length; i++) {
48736+
this.data.push({
48737+
name: this.fields[i].name,
48738+
value: this.fields[i].value
48739+
});
48740+
}
4872948741
},
4873048742

4873148743
methods: {
48732-
change: function change() {
48733-
var values = [];
48734-
for (var i = 0; i < this.fields.length; i++) {
48735-
var el = document.getElementById(this.fields[i].name);
48736-
48737-
if (el.type == "checkbox") {
48738-
if (el.checked) {
48739-
values.push({ name: el.name, value: 1 });
48740-
}
48741-
} else if (el.value) {
48742-
values.push({ name: el.name, value: el.value });
48743-
}
48744+
change: function change(index, el) {
48745+
48746+
if (el.type == "checkbox") {
48747+
this.data[index].value = el.checked ? 1 : '';
48748+
} else {
48749+
this.data[index].value = el.value;
4874448750
}
4874548751

48746-
this.$emit("changed", values);
48752+
this.$emit("changed", this.data.filter(function (item) {
48753+
return !!item.value;
48754+
}));
4874748755
}
4874848756
}
4874948757
});
@@ -48845,20 +48853,32 @@ var render = function() {
4884548853
field.type == "number"
4884648854
? _c("input", {
4884748855
directives: [{ name: "int", rawName: "v-int" }],
48848-
attrs: { id: field.name, type: "text", name: field.name },
48849-
domProps: { value: field.value },
48850-
on: { input: _vm.change }
48856+
attrs: { type: "text", name: field.name },
48857+
domProps: { value: _vm.data.value },
48858+
on: {
48859+
input: function($event) {
48860+
_vm.change(index, $event.target)
48861+
}
48862+
}
4885148863
})
4885248864
: field.type == "checkbox"
4885348865
? _c("input", {
48854-
attrs: { id: field.name, type: "checkbox", name: field.name },
48855-
domProps: { checked: field.value },
48856-
on: { click: _vm.change }
48866+
attrs: { type: "checkbox", name: field.name },
48867+
domProps: { checked: _vm.data.value },
48868+
on: {
48869+
click: function($event) {
48870+
_vm.change(index, $event.target)
48871+
}
48872+
}
4885748873
})
4885848874
: _c("input", {
48859-
attrs: { id: field.name, type: "text", name: field.name },
48860-
domProps: { value: field.value },
48861-
on: { input: _vm.change }
48875+
attrs: { type: "text", name: field.name },
48876+
domProps: { value: _vm.data.value },
48877+
on: {
48878+
input: function($event) {
48879+
_vm.change(index, $event.target)
48880+
}
48881+
}
4886248882
})
4886348883
])
4886448884
}),

resources/js/components/Filter.vue

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
<div class="filter_table">
33
<div v-for="(field, index) in fields" class="form-group" :key="index">
44
<label v-text="field.label" :for="field.name"></label>
5-
6-
<input v-if="field.type == 'number'" v-int :id="field.name" type="text" :name="field.name" :value="field.value" @input="change">
5+
6+
<input v-if="field.type == 'number'" v-int type="text" :name="field.name" :value="data.value" @input="change(index, $event.target)">
77

8-
<input v-else-if="field.type == 'checkbox'" v-bind:id="field.name" type="checkbox" :name="field.name" :checked="field.value" @click="change">
8+
<input v-else-if="field.type == 'checkbox'" type="checkbox" :name="field.name" :checked="data.value" @click="change(index, $event.target)">
99

10-
<input v-else :id="field.name" type="text" :name="field.name" :value="field.value" @input="change">
11-
10+
<input v-else type="text" :name="field.name" :value="data.value" @input="change(index, $event.target)">
11+
1212
</div>
1313
</div>
1414
</template>
@@ -17,25 +17,31 @@ import debounce from "debounce";
1717
1818
export default {
1919
props: ["fields"],
20+
data() {
21+
return {
22+
data: []
23+
};
24+
},
2025
created() {
2126
this.change = debounce(this.change, 500);
27+
28+
for (let i = 0; i < this.fields.length; i++) {
29+
this.data.push({
30+
name: this.fields[i].name,
31+
value: this.fields[i].value
32+
});
33+
}
2234
},
2335
methods: {
24-
change() {
25-
let values = [];
26-
for (let i = 0; i < this.fields.length; i++) {
27-
let el = document.getElementById(this.fields[i].name);
36+
change(index, el) {
2837
29-
if (el.type == "checkbox") {
30-
if(el.checked){
31-
values.push({ name: el.name, value: 1 });
32-
}
33-
} else if (el.value) {
34-
values.push({ name: el.name, value: el.value });
35-
}
38+
if (el.type == "checkbox") {
39+
this.data[index].value = el.checked ? 1 : '';
40+
} else {
41+
this.data[index].value = el.value;
3642
}
37-
38-
this.$emit("changed", values);
43+
44+
this.$emit("changed", this.data.filter((item) => !!item.value));
3945
}
4046
}
4147
};

0 commit comments

Comments
 (0)