mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Improved naming in AdminX dirty state handling
refs https://github.com/TryGhost/Team/issues/3349
This commit is contained in:
parent
5a6421c8bb
commit
384b468cbb
1 changed files with 11 additions and 11 deletions
|
@ -1,23 +1,23 @@
|
||||||
import React, {useCallback, useContext, useEffect, useId, useState} from 'react';
|
import React, {useCallback, useContext, useEffect, useId, useState} from 'react';
|
||||||
|
|
||||||
interface GlobalDirtyState {
|
interface GlobalDirtyState {
|
||||||
setGlobalDirtyState: (reason: string, dirty: boolean) => void;
|
setGlobalDirtyState: (id: string, dirty: boolean) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const GlobalDirtyStateContext = React.createContext<GlobalDirtyState>({setGlobalDirtyState: () => {}});
|
const GlobalDirtyStateContext = React.createContext<GlobalDirtyState>({setGlobalDirtyState: () => {}});
|
||||||
|
|
||||||
export const GlobalDirtyStateProvider = ({setDirty, children}: {setDirty?: (dirty: boolean) => void; children: React.ReactNode}) => {
|
export const GlobalDirtyStateProvider = ({setDirty, children}: {setDirty?: (dirty: boolean) => void; children: React.ReactNode}) => {
|
||||||
// Allows each component to register itself as dirty, so when one is reset/saved the overall page dirty state persists
|
// Allows each component to register itself as dirty with a unique ID, so when one is reset/saved the overall page dirty state persists
|
||||||
const [dirtyReasons, setDirtyReasons] = useState<string[]>([]);
|
const [dirtyIds, setDirtyIds] = useState<string[]>([]);
|
||||||
|
|
||||||
const setGlobalDirtyState = useCallback((reason: string, dirty: boolean) => {
|
const setGlobalDirtyState = useCallback((id: string, dirty: boolean) => {
|
||||||
setDirtyReasons((current) => {
|
setDirtyIds((current) => {
|
||||||
if (dirty && !current.includes(reason)) {
|
if (dirty && !current.includes(id)) {
|
||||||
return [...current, reason];
|
return [...current, id];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dirty && current.includes(reason)) {
|
if (!dirty && current.includes(id)) {
|
||||||
return current.filter(currentReason => currentReason !== reason);
|
return current.filter(currentId => currentId !== id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return current;
|
return current;
|
||||||
|
@ -25,8 +25,8 @@ export const GlobalDirtyStateProvider = ({setDirty, children}: {setDirty?: (dirt
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setDirty?.(dirtyReasons.length > 0);
|
setDirty?.(dirtyIds.length > 0);
|
||||||
}, [dirtyReasons, setDirty]);
|
}, [dirtyIds, setDirty]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<GlobalDirtyStateContext.Provider value={{setGlobalDirtyState}}>
|
<GlobalDirtyStateContext.Provider value={{setGlobalDirtyState}}>
|
||||||
|
|
Loading…
Add table
Reference in a new issue