Skip to content

Commit

Permalink
Merge pull request #51 from leepeuker/improve-settings-page-alerts
Browse files Browse the repository at this point in the history
Add more alerts to settings page
  • Loading branch information
leepeuker authored Jul 14, 2022
2 parents d97419e + 5616fb5 commit dabddf0
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 14 deletions.
23 changes: 17 additions & 6 deletions src/HttpController/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use Movary\ValueObject\Http\Request;
use Movary\ValueObject\Http\Response;
use Movary\ValueObject\Http\StatusCode;
use Psr\Log\LoggerInterface;

class ImportController
{
public function __construct(
private readonly Authentication $authenticationService,
private readonly ImportService $importService
private readonly ImportService $importService,
private readonly LoggerInterface $logger,
) {
}

Expand All @@ -27,11 +29,16 @@ public function handleCsvImport(Request $request) : Response
$exportType = $request->getRouteParameters()['exportType'];
$fileParameters = $request->getFileParameters();

match ($exportType) {
'history' => $this->importHistory($userId, $fileParameters),
'ratings' => $this->importRatings($userId, $fileParameters),
default => throw new \RuntimeException('Export type not handled: ' . $exportType)
};
try {
match ($exportType) {
'history' => $this->importHistory($userId, $fileParameters),
'ratings' => $this->importRatings($userId, $fileParameters),
default => throw new \RuntimeException('Export type not handled: ' . $exportType)
};
} catch (\Throwable $t) {
$this->logger->error('Could not import: ' . $exportType, ['exception' => $t]);
$_SESSION['importHistoryError'] = $exportType;
}

return Response::create(
StatusCode::createSeeOther(),
Expand All @@ -47,6 +54,8 @@ private function importHistory(int $userId, array $fileParameter) : void
}

$this->importService->importHistory($userId, $fileParameter['history']['tmp_name']);

$_SESSION['importHistorySuccessful'] = true;
}

private function importRatings(int $userId, array $fileParameter) : void
Expand All @@ -56,5 +65,7 @@ private function importRatings(int $userId, array $fileParameter) : void
}

$this->importService->importRatings($userId, $fileParameter['ratings']['tmp_name']);

$_SESSION['importRatingsSuccessful'] = true;
}
}
51 changes: 50 additions & 1 deletion src/HttpController/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
) {
}

// phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh
public function render() : Response
{
if ($this->authenticationService->isUserAuthenticated() === false) {
Expand All @@ -34,7 +35,20 @@ public function render() : Response
$passwordErrorMinLength = empty($_SESSION['passwordErrorMinLength']) === false ? $_SESSION['passwordErrorMinLength'] : null;
$passwordErrorCurrentInvalid = empty($_SESSION['passwordErrorCurrentInvalid']) === false ? $_SESSION['passwordErrorCurrentInvalid'] : null;
$passwordUpdated = empty($_SESSION['passwordUpdated']) === false ? $_SESSION['passwordUpdated'] : null;
unset($_SESSION['passwordUpdated'], $_SESSION['passwordErrorCurrentInvalid'], $_SESSION['passwordErrorMinLength'], $_SESSION['passwordErrorNotEqual']);
$traktCredentialsUpdated = empty($_SESSION['traktCredentialsUpdated']) === false ? $_SESSION['traktCredentialsUpdated'] : null;
$importHistorySuccessful = empty($_SESSION['importHistorySuccessful']) === false ? $_SESSION['importHistorySuccessful'] : null;
$importRatingsSuccessful = empty($_SESSION['importRatingsSuccessful']) === false ? $_SESSION['importRatingsSuccessful'] : null;
$importHistoryError = empty($_SESSION['importHistoryError']) === false ? $_SESSION['importHistoryError'] : null;
unset(
$_SESSION['passwordUpdated'],
$_SESSION['passwordErrorCurrentInvalid'],
$_SESSION['passwordErrorMinLength'],
$_SESSION['passwordErrorNotEqual'],
$_SESSION['traktCredentialsUpdated'],
$_SESSION['importHistorySuccessful'],
$_SESSION['importRatingsSuccessful'],
$_SESSION['importHistoryError'],
);

return Response::create(
StatusCode::createOk(),
Expand All @@ -43,7 +57,11 @@ public function render() : Response
'passwordErrorNotEqual' => $passwordErrorNotEqual,
'passwordErrorMinLength' => $passwordErrorMinLength,
'passwordErrorCurrentInvalid' => $passwordErrorCurrentInvalid,
'traktCredentialsUpdated' => $traktCredentialsUpdated,
'importHistorySuccessful' => $importHistorySuccessful,
'importRatingsSuccessful' => $importRatingsSuccessful,
'passwordUpdated' => $passwordUpdated,
'importHistoryError' => $importHistoryError,
'traktClientId' => $this->userApi->findTraktClientId($userId),
'traktUserName' => $this->userApi->findTraktUserName($userId),
'applicationVersion' => $this->applicationVersion ?? '-',
Expand Down Expand Up @@ -104,4 +122,35 @@ public function updatePassword(Request $request) : Response
[Header::createLocation($_SERVER['HTTP_REFERER'])]
);
}

public function updateTrakt(Request $request) : Response
{
if ($this->authenticationService->isUserAuthenticated() === false) {
return Response::createFoundRedirect('/');
}

$userId = $this->authenticationService->getCurrentUserId();
$postParameters = $request->getPostParameters();

$traktClientId = $postParameters['traktClientId'];
if (empty($traktClientId) === true) {
$traktClientId = null;
}

$traktUserName = $postParameters['traktUserName'];
if (empty($traktUserName) === true) {
$traktUserName = null;
}

$this->userApi->updateTraktClientId($userId, $traktClientId);
$this->userApi->updateTraktUserName($userId, $traktUserName);

$_SESSION['traktCredentialsUpdated'] = true;

return Response::create(
StatusCode::createSeeOther(),
null,
[Header::createLocation($_SERVER['HTTP_REFERER'])]
);
}
}
51 changes: 44 additions & 7 deletions templates/page/settings.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,26 @@

{% if passwordErrorNotEqual == true %}
<div class="alert alert-danger alert-dismissible" role="alert" style="margin-left: 10%;margin-right: 10%;margin-bottom: 0.7rem!important;">
Passwords were not equal. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
Passwords were not equal.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}
{% if passwordErrorMinLength == true %}
<div class="alert alert-danger alert-dismissible" role="alert" style="margin-left: 10%;margin-right: 10%;margin-bottom: 0.7rem!important;">
Password must be at least {{ passwordErrorMinLength }} characters long. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
Password must be at least {{ passwordErrorMinLength }} characters long.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}
{% if passwordErrorCurrentInvalid == true %}
<div class="alert alert-danger alert-dismissible" role="alert" style="margin-left: 10%;margin-right: 10%;margin-bottom: 0.7rem!important;">
Current password is not correct. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
Current password is not correct.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}
{% if passwordUpdated == true %}
<div class="alert alert-success alert-dismissible" role="alert" style="margin-left: 10%;margin-right: 10%;margin-bottom: 0.7rem!important;">
Password was updated. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
Password was updated.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}
<button class="btn btn-primary btn-sm" type="submit">Submit</button>
Expand All @@ -65,7 +69,7 @@
<div style="padding-top: 1rem;padding-bottom: 1rem">
<h5 style="padding-bottom: 0.5rem">trakt.tv</h5>

<p>To generate a client id visit this url <a href="https://trakt.tv/oauth/applications">here</a>.</p>
<p>To get your username and generate a client id visit <a href="https://trakt.tv/oauth/applications" target="_blank">this url</a>.</p>

<form action="/user/trakt" method="post">
<p style="font-size: 0.8rem;margin-bottom: 0.5rem">Username:</p>
Expand All @@ -79,6 +83,14 @@
<input type="text" class="form-control" name="traktClientId" placeholder="Enter client ID here" value="{{ traktClientId }}"
style="margin-left: 10%;margin-right: 10%;text-align: center;">
</div>

{% if traktCredentialsUpdated == true %}
<div class="alert alert-success alert-dismissible" role="alert" style="margin-left: 10%;margin-right: 10%;margin-bottom: 0.7rem!important;">
Trakt credentials were updated.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}

<button class="btn btn-primary btn-sm" type="submit">Submit</button>
</form>
</div>
Expand Down Expand Up @@ -109,14 +121,39 @@
</div>
</form>

<br>
{% if importHistorySuccessful == true %}
<div class="alert alert-success alert-dismissible" role="alert" style="margin-left: 10%;margin-right: 10%;margin-bottom: 0.7rem!important;margin-top: 1rem">
History was successfully imported.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}

<form action="/user/import/csv/ratings" method="post" enctype="multipart/form-data" style="padding-bottom: 0.2rem">
{% if importHistoryError == 'history' %}
<div class="alert alert-danger alert-dismissible" role="alert" style="margin-left: 10%;margin-right: 10%;margin-bottom: 0.7rem!important;margin-top: 1rem">
History could not be imported.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}

<form action="/user/import/csv/ratings" method="post" enctype="multipart/form-data" style="padding-bottom: 0.2rem;margin-top: 1rem">
<div class="input-group input-group-sm">
<input type="file" class="form-control" name="ratings" required>
<button class="btn btn-primary" type="submit">Import rating.csv</button>
</div>
</form>
{% if importRatingsSuccessful == true %}
<div class="alert alert-success alert-dismissible" role="alert" style="margin-left: 10%;margin-right: 10%;margin-bottom: 0.7rem!important;margin-top: 1rem">
Ratings were successfully imported.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}

{% if importHistoryError == 'ratings' %}
<div class="alert alert-danger alert-dismissible" role="alert" style="margin-left: 10%;margin-right: 10%;margin-bottom: 0.7rem!important;margin-top: 1rem">
Ratings could not be imported.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}
</div>

<hr style="margin: 0;padding: 0">
Expand Down

0 comments on commit dabddf0

Please sign in to comment.