Migrating drafts apis

This commit is contained in:
Nouman Tahir 2021-06-29 17:02:56 +05:00
parent f44da57529
commit 2b444d6354
3 changed files with 70 additions and 90 deletions

View File

@ -26,98 +26,82 @@ export const getCurrencyTokenRate = (currency, token) =>
return 0;
});
/**
* @params username
*/
export const getDrafts = (username) =>
ecencyApi
.post('/private-api/drafts')
.then((resp) => resp.data)
.catch((error) => {
bugsnag.notify(error);
reject(error);
});
/*export const getDrafts = data =>
new Promise((resolve, reject) => {
api
.get(`/drafts/${data}`)
.then(res => {
resolve(res.data);
})
.catch(error => {
bugsnag.notify(error);
reject(error);
});
});
*/
/**
* @params username
* @params draftID
*/
export const removeDraft = (username, id) =>
new Promise((resolve, reject) => {
api
.delete(`/drafts/${username}/${id}`)
.then((res) => {
resolve(res.data);
})
.catch((error) => {
bugsnag.notify(error);
reject(error);
});
});
/**
* @params username
* @params body
* returns list of saved drafts on ecency server
*/
export const getDrafts = async () => {
try{
const res = await ecencyApi.post('/private-api/drafts');
return res.data;
}catch(error){
bugsnag.notify(error);
throw error;
}
}
/**
* @params draftId
*/
export const removeDraft = async (draftId:string) => {
try{
const data = { id:draftId }
const res = await ecencyApi.post(`/private-api/drafts-delete`, data);
return res.data
}catch(error){
bugsnag.notify(error);
throw error;
}
}
/**
* @params title
* @params body
* @params tags
*/
export const addDraft = (data) =>
new Promise((resolve, reject) => {
api
.post('/draft', data)
.then((res) => {
const { drafts } = res.data;
if (drafts) {
resolve(drafts.pop());
} else {
reject(new Error('No drafts returned in response'));
}
})
.catch((error) => {
bugsnag.notify(error);
reject(error);
});
});
export const addDraft = async (title:string, body:string, tags:string) => {
try {
const data = { title, body, tags }
const res = await ecencyApi.post('/private-api/drafts-add', data)
const { drafts } = res.data;
if (drafts) {
return drafts.pop(); //return recently saved last draft in the list
} else {
throw new Error('No drafts returned in response');
}
} catch(error){
bugsnag.notify(error);
throw error;
}
}
/**
* @params username
* @params body
* @params draftId
* @params title
* @params body
* @params tags
*/
export const updateDraft = (data) =>
new Promise((resolve, reject) => {
api
.put(`/drafts/${data.username}/${data.draftId}`, {
title: data.title,
body: data.body,
tags: data.tags,
})
.then((res) => {
if (res.data) {
resolve(res.data);
} else {
reject(new Error('No data retuend in update response'));
}
})
.catch((error) => {
bugsnag.notify(error);
reject(error);
});
});
export const updateDraft = async (draftId:string, title:string, body:string, tags:string) => {
try {
const data = {id:draftId, title, body, tags }
const res = await ecencyApi.post(`/private-api/drafts-update`, data)
if(res.data){
return res.data
} else {
throw new Error("No data returned in response")
}
} catch(error){
bugsnag.notify(error);
throw error;
}
};
export const addBookmark = (username, author, permlink) =>
api

View File

@ -52,7 +52,7 @@ const DraftsContainer = ({ currentAccount, intl, navigation, dispatch }) => {
const _getDrafts = () => {
setIsLoading(true);
getDrafts(currentAccount.name)
getDrafts()
.then((data) => {
setDrafts(_sortData(data));
setIsLoading(false);
@ -64,7 +64,7 @@ const DraftsContainer = ({ currentAccount, intl, navigation, dispatch }) => {
};
const _removeDraft = (id) => {
removeDraft(currentAccount.name, id)
removeDraft(id)
.then(() => {
const newDrafts = [...drafts].filter((draft) => draft._id !== id);
setDrafts(_sortData(newDrafts));

View File

@ -440,7 +440,6 @@ class EditorContainer extends Component {
try {
if (!isDraftSaved) {
const username = get(currentAccount, 'name', '');
let draftField;
if (this._isMounted) {
@ -453,16 +452,12 @@ class EditorContainer extends Component {
draftField = {
...fields,
tags: fields.tags.join(' '),
username,
};
}
//update draft is draftId is present
if (draftId && draftField) {
await updateDraft({
...draftField,
draftId,
});
await updateDraft(draftId, draftField.title, draftField.body, draftField.tags);
if (this._isMounted) {
this.setState({
@ -474,7 +469,7 @@ class EditorContainer extends Component {
//create new darft otherwise
else if (draftField) {
const response = await addDraft(draftField);
const response = await addDraft(draftField.title, draftField.body, draftField.tags);
if (this._isMounted) {
this.setState({
@ -485,6 +480,7 @@ class EditorContainer extends Component {
}
//clear local copy is darft save is successful
const username = get(currentAccount, 'name', '');
setDraftPost(
{
title: '',