Skip to content

Commit

Permalink
Refactor: Set form status as draft by default (impress-org#147)
Browse files Browse the repository at this point in the history
* refactor: Set form status as draft by default

* fix: Account for null viewing draft form as guest

* fix: Prepend page URL with post type argument
  • Loading branch information
kjohnson committed Mar 31, 2023
1 parent 937aee4 commit 731da64
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 16 deletions.
44 changes: 34 additions & 10 deletions packages/form-builder/src/containers/HeaderContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Button} from '@wordpress/components';
import {__} from '@wordpress/i18n';
import {Header} from '../components';
import {Storage} from '../common';
import {FormSettings} from '@givewp/form-builder/types';
import {FormSettings, FormStatus} from '@givewp/form-builder/types';
import {setIsDirty} from '@givewp/form-builder/stores/form-state/reducer';

const Logo = () => (
Expand Down Expand Up @@ -39,16 +39,22 @@ const HeaderContainer = ({

const {formTitle} = formSettings;
const dispatch = useFormStateDispatch();
const [isSaving, setSaving] = useState(false);
const [isSaving, setSaving] = useState(null);

const onSave = () => {
setSaving(true);
Storage.save({blocks, formSettings})
const isDraftDisabled = ( isSaving || !isDirty ) && 'draft' === formSettings.formStatus;
const isPublishDisabled = ( isSaving || !isDirty ) && 'publish' === formSettings.formStatus;

const onSave = (formStatus: FormStatus) => {
setSaving(formStatus);

dispatch(setFormSettings({formStatus}))

Storage.save({blocks, formSettings: {...formSettings, formStatus}})
.catch((error) => alert(error.message))
.then(({pageSlug}: FormSettings) => {
dispatch(setFormSettings({pageSlug}));
dispatch(setIsDirty(false));
setSaving(false);
setSaving(null);
onSaveNotice();
});
};
Expand Down Expand Up @@ -85,12 +91,30 @@ const HeaderContainer = ({
contentRight={
<>
<Button
onClick={onSave}
aria-disabled={isSaving || !isDirty}
disabled={isSaving || !isDirty}
onClick={() => onSave('draft')}
aria-disabled={isDraftDisabled}
disabled={isDraftDisabled}
variant="tertiary"
>
{isSaving && 'draft' === isSaving
? __('Saving...', 'give')
: 'draft' === formSettings.formStatus
? __('Save as Draft', 'give')
: __('Switch to Draft', 'give')
}
</Button>
<Button
onClick={() => onSave('publish')}
aria-disabled={isPublishDisabled}
disabled={isPublishDisabled}
variant="primary"
>
{isSaving ? __('Updating...', 'give') : __('Update', 'give')}
{isSaving && 'publish' === isSaving
? __('Updating...', 'give')
: 'publish' === formSettings.formStatus
? __('Update', 'give')
: __('Publish', 'give')
}
</Button>
<Button onClick={toggleShowSidebar} isPressed={showSidebar} icon={drawerRight} />
</>
Expand Down
3 changes: 3 additions & 0 deletions packages/form-builder/src/types/formSettings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {FormStatus} from "@givewp/form-builder/types/formStatus";

/**
* @since 0.1.0
*/
Expand All @@ -19,4 +21,5 @@ export type FormSettings = {
pageSlug: string;
receiptHeading: string;
receiptDescription: string;
formStatus: FormStatus;
};
1 change: 1 addition & 0 deletions packages/form-builder/src/types/formStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type FormStatus = "draft" | "publish";
1 change: 1 addition & 0 deletions packages/form-builder/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export type {FormDesign} from './formDesign';
export type {FormSettings} from './formSettings';
export type {FormState} from './formState';
export type {FormPageSettings} from './formPageSettings';
export type {FormStatus} from './formStatus';
2 changes: 2 additions & 0 deletions src/FormBuilder/Controllers/FormBuilderResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Give\Framework\FieldsAPI\Form;
use Give\NextGen\DonationForm\Models\DonationForm;
use Give\NextGen\DonationForm\Properties\FormSettings;
use Give\NextGen\DonationForm\ValueObjects\DonationFormStatus;
use Give\NextGen\Framework\Blocks\BlockCollection;
use WP_Error;
use WP_HTTP_Response;
Expand Down Expand Up @@ -76,6 +77,7 @@ public function update(WP_REST_Request $request)
return rest_ensure_response($requiredFieldsError);
}

$form->status = $updatedSettings->formStatus;
$form->save();

return rest_ensure_response([
Expand Down
2 changes: 1 addition & 1 deletion src/FormBuilder/Routes/CreateFormRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __invoke()

$form = DonationForm::create([
'title' => __('GiveWP Donation Form', 'give'),
'status' => DonationFormStatus::PUBLISHED(),
'status' => DonationFormStatus::DRAFT(),
'settings' => FormSettings::fromArray([
'goalAmount' => 1000,
]),
Expand Down
2 changes: 1 addition & 1 deletion src/FormBuilder/ViewModels/FormBuilderViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function storageData(int $donationFormId): array
}, give(FormDesignRegistrar::class)->getDesigns()),
'formPage' => [
'isEnabled' => give_is_setting_enabled(give_get_option('forms_singular')), // Note: Boolean values must be nested in an array to maintain boolean type, see \WP_Scripts::localize().
'permalink' => add_query_arg(['p' => $donationFormId], site_url()),
'permalink' => add_query_arg(['p' => $donationFormId], site_url('?post_type=give_forms')),
'rewriteSlug' => get_post_type_object('give_forms')->rewrite['slug'],
],
];
Expand Down
7 changes: 7 additions & 0 deletions src/NextGen/DonationForm/Properties/FormSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Give\Framework\Support\Contracts\Arrayable;
use Give\Framework\Support\Contracts\Jsonable;
use Give\NextGen\DonationForm\FormDesigns\ClassicFormDesign\ClassicFormDesign;
use Give\NextGen\DonationForm\ValueObjects\DonationFormStatus;
use Give\NextGen\DonationForm\ValueObjects\GoalType;

class FormSettings implements Arrayable, Jsonable
Expand Down Expand Up @@ -85,6 +86,11 @@ class FormSettings implements Arrayable, Jsonable
*/
public $receiptDescription;

/**
* @var DonationFormStatus
*/
public $formStatus;

/**
* @since 0.1.0
*/
Expand Down Expand Up @@ -122,6 +128,7 @@ public static function fromArray(array $array): self
'{first_name}, your contribution means a lot and will be put to good use in making a difference. We’ve sent your donation receipt to {email}.',
'give'
);
$self->formStatus = !empty($array['formStatus']) ? new DonationFormStatus($array['formStatus']) : DonationFormStatus::DRAFT();

return $self;
}
Expand Down
9 changes: 5 additions & 4 deletions src/NextGen/FormPage/TemplateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class TemplateHandler
{
/**
* @var Post
* @var Post|null
*/
private $post;

Expand All @@ -16,7 +16,7 @@ class TemplateHandler
*/
private $formPageTemplatePath;

public function __construct( Post $post, string $formPageTemplatePath )
public function __construct( $post, string $formPageTemplatePath )
{
$this->post = $post;
$this->formPageTemplatePath = $formPageTemplatePath;
Expand All @@ -31,7 +31,8 @@ public function handle($template)

protected function isNextGenForm(): bool
{
return 'give_forms' === $this->post->post_type
&& $this->post->post_content;
return $this->post
&& $this->post->post_content
&&'give_forms' === $this->post->post_type;
}
}

0 comments on commit 731da64

Please sign in to comment.