Header title will update on save
This commit is contained in:
parent
e598adae85
commit
3f545a681e
1 changed files with 26 additions and 13 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { useSnapshot } from 'valtio'
|
import { subscribe, useSnapshot } from 'valtio'
|
||||||
|
import { subscribeKey } from 'valtio/utils'
|
||||||
import { deleteCookie } from 'cookies-next'
|
import { deleteCookie } from 'cookies-next'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import { Trans, useTranslation } from 'next-i18next'
|
import { Trans, useTranslation } from 'next-i18next'
|
||||||
|
|
@ -49,11 +50,21 @@ const Header = () => {
|
||||||
const [settingsModalOpen, setSettingsModalOpen] = useState(false)
|
const [settingsModalOpen, setSettingsModalOpen] = useState(false)
|
||||||
const [leftMenuOpen, setLeftMenuOpen] = useState(false)
|
const [leftMenuOpen, setLeftMenuOpen] = useState(false)
|
||||||
const [rightMenuOpen, setRightMenuOpen] = useState(false)
|
const [rightMenuOpen, setRightMenuOpen] = useState(false)
|
||||||
|
|
||||||
|
const [name, setName] = useState('')
|
||||||
const [originalName, setOriginalName] = useState('')
|
const [originalName, setOriginalName] = useState('')
|
||||||
|
|
||||||
// Snapshots
|
// Snapshots
|
||||||
const { account } = useSnapshot(accountState)
|
const { account } = useSnapshot(accountState)
|
||||||
const { party } = useSnapshot(appState)
|
const { party: partySnapshot } = useSnapshot(appState)
|
||||||
|
|
||||||
|
const unsubscribe = subscribe(appState, () => {
|
||||||
|
const newName =
|
||||||
|
appState.party && appState.party.name ? appState.party.name : ''
|
||||||
|
setName(newName)
|
||||||
|
})
|
||||||
|
|
||||||
|
useEffect(() => () => unsubscribe(), [])
|
||||||
|
|
||||||
function handleCopyToastOpenChanged(open: boolean) {
|
function handleCopyToastOpenChanged(open: boolean) {
|
||||||
setCopyToastOpen(open)
|
setCopyToastOpen(open)
|
||||||
|
|
@ -140,31 +151,31 @@ const Header = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleFavorite() {
|
function toggleFavorite() {
|
||||||
if (party.favorited) unsaveFavorite()
|
if (partySnapshot.favorited) unsaveFavorite()
|
||||||
else saveFavorite()
|
else saveFavorite()
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveFavorite() {
|
function saveFavorite() {
|
||||||
if (party.id)
|
if (partySnapshot.id)
|
||||||
api.saveTeam({ id: party.id }).then((response) => {
|
api.saveTeam({ id: partySnapshot.id }).then((response) => {
|
||||||
if (response.status == 201) appState.party.favorited = true
|
if (response.status == 201) appState.party.favorited = true
|
||||||
})
|
})
|
||||||
else console.error('Failed to save team: No party ID')
|
else console.error('Failed to save team: No party ID')
|
||||||
}
|
}
|
||||||
|
|
||||||
function unsaveFavorite() {
|
function unsaveFavorite() {
|
||||||
if (party.id)
|
if (partySnapshot.id)
|
||||||
api.unsaveTeam({ id: party.id }).then((response) => {
|
api.unsaveTeam({ id: partySnapshot.id }).then((response) => {
|
||||||
if (response.status == 200) appState.party.favorited = false
|
if (response.status == 200) appState.party.favorited = false
|
||||||
})
|
})
|
||||||
else console.error('Failed to unsave team: No party ID')
|
else console.error('Failed to unsave team: No party ID')
|
||||||
}
|
}
|
||||||
|
|
||||||
function remixTeam() {
|
function remixTeam() {
|
||||||
setOriginalName(party.name ? party.name : t('no_title'))
|
setOriginalName(partySnapshot.name ? partySnapshot.name : t('no_title'))
|
||||||
|
|
||||||
if (party.shortcode)
|
if (partySnapshot.shortcode)
|
||||||
api.remix(party.shortcode).then((response) => {
|
api.remix(partySnapshot.shortcode).then((response) => {
|
||||||
const remix = response.data.party
|
const remix = response.data.party
|
||||||
router.push(`/p/${remix.shortcode}`)
|
router.push(`/p/${remix.shortcode}`)
|
||||||
setRemixToastOpen(true)
|
setRemixToastOpen(true)
|
||||||
|
|
@ -265,10 +276,12 @@ const Header = () => {
|
||||||
leftAccessoryIcon={<SaveIcon />}
|
leftAccessoryIcon={<SaveIcon />}
|
||||||
className={classNames({
|
className={classNames({
|
||||||
Save: true,
|
Save: true,
|
||||||
Saved: party.favorited,
|
Saved: partySnapshot.favorited,
|
||||||
})}
|
})}
|
||||||
blended={true}
|
blended={true}
|
||||||
text={party.favorited ? t('buttons.saved') : t('buttons.save')}
|
text={
|
||||||
|
partySnapshot.favorited ? t('buttons.saved') : t('buttons.save')
|
||||||
|
}
|
||||||
onClick={toggleFavorite}
|
onClick={toggleFavorite}
|
||||||
/>
|
/>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
|
|
@ -348,7 +361,7 @@ const Header = () => {
|
||||||
<section>
|
<section>
|
||||||
{router.route === '/p/[party]' &&
|
{router.route === '/p/[party]' &&
|
||||||
account.user &&
|
account.user &&
|
||||||
(!party.user || party.user.id !== account.user.id) &&
|
(!partySnapshot.user || partySnapshot.user.id !== account.user.id) &&
|
||||||
!appState.errorCode
|
!appState.errorCode
|
||||||
? saveButton()
|
? saveButton()
|
||||||
: ''}
|
: ''}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue