Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { useCommunityDetails } from 'hooks';
import { kebabToString } from 'utils';
import { Heading } from '@chakra-ui/react';
import { yupResolver } from '@hookform/resolvers/yup';
import identity from 'lodash/identity';
import pick from 'lodash/pick';
import pickBy from 'lodash/pickBy';
import { stepTwo } from '../FormConfig';
import VotingSelector from './VotingSelector';

Expand Down Expand Up @@ -71,24 +73,24 @@ const StepTwo = ({
moveToNextStep();
};

const defaultValueStrategy = useWatch({ control, name: 'strategy' });
const voteType = useWatch({ control, name: 'voteType' });
// watching all fields
const fields = useWatch({ control });
const {
strategy: defaultValueStrategy,
voteType,
choices: choicesTemp,
tabOption,
maxWeight,
minBalance,
} = fields;

// save vote type
useEffect(() => {
onDataChange({ voteType });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [voteType]);
// **************************************************************
// This is to enable having choices when entering in preview mode
// fields are saved and validated when user hits on next
// by doing this we are saving the options before without validation
// when user hits next fields will be validated and overwritten with valid values
// for example it's possible to enter in preview mode with duplicated voting options
// but next will let the user know this needs to be updated
const choicesTemp = useWatch({ control, name: 'choices' });
const tabOption = useWatch({ control, name: 'tabOption' });

useEffect(() => {
let choices = choicesTemp;
if (tabOption === 'visual') {
Expand All @@ -102,6 +104,17 @@ const StepTwo = ({
}, [choicesTemp, tabOption]);
// **************************************************************

// saving temp updates on fields
useEffect(() => {
// removes undefined values
const data = pickBy(
{ voteType, strategy: defaultValueStrategy, maxWeight, minBalance },
identity
);
onDataChange(data);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [voteType, defaultValueStrategy, maxWeight, minBalance]);

const { isSubmitting, isValid, errors, isDirty } = formState;

useEffect(() => {
Expand Down