Repository: mdn/kuma

Issue:
"Help us translate this document" link not working
**Summary**
<img width="1089" alt="Screen Shot 2020-02-19 at 9 29 52 AM" src="https://user-images.githubusercontent.com/26739/74843888-94cd3700-52fa-11ea-9407-d6cefd8651eb.png">


**Steps To Reproduce (STR)**

Try https://developer.mozilla.org/sv-SE/docs/Web/JavaScript/Reference/Functions
You might need to be signed in. 

**Actual behavior**
That the`Snälla hjälp oss att översätta denna artikel från engelska` text is a clicklable link. 


**Expected behavior**
It's not clickable because the `<a>` element lacks a `href`. 


**Additional context**
Seems to happen in FirefoxNightly and latest stable Chrome. 



Discussion:
What's curious is that this can happen [if `translateURL` is `null` or `undefined`](https://codesandbox.io/s/relaxed-austin-bppfy). Ok. 
Does that mean it's `null` or `undefined` in runtime only? I think so. 
I looked at the `window._react_data` and it looks like this:
<img width="483" alt="Screen Shot 2020-02-19 at 9 52 14 AM" src="https://user-images.githubusercontent.com/26739/74845918-ac59ef00-52fd-11ea-8119-5b483469b0c7.png">

So, in a sense this smells like a Django area bug. However, this totally breaks the "contract" of Flow that should have caught that earlier but obviously Flow isn't participating in the production bundle. So, ideally, perhaps we should put something like:

```javascript
if (!translateURL) {
    throw new Error('translateURL is falsy so can't make link to translate')
}
```
[somewhere here](https://github.com/mdn/kuma/blob/6dbcef5faafad14fb84175e92e0ebf1382ccf103/kuma/javascript/src/article.jsx#L28)
Actually, there's something stinky about the types and assumptions. The `DocumentData` Flow type says it's [supposed to be a string](https://github.com/mdn/kuma/blob/6dbcef5faafad14fb84175e92e0ebf1382ccf103/kuma/javascript/src/document.jsx#L32) and it's not marked as optional. Right. 
But in the document data on the server, it *can* be null if the document is NOT localizable. See: https://github.com/mdn/kuma/blob/6dbcef5faafad14fb84175e92e0ebf1382ccf103/kuma/api/v1/views.py#L167-L172

So, which one is right? The `document.jsx` or the `api/v1/views.py`?

A quick and filthy remedy is to do something like this in [`article.jsx`](https://github.com/mdn/kuma/blob/6dbcef5faafad14fb84175e92e0ebf1382ccf103/kuma/javascript/src/article.jsx#L24):

```javascript
if (!translateURL) {
    return null;
}
```

**But why is the document not localizable?** 
[It's not dynamically computed but it's a boolean set on the Document.](https://github.com/mdn/kuma/blob/6dbcef5faafad14fb84175e92e0ebf1382ccf103/kuma/wiki/models.py#L235)
So what does that mean? Why is it set to not be localizable?

By the way, [on the wiki](https://wiki.developer.mozilla.org/sv-SE/docs/Web/JavaScript/Reference/Functions) the link is working perfectly fine. 

From what it seems, the Wiki does NOT care about the `doc.is_localizable` boolean. It just makes [a link to the edit URL](https://github.com/mdn/kuma/blob/6dbcef5faafad14fb84175e92e0ebf1382ccf103/kuma/wiki/jinja2/wiki/document.html#L207-L208).

Why is this so different?
Child document being checked for is_localizable, when it shouldn't be. Parent doc should be checked. 


Resolving patch:
diff --git a/kuma/api/v1/views.py b/kuma/api/v1/views.py
--- a/kuma/api/v1/views.py
+++ b/kuma/api/v1/views.py
@@ -163,9 +163,13 @@ def document_api_data(doc=None, redirect_url=None):
             "hrefLang": doc.get_hreflang(available_locales),
             "absoluteURL": doc_absolute_url,
             "wikiURL": absolutify(doc_absolute_url, for_wiki_site=True),
+            "editURL": absolutify(
+                reverse("wiki.edit", args=(doc.slug,), locale=doc.locale),
+                for_wiki_site=True,
+            ),
             "translateURL": (
                 absolutify(
-                    reverse("wiki.select_locale", args=(doc.slug,), locale=doc.locale,),
+                    reverse("wiki.select_locale", args=(doc.slug,), locale=doc.locale),
                     for_wiki_site=True,
                 )
                 if doc.is_localizable


Test patch:
diff --git a/kuma/api/v1/tests/test_views.py b/kuma/api/v1/tests/test_views.py
--- a/kuma/api/v1/tests/test_views.py
+++ b/kuma/api/v1/tests/test_views.py
@@ -89,6 +89,10 @@ def test_doc_api(client, trans_doc):
     assert doc_data["wikiURL"] == absolutify(
         trans_doc.get_absolute_url(), for_wiki_site=True
     )
+    assert doc_data["editURL"] == absolutify(
+        reverse("wiki.edit", args=(trans_doc.slug,), locale=trans_doc.locale),
+        for_wiki_site=True,
+    )
     assert doc_data["translateURL"] is None
     assert doc_data["bodyHTML"] == trans_doc.get_body_html()
     assert doc_data["quickLinksHTML"] == trans_doc.get_quick_links_html()
diff --git a/kuma/javascript/src/document.test.js b/kuma/javascript/src/document.test.js
--- a/kuma/javascript/src/document.test.js
+++ b/kuma/javascript/src/document.test.js
@@ -25,6 +25,8 @@ export const fakeDocumentData = {
     translateURL: '[fake translate url]',
     wikiURL:
         'https://wiki.mdn.developer.mozilla.org/en-US/docs/Web/CSS/box-shadow',
+    editURL:
+        'https://wiki.mdn.developer.mozilla.org/en-US/docs/Web/CSS/box-shadow$edit',
     bodyHTML: '[fake body HTML]',
     quickLinksHTML: '[fake quicklinks HTML]',
     tocHTML: '[fake TOC HTML]',