TopSiteForm.jsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import {actionCreators as ac, actionTypes as at} from "common/Actions.jsm";
  2. import {A11yLinkButton} from "content-src/components/A11yLinkButton/A11yLinkButton";
  3. import {FormattedMessage} from "react-intl";
  4. import React from "react";
  5. import {TOP_SITES_SOURCE} from "./TopSitesConstants";
  6. import {TopSiteFormInput} from "./TopSiteFormInput";
  7. import {TopSiteLink} from "./TopSite";
  8. export class TopSiteForm extends React.PureComponent {
  9. constructor(props) {
  10. super(props);
  11. const {site} = props;
  12. this.state = {
  13. label: site ? (site.label || site.hostname) : "",
  14. url: site ? site.url : "",
  15. validationError: false,
  16. customScreenshotUrl: site ? site.customScreenshotURL : "",
  17. showCustomScreenshotForm: site ? site.customScreenshotURL : false,
  18. };
  19. this.onClearScreenshotInput = this.onClearScreenshotInput.bind(this);
  20. this.onLabelChange = this.onLabelChange.bind(this);
  21. this.onUrlChange = this.onUrlChange.bind(this);
  22. this.onCancelButtonClick = this.onCancelButtonClick.bind(this);
  23. this.onClearUrlClick = this.onClearUrlClick.bind(this);
  24. this.onDoneButtonClick = this.onDoneButtonClick.bind(this);
  25. this.onCustomScreenshotUrlChange = this.onCustomScreenshotUrlChange.bind(this);
  26. this.onPreviewButtonClick = this.onPreviewButtonClick.bind(this);
  27. this.onEnableScreenshotUrlForm = this.onEnableScreenshotUrlForm.bind(this);
  28. this.validateUrl = this.validateUrl.bind(this);
  29. }
  30. onLabelChange(event) {
  31. this.setState({"label": event.target.value});
  32. }
  33. onUrlChange(event) {
  34. this.setState({
  35. url: event.target.value,
  36. validationError: false,
  37. });
  38. }
  39. onClearUrlClick() {
  40. this.setState({
  41. url: "",
  42. validationError: false,
  43. });
  44. }
  45. onEnableScreenshotUrlForm() {
  46. this.setState({showCustomScreenshotForm: true});
  47. }
  48. _updateCustomScreenshotInput(customScreenshotUrl) {
  49. this.setState({
  50. customScreenshotUrl,
  51. validationError: false,
  52. });
  53. this.props.dispatch({type: at.PREVIEW_REQUEST_CANCEL});
  54. }
  55. onCustomScreenshotUrlChange(event) {
  56. this._updateCustomScreenshotInput(event.target.value);
  57. }
  58. onClearScreenshotInput() {
  59. this._updateCustomScreenshotInput("");
  60. }
  61. onCancelButtonClick(ev) {
  62. ev.preventDefault();
  63. this.props.onClose();
  64. }
  65. onDoneButtonClick(ev) {
  66. ev.preventDefault();
  67. if (this.validateForm()) {
  68. const site = {url: this.cleanUrl(this.state.url)};
  69. const {index} = this.props;
  70. if (this.state.label !== "") {
  71. site.label = this.state.label;
  72. }
  73. if (this.state.customScreenshotUrl) {
  74. site.customScreenshotURL = this.cleanUrl(this.state.customScreenshotUrl);
  75. } else if (this.props.site && this.props.site.customScreenshotURL) {
  76. // Used to flag that previously cached screenshot should be removed
  77. site.customScreenshotURL = null;
  78. }
  79. this.props.dispatch(ac.AlsoToMain({
  80. type: at.TOP_SITES_PIN,
  81. data: {site, index},
  82. }));
  83. this.props.dispatch(ac.UserEvent({
  84. source: TOP_SITES_SOURCE,
  85. event: "TOP_SITES_EDIT",
  86. action_position: index,
  87. }));
  88. this.props.onClose();
  89. }
  90. }
  91. onPreviewButtonClick(event) {
  92. event.preventDefault();
  93. if (this.validateForm()) {
  94. this.props.dispatch(ac.AlsoToMain({
  95. type: at.PREVIEW_REQUEST,
  96. data: {url: this.cleanUrl(this.state.customScreenshotUrl)},
  97. }));
  98. this.props.dispatch(ac.UserEvent({
  99. source: TOP_SITES_SOURCE,
  100. event: "PREVIEW_REQUEST",
  101. }));
  102. }
  103. }
  104. cleanUrl(url) {
  105. // If we are missing a protocol, prepend http://
  106. if (!url.startsWith("http:") && !url.startsWith("https:")) {
  107. return `http://${url}`;
  108. }
  109. return url;
  110. }
  111. _tryParseUrl(url) {
  112. try {
  113. return new URL(url);
  114. } catch (e) {
  115. return null;
  116. }
  117. }
  118. validateUrl(url) {
  119. const validProtocols = ["http:", "https:"];
  120. const urlObj = this._tryParseUrl(url) || this._tryParseUrl(this.cleanUrl(url));
  121. return urlObj && validProtocols.includes(urlObj.protocol);
  122. }
  123. validateCustomScreenshotUrl() {
  124. const {customScreenshotUrl} = this.state;
  125. return !customScreenshotUrl || this.validateUrl(customScreenshotUrl);
  126. }
  127. validateForm() {
  128. const validate = this.validateUrl(this.state.url) && this.validateCustomScreenshotUrl();
  129. if (!validate) {
  130. this.setState({validationError: true});
  131. }
  132. return validate;
  133. }
  134. _renderCustomScreenshotInput() {
  135. const {customScreenshotUrl} = this.state;
  136. const requestFailed = this.props.previewResponse === "";
  137. const validationError = (this.state.validationError && !this.validateCustomScreenshotUrl()) || requestFailed;
  138. // Set focus on error if the url field is valid or when the input is first rendered and is empty
  139. const shouldFocus = (validationError && this.validateUrl(this.state.url)) || !customScreenshotUrl;
  140. const isLoading = this.props.previewResponse === null &&
  141. customScreenshotUrl && this.props.previewUrl === this.cleanUrl(customScreenshotUrl);
  142. if (!this.state.showCustomScreenshotForm) {
  143. return (<A11yLinkButton onClick={this.onEnableScreenshotUrlForm} className="enable-custom-image-input">
  144. <FormattedMessage id="topsites_form_use_image_link" />
  145. </A11yLinkButton>);
  146. }
  147. return (<div className="custom-image-input-container">
  148. <TopSiteFormInput
  149. errorMessageId={requestFailed ? "topsites_form_image_validation" : "topsites_form_url_validation"}
  150. loading={isLoading}
  151. onChange={this.onCustomScreenshotUrlChange}
  152. onClear={this.onClearScreenshotInput}
  153. shouldFocus={shouldFocus}
  154. typeUrl={true}
  155. value={customScreenshotUrl}
  156. validationError={validationError}
  157. titleId="topsites_form_image_url_label"
  158. placeholderId="topsites_form_url_placeholder"
  159. intl={this.props.intl} />
  160. </div>);
  161. }
  162. render() {
  163. const {customScreenshotUrl} = this.state;
  164. const requestFailed = this.props.previewResponse === "";
  165. // For UI purposes, editing without an existing link is "add"
  166. const showAsAdd = !this.props.site;
  167. const previous = (this.props.site && this.props.site.customScreenshotURL) || "";
  168. const changed = customScreenshotUrl && this.cleanUrl(customScreenshotUrl) !== previous;
  169. // Preview mode if changes were made to the custom screenshot URL and no preview was received yet
  170. // or the request failed
  171. const previewMode = changed && !this.props.previewResponse;
  172. const previewLink = Object.assign({}, this.props.site);
  173. if (this.props.previewResponse) {
  174. previewLink.screenshot = this.props.previewResponse;
  175. previewLink.customScreenshotURL = this.props.previewUrl;
  176. }
  177. // Handles the form submit so an enter press performs the correct action
  178. const onSubmit = previewMode ? this.onPreviewButtonClick : this.onDoneButtonClick;
  179. return (
  180. <form className="topsite-form" onSubmit={onSubmit}>
  181. <div className="form-input-container">
  182. <h3 className="section-title">
  183. <FormattedMessage id={showAsAdd ? "topsites_form_add_header" : "topsites_form_edit_header"} />
  184. </h3>
  185. <div className="fields-and-preview">
  186. <div className="form-wrapper">
  187. <TopSiteFormInput onChange={this.onLabelChange}
  188. value={this.state.label}
  189. titleId="topsites_form_title_label"
  190. placeholderId="topsites_form_title_placeholder"
  191. intl={this.props.intl} />
  192. <TopSiteFormInput onChange={this.onUrlChange}
  193. shouldFocus={this.state.validationError && !this.validateUrl(this.state.url)}
  194. value={this.state.url}
  195. onClear={this.onClearUrlClick}
  196. validationError={this.state.validationError && !this.validateUrl(this.state.url)}
  197. titleId="topsites_form_url_label"
  198. typeUrl={true}
  199. placeholderId="topsites_form_url_placeholder"
  200. errorMessageId="topsites_form_url_validation"
  201. intl={this.props.intl} />
  202. {this._renderCustomScreenshotInput()}
  203. </div>
  204. <TopSiteLink link={previewLink}
  205. defaultStyle={requestFailed}
  206. title={this.state.label} />
  207. </div>
  208. </div>
  209. <section className="actions">
  210. <button className="cancel" type="button" onClick={this.onCancelButtonClick} >
  211. <FormattedMessage id="topsites_form_cancel_button" />
  212. </button>
  213. {previewMode ?
  214. <button className="done preview" type="submit" >
  215. <FormattedMessage id="topsites_form_preview_button" />
  216. </button> :
  217. <button className="done" type="submit" >
  218. <FormattedMessage id={showAsAdd ? "topsites_form_add_button" : "topsites_form_save_button"} />
  219. </button>}
  220. </section>
  221. </form>
  222. );
  223. }
  224. }
  225. TopSiteForm.defaultProps = {
  226. site: null,
  227. index: -1,
  228. };