Skip to content

Commit

Permalink
fix: fix add gender type (#4548)
Browse files Browse the repository at this point in the history
* fix default gender type

* add default gender type in validation

* fix validation error in default gender setting update

* set default gender from settings instead of static value

* update assets

* fix method

* fixes

* fix style

* Update CHANGELOG.md

Co-authored-by: Alexis Saettler <alexis@saettler.org>
  • Loading branch information
kavanpancholi and asbiin authored Jan 1, 2021
1 parent ebf7c08 commit c0561ce
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### Fixes:

* Fix adding a gender type
* Fix update activity with emotions
* Fix bad return with wrong credential on oauth/login api

Expand Down
10 changes: 6 additions & 4 deletions app/Http/Controllers/Settings/GendersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use App\Helpers\GenderHelper;
use App\Models\Contact\Gender;
use Illuminate\Validation\Rule;
use App\Helpers\CollectionHelper;
use App\Http\Controllers\Controller;
use App\Traits\JsonRespondController;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function store(Request $request)
{
Validator::make($request->all(), [
'name' => 'required|max:255',
'type' => ['required', Rule::in(Gender::LIST)],
])->validate();

$gender = auth()->user()->account->genders()->create(
Expand Down Expand Up @@ -90,7 +92,7 @@ public function update(GendersRequest $request, Gender $gender)
])
);
if ($request->input('isDefault')) {
$this->updateDefault($request, $gender);
$this->updateDefault($gender);
$gender->refresh();
} elseif ($gender->isDefault()) {
// Case of this gender was the default one previously
Expand All @@ -106,7 +108,7 @@ public function update(GendersRequest $request, Gender $gender)
/**
* Destroy a gender type.
*/
public function destroyAndReplaceGender(GendersRequest $request, Gender $gender, $genderId)
public function destroyAndReplaceGender(Gender $gender, $genderId)
{
$account = auth()->user()->account;
try {
Expand Down Expand Up @@ -134,7 +136,7 @@ public function destroyAndReplaceGender(GendersRequest $request, Gender $gender,
/**
* Destroy a gender type.
*/
public function destroy(GendersRequest $request, Gender $gender)
public function destroy(Gender $gender)
{
$gender->delete();

Expand All @@ -144,7 +146,7 @@ public function destroy(GendersRequest $request, Gender $gender)
/**
* Update the given gender to the default gender.
*/
public function updateDefault(GendersRequest $request, Gender $gender)
public function updateDefault(Gender $gender)
{
$account = auth()->user()->account;
$account->default_gender_id = $gender->id;
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Requests/Settings/GendersRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Requests\Settings;

use App\Models\Contact\Gender;
use Illuminate\Validation\Rule;
use App\Http\Requests\AuthorizedRequest;

class GendersRequest extends AuthorizedRequest
Expand All @@ -14,6 +16,7 @@ class GendersRequest extends AuthorizedRequest
public function rules()
{
return [
'type' => ['required', Rule::in(Gender::LIST)],
'name' => 'max:255',
'id' => 'integer',
];
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Contact/Gender.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Gender extends Model
*/
public const NONE = 'N';

public const LIST = ['M', 'F', 'O', 'U', 'N'];

/**
* Get the account record associated with the gender.
*
Expand Down
2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"/js/manifest.js": "/js/manifest.js?id=7db827d654313dce4250",
"/js/vendor.js": "/js/vendor.js?id=2155659b9e6099142cca",
"/js/app.js": "/js/app.js?id=eaa287c0e6c4e2864f61",
"/js/app.js": "/js/app.js?id=81cf97a3099ead1b523e",
"/css/app-ltr.css": "/css/app-ltr.css?id=2b577e6d6903f823cc6c",
"/css/app-rtl.css": "/css/app-rtl.css?id=a6414e43c80b63fd0a22",
"/css/stripe.css": "/css/stripe.css?id=746c8aaac01c56d3cee1",
Expand Down
13 changes: 11 additions & 2 deletions resources/js/components/settings/Genders.vue
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ export default {
checked: this.$t('app.yes'),
unchecked: this.$t('app.no')
};
},
defaultGenderType() {
const defaultGender = _.findIndex(this.genders, ['isDefault', true]);
return this.genders[defaultGender >= 0 ? defaultGender : 0];
}
},
Expand All @@ -320,10 +325,15 @@ export default {
]);
},
setDefaultGenderType() {
this.createForm.type = this.defaultGenderType.type;
},
getGenders() {
return axios.get('settings/personalization/genders')
.then(response => {
this.genders = _.toArray(response.data);
this.setDefaultGenderType();
});
},
Expand Down Expand Up @@ -355,8 +365,7 @@ export default {
},
showDefaultGenderModal() {
var defaultGender = _.findIndex(this.genders, ['isDefault', true]);
this.defaultGenderId = this.genders[defaultGender >= 0 ? defaultGender : 0].id;
this.defaultGenderId = this.defaultGenderType.id;
this.$refs.defaultGenderModal.open();
},
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Controllers/Settings/GendersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function it_updates_a_gender()

$response = $this->json('PUT', '/settings/personalization/genders/'.$gender->id, [
'name' => 'gender',
'type' => 'U',
]);

$response->assertStatus(200);
Expand Down

0 comments on commit c0561ce

Please sign in to comment.