Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: store state on create slack group checkbox #911

Merged
merged 3 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
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 @@ -43,6 +43,7 @@ const MainBoardCard = React.memo(({ team }: MainBoardCardInterface) => {
const {
handleAddTeam,
handleRemoveTeam,
handleSlackToggle,
createBoardData: { board },
setCreateBoardData,
canAdd,
Expand Down Expand Up @@ -153,10 +154,9 @@ const MainBoardCard = React.memo(({ team }: MainBoardCardInterface) => {
</MainContainer>
<SubBoardList dividedBoards={board.dividedBoards} setBoard={setCreateBoardData} />
<Box>
{/* onClick={slackEnableHandler} */}
<Checkbox
// checked={board.slackEnable}
shouldUseForm
handleChange={handleSlackToggle}
checked={board.slackEnable}
id="slackEnable"
label="Create Slack group for each sub-team"
size="16"
Expand Down
12 changes: 1 addition & 11 deletions frontend/src/components/Primitives/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { Dispatch, useState } from 'react';
import * as CheckboxPrimitive from '@radix-ui/react-checkbox';
import { styled } from '@stitches/react';

import { useFormContext } from 'react-hook-form';
import Flex from './Flex';
import Text from './Text';
import Icon from '../icons/Icon';
Expand Down Expand Up @@ -69,8 +68,7 @@ const Checkbox: React.FC<{
disabled?: boolean;
size: '12' | '16';
setCheckedTerms?: Dispatch<React.SetStateAction<boolean>> | null;
handleChange?: (value: string) => void;
shouldUseForm?: boolean;
handleChange?: ((value: string) => void) | (() => void);
handleSelectAll?: () => void;
hasSelectAll?: boolean;
}> = ({
Expand All @@ -81,7 +79,6 @@ const Checkbox: React.FC<{
checked,
disabled,
handleChange,
shouldUseForm,
setCheckedTerms,
handleSelectAll,
hasSelectAll,
Expand All @@ -92,13 +89,10 @@ const Checkbox: React.FC<{
disabled: false,
handleChange: undefined,
setCheckedTerms: null,
shouldUseForm: false,
handleSelectAll: undefined,
hasSelectAll: false,
};

const formContext = useFormContext();

const [currentCheckValue, setCurrentCheckValue] = useState<boolean | undefined | 'indeterminate'>(
checked,
);
Expand All @@ -107,10 +101,6 @@ const Checkbox: React.FC<{
if (handleSelectAll) handleSelectAll();
setCurrentCheckValue(isChecked);
if (setCheckedTerms != null) setCheckedTerms(!!isChecked);

if (shouldUseForm) {
formContext.setValue('slackEnable', !!isChecked);
}
};
const checkValue = hasSelectAll ? checked : currentCheckValue;

Expand Down
11 changes: 11 additions & 0 deletions frontend/src/hooks/useCreateBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,22 @@ const useCreateBoard = (team?: Team) => {
}));
};

const handleSlackToggle = () => {
setCreateBoardData((prev) => ({
...prev,
board: {
...prev.board,
slackEnable: !prev.board.slackEnable,
},
}));
};

return {
createBoardData,
setCreateBoardData,
handleAddTeam,
handleRemoveTeam,
handleSlackToggle,
canAdd,
canReduce,
generateSubBoard,
Expand Down
28 changes: 12 additions & 16 deletions frontend/src/pages/boards/newSplitBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,16 @@ const NewSplitBoard: NextPage = () => {
/**
* React Hook Form
*/
const methods = useForm<{ text: string; team: string; maxVotes?: number; slackEnable?: boolean }>(
{
mode: 'onBlur',
reValidateMode: 'onBlur',
defaultValues: {
text: '',
maxVotes: boardState.board.maxVotes,
slackEnable: false,
team: undefined,
},
resolver: joiResolver(SchemaCreateBoard),
const methods = useForm<{ text: string; team: string; maxVotes?: number }>({
mode: 'onBlur',
reValidateMode: 'onBlur',
defaultValues: {
text: '',
maxVotes: boardState.board.maxVotes,
team: undefined,
},
);
resolver: joiResolver(SchemaCreateBoard),
});

const mainBoardName = useWatch({
control: methods.control,
Expand Down Expand Up @@ -153,7 +150,7 @@ const NewSplitBoard: NextPage = () => {
* @param title Board Title
* @param maxVotes Maxium number of votes allowed
*/
const saveBoard = (title: string, team: string, maxVotes?: number, slackEnable?: boolean) => {
const saveBoard = (title: string, team: string, maxVotes?: number) => {
const responsibles: string[] = [];
const newDividedBoards: CreateBoardDto[] = boardState.board.dividedBoards.map((subBoard) => {
const newSubBoard: CreateBoardDto = { ...subBoard, users: [], dividedBoards: [] };
Expand Down Expand Up @@ -182,7 +179,6 @@ const NewSplitBoard: NextPage = () => {
title,
dividedBoards: newDividedBoards,
maxVotes,
slackEnable,
maxUsers: boardState.count.maxUsersCount,
team,
responsibles,
Expand Down Expand Up @@ -257,8 +253,8 @@ const NewSplitBoard: NextPage = () => {
direction="column"
onSubmit={
!haveError
? methods.handleSubmit(({ text, team, maxVotes, slackEnable }) => {
saveBoard(text, team, maxVotes, slackEnable);
? methods.handleSubmit(({ text, team, maxVotes }) => {
saveBoard(text, team, maxVotes);
})
: undefined
}
Expand Down