docbrown/app/javascript/article-form/actions.js
Ali Spittel 704b7bce9c Feature/autosave editor (#1633)
* Add myself to core contributors

* add myself to the core contributors

* implement autosave in sessionStorage

* clear sessionstorage on save

* add new changes flag

* add clear  button and css for it

* add clear button functionality

* make clear session storage only run if save is successful

* update jest snapshot for autosave

* add a few tests

* add a few tests

* add test for clear button

* make autosave work for title, tags, and cover image

* remove console.log

* make clear button into a message instead

* move to button with no styling instead of span

* remove warning on refresh

* remove warning on refresh

* move to localstorage so that persists across tabs

* fix blip on change from HTML to JSX

* make confirm work properly

* fix css jump for v2 editor

* fix v2 editor height jump

* remove unused onKeyUp prop

* indent _v2_form.html.erb file

* indent v2 editor erb

* Update package.json with missing package
2019-02-28 07:00:58 -08:00

71 lines
1.8 KiB
JavaScript

export function previewArticle(payload, successCb, failureCb) {
fetch('/articles/preview', {
method: 'POST',
headers: {
Accept: 'application/json',
'X-CSRF-Token': window.csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({
article_body: payload,
}),
credentials: 'same-origin',
})
.then(response => response.json())
.then(successCb)
.catch(failureCb);
}
export function getArticle() {}
export function submitArticle(payload, clearStorage, errorCb, failureCb) {
const method = payload.id ? 'PUT' : 'POST';
const url = payload.id ? `/api/articles/${payload.id}` : '/api/articles';
fetch(url, {
method,
headers: {
Accept: 'application/json',
'X-CSRF-Token': window.csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({
article: payload,
}),
credentials: 'same-origin',
})
.then(response => response.json())
.then(response => {
if (response.current_state_path) {
clearStorage();
window.location.replace(response.current_state_path);
} else {
errorCb(response);
}
})
.catch(failureCb);
}
export function generateMainImage(payload, successCb, failureCb) {
fetch('/image_uploads', {
method: 'POST',
headers: {
'X-CSRF-Token': csrfToken,
},
body: generateUploadFormdata(payload),
credentials: 'same-origin',
})
.then(response => response.json())
.then(successCb)
.catch(failureCb);
}
function generateUploadFormdata(payload) {
const token = window.csrfToken;
const formData = new FormData();
formData.append('authenticity_token', token);
formData.append('image', payload.image[0]);
if (payload.wrap_cloudinary) {
formData.append('wrap_cloudinary', 'true');
}
return formData;
}