-
Notifications
You must be signed in to change notification settings - Fork 7
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 #83 from globalfund/feat/DE-1458
feat: Setup country summaries in strapi CMS
- Loading branch information
Showing
7 changed files
with
139 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from "react"; | ||
import { useUpdateEffect } from "react-use"; | ||
import { useStoreActions, useStoreState } from "app/state/store/hooks"; | ||
|
||
interface Props { | ||
loadData?: boolean; | ||
returnData?: boolean; | ||
} | ||
|
||
export function useCMSCollections(props: Props) { | ||
const cmsData = useStoreState((state) => state.cms.formattedCollections); | ||
const setCMSData = useStoreActions( | ||
(actions) => actions.cms.formattedCollections.setPagesData | ||
); | ||
|
||
// Collections state | ||
const countrySummaryCMSData = useStoreState( | ||
(state) => state.cms.collections.countrySummary.data | ||
); | ||
|
||
// Collections actions | ||
const countrySummaryCMSAction = useStoreActions( | ||
(actions) => actions.cms.collections.countrySummary.fetch | ||
); | ||
|
||
function formatCMSData() { | ||
const currentLanguage = "en"; | ||
const items = [ | ||
{ | ||
key: "countrySummary", | ||
data: countrySummaryCMSData ?? {}, | ||
}, | ||
]; | ||
|
||
const formattedData: any = { | ||
countrySummary: [], | ||
}; | ||
items.forEach((item) => { | ||
// @ts-ignore | ||
formattedData[item.key] = item.data.data | ||
?.filter((d: any) => d.attributes.locale === currentLanguage) | ||
.map((d: any) => ({ | ||
...d.attributes, | ||
})); | ||
}); | ||
setCMSData(formattedData); | ||
} | ||
|
||
React.useEffect(() => { | ||
if (props.loadData) { | ||
countrySummaryCMSAction({ | ||
isCMSfetch: true, | ||
filterString: `locale=all&pagination[page]=1&pagination[pageSize]=150`, | ||
}); | ||
} | ||
}, []); | ||
|
||
useUpdateEffect(() => { | ||
if (props.loadData) { | ||
formatCMSData(); | ||
} | ||
}, [countrySummaryCMSData]); | ||
|
||
if (props.returnData) { | ||
return cmsData; | ||
} | ||
|
||
return null; | ||
} |
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,6 @@ | ||
import { APIModel } from "app/state/api"; | ||
import { CMSApiCallModel } from "app/state/api/interfaces"; | ||
|
||
export const countrySummary: CMSApiCallModel = { | ||
...APIModel(`${process.env.REACT_APP_CMS_API}/country-summaries`), | ||
}; |
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,10 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import { action } from "easy-peasy"; | ||
import { CMSFormattedCollectionsModel } from "app/state/api/interfaces"; | ||
|
||
export const formattedCollections: CMSFormattedCollectionsModel = { | ||
countrySummary: {}, | ||
setPagesData: action((state, payload) => { | ||
state.countrySummary = payload.countrySummary; | ||
}), | ||
}; |
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