Ensure content gets reset when edits are committed

Here, we do some tactical bandaid fixes to ensure that when the user saves data to the server, the editor will show the freshest data in both editable and read-only mode.

In the Editor, its as easy as calling the setContent command in a useEffect hook when the content changes.

In the party, we are saving party in a state and passing it down to the components via props. This is because the party prop we get from pages is only from the first time the server loaded data, so any edits are not reflected. The app state should have the latest updates, but due to reasons I don't completely understand, it is showing the old state first and then the one we want, causing the Editor to get stuck on old data.

By storing the party in a state, we can populate the state from the server when the component mounts, then update it whenever the user saves data since all party data is saved in that component.
This commit is contained in:
Justin Edmund 2023-07-06 02:45:37 -07:00
parent 62e17e429d
commit f6d0a0b795
2 changed files with 17 additions and 10 deletions

View file

@ -41,13 +41,12 @@ const Editor = ({
onUpdate,
...props
}: Props) => {
// Hooks: Router
const router = useRouter()
const locale = router.locale || 'en'
useEffect(() => {
// console.log('Recreating editor...')
// editor?.destroy()
// setEditor(newEditor)
editor?.commands.setContent(formatContent(content))
}, [content])
// Setup: Editor

View file

@ -46,6 +46,7 @@ const Party = (props: Props) => {
// Set up states
const { party } = useSnapshot(appState)
const [updatedParty, setUpdatedParty] = useState<Party | undefined>()
const [editable, setEditable] = useState(false)
const [refresh, setRefresh] = useState(false)
const [errorMessage, setErrorMessage] = useState('')
@ -57,7 +58,10 @@ const Party = (props: Props) => {
useEffect(() => {
const resetState = clonedeep(initialAppState)
appState.grid = resetState.grid
if (props.team) storeParty(props.team)
if (props.team) {
storeParty(props.team)
setUpdatedParty(props.team)
}
}, [])
// Subscribe to app state to listen for account changes and
@ -108,9 +112,10 @@ const Party = (props: Props) => {
let payload = {}
if (details) payload = formatDetailsObject(details)
return await api.endpoints.parties
.create(payload)
.then((response) => storeParty(response.data.party))
return await api.endpoints.parties.create(payload).then((response) => {
storeParty(response.data.party)
setUpdatedParty(response.data.party)
})
}
async function updateParty(details: DetailsObject) {
@ -119,7 +124,10 @@ const Party = (props: Props) => {
if (props.team && props.team.id) {
return await api.endpoints.parties
.update(props.team.id, payload)
.then((response) => storeParty(response.data.party))
.then((response) => {
storeParty(response.data.party)
setUpdatedParty(response.data.party)
})
.catch((error) => {
const data = error.response.data
if (data.errors && Object.keys(data.errors).includes('guidebooks')) {
@ -438,7 +446,7 @@ const Party = (props: Props) => {
{errorAlert()}
<PartyHeader
party={props.team}
party={updatedParty}
new={props.new || false}
editable={props.new ? true : party.editable}
raidGroups={props.raidGroups}
@ -452,7 +460,7 @@ const Party = (props: Props) => {
<section id="Party">{currentGrid()}</section>
<PartyFooter
party={props.team}
party={updatedParty}
new={props.new || false}
editable={party.editable}
raidGroups={props.raidGroups}