forked from zendesk/copenhagen_theme
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03fd634
commit 2bf2b89
Showing
10 changed files
with
237 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { FC } from 'react'; | ||
import { SideNavData } from '../../lib/types'; | ||
import cn from 'classnames'; | ||
|
||
type Props = { | ||
sideNavData: SideNavData; | ||
}; | ||
|
||
export const SideNav: FC<Props> = ({ sideNavData }) => { | ||
return <div>Siiiide naaav</div>; | ||
}; |
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,140 @@ | ||
import { SideNavData } from '../../lib/types'; | ||
|
||
const makeArrayToHaveUniqueValues = (array: { id: number }[]) => { | ||
return array.filter((value, index, self) => self.findIndex((v) => v.id === value.id) === index); | ||
}; | ||
|
||
type SideNavApiResponse = { | ||
articles: { | ||
section_id: number; | ||
id: number; | ||
url: string; | ||
name: string; | ||
position: number; | ||
}[]; | ||
categories: { | ||
id: number; | ||
position: number; | ||
url: string; | ||
name: string; | ||
}[]; | ||
sections: { | ||
category_id: number; | ||
id: number; | ||
url: string; | ||
name: string; | ||
position: number; | ||
}[]; | ||
}; | ||
|
||
const sanitizeResponse = (response: SideNavApiResponse): SideNavData => { | ||
if (!response) { | ||
return null; | ||
} | ||
|
||
if (!response.articles || !response.categories || !response.sections) { | ||
return null; | ||
} | ||
|
||
const categories = response.categories.map((category) => { | ||
const sections = response.sections | ||
.filter((section) => section.category_id === category.id) | ||
.map((section) => { | ||
const articles = response.articles | ||
.filter((article) => article.section_id === section.id) | ||
.map((article) => ({ | ||
id: article.id, | ||
name: article.name, | ||
url: article.url, | ||
position: article.position, | ||
})); | ||
|
||
return { | ||
id: section.id, | ||
name: section.name, | ||
position: section.position, | ||
url: section.url, | ||
articles: articles.sort((a, b) => a.position - b.position), | ||
}; | ||
}); | ||
|
||
return { | ||
id: category.id, | ||
name: category.name, | ||
position: category.position, | ||
url: category.url, | ||
sections: sections.sort((a, b) => a.position - b.position), | ||
}; | ||
}); | ||
|
||
return { categories: categories.sort((a, b) => a.position - b.position) }; | ||
}; | ||
|
||
export const sideNav = { | ||
get: async (): Promise<SideNavData> => { | ||
const url = `${window.location.origin}/api/v2/help_center/en-us/articles.json?include=categories,sections&per_page=100`; | ||
|
||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Response status: ${response.status}`); | ||
} | ||
|
||
const responseData = await response.json(); | ||
|
||
if (responseData.page_count <= 1) { | ||
return sanitizeResponse(responseData); | ||
} | ||
|
||
// fetch the rest of the pages since first page is already fetched above. | ||
const pages = await Promise.all( | ||
Array.from({ length: responseData.page_count - 1 }, (_, i) => { | ||
return sideNav.getPage(i + 2); | ||
}) | ||
); | ||
|
||
const allPagesArticleResponseData = makeArrayToHaveUniqueValues( | ||
responseData.articles.concat( | ||
...pages.map((page: SideNavApiResponse | null) => page?.articles || []) | ||
) | ||
); | ||
const allPagesSectionResponseData = makeArrayToHaveUniqueValues( | ||
responseData.sections.concat( | ||
...pages.map((page: SideNavApiResponse | null) => page?.sections || []) | ||
) | ||
); | ||
const allPagesCategoryResponseData = makeArrayToHaveUniqueValues( | ||
responseData.categories.concat( | ||
...pages.map((page: SideNavApiResponse | null) => page?.categories || []) | ||
) | ||
); | ||
const allPagesResponseData = { | ||
...responseData, | ||
articles: allPagesArticleResponseData, | ||
sections: allPagesSectionResponseData, | ||
categories: allPagesCategoryResponseData, | ||
}; | ||
const sanitizedResponse = sanitizeResponse(allPagesResponseData); | ||
console.log(sanitizedResponse); | ||
return sanitizedResponse; | ||
} catch (error) { | ||
console.error(error); | ||
return null; | ||
} | ||
}, | ||
getPage: async (page: number): Promise<SideNavApiResponse | null> => { | ||
const url = `${window.location.origin}/api/v2/help_center/en-us/articles.json?include=categories,sections&page=${page}&per_page=100`; | ||
|
||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Response status: ${response.status}`); | ||
} | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
console.error(error); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { renderSideNav } from './renderSideNav'; | ||
export { sideNav } from './api'; |
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,18 @@ | ||
import { render } from 'react-dom'; | ||
import { SideNavData } from '../../lib/types'; | ||
import { Settings } from '../shared'; | ||
import { createTheme, ThemeProviders } from '../shared'; | ||
import { SideNav } from './SideNavModule'; | ||
|
||
export async function renderSideNav( | ||
settings: Settings, | ||
sideNavData: SideNavData, | ||
container: HTMLElement | ||
) { | ||
render( | ||
<ThemeProviders theme={createTheme(settings)}> | ||
<SideNav sideNavData={sideNavData} /> | ||
</ThemeProviders>, | ||
container | ||
); | ||
} |
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