migrated fragment/snippets api

This commit is contained in:
Nouman Tahir 2021-07-05 13:48:21 +05:00
parent 41bab247d8
commit 3463b55fcb
4 changed files with 82 additions and 71 deletions

View File

@ -465,7 +465,6 @@ const MarkdownEditorView = ({
style={styles.modalStyle}
>
<SnippetsModal
username={currentAccount.username}
handleOnSelect={_handleOnSnippetReceived}
/>
</Modal>

View File

@ -4,7 +4,7 @@ import { Alert, KeyboardAvoidingView, Platform, View } from 'react-native';
import { TextInput } from '..';
import { ThemeContainer } from '../../containers';
import { Snippet } from '../../models';
import { addSnippet, updateSnippet } from '../../providers/ecency/ecency';
import { addFragment, updateFragment} from '../../providers/ecency/ecency';
import { TextButton } from '../buttons';
import Modal from '../modal';
import styles from './snippetEditorModalStyles';
@ -16,11 +16,10 @@ export interface SnippetEditorModalRef {
}
interface SnippetEditorModalProps {
username:string;
onSnippetsUpdated:(snips:Array<Snippet>)=>void;
}
const SnippetEditorModal = ({username, onSnippetsUpdated}: SnippetEditorModalProps, ref) => {
const SnippetEditorModal = ({onSnippetsUpdated}: SnippetEditorModalProps, ref) => {
const intl = useIntl();
const titleInputRef = useRef(null);
const bodyInputRef = useRef(null);
@ -59,12 +58,12 @@ const SnippetEditorModal = ({username, onSnippetsUpdated}: SnippetEditorModalPro
let response = [];
if(!isNewSnippet){
console.log("Updating snippet:", username, snippetId, title, body)
response = await updateSnippet(username, snippetId, title, body)
console.log("Updating snippet:", snippetId, title, body)
response = await updateFragment(snippetId, title, body);
console.log("Response from add snippet: ", response)
}else{
console.log("Saving snippet:", username, title, body)
const res = await addSnippet(username, title, body)
console.log("Saving snippet:", title, body)
const res = await addFragment(title, body)
response = res && res.fragments
console.log("Response from add snippet: ", response)
}

View File

@ -1,28 +1,29 @@
import React, { useState, useEffect, useRef } from 'react';
import { View, FlatList, Text, TouchableOpacity, Alert } from 'react-native';
import { useIntl } from 'react-intl';
import { getSnippets, removeSnippet } from '../../providers/ecency/ecency';
import { getFragments, deleteFragment } from '../../providers/ecency/ecency';
import { MainButton } from '..';
import styles from './snippetsModalStyles';
import { RefreshControl } from 'react-native';
import SnippetEditorModal, { SnippetEditorModalRef } from '../snippetEditorModal/snippetEditorModal';
import SnippetItem from './snippetItem';
import { Snippet } from '../../models';
import { useAppSelector } from '../../hooks';
interface SnippetsModalProps {
username:string,
handleOnSelect:(snippetText:string)=>void,
}
const SnippetsModal = ({ username, handleOnSelect }:SnippetsModalProps) => {
const SnippetsModal = ({ handleOnSelect }:SnippetsModalProps) => {
const editorRef = useRef<SnippetEditorModalRef>(null);
const intl = useIntl();
console.log('username', username);
const isLoggedIn = useAppSelector(state => state.application.isLoggedIn)
const [snippets, setSnippets] = useState([]);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
console.log(username);
_getSnippets();
}, []);
@ -31,13 +32,13 @@ const SnippetsModal = ({ username, handleOnSelect }:SnippetsModalProps) => {
//fetch snippets from server
const _getSnippets = async () => {
try{
if (username) {
setIsLoading(true);
const snips = await getSnippets(username)
const snips = await getFragments()
console.log("snips received", snips)
setSnippets(snips);
setIsLoading(false);
}
}catch(err){
console.warn("Failed to get snippets")
setIsLoading(false);
@ -47,12 +48,12 @@ const SnippetsModal = ({ username, handleOnSelect }:SnippetsModalProps) => {
//removes snippet from users snippet collection on user confirmation
const _removeSnippet = async (id:string) => {
try{
if (username) {
setIsLoading(true);
const snips = await removeSnippet(username, id)
const snips = await deleteFragment(id)
setSnippets(snips);
setIsLoading(false);
}
}catch(err){
console.warn("Failed to get snippets")
setIsLoading(false);
@ -118,6 +119,10 @@ const SnippetsModal = ({ username, handleOnSelect }:SnippetsModalProps) => {
//renders footer with add snipept button and shows new snippet modal
const _renderFloatingButton = () => {
if(!isLoggedIn){
return null;
}
const _onPress = () => {
if(editorRef.current){
editorRef.current.showNewModal();
@ -160,7 +165,6 @@ const SnippetsModal = ({ username, handleOnSelect }:SnippetsModalProps) => {
<SnippetEditorModal
ref={editorRef}
username={username}
onSnippetsUpdated={setSnippets}
/>
</View>

View File

@ -221,68 +221,77 @@ export const removeFavorite = (currentUsername, targetUsername) =>
* ************************************
*/
/**
* @params current username
*/
export const getSnippets = (username) =>
api
.get(`/fragments/${username}`)
.then((resp) => resp.data)
.catch((error) => bugsnag.notify(error));
/**
* @params current username
* Fetches all saved user fragments/snippets from ecency
* @returns array of fragments
*/
export const getFragments = async () => {
try {
const response = await ecencyApi.post(`/private-api/fragments`);
return response.data;
} catch(error) {
console.warn("Failed to get fragments", error);
bugsnag.notify(error)
throw error;
}
}
/**
* Adds new fragment/snippets to user's saved fragments/snippets
* @params title title
* @params body body
* @returns array of fragments
*/
export const addSnippet = (currentUsername, title, body) =>
api
.post('/fragment', {
username: currentUsername,
title,
body,
})
.then((resp) => resp.data)
.catch((error) => bugsnag.notify(error));
export const addFragment = async (title: string, body: string) => {
try{
const data = { title, body };
const response = await ecencyApi.post(`/private-api/fragments-add`, data);
return response.data;
} catch(error) {
console.warn("Failed to add fragment", error);
bugsnag.notify(error)
throw error;
}
}
/**
* @params current username
* @params fragmentid id
* @params title title
* @params body body
* Updates a fragment content using fragment id
* @params fragmentId
* @params title
* @params body
* @returns array of fragments
*/
export const updateSnippet = (username, id, title, body) =>
new Promise((resolve, reject) => {
api
.put(`/fragments/${username}/${id}`, {
title,
body,
})
.then((res) => {
resolve(res.data);
})
.catch((error) => {
bugsnag.notify(error);
reject(error);
});
});
export const updateFragment = async (fragmentId:string, title: string, body: string) => {
try{
const data = { id:fragmentId, title, body };
const response = await ecencyApi.post(`/private-api/fragments-update`, data);
return response.data;
} catch(error) {
console.warn("Failed to update fragment", error);
bugsnag.notify(error)
throw error;
}
}
/**
* @params current username
* @params fragmentid id
* Deletes user saved fragment using specified fragment id
* @params fragmentId
* @returns array of fragments
*/
export const removeSnippet = (username, id) =>
new Promise((resolve, reject) => {
api
.delete(`/fragments/${username}/${id}`)
.then((res) => {
resolve(res.data);
})
.catch((error) => {
bugsnag.notify(error);
reject(error);
});
});
export const deleteFragment = async (fragmentId:string) => {
try{
const data = { id:fragmentId };
const response = await ecencyApi.post(`/private-api/fragments-delete`, data);
return response.data;
} catch(error) {
console.warn("Failed to delete fragment", error);
bugsnag.notify(error)
throw error;
}
}