From eb137ae70e732f2615999bde47dcea9508793f61 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Fri, 28 May 2021 17:41:34 +0100 Subject: [PATCH] Complete feature settings --- src/pages/FeatureSettings/index.tsx | 119 ++++++++++++++++++++++++++-- 1 file changed, 113 insertions(+), 6 deletions(-) diff --git a/src/pages/FeatureSettings/index.tsx b/src/pages/FeatureSettings/index.tsx index 45fc9ef..b150a68 100644 --- a/src/pages/FeatureSettings/index.tsx +++ b/src/pages/FeatureSettings/index.tsx @@ -15,6 +15,7 @@ import { Spinner, Modal, CheckBox, + ImagePreview, } from '../../components'; import { Wrapper } from './styles'; import { ArrowLeft, Design, General } from '../../assets'; @@ -43,7 +44,18 @@ const FeatureSettings = () => { >('general'); const [error, setError] = useState(''); const [success, setSuccess] = useState(false); - const [feature, setFeature] = useState(); + const [feature, setFeature] = useState({ + id, + name: '', + description: '', + featureType: '', + image: { + name: '', + src: '', + }, + price: 0, + repo: '', + }); const [deleteFeatureModal, setDeleteFeatureModal] = useState(false); @@ -137,6 +149,35 @@ const FeatureSettings = () => { enableReinitialize: true, }); + const wireframesForm = useFormik<{ + wireframes: Array<{ name: string; src: string }>; + }>({ + initialValues: { + wireframes: + feature?.wireframes?.map((wireframe) => ({ + name: wireframe.name, + src: wireframe.src, + })) || [], + }, + onSubmit: ({ wireframes }) => { + updateFeature({ + variables: { + id, + feature: { + name: feature.name, + description: feature.description, + price: feature.price, + repo: feature.repo, + featureType: feature.featureType, + image: { name: feature.image.name, src: feature.image.src }, + wireframes, + }, + }, + }); + }, + enableReinitialize: true, + }); + return role === 'developer' ? ( @@ -162,7 +203,8 @@ const FeatureSettings = () => { icon={} color={role || 'client'} text='General' - selected + selected={selectedSection === 'general'} + onClick={() => setSelectedSection('general')} /> } @@ -187,11 +229,11 @@ const FeatureSettings = () => { marginBottom='50px' > - General + {selectedSection === 'general' ? 'General' : 'Wireframes'} {error && } {success && ( - + )} {selectedSection === 'general' && ( @@ -264,8 +306,9 @@ const FeatureSettings = () => { } }} error={ - !!generalForm.errors.imageName || - !!generalForm.errors.imageSource + generalForm.touched.imageName && + (!!generalForm.errors.imageName || + !!generalForm.errors.imageSource) } errorMessage={generalForm.errors.imageName} /> @@ -451,6 +494,70 @@ const FeatureSettings = () => { )} )} + {selectedSection === 'wireframes' && ( +
+ + + ) => { + const formData = new FormData(); + + if (event.target.files && event.target.files[0]) { + formData.append('file', event.target.files[0]); + formData.append('upload_preset', 'xofll5kc'); + + const data = await ( + await fetch(`${process.env.REACT_APP_CLOUDINARY_URL}`, { + method: 'POST', + body: formData, + }) + ).json(); + + const filename = data.original_filename; + const filesource = data.secure_url; + + wireframesForm.setFieldValue('wireframes', [ + ...wireframesForm.values.wireframes, + { name: filename, src: filesource }, + ]); + } + }} + /> + {wireframesForm.values.wireframes.map((image) => ( + { + wireframesForm.setFieldValue( + 'wireframes', + wireframesForm.values.wireframes.filter( + ({ name }) => name !== image.name + ) + ); + }} + /> + ))} + + +