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 #7531

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 41 additions & 0 deletions src/Campaigns/Actions/LoadCampaignDetailsAssets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Give\Campaigns\Actions;

use Give\Campaigns\Models\Campaign;
use Give\Campaigns\ViewModels\CampaignDetailsPage;

/**
* @unreleased
*/
class LoadCampaignDetailsAssets
{
/**
* @unreleased
*/
public function __invoke(Campaign $campaign)
{
$handleName = 'givewp-admin-campaign-details';

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

wp_localize_script($handleName, 'GiveCampaignDetails',
[
'apiRoot' => esc_url_raw(rest_url('give-api/v2/campaigns')),
'apiNonce' => wp_create_nonce('wp_rest'),
'adminUrl' => admin_url(),
'pluginUrl' => GIVE_PLUGIN_URL,
'campaignDetailsPage' => (new CampaignDetailsPage($campaign))->exports(),
]
);

wp_enqueue_script($handleName);
wp_enqueue_style('givewp-design-system-foundation');
}
}
14 changes: 13 additions & 1 deletion src/Campaigns/CampaignsAdminPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Give\Campaigns;

use Give\Campaigns\Actions\LoadCampaignDetailsAssets;
use Give\Campaigns\Actions\LoadCampaignsListTableAssets;
use Give\Campaigns\Models\Campaign;

/**
* @unreleased
Expand Down Expand Up @@ -30,7 +32,17 @@ public function addCampaignsSubmenuPage()
*/
public function renderCampaignsPage()
{
give(LoadCampaignsListTableAssets::class)();
if (isset($_GET['id'])) {
$campaign = Campaign::find(absint($_GET['id']));

if ( ! $campaign) {
wp_die(__('Campaign not found', 'give'), 404);
}

give(LoadCampaignDetailsAssets::class)($campaign);
} else {
give(LoadCampaignsListTableAssets::class)();
}

echo '<div id="give-admin-campaigns-root"></div>';
}
Expand Down
1 change: 0 additions & 1 deletion src/Campaigns/Routes/CreateCampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public function registerRoute()
public function handleRequest(WP_REST_Request $request): WP_REST_Response
{
$campaign = Campaign::create([
'pageId' => 0,
'type' => CampaignType::CORE(),
'title' => $request->get_param('title'),
'shortDescription' => $request->get_param('shortDescription'),
Expand Down
41 changes: 41 additions & 0 deletions src/Campaigns/ViewModels/CampaignDetailsPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Give\Campaigns\ViewModels;

use Give\Campaigns\Models\Campaign;

/**
* @unreleased
*/
class CampaignDetailsPage
{
/**
* @unreleased
*
* @var Campaign
*/
protected $campaign;

/**
* @unreleased
*/
public function __construct(Campaign $campaign)
{
$this->campaign = $campaign;
}

/**
* @unreleased
*/
public function exports(): array
{
return [
'overviewTab' => $this->campaign->toArray(),
'settingsTab' => [
'landingPageUrl' => admin_url('?action=edit_campaign_page&campaign_id=' . $this->campaign->id),
],
'reportTab' => [],
'updatesTab' => [],
];
}
}
6 changes: 6 additions & 0 deletions src/Campaigns/resources/admin/campaign-details.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {createRoot} from 'react-dom/client';
import CampaignsDetailsPage from './components/CampaignDetailsPage';

const container = document.getElementById('give-admin-campaigns-root');
const root = createRoot(container!);
root.render(<CampaignsDetailsPage />);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.container {
margin-top: 3rem;
padding: 3rem;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {GiveCampaignDetails} from './types';
import styles from './CampaignDetailsPage.module.scss';

declare const window: {
GiveCampaignDetails: GiveCampaignDetails;
} & Window;

export function getGiveCampaignDetailsWindowData() {
return window.GiveCampaignDetails;
}

const {campaignDetailsPage} = getGiveCampaignDetailsWindowData();

console.log(Object.values(campaignDetailsPage.overviewTab));

export default function CampaignsDetailsPage() {
return (
<div className={styles.container}>
<h1>
<strong>Campaign details goes here...</strong>
</h1>
<p>Just below you can see a few data from the details page separated by tabs.</p>
<br />
<h2>
<strong>Overview Tab</strong>
</h2>
<ul>
{Object.entries(campaignDetailsPage.overviewTab).map(([property, value], index) => (
<li key={index}>
<span>
<strong>{property}:</strong> {String(value)}
</span>
</li>
))}
</ul>
<br />
<h2>
<strong>Settings Tab</strong>
</h2>
<p>
<a
style={{fontSize: '1.5rem'}}
href={campaignDetailsPage.settingsTab.landingPageUrl}
target="_blank"
rel="noopener noreferrer"
>
Edit Campaign Landing Page ⭷
</a>
</p>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface GiveCampaignDetails {
apiRoot: string;
apiNonce: string;
adminUrl: string;
pluginUrl: string;
campaignDetailsPage: {
overviewTab: any;
settingsTab: {
landingPageUrl: string;
};
};
}
1 change: 1 addition & 0 deletions webpack.mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ mix.setPublicPath('assets/dist')
.ts('src/Promotions/InPluginUpsells/resources/js/payment-gateway.ts', 'js/payment-gateway.js')
.ts('src/Promotions/WelcomeBanner/resources/js/index.tsx', 'js/welcome-banner.js')
.ts('src/Campaigns/resources/admin/campaigns-list-table.tsx', 'js/give-admin-campaigns-list-table.js')
.ts('src/Campaigns/resources/admin/campaign-details.tsx', 'js/give-admin-campaign-details.js')

.react()
.sourceMaps(false, 'source-map')
Expand Down
Loading