Commit d83b3785 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Display pending updates notification when there have been deletions

parent 5ef08e4b
...@@ -27,7 +27,7 @@ function PendingUpdatesNotification({ ...@@ -27,7 +27,7 @@ function PendingUpdatesNotification({
}: PendingUpdatesNotificationProps) { }: PendingUpdatesNotificationProps) {
const store = useSidebarStore(); const store = useSidebarStore();
const pendingUpdateCount = store.pendingUpdateCount(); const pendingUpdateCount = store.pendingUpdateCount();
const hasPendingUpdates = store.hasPendingUpdates(); const hasPendingChanges = store.hasPendingUpdatesOrDeletions();
const applyPendingUpdates = useCallback( const applyPendingUpdates = useCallback(
() => streamer.applyPendingUpdates(), () => streamer.applyPendingUpdates(),
[streamer], [streamer],
...@@ -35,10 +35,10 @@ function PendingUpdatesNotification({ ...@@ -35,10 +35,10 @@ function PendingUpdatesNotification({
const [collapsed, setCollapsed] = useState(false); const [collapsed, setCollapsed] = useState(false);
const timeout = useRef<number | null>(null); const timeout = useRef<number | null>(null);
useShortcut('l', () => hasPendingUpdates && applyPendingUpdates()); useShortcut('l', () => hasPendingChanges && applyPendingUpdates());
useEffect(() => { useEffect(() => {
if (hasPendingUpdates) { if (hasPendingChanges) {
timeout.current = setTimeout_(() => { timeout.current = setTimeout_(() => {
setCollapsed(true); setCollapsed(true);
timeout.current = null; timeout.current = null;
...@@ -48,9 +48,9 @@ function PendingUpdatesNotification({ ...@@ -48,9 +48,9 @@ function PendingUpdatesNotification({
} }
return () => timeout.current && clearTimeout_(timeout.current); return () => timeout.current && clearTimeout_(timeout.current);
}, [clearTimeout_, hasPendingUpdates, setTimeout_]); }, [clearTimeout_, hasPendingChanges, setTimeout_]);
if (!hasPendingUpdates) { if (!hasPendingChanges) {
return null; return null;
} }
......
...@@ -20,7 +20,7 @@ describe('PendingUpdatesNotification', () => { ...@@ -20,7 +20,7 @@ describe('PendingUpdatesNotification', () => {
}; };
fakeStore = { fakeStore = {
pendingUpdateCount: sinon.stub().returns(3), pendingUpdateCount: sinon.stub().returns(3),
hasPendingUpdates: sinon.stub().returns(true), hasPendingUpdatesOrDeletions: sinon.stub().returns(true),
}; };
$imports.$mock({ $imports.$mock({
...@@ -68,7 +68,7 @@ describe('PendingUpdatesNotification', () => { ...@@ -68,7 +68,7 @@ describe('PendingUpdatesNotification', () => {
} }
it('does not render anything while there are no pending updates', () => { it('does not render anything while there are no pending updates', () => {
fakeStore.hasPendingUpdates.returns(false); fakeStore.hasPendingUpdatesOrDeletions.returns(false);
const wrapper = createComponent(); const wrapper = createComponent();
assert.isEmpty(wrapper); assert.isEmpty(wrapper);
...@@ -112,7 +112,7 @@ describe('PendingUpdatesNotification', () => { ...@@ -112,7 +112,7 @@ describe('PendingUpdatesNotification', () => {
[true, false].forEach(hasPendingUpdates => { [true, false].forEach(hasPendingUpdates => {
it('applies updates when "l" is pressed', () => { it('applies updates when "l" is pressed', () => {
fakeStore.hasPendingUpdates.returns(hasPendingUpdates); fakeStore.hasPendingUpdatesOrDeletions.returns(hasPendingUpdates);
let wrapper; let wrapper;
const container = document.createElement('div'); const container = document.createElement('div');
document.body.append(container); document.body.append(container);
......
...@@ -213,6 +213,14 @@ function hasPendingUpdates(state: State): boolean { ...@@ -213,6 +213,14 @@ function hasPendingUpdates(state: State): boolean {
return Object.keys(state.pendingUpdates).length > 0; return Object.keys(state.pendingUpdates).length > 0;
} }
/**
* Return true if at least one annotation has been created or deleted on the
* server, but it has not yet been applied.
*/
function hasPendingUpdatesOrDeletions(state: State): boolean {
return pendingUpdateCount(state) > 0;
}
export const realTimeUpdatesModule = createStoreModule(initialState, { export const realTimeUpdatesModule = createStoreModule(initialState, {
namespace: 'realTimeUpdates', namespace: 'realTimeUpdates',
reducers, reducers,
...@@ -223,6 +231,7 @@ export const realTimeUpdatesModule = createStoreModule(initialState, { ...@@ -223,6 +231,7 @@ export const realTimeUpdatesModule = createStoreModule(initialState, {
selectors: { selectors: {
hasPendingDeletion, hasPendingDeletion,
hasPendingUpdates, hasPendingUpdates,
hasPendingUpdatesOrDeletions,
pendingDeletions, pendingDeletions,
pendingUpdates, pendingUpdates,
pendingUpdateCount, pendingUpdateCount,
......
...@@ -193,4 +193,20 @@ describe('sidebar/store/modules/real-time-updates', () => { ...@@ -193,4 +193,20 @@ describe('sidebar/store/modules/real-time-updates', () => {
assert.equal(store.hasPendingUpdates(), true); assert.equal(store.hasPendingUpdates(), true);
}); });
}); });
describe('hasPendingUpdatesOrDeletions', () => {
it('returns false if there are no pending updates nor deletions', () => {
assert.isFalse(store.hasPendingUpdatesOrDeletions());
});
it('returns true if there are pending updates', () => {
addPendingUpdates(store);
assert.isTrue(store.hasPendingUpdatesOrDeletions());
});
it('returns true if there are pending deletions', () => {
addPendingDeletions(store);
assert.isTrue(store.hasPendingUpdatesOrDeletions());
});
});
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment