Skip to content

Commit

Permalink
feat: AU-1771: Profile forms - Helsinki Profiili data update.
Browse files Browse the repository at this point in the history
* AU-1771: Unregistered HP profile update callbacks

* AU-1771: Private person email update, change success log position

* AU-1771: Margin to refresh button. Remove duplicate button in private person. Move private person status messages to top

* AU-1771: PHPCS fix

* AU-1771: Lint fixes. Fix bank accounts generating new ids every request
  • Loading branch information
oskarimysteerio authored Nov 16, 2023
1 parent 5221feb commit 231f97d
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 30 deletions.
3 changes: 3 additions & 0 deletions public/modules/custom/grants_profile/grants_profile.module
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function grants_profile_theme(): array {
'variables' => [
'myProfile' => NULL,
'editHelsinkiProfileLink' => NULL,
'updatelink' => NULL,
],
];

Expand Down Expand Up @@ -253,6 +254,7 @@ function grants_profile_preprocess_own_profile_form_unregistered_community(&$var
$variables['profile'] = $variables["form"]["#profilecontent"];

$variables['basic_info'] = $variables["form"]["#basic_info"];
$variables['basic_info']['#updatelink'] = $variables["form"]["updatelink"];
$variables["form"]['#attached']['library'][] = 'grants_profile/profile_dialog';

// Unset just in case.
Expand All @@ -275,6 +277,7 @@ function grants_profile_preprocess_own_profile_form_private_person(&$variables)
unset($variables["form"]["#helsinkiprofilecontent"]);

$variables['basic_info'] = $variables["form"]["#basic_info"];
$variables['basic_info']['#updatelink'] = $variables["form"]["updatelink"];

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
Expand Down Expand Up @@ -506,10 +508,7 @@ protected function addBankAccountBits(
}

// Make sure we have proper UUID as address id.
if (!isset($bankAccount['bank_account_id']) ||
!$this->grantsProfileService->isValidUuid($bankAccount['bank_account_id'])) {
$bankAccount['bank_account_id'] = Uuid::uuid4()->toString();
}
$this->ensureBankAccountIdExists($bankAccount);
$nonEditable = FALSE;
foreach ($bankAccounts as $profileAccount) {
if (!self::accountsAreEqual($bankAccount['bankAccount'], $profileAccount['bankAccount'])) {
Expand Down Expand Up @@ -539,6 +538,8 @@ protected function addBankAccountBits(
$strings,
$nonEditable,
$bankAccount['bankAccount'],
FALSE,
$bankAccount['bank_account_id'] ?? '',
);
}

Expand All @@ -558,6 +559,7 @@ protected function addBankAccountBits(
FALSE,
'',
TRUE,
$bankAccount['bank_account_id'] ?? '',
);
$formState->setValue('newItem', NULL);
}
Expand All @@ -581,6 +583,21 @@ protected function addBankAccountBits(
];
}

/**
* Validates that the bank account has an ID.
*
* @param array $bankAccount
* Bank account data array.
*/
private function ensureBankAccountIdExists(array &$bankAccount) {
// Make sure we have proper UUID as address id.
if (!isset($bankAccount['bank_account_id']) ||
!$this->grantsProfileService->isValidUuid($bankAccount['bank_account_id'])
) {
$bankAccount['bank_account_id'] = Uuid::uuid4()->toString();
}
}

/**
* Builder function for bank account arrays for profile form.
*
Expand All @@ -600,6 +617,8 @@ protected function addBankAccountBits(
* Bank account number.
* @param bool $newDelta
* If this is a new Bank Array or old one.
* @param string $bankAccountId
* Bank account id, if it exists already.
*
* @return array
* Bank account element in array form.
Expand All @@ -612,7 +631,8 @@ private function buildBankArray(
array|null $strings = [],
bool $nonEditable = FALSE,
string|null $bankAccount = NULL,
bool $newDelta = FALSE
bool $newDelta = FALSE,
string $bankAccountId = '',
): array {
$ownerValues = FALSE;
if (!empty($helsinkiProfileContent)) {
Expand Down Expand Up @@ -695,6 +715,7 @@ private function buildBankArray(
];
$fields['bank_account_id'] = [
'#type' => 'hidden',
'#value' => $bankAccountId,
];
$fields['deleteButton'] = [
'#icon_left' => 'trash',
Expand Down Expand Up @@ -824,6 +845,18 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
'#value' => NULL,
];

$form['updatelink']['link'] = [
'#type' => 'submit',
'#value' => $this->t('Get updated information', [], $this->tOpts),
'#name' => 'refresh_profile',
'#submit' => [[$this, 'profileDataRefreshSubmitHandler']],
'#ajax' => [
'callback' => [$this, 'profileDataRefreshAjaxCallback'],
'wrapper' => 'form',
],
'#limit_validation_errors' => [],
];

$form['#tree'] = TRUE;

$form['actions']['submit']['#submit'][] = 'Drupal\grants_profile\Form\GrantsProfileFormBase::removeAttachments';
Expand Down Expand Up @@ -1141,4 +1174,13 @@ protected function handleViolations(

}

/**
* Profile data refresh ajax callback.
*/
public function profileDataRefreshAjaxCallback(array $form) {
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('form', $form));
return $response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,48 @@ public function submitForm(array &$form, FormStateInterface $formState) {
$formState->setRedirect('grants_profile.show');
}

/**
* Profile data refresh submit handler.
*/
public function profileDataRefreshSubmitHandler(array $form, FormStateInterface $form_state) {
$storage = $form_state->getStorage();
$document = $storage['profileDocument'];
$originalData = $document->getContent();

try {
$freshData = $this->helsinkiProfiiliUserData->getUserProfileData(TRUE);

$possibleChanges = [];

// Email seems to be the only thing user cannot change by themself.
if (isset($freshData['myProfile']['primaryEmail'])) {
$possibleChanges['email'] = $freshData['myProfile']['primaryEmail']['email'];
}

$oldData = array_intersect_key($originalData, $possibleChanges);
$diff = array_diff($possibleChanges, $oldData);
if (!empty($diff)) {
$content = array_merge($originalData, $diff);
$this->grantsProfileService->saveGrantsProfile($content);
}

$this->messenger()->addStatus(
$this->t('Data from Helsinki Profile successfully updated.', [], $this->tOpts)
);
}
catch (\Exception $e) {
$this->logger('grants_profile')
->error(
'Grants profile Helsinki Profile (unregistered) update failed. Error: @error',
['@error' => $e->getMessage()]
);
$this->messenger()->addError(
$this->t('Updating Helsinki Profile data failed.', [], $this->tOpts)
);
}

$form_state->setRebuild();
return $form;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Drupal\grants_profile\Form;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\TypedData\TypedDataManager;
Expand Down Expand Up @@ -206,18 +204,6 @@ public function buildForm(array $form, FormStateInterface $form_state): array {

$form['#profilecontent'] = $grantsProfileContent;

$form['updatelink']['link'] = [
'#type' => 'submit',
'#value' => $this->t('Get updated information', [], $this->tOpts),
'#name' => 'refresh_profile',
'#submit' => [[$this, 'profileDataRefreshSubmitHandler']],
'#ajax' => [
'callback' => [$this, 'profileDataRefreshAjaxCallback'],
'wrapper' => 'form',
],
'#limit_validation_errors' => [],
];

$form_state->setStorage($storage);

$form['actions']['submit_cancel']["#submit"] = [
Expand All @@ -227,15 +213,6 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
return $form;
}

/**
* Profile data refresh ajax callback.
*/
public function profileDataRefreshAjaxCallback(array $form) {
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('form', $form));
return $response;
}

/**
* Profile data refresh submit handler.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,36 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
return $form;
}

/**
* Profile data refresh submit handler.
*/
public function profileDataRefreshSubmitHandler(array $form, FormStateInterface $form_state) {
// Unregistered grants profile doesn't use
// HP data directly, only for the initial address &
// person details, but let's update the basic info
// data, so users might get less confused.
try {
$this->helsinkiProfiiliUserData->getUserProfileData(TRUE);

$this->messenger()->addStatus(
$this->t('Data from Helsinki Profile successfully updated.', [], $this->tOpts)
);
}
catch (\Exception $e) {
$this->logger('grants_profile')
->error(
'Grants profile Helsinki Profile (unregistered) update failed. Error: @error',
['@error' => $e->getMessage()]
);
$this->messenger()->addError(
$this->t('Updating Helsinki Profile data failed.', [], $this->tOpts)
);
}

$form_state->setRebuild();
return $form;
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@
{% if editHelsinkiProfileLink %}
{{ editHelsinkiProfileLink }} ({{ "The link opens in a new tab"|t({}, {'context': 'grants_profile'}) }})
{% endif %}
<dl>
<div>
{{ updatelink }}
</div>
</dl>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<div class="container">
{{ form.status_messages}}
<h2>{{ "Personal information"|t({}, {'context': 'Yksityishenkilön asiointi'}) }}</h2>
<p class="grants-profile--infotext">{{ "By completing your own information, you will facilitate your dealings with grants. Based on the information, we can find grants that are more suitable for you or your community. In addition, filling out applications will be faster in the future."|t({}, {'context': 'grants_profile'}) }}</p>
<div class="grants-profile">
{{ basic_info }}
<h3 class="info-grants">{{ "Own contact information"|t({}, {'context': 'Yksityishenkilön asiointi'}) }}</h3>
<div class="grants-profile--extrainfo">
{{ form }}
{{ form|without('updatelink', 'status_messages') }}
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

<div class="container">
{{ form.status_messages}}
<h2>{{ "Community or group information"|t({}, {'context': 'grants_profile'}) }}</h2>
<p class="grants-profile--infotext">{{ "To apply for grants, the community or group data must be completed. When you submit a grant application, the information you have provided will be filled in automatically."|t({}, {'context': 'grants_profile'}) }}</p>
<p class="grants-profile--infotext">{{ "When you form a community or group, you will be responsible for the possible grant awarded to the community or group, and reporting on the use of the grant. If the grant is awarded, it will be paid to your account. A group can be youth activity group or an artistic working group, for example."|t({}, {'context': 'grants_profile'}) }}</p>
Expand All @@ -8,7 +9,7 @@
{{ basic_info }}
<h3 class="info-grants">{{ "Add community or group information"|t({}, {'context': 'grants_profile'}) }}</h3>
<div class="grants-profile--extrainfo">
{{ form }}
{{ form|without('updatelink', 'status_messages') }}
</div>
</div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions public/modules/custom/grants_profile/translations/fi.po
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,11 @@ msgstr "Tietojen hakeminen PRH:stä epäonnistui."
msgctxt "grants_profile"
msgid "Get updated information"
msgstr "Hae päivittyneet tiedot"

msgctxt "grants_profile"
msgid "Data from Helsinki Profile successfully updated."
msgstr "Tiedot päivitetty Helsinki Profiilista onnistuneesti."

msgctxt "grants_profile"
msgid "Updating Helsinki Profile data failed."
msgstr "Tietojen hakeminen Helsinki Profiilista epäonnistui."
8 changes: 8 additions & 0 deletions public/modules/custom/grants_profile/translations/sv.po
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,11 @@ msgstr "Det gick inte att uppdatera PRH-data."
msgctxt "grants_profile"
msgid "Get updated information"
msgstr "Få uppdaterad information"

msgctxt "grants_profile"
msgid "Data from Helsinki Profile successfully updated."
msgstr "Data från Helsingforsprofilen har uppdaterats."

msgctxt "grants_profile"
msgid "Updating PRH data failed."
msgstr "Det gick inte att uppdatera Helsingforsprofilen data."

0 comments on commit 231f97d

Please sign in to comment.