Update poll reactively

This commit is contained in:
Lim Chee Aun 2022-12-21 19:29:37 +08:00
parent 3b6f0f277e
commit 23745d0683

View file

@ -359,7 +359,15 @@ function Status({
}),
}}
/>
{!!poll && <Poll poll={poll} readOnly={readOnly} />}
{!!poll && (
<Poll
poll={poll}
readOnly={readOnly}
onUpdate={(newPoll) => {
states.statuses.get(id).poll = newPoll;
}}
/>
)}
{!spoilerText && sensitive && !!mediaAttachments.length && (
<button
class="plain spoiler"
@ -873,14 +881,9 @@ function Card({ card }) {
}
}
function Poll({ poll, readOnly }) {
const [pollSnapshot, setPollSnapshot] = useState(poll);
function Poll({ poll, readOnly, onUpdate = () => {} }) {
const [uiState, setUIState] = useState('default');
useEffect(() => {
setPollSnapshot(poll);
}, [poll]);
const {
expired,
expiresAt,
@ -891,7 +894,7 @@ function Poll({ poll, readOnly }) {
voted,
votersCount,
votesCount,
} = pollSnapshot;
} = poll;
const expiresAtDate = !!expiresAt && new Date(expiresAt);
@ -953,7 +956,7 @@ function Poll({ poll, readOnly }) {
choices: votes,
});
console.log(pollResponse);
setPollSnapshot(pollResponse);
onUpdate(pollResponse);
setUIState('default');
}}
style={{