Skip to content

Commit

Permalink
Refactor: Move fields blocks to form meta (impress-org#110)
Browse files Browse the repository at this point in the history
* refactor: Move fields blocks to form meta
  • Loading branch information
kjohnson authored Jan 11, 2023
1 parent 007fda2 commit 5170151
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __invoke($queryObject): DonationForm
'updatedAt' => Temporal::toDateTime($queryObject->updatedAt),
'status' => new DonationFormStatus($queryObject->status),
'settings' => FormSettings::fromjson($queryObject->{DonationFormMetaKeys::SETTINGS()->getKeyAsCamelCase()}),
'blocks' => BlockCollection::fromJson($queryObject->blocks)
'blocks' => BlockCollection::fromJson($queryObject->{DonationFormMetaKeys::FIELDS()->getKeyAsCamelCase()}),
]);
}
}
21 changes: 18 additions & 3 deletions src/NextGen/DonationForm/Repositories/DonationFormRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function insert(DonationForm $donationForm)
'post_type' => 'give_forms',
'post_parent' => 0,
'post_title' => $donationForm->title,
'post_content' => $donationForm->blocks->toJson(),
'post_content' => (new BlockCollection([]))->toJson(), // @todo Repurpose as form page.
]);

$donationFormId = DB::last_insert_id();
Expand All @@ -105,6 +105,13 @@ public function insert(DonationForm $donationForm)
'meta_key' => DonationFormMetaKeys::SETTINGS()->getValue(),
'meta_value' => $donationForm->settings->toJson()
]);

DB::table('give_formmeta')
->insert([
'form_id' => $donationFormId,
'meta_key' => DonationFormMetaKeys::FIELDS()->getValue(),
'meta_value' => $donationForm->blocks->toJson()
]);
} catch (Exception $exception) {
DB::query('ROLLBACK');

Expand Down Expand Up @@ -152,7 +159,7 @@ public function update(DonationForm $donationForm)
'post_modified_gmt' => get_gmt_from_date($date),
'post_status' => $donationForm->status->getValue(),
'post_title' => $donationForm->title,
'post_content' => $donationForm->blocks->toJson(),
'post_content' => (new BlockCollection([]))->toJson(), // @todo Repurpose as form page.
]);

DB::table('give_formmeta')
Expand All @@ -161,6 +168,14 @@ public function update(DonationForm $donationForm)
->update([
'meta_value' => $donationForm->settings->toJson()
]);


DB::table('give_formmeta')
->where('form_id', $donationForm->id)
->where('meta_key', DonationFormMetaKeys::FIELDS()->getValue())
->update([
'meta_value' => $donationForm->blocks->toJson()
]);
} catch (Exception $exception) {
DB::query('ROLLBACK');

Expand Down Expand Up @@ -240,7 +255,7 @@ public function prepareQuery(): ModelQueryBuilder
['post_modified', 'updatedAt'],
['post_status', 'status'],
['post_title', 'title'],
['post_content', 'blocks']
['post_content', 'page_content'] // @todo Repurpose as form page.
)
->attachMeta(
'give_formmeta',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
* @unreleased
*
* @method static DonationFormMetaKeys SETTINGS()
* @method static DonationFormMetaKeys FIELDS()
* @method bool isSettings()
* @method bool isFields()
*/
class DonationFormMetaKeys extends Enum
{
use EnumInteractsWithQueryBuilder;

const SETTINGS = 'formBuilderSettings';

const FIELDS = 'formBuilderFields';
}
2 changes: 1 addition & 1 deletion tests/Unit/Actions/ConvertQueryDataToDonationFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testShouldReturnDonationForm()
'registration' => 'none',
'goalType' => GoalType::AMOUNT()->getValue(),
]),
'blocks' => $blockCollection->toJson()
'fields' => $blockCollection->toJson()
];

$donationForm = (new ConvertQueryDataToDonationForm())($queryData);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ViewModels/FormBuilderViewModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testShouldReturnStorageData()
'resourceURL' => rest_url(FormBuilderRestRouteConfig::NAMESPACE . '/form/' . $formId),
'previewURL' => (new GenerateDonationFormPreviewRouteUrl())($formId),
'nonce' => wp_create_nonce('wp_rest'),
'blockData' => get_post($formId)->post_content,
'blockData' => get_post_meta($formId, 'formBuilderFields', true),
'settings' => get_post_meta($formId, 'formBuilderSettings', true),
'currency' => give_get_currency(),
],
Expand Down

0 comments on commit 5170151

Please sign in to comment.