Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add campaign details page header with tabs, save buttons, and form with sample input #7536

Merged
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
50a14c4
feature: add initial header with tabs
glaubersilva Sep 12, 2024
01da9c0
Merge branch 'epic/campaigns' into feature/campaigns-details-header-G…
glaubersilva Sep 12, 2024
436669c
refactor: change tab options logic
glaubersilva Sep 12, 2024
01ca995
fix: wp-link-wrap z-index
glaubersilva Sep 13, 2024
0ea7e12
feature: add routing and navigation logic for tabs
glaubersilva Sep 13, 2024
e6aa6af
refactor: extract campaign tabs array to a separate file
glaubersilva Sep 13, 2024
f3938d0
refactor: rename type and const
glaubersilva Sep 13, 2024
d3ef5fd
refactor: change tabs and button styles
glaubersilva Sep 13, 2024
5bf8081
feature: add routes for the update button
glaubersilva Sep 13, 2024
54023a1
feature: add "save as draft" button
glaubersilva Sep 13, 2024
fd23d20
feature: add form provider
glaubersilva Sep 16, 2024
27a00de
feature: add status classes
glaubersilva Sep 16, 2024
3de7904
refactor: move status classes
glaubersilva Sep 16, 2024
70e8d3b
refactor: use react-aria-components for tabs
glaubersilva Sep 19, 2024
d458f98
refactor: change handleTabNavigation parameter
glaubersilva Sep 19, 2024
68a9853
refactor: simplify handleTabNavigation logic
glaubersilva Sep 19, 2024
c4f87ac
refactor: css tweaks
glaubersilva Sep 19, 2024
d362f6f
refactor: simplify UpdateCampaign logic
glaubersilva Sep 20, 2024
7f6062c
refactor: simplify PublishCampaign logic
glaubersilva Sep 20, 2024
d465a37
refactor: change status and methods
glaubersilva Sep 20, 2024
7f64906
refactor: remove RestResponses trait
glaubersilva Sep 20, 2024
ea7a3d1
refactor: remove trait import
glaubersilva Sep 20, 2024
849de72
refactor: change endpoint
glaubersilva Sep 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Campaigns/Actions/LoadCampaignDetailsAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Give\Campaigns\Models\Campaign;
use Give\Campaigns\ViewModels\CampaignDetailsPage;
use Give\Framework\Support\Facades\Scripts\ScriptAsset;

/**
* @unreleased
Expand All @@ -16,12 +17,13 @@ class LoadCampaignDetailsAssets
public function __invoke(Campaign $campaign)
{
$handleName = 'givewp-admin-campaign-details';
$asset = ScriptAsset::get(GIVE_PLUGIN_DIR . 'assets/dist/js/give-admin-campaign-details.asset.php');

wp_register_script(
$handleName,
GIVE_PLUGIN_URL . 'assets/dist/js/give-admin-campaign-details.js',
[],
GIVE_VERSION,
$asset['dependencies'],
$asset['version'],
true
);

Expand All @@ -31,7 +33,7 @@ public function __invoke(Campaign $campaign)
'apiNonce' => wp_create_nonce('wp_rest'),
'adminUrl' => admin_url(),
'pluginUrl' => GIVE_PLUGIN_URL,
'campaignDetailsPage' => (new CampaignDetailsPage($campaign))->exports(),
'campaign' => (new CampaignDetailsPage($campaign))->exports(),
]
);

Expand Down
73 changes: 73 additions & 0 deletions src/Campaigns/Routes/PublishCampaign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Give\Campaigns\Routes;

use Give\API\RestRoute;
use Give\Campaigns\Models\Campaign;
use Give\Campaigns\Routes\Traits\RestResponses;
use Give\Campaigns\ValueObjects\CampaignStatus;
use Give\Framework\Exceptions\Primitives\Exception;
use WP_REST_Request;
use WP_REST_Response;

/**
* @unreleased
*/
class PublishCampaign implements RestRoute
{
use RestResponses;

/** @var string */
protected $endpoint = 'campaigns/publish/(?P<id>[0-9]+)';
glaubersilva marked this conversation as resolved.
Show resolved Hide resolved

/**
* @unreleased
*/
public function registerRoute()
{
register_rest_route(
'give-api/v2',
$this->endpoint,
[
[
'methods' => 'PUT',
'callback' => [$this, 'handleRequest'],
'permission_callback' => function () {
return current_user_can('manage_options');
},
],
'args' => [
'id' => [
'type' => 'string',
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
],
],
]
);
}

/**
* @unreleased
*/
public function handleRequest(WP_REST_Request $request): WP_Rest_Response
{
$campaignId = $request->get_param('id');
$campaign = Campaign::find($campaignId);

if ( ! $campaign) {
glaubersilva marked this conversation as resolved.
Show resolved Hide resolved
return $this->notFoundResponse(esc_html__(sprintf('Campaign %s not found.', $campaignId), 'give'));
}

try {
$campaign->status = CampaignStatus::ACTIVE();
$campaign->save();

$response = json_encode($campaign->toArray());
} catch (Exception $e) {
return $this->badRequestResponse($e);
glaubersilva marked this conversation as resolved.
Show resolved Hide resolved
}

return new WP_REST_Response($response);
}
}
51 changes: 51 additions & 0 deletions src/Campaigns/Routes/Traits/RestResponses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Give\Campaigns\Routes\Traits;

use Give\Framework\Exceptions\Primitives\Exception;
use WP_REST_Response;

/**
* @unreleased
*/
trait RestResponses
{
/**
* @unreleased
*/
public function notFoundResponse($message, $response = 'rest_not_found'): WP_REST_Response
{
return new WP_REST_Response(
[
'status' => 404,
'response' => $response,
'body_response' => [
'message' => html_entity_decode(
$message
),
],
]
);
}

/**
* @unreleased
*/
public function badRequestResponse(Exception $e, $response = 'rest_bad_request'): WP_REST_Response
{
return new WP_REST_Response(
[
'status' => 400,
'response' => $response,
'body_response' => [
'message' => html_entity_decode(
sprintf(
esc_html__('%s. Search the logs at Donations > Tools > Logs for a more specific cause of the problem.',
glaubersilva marked this conversation as resolved.
Show resolved Hide resolved
'give'), rtrim($e->getMessage(), '.')
)
),
],
]
);
}
}
76 changes: 76 additions & 0 deletions src/Campaigns/Routes/UpdateCampaign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Give\Campaigns\Routes;

use Give\API\RestRoute;
use Give\Campaigns\Models\Campaign;
use Give\Campaigns\Routes\Traits\RestResponses;
use Give\Framework\Exceptions\Primitives\Exception;
use WP_REST_Request;
use WP_REST_Response;

/**
* @unreleased
*/
class UpdateCampaign implements RestRoute
{
use RestResponses;

/** @var string */
protected $endpoint = 'campaigns/(?P<id>[0-9]+)';

public function registerRoute()
{
register_rest_route(
'give-api/v2',
$this->endpoint,
[
[
'methods' => 'PUT',
'callback' => [$this, 'handleRequest'],
'permission_callback' => function () {
return current_user_can('manage_options');
},
],
'args' => [
'id' => [
'type' => 'string',
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
],
'title' => [
'type' => 'string',
'required' => false,
'sanitize_callback' => 'sanitize_text_field',
],
],
]
);
}

/**
* @unreleased
*/
public function handleRequest(WP_REST_Request $request): WP_REST_Response
{
$campaignId = $request->get_param('id');
$campaign = Campaign::find($campaignId);

if ( ! $campaign) {
return $this->notFoundResponse(esc_html__(sprintf('Campaign %s not found.', $campaignId), 'give'));
}

try {
$title = $request->get_param('title');
$campaign->title = $title ? $request->get_param('title') : $campaign->title;
glaubersilva marked this conversation as resolved.
Show resolved Hide resolved

$campaign->save();

$response = json_encode($campaign->toArray());
} catch (Exception $e) {
return $this->badRequestResponse($e);
}

return new WP_REST_Response($response);
}
}
2 changes: 2 additions & 0 deletions src/Campaigns/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function boot(): void
private function registerRoutes()
{
Hooks::addAction('rest_api_init', Routes\CreateCampaign::class, 'registerRoute');
Hooks::addAction('rest_api_init', Routes\PublishCampaign::class, 'registerRoute');
Hooks::addAction('rest_api_init', Routes\UpdateCampaign::class, 'registerRoute');
Hooks::addAction('rest_api_init', Routes\GetCampaignsListTable::class, 'registerRoute');
Hooks::addAction('rest_api_init', Routes\DeleteCampaignListTable::class, 'registerRoute');
}
Expand Down
6 changes: 2 additions & 4 deletions src/Campaigns/ViewModels/CampaignDetailsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ public function __construct(Campaign $campaign)
public function exports(): array
{
return [
'overviewTab' => $this->campaign->toArray(),
'settingsTab' => [
'properties' => $this->campaign->toArray(),
'settings' => [
'landingPageUrl' => admin_url('?action=edit_campaign_page&campaign_id=' . $this->campaign->id),
],
'reportTab' => [],
'updatesTab' => [],
];
}
}
Loading
Loading