-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adaptive cards work
- Loading branch information
Dan Wilson
authored
Nov 4, 2021
1 parent
f0c4109
commit 774d83a
Showing
14 changed files
with
4,226 additions
and
3,851 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Template } from 'adaptivecards-templating'; | ||
import axios from 'axios'; | ||
import classNames from 'classnames/bind'; | ||
import PropTypes from 'prop-types'; | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
import { useCard } from 'hooks/swr'; | ||
|
||
import AdaptiveCard from '../../../shared/adaptive-card'; | ||
|
||
import css from './card.module.scss'; | ||
|
||
const cx = classNames.bind(css); | ||
|
||
const loadAdaptiveCard = async (url, json, setAdaptiveCard) => { | ||
const schema = await axios({ method: 'GET', url }); | ||
const template = new Template(schema?.data); | ||
const cardPayload = await template.expand({ | ||
$root: { | ||
...json, | ||
}, | ||
}); | ||
setAdaptiveCard(cardPayload); | ||
}; | ||
|
||
const Card = ({ | ||
data, namespace, name, build, | ||
}) => { | ||
const { | ||
data: json, isError: jsonIsError, isLoading: jsonIsLoading, | ||
} = useCard({ | ||
namespace, name, build, stage: data.stage, step: data.step, | ||
}); | ||
|
||
const [adaptiveCard, setAdaptiveCard] = useState(null); | ||
|
||
useEffect(() => { | ||
if (!jsonIsLoading && !adaptiveCard) { | ||
loadAdaptiveCard(data.schema, json, setAdaptiveCard); | ||
} | ||
}, [jsonIsLoading, data, json, adaptiveCard]); | ||
|
||
if (jsonIsLoading || !adaptiveCard) { | ||
return <li className={cx('card', 'card-message')}>CARD LOADING...</li>; | ||
} | ||
if (jsonIsError) { | ||
return <li className={cx('card', 'card-message')}>CARD ERROR</li>; | ||
} | ||
return <li className={cx('card')}><AdaptiveCard payload={adaptiveCard} /></li>; | ||
}; | ||
|
||
Card.propTypes = { | ||
namespace: PropTypes.string.isRequired, | ||
name: PropTypes.string.isRequired, | ||
build: PropTypes.string.isRequired, | ||
data: PropTypes.shape({ | ||
schema: PropTypes.string.isRequired, | ||
stage: PropTypes.number.isRequired, | ||
step: PropTypes.number.isRequired, | ||
}).isRequired, | ||
}; | ||
|
||
export default Card; |
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 @@ | ||
.card { | ||
background-color: white; | ||
display: flex; | ||
align-items: center; | ||
margin: 0 0 1rem 0; | ||
border: 1px lightgrey solid; | ||
|
||
&-message { | ||
padding: 1rem; | ||
} | ||
} |
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,46 @@ | ||
import classNames from 'classnames/bind'; | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
|
||
import Card from './card'; | ||
import css from './cards-view.module.scss'; | ||
|
||
const cx = classNames.bind(css); | ||
|
||
const CardsView = ({ | ||
data, isDataLoading, namespace, name, build, | ||
}) => { | ||
if (isDataLoading) { | ||
return <div className={cx('message')}>LOADING...</div>; | ||
} if (!data.length) { | ||
return <div className={cx('card-list')}><span className={cx('message')}>NO ADAPTIVE CARDS FOUND</span></div>; | ||
} | ||
return ( | ||
<ul className={cx('card-list')}> | ||
{data?.map((cardData) => { | ||
// causes a weird error when I use template literals... | ||
// eslint-disable-next-line prefer-template | ||
const key = cardData.stage.toString() + ' - ' + cardData.step.toString(); | ||
return <Card key={key} data={cardData} namespace={namespace} name={name} build={build} />; | ||
})} | ||
</ul> | ||
); | ||
}; | ||
|
||
CardsView.propTypes = { | ||
data: PropTypes.arrayOf(PropTypes.shape({ | ||
schema: PropTypes.string.isRequired, | ||
stage: PropTypes.number.isRequired, | ||
step: PropTypes.number.isRequired, | ||
})), | ||
isDataLoading: PropTypes.bool.isRequired, | ||
namespace: PropTypes.string.isRequired, | ||
name: PropTypes.string.isRequired, | ||
build: PropTypes.string.isRequired, | ||
}; | ||
|
||
CardsView.defaultProps = { | ||
data: [], | ||
}; | ||
|
||
export default CardsView; |
12 changes: 12 additions & 0 deletions
12
src/components/pages/build/cards-view/cards-view.module.scss
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,12 @@ | ||
.card-list { | ||
margin: 1rem 2rem; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
|
||
.message { | ||
padding: 1rem; | ||
background-color: white; | ||
border: 1px lightgrey solid; | ||
} |
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 @@ | ||
export { default } from './cards-view'; |
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,25 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
import { AdaptiveCard } from 'adaptivecards'; | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
|
||
const AdaptiveCardComponent = ({ payload }) => { | ||
const adaptiveCard = new AdaptiveCard(); | ||
|
||
adaptiveCard.parse(payload); | ||
const result = adaptiveCard.render(); | ||
return ( | ||
<div | ||
ref={(n) => { | ||
n && n.firstChild && n.removeChild(n.firstChild); | ||
n && n.appendChild(result); | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
AdaptiveCardComponent.propTypes = { | ||
payload: PropTypes.shape().isRequired, | ||
}; | ||
|
||
export default AdaptiveCardComponent; |
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 @@ | ||
export { default } from './adaptive-card'; |
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,5 @@ | ||
import useSWRBase from './use-swr-base'; | ||
|
||
export const useCard = ({ | ||
namespace, name, build, stage, step, | ||
}) => useSWRBase(`/api/repos/${namespace}/${name}/cards/${build}/${stage}/${step}`); |
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.