-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding drag & drop file upload
file upload and validation File upload progression adding a functional drag and drop upload field updating builder
- Loading branch information
1 parent
a2171f8
commit 196ef76
Showing
57 changed files
with
5,395 additions
and
1,071 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/builder/src/components/Composer/FieldTypes/DragAndDropFile.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { FILE } from './../../../constants/FieldTypes'; | ||
import Badge from './Components/Badge'; | ||
import HtmlInput from './HtmlInput'; | ||
import styled from 'styled-components'; | ||
|
||
const Wrapper = styled.div` | ||
padding: 10px 0; | ||
border-radius: 5px; | ||
border: 3px dashed grey; | ||
background: lightgrey; | ||
text-align: center; | ||
`; | ||
|
||
export default class DragAndDropFile extends HtmlInput { | ||
static propTypes = { | ||
properties: PropTypes.shape({ | ||
label: PropTypes.string.isRequired, | ||
required: PropTypes.bool.isRequired, | ||
assetSourceId: PropTypes.number, | ||
}).isRequired, | ||
}; | ||
|
||
getClassName() { | ||
return 'DragAndDropFile'; | ||
} | ||
|
||
getType() { | ||
return FILE; | ||
} | ||
|
||
getBadges() { | ||
const badges = super.getBadges(); | ||
const { | ||
properties: { assetSourceId }, | ||
} = this.props; | ||
|
||
if (!assetSourceId) { | ||
badges.push(<Badge key={'asset'} label="No Asset Source" type={Badge.WARNING} />); | ||
} | ||
|
||
return badges; | ||
} | ||
|
||
renderInput() { | ||
return <Wrapper>Drag & Drop a file</Wrapper>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/plugin/src/Bundles/Form/Fields/CheckboxField/CheckboxFieldBundle.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Solspace\Freeform\Bundles\Form\Fields\CheckboxField; | ||
|
||
use Solspace\Freeform\Bundles\GraphQL\Interfaces\FieldInterface; | ||
use Solspace\Freeform\Events\Fields\TransformValueEvent; | ||
use Solspace\Freeform\Fields\CheckboxField; | ||
use Solspace\Freeform\Library\Bundles\BundleInterface; | ||
use yii\base\Event; | ||
|
||
class CheckboxFieldBundle implements BundleInterface | ||
{ | ||
public function __construct() | ||
{ | ||
Event::on(FieldInterface::class, FieldInterface::EVENT_TRANSFORM_FROM_POST, [$this, 'handleTransform']); | ||
} | ||
|
||
public function handleTransform(TransformValueEvent $event) | ||
{ | ||
if (!$event instanceof CheckboxField) { | ||
return; | ||
} | ||
|
||
$event->setValue((bool) $event->getValue()); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
packages/plugin/src/Bundles/Form/Fields/FileUpload/FileUploadBundle.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Solspace\Freeform\Bundles\Form\Fields\FileUpload; | ||
|
||
use Solspace\Freeform\Bundles\GraphQL\Interfaces\FieldInterface; | ||
use Solspace\Freeform\Events\Fields\TransformValueEvent; | ||
use Solspace\Freeform\Events\Forms\SubmitEvent; | ||
use Solspace\Freeform\Fields\FileUploadField; | ||
use Solspace\Freeform\Fields\Pro\DragAndDropFileField; | ||
use Solspace\Freeform\Library\Bundles\BundleInterface; | ||
use Solspace\Freeform\Library\Composer\Components\Form; | ||
use Solspace\Freeform\Records\UnfinalizedFileRecord; | ||
use yii\base\Event; | ||
|
||
class FileUploadBundle implements BundleInterface | ||
{ | ||
public function __construct() | ||
{ | ||
Event::on(Form::class, Form::EVENT_AFTER_SUBMIT, [$this, 'finalizeFiles']); | ||
Event::on(FieldInterface::class, FieldInterface::EVENT_TRANSFORM_FROM_POST, [$this, 'handleDnDPost']); | ||
} | ||
|
||
public function finalizeFiles(SubmitEvent $event) | ||
{ | ||
$form = $event->getForm(); | ||
$submission = $event->getSubmission(); | ||
|
||
// Handle only stored submissions | ||
if (!$submission->id) { | ||
return; | ||
} | ||
|
||
$fields = $form->getLayout()->getFields(FileUploadField::class); | ||
$assetIds = []; | ||
foreach ($fields as $field) { | ||
$assetIds = array_merge($assetIds, $field->getValue()); | ||
} | ||
|
||
if (empty($assetIds)) { | ||
return; | ||
} | ||
|
||
$records = UnfinalizedFileRecord::findAll(['assetId' => $assetIds]); | ||
foreach ($records as $record) { | ||
$record->delete(); | ||
} | ||
} | ||
|
||
public function handleDnDPost(TransformValueEvent $event) | ||
{ | ||
$field = $event->getField(); | ||
if (!$field instanceof DragAndDropFileField) { | ||
return; | ||
} | ||
|
||
$uids = $event->getValue(); | ||
if (!\is_array($uids)) { | ||
$event->setValue([]); | ||
|
||
return; | ||
} | ||
|
||
$ids = []; | ||
foreach ($uids as $uid) { | ||
$asset = \Craft::$app->getElements()->getElementByUid($uid); | ||
$ids[] = $asset->id; | ||
} | ||
|
||
$event->setValue($ids); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.