-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #452 from Vizzuality/fix/wdpa-flow
WDPA: read them from scenario instead of store in certain cases
- Loading branch information
Showing
23 changed files
with
186 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { QueryClient } from 'react-query'; | ||
|
||
import { getSession } from 'next-auth/client'; | ||
import { dehydrate } from 'react-query/hydration'; | ||
|
||
import SCENARIOS from 'services/scenarios'; | ||
|
||
import { mergeDehydratedState } from './utils'; | ||
|
||
export function withScenario(getServerSidePropsFunc?: Function) { | ||
return async (context: any) => { | ||
const session = await getSession(context); | ||
|
||
if (!session) { | ||
if (getServerSidePropsFunc) { | ||
const SSPF = await getServerSidePropsFunc(context) || {}; | ||
|
||
return { | ||
props: { | ||
...SSPF.props, | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
props: {}, | ||
}; | ||
} | ||
|
||
const { params } = context; | ||
|
||
const { sid } = params; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
await queryClient.prefetchQuery(['scenarios', sid], () => SCENARIOS.request({ | ||
method: 'GET', | ||
url: `/${sid}`, | ||
headers: { | ||
Authorization: `Bearer ${session.accessToken}`, | ||
}, | ||
}).then((response) => { | ||
return response.data; | ||
})); | ||
|
||
if (getServerSidePropsFunc) { | ||
const SSPF = await getServerSidePropsFunc(context) || {}; | ||
|
||
const { dehydratedState: prevDehydratedState } = SSPF.props; | ||
const currentDehydratedState = JSON.parse(JSON.stringify(dehydrate(queryClient))); | ||
|
||
const newDehydratedState = mergeDehydratedState(prevDehydratedState, currentDehydratedState); | ||
|
||
return { | ||
...SSPF, | ||
props: { | ||
session, | ||
...SSPF.props, | ||
dehydratedState: newDehydratedState, | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
props: { | ||
session, | ||
dehydratedState: JSON.parse(JSON.stringify(dehydrate(queryClient))), | ||
}, | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.