Skip to content

Commit

Permalink
fix: Update all config on update
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Nov 13, 2024
1 parent 27d991a commit e72cdc0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/storyMap/components/StoryMapForm/TopBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const SaveStatus = props => {

const ActionsMenu = () => {
const { t } = useTranslation();
const { storyMap, setPreview, slug } = useStoryMapConfigContext();
const { storyMap, setPreview } = useStoryMapConfigContext();
const [anchorEl, setAnchorEl] = useState(null);
const [openShareDialog, setOpenShareDialog] = useState(false);

Expand Down Expand Up @@ -130,7 +130,7 @@ const ActionsMenu = () => {
<MenuItem
dense
component={Link}
href={generateStoryMapUrl({ ...storyMap, slug })}
href={generateStoryMapUrl(storyMap)}
target="_blank"
>
{t('storyMap.form_view_published_button')}
Expand Down
2 changes: 1 addition & 1 deletion src/storyMap/components/StoryMapForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import TopBarPreview from './TopBarPreview';

import theme from 'theme';

const AUTO_SAVE_DEBOUNCE = 1000;
const AUTO_SAVE_DEBOUNCE = 3000;

const BASE_CHAPTER = {
alignment: 'left',
Expand Down
14 changes: 8 additions & 6 deletions src/storyMap/components/StoryMapForm/storyMapConfigContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const StoryMapConfigContextProvider = props => {
const [preview, setPreview] = useState(false);
const [mediaFiles, setMediaFiles] = useState({});
const [isDirty, setIsDirty] = useState(false);
const [slug, setSlug] = useState(storyMap.slug);
const init = useRef(false);

const addMediaFile = useCallback((content, file) => {
Expand All @@ -43,12 +42,16 @@ export const StoryMapConfigContextProvider = props => {
return id;
}, []);

const clearMediaFiles = useCallback(() => {
setMediaFiles({});
}, []);

const getMediaFile = useCallback(id => mediaFiles[id]?.content, [mediaFiles]);

const saved = useCallback(() => setIsDirty(false), []);

const setConfigWrapper = useCallback(
newConfigSetter => {
(newConfigSetter, dirty = true) => {
setConfig(currentConfig => {
const newConfig =
typeof newConfigSetter === 'function'
Expand All @@ -67,7 +70,7 @@ export const StoryMapConfigContextProvider = props => {
dataLayers: _.pick(usedDataLayersIds, newConfig.dataLayers),
};
});
setIsDirty(true);
setIsDirty(dirty);
},
[setConfig]
);
Expand All @@ -82,11 +85,10 @@ export const StoryMapConfigContextProvider = props => {
mediaFiles,
addMediaFile,
getMediaFile,
clearMediaFiles,
init,
saved,
isDirty,
slug,
setSlug,
}),
[
storyMap,
Expand All @@ -95,11 +97,11 @@ export const StoryMapConfigContextProvider = props => {
mediaFiles,
addMediaFile,
getMediaFile,
clearMediaFiles,
init,
setConfigWrapper,
isDirty,
saved,
slug,
]
);

Expand Down
14 changes: 6 additions & 8 deletions src/storyMap/components/StoryMapUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const StoryMapUpdate = props => {
const { t } = useTranslation();
const { trackEvent } = useAnalytics();
const [saved, setSaved] = useState();
const { storyMap, setSlug } = useStoryMapConfigContext();
const { storyMap, setConfig, clearMediaFiles } = useStoryMapConfigContext();

useDocumentTitle(
t('storyMap.edit_document_title', {
Expand Down Expand Up @@ -86,20 +86,15 @@ const StoryMapUpdate = props => {
}

if (title !== storyMap?.title) {
// window.location.replace(generateStoryMapEditUrl({ slug, storyMapId }));
// navigate(generateStoryMapEditUrl({ slug, storyMapId }));
window.history.pushState(
null,
t('storyMap.edit_document_title', {
name: _.get('title', storyMap),
}),
generateStoryMapEditUrl({ slug, storyMapId })
);
setSlug(slug);
// TODO data is not pudated after save
// dispatch(fetchStoryMapForm({ slug, storyMapId }));
}
}, [storyMap, navigate, trackEvent, saved, t, dispatch, setSlug]);
}, [storyMap, navigate, trackEvent, saved, t, dispatch]);

const save = useCallback(
(config, mediaFiles, publish) =>
Expand All @@ -119,6 +114,7 @@ const StoryMapUpdate = props => {
const storyMapId = _.get('payload.story_map_id', data);
const title = _.get('payload.title', data);
const id = _.get('payload.id', data);
const config = _.get('payload.configuration', data);

setSaved({
id,
Expand All @@ -127,11 +123,13 @@ const StoryMapUpdate = props => {
storyMapId,
published: publish,
});
clearMediaFiles();
setConfig(config, false);
return;
}
return Promise.reject(data);
}),
[storyMap?.id, dispatch]
[storyMap?.id, dispatch, clearMediaFiles, setConfig]
);
const onPublish = useCallback(
(config, mediaFiles) => save(config, mediaFiles, true),
Expand Down

0 comments on commit e72cdc0

Please sign in to comment.