Guest User

Untitled

a guest
May 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. import { HtmlHTMLAttributes, ReactHTML } from "react";
  2.  
  3. export interface INewsPics {
  4. Title: any;
  5. FileLeafRef: any;
  6. Id: any;
  7. BT_News_Publishing_Date: any;
  8. PublishingImage: any;
  9. NewsArticle: any;
  10. }
  11.  
  12. export default class LatestNews extends React.Component<ILatestNewsProps, any> {
  13.  
  14. constructor() {
  15. super();
  16. this.state = {
  17. newsst: []
  18. };
  19. }
  20. // tslint:disable-next-line:typedef
  21. public componentDidMount() {
  22. // read items from json func
  23. this._getNewsFeed();
  24. }
  25. // tslint:disable-next-line:typedef
  26. public componentDidUpdate(prevProps, prevState) {
  27. if (this.props.NewsCounter !== prevProps.NewsCounter ||
  28. this.props.listName !== prevProps.listName)
  29. // tslint:disable-next-line:one-line
  30. {
  31. this._getNewsFeed(); // if wp props settings compare were changed previous state and reload
  32. }
  33. }
  34.  
  35. public render(): React.ReactElement<ILatestNewsProps> {
  36. return (
  37. <div className="ArticleNewsWp">
  38. <NewsCard header={this.props.title}
  39. list={this.props.listName}
  40. surl={this.props.siteurl}
  41. CharCounter={this.props.CharCounter}
  42. items={this.state.newsst} />
  43. </div>
  44. );
  45. }
  46.  
  47. public _getNewsFeed(options?: any): Promise<INewsPics[]> {
  48. return new Promise<INewsPics[]>((resolve: any) => {
  49. pnp.setup({
  50. sp: {
  51. headers: {
  52. "Accept": "application/json; odata=verbose"
  53. }
  54. }
  55. });
  56.  
  57. var SHAREPOINT_LIST: string = this.props.listName;
  58. var SHAREPOINT_MAX: string = this.props.NewsCounter;
  59. pnp.sp.web.lists.getByTitle(SHAREPOINT_LIST).items
  60. // tslint:disable-next-line:max-line-length
  61. .filter("ContentType eq 'BT_News_Feeds'")
  62. .top(Number(SHAREPOINT_MAX))
  63. .orderBy("BT_News_Publishing_Date", false)
  64. .get().then((response: any) => {
  65. let items: INewsPics[] = [];
  66. let promises: Promise<any>[] = [];
  67. // tslint:disable-next-line:typedef
  68. response.forEach(function (item: INewsPics) {
  69. // tslint:disable-next-line:max-line-length
  70. // tslint:disable-next-line:typedef
  71. let promise = pnp.sp.web.lists.getByTitle(SHAREPOINT_LIST).items
  72. .getById(item.Id).fieldValuesAsHTML
  73. // tslint:disable-next-line:max-line-length
  74. .select("Id", "Title", "FileLeafRef", "BT_News_Image", "BT_News_Publishing_Date", "BT_News_Article").get();
  75. promises.push(promise);
  76. promise.then((response: any) => {
  77. // tslint:disable-next-line:max-line-length
  78. items.push({ Id: item.Id, Title: item.Title, FileLeafRef: item.FileLeafRef, PublishingImage: item.PublishingImage, BT_News_Publishing_Date: item.BT_News_Publishing_Date, NewsArticle: item.NewsArticle });
  79. });
  80. console.log(item.BT_News_Publishing_Date);
  81. });
  82. Promise.all(promises).then((items: INewsPics[]) => {
  83. this.setState({ newsst: items });
  84. resolve(items);
  85. console.log(items);
  86. });
  87. });
  88. });
  89. }
  90. }
  91.  
  92. export class NewsCard extends React.Component<any, any> {
  93. // tslint:disable-next-line:typedef
  94. private _redirectToPage(event, itemId, SHAREPOINT_LIST) {
  95. try {
  96. location.href = this.props.surl + "/" + SHAREPOINT_LIST + "/" + itemId;
  97. console.log(location.href);
  98. }
  99. // tslint:disable-next-line:one-line
  100. catch (e) {
  101. if (itemId === "undefined")
  102. // tslint:disable-next-line:one-line
  103. {
  104. location.href = this.props.surl + "/" + SHAREPOINT_LIST + "/" + "PageNotFoundError.aspx";
  105. console.log(e);
  106. }
  107. }
  108. }
  109.  
  110. public render(): JSX.Element {
  111.  
  112. // tslint:disable-next-line:typedef
  113. var regex = /<img.*?src=['"](.*?)['"]/;
  114. var SL: string = this.props.list;
  115. var CH: string = this.props.CharCounter;
  116. // tslint:disable-next-line:typedef
  117. var finalArticle;
  118. var col: any = (this.props.items) ? this.props.items : [];
  119. return (
  120. <div className={styles.container}>
  121. <h3>{this.props.header}</h3>
  122. {
  123. col.map((item: INewsPics) => {
  124. console.log("Publishing date:" + item.BT_News_Publishing_Date + " Title:" + item.Title);
  125. // tslint:disable-next-line:typedef
  126. var itemstring: any = (item.PublishingImage).toString();
  127. // tslint:disable-next-line:typedef
  128. var itemstringArticle: any = (item.NewsArticle).toString();
  129. // tslint:disable-next-line:typedef
  130. function strip(itemstringArticle) {
  131. // tslint:disable-next-line:typedef
  132. var resRndrArticle = new DOMParser().parseFromString(itemstringArticle, "text/html");
  133. return resRndrArticle.body.textContent || "";
  134. }
  135. // tslint:disable-next-line:typedef
  136. finalArticle = strip(itemstringArticle);
  137. finalArticle = finalArticle.substring(0, CH);
  138. return (
  139. <div className={styles.container}>
  140. <div className={styles.stapler}>
  141. <div className={styles.NewsLeft}>
  142. <img className={styles.newsimage} src={regex.exec(itemstring)[1]} alt="" />
  143. </div>
  144. <div className={styles.NewsRight}>
  145. <b>{item.Title}</b>
  146. <p>{finalArticle}
  147. <b className={styles.Rm} onClick={(event) =>
  148. this._redirectToPage(event, item.FileLeafRef, SL)}> [... More]</b></p>
  149. <b className={styles.NDate}>Article Date: {item.BT_News_Publishing_Date}</b>
  150. </div>
  151. </div>
  152. <hr/>
  153. </div>
  154. );
  155. })
  156. }
  157. </div>
  158. );
  159. }
  160. }
Add Comment
Please, Sign In to add comment