-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- usePollState 작성
- Loading branch information
Showing
2 changed files
with
102 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export function usePollState({ initialPollItems = [] }) { | ||
const initialPollItemsSet = new Set(initialPollItems); | ||
|
||
const [selectedPollItems, setSelectedPollItems] = useState<Set<string>>(new Set()); | ||
|
||
// calculate the difference btw poll and initialPoll | ||
const isModifiedPoll = | ||
selectedPollItems.size !== initialPollItemsSet.size || | ||
Array.from(selectedPollItems.keys()).some((id) => !initialPollItemsSet.has(id)) || | ||
Array.from(initialPollItemsSet.keys()).some((id) => !selectedPollItems.has(id)); | ||
|
||
const [showRePollButton, setShowRePollButton] = useState(false); | ||
const [isActiveRePollButton, setIsActiveRePollButton] = useState(false); | ||
|
||
useEffect(() => { | ||
if (initialPollItems) { | ||
setSelectedPollItems(new Set(initialPollItems)); | ||
if (initialPollItems.length !== 0) { | ||
setShowRePollButton(true); | ||
setIsActiveRePollButton(true); | ||
} else { | ||
setIsActiveRePollButton(false); | ||
} | ||
} | ||
}, [initialPollItems]); | ||
|
||
const handlePoll = ({ canMultiple, pollItem }: { canMultiple: boolean; pollItem: string }) => { | ||
const isChecked = selectedPollItems.has(pollItem); | ||
if (canMultiple) { | ||
setSelectedPollItems((prev) => { | ||
const cloned = new Set(prev); | ||
if (isChecked) cloned.delete(pollItem); | ||
else cloned.add(pollItem); | ||
return cloned; | ||
}); | ||
} else { | ||
setSelectedPollItems((prev) => { | ||
const cloned = new Set(prev); | ||
if (isChecked) { | ||
cloned.delete(pollItem); | ||
} else if (cloned.size !== 0) { | ||
// if already checking other poll item | ||
cloned.clear(); | ||
cloned.add(pollItem); | ||
} else { | ||
cloned.add(pollItem); | ||
} | ||
return cloned; | ||
}); | ||
} | ||
}; | ||
|
||
const isSelected = (pollItem: string) => { | ||
return selectedPollItems.has(pollItem); | ||
}; | ||
|
||
const hideRePollButton = () => { | ||
setShowRePollButton(false); | ||
}; | ||
|
||
const cancelPoll = () => { | ||
setShowRePollButton(true); | ||
setSelectedPollItems(initialPollItemsSet); | ||
}; | ||
|
||
return { | ||
isModified: isModifiedPoll, | ||
selectedPollItems: Array.from(selectedPollItems.keys()), | ||
showRePollButton, | ||
isActiveRePollButton, | ||
handlePoll, | ||
isSelected, | ||
hideRePollButton, | ||
cancelPoll, | ||
}; | ||
} |
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