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} style={styles.modalStyle}
> >
<SnippetsModal <SnippetsModal
username={currentAccount.username}
handleOnSelect={_handleOnSnippetReceived} handleOnSelect={_handleOnSnippetReceived}
/> />
</Modal> </Modal>

View File

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

View File

@ -1,28 +1,29 @@
import React, { useState, useEffect, useRef } from 'react'; import React, { useState, useEffect, useRef } from 'react';
import { View, FlatList, Text, TouchableOpacity, Alert } from 'react-native'; import { View, FlatList, Text, TouchableOpacity, Alert } from 'react-native';
import { useIntl } from 'react-intl'; import { useIntl } from 'react-intl';
import { getSnippets, removeSnippet } from '../../providers/ecency/ecency'; import { getFragments, deleteFragment } from '../../providers/ecency/ecency';
import { MainButton } from '..'; import { MainButton } from '..';
import styles from './snippetsModalStyles'; import styles from './snippetsModalStyles';
import { RefreshControl } from 'react-native'; import { RefreshControl } from 'react-native';
import SnippetEditorModal, { SnippetEditorModalRef } from '../snippetEditorModal/snippetEditorModal'; import SnippetEditorModal, { SnippetEditorModalRef } from '../snippetEditorModal/snippetEditorModal';
import SnippetItem from './snippetItem'; import SnippetItem from './snippetItem';
import { Snippet } from '../../models'; import { Snippet } from '../../models';
import { useAppSelector } from '../../hooks';
interface SnippetsModalProps { interface SnippetsModalProps {
username:string,
handleOnSelect:(snippetText:string)=>void, handleOnSelect:(snippetText:string)=>void,
} }
const SnippetsModal = ({ username, handleOnSelect }:SnippetsModalProps) => { const SnippetsModal = ({ handleOnSelect }:SnippetsModalProps) => {
const editorRef = useRef<SnippetEditorModalRef>(null); const editorRef = useRef<SnippetEditorModalRef>(null);
const intl = useIntl(); const intl = useIntl();
console.log('username', username);
const isLoggedIn = useAppSelector(state => state.application.isLoggedIn)
const [snippets, setSnippets] = useState([]); const [snippets, setSnippets] = useState([]);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
useEffect(() => { useEffect(() => {
console.log(username);
_getSnippets(); _getSnippets();
}, []); }, []);
@ -31,13 +32,13 @@ const SnippetsModal = ({ username, handleOnSelect }:SnippetsModalProps) => {
//fetch snippets from server //fetch snippets from server
const _getSnippets = async () => { const _getSnippets = async () => {
try{ try{
if (username) {
setIsLoading(true); setIsLoading(true);
const snips = await getSnippets(username) const snips = await getFragments()
console.log("snips received", snips) console.log("snips received", snips)
setSnippets(snips); setSnippets(snips);
setIsLoading(false); setIsLoading(false);
}
}catch(err){ }catch(err){
console.warn("Failed to get snippets") console.warn("Failed to get snippets")
setIsLoading(false); setIsLoading(false);
@ -47,12 +48,12 @@ const SnippetsModal = ({ username, handleOnSelect }:SnippetsModalProps) => {
//removes snippet from users snippet collection on user confirmation //removes snippet from users snippet collection on user confirmation
const _removeSnippet = async (id:string) => { const _removeSnippet = async (id:string) => {
try{ try{
if (username) {
setIsLoading(true); setIsLoading(true);
const snips = await removeSnippet(username, id) const snips = await deleteFragment(id)
setSnippets(snips); setSnippets(snips);
setIsLoading(false); setIsLoading(false);
}
}catch(err){ }catch(err){
console.warn("Failed to get snippets") console.warn("Failed to get snippets")
setIsLoading(false); setIsLoading(false);
@ -118,6 +119,10 @@ const SnippetsModal = ({ username, handleOnSelect }:SnippetsModalProps) => {
//renders footer with add snipept button and shows new snippet modal //renders footer with add snipept button and shows new snippet modal
const _renderFloatingButton = () => { const _renderFloatingButton = () => {
if(!isLoggedIn){
return null;
}
const _onPress = () => { const _onPress = () => {
if(editorRef.current){ if(editorRef.current){
editorRef.current.showNewModal(); editorRef.current.showNewModal();
@ -160,7 +165,6 @@ const SnippetsModal = ({ username, handleOnSelect }:SnippetsModalProps) => {
<SnippetEditorModal <SnippetEditorModal
ref={editorRef} ref={editorRef}
username={username}
onSnippetsUpdated={setSnippets} onSnippetsUpdated={setSnippets}
/> />
</View> </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 title title
* @params body body * @params body body
* @returns array of fragments
*/ */
export const addSnippet = (currentUsername, title, body) =>
api export const addFragment = async (title: string, body: string) => {
.post('/fragment', { try{
username: currentUsername, const data = { title, body };
title, const response = await ecencyApi.post(`/private-api/fragments-add`, data);
body, return response.data;
}) } catch(error) {
.then((resp) => resp.data) console.warn("Failed to add fragment", error);
.catch((error) => bugsnag.notify(error)); bugsnag.notify(error)
throw error;
}
}
/** /**
* @params current username * Updates a fragment content using fragment id
* @params fragmentid id * @params fragmentId
* @params title title * @params title
* @params body body * @params body
* @returns array of fragments
*/ */
export const updateSnippet = (username, id, title, body) => export const updateFragment = async (fragmentId:string, title: string, body: string) => {
new Promise((resolve, reject) => { try{
api const data = { id:fragmentId, title, body };
.put(`/fragments/${username}/${id}`, { const response = await ecencyApi.post(`/private-api/fragments-update`, data);
title, return response.data;
body, } catch(error) {
}) console.warn("Failed to update fragment", error);
.then((res) => { bugsnag.notify(error)
resolve(res.data); throw error;
}) }
.catch((error) => { }
bugsnag.notify(error);
reject(error);
});
});
/** /**
* @params current username * Deletes user saved fragment using specified fragment id
* @params fragmentid id * @params fragmentId
* @returns array of fragments
*/ */
export const removeSnippet = (username, id) => export const deleteFragment = async (fragmentId:string) => {
new Promise((resolve, reject) => { try{
api const data = { id:fragmentId };
.delete(`/fragments/${username}/${id}`) const response = await ecencyApi.post(`/private-api/fragments-delete`, data);
.then((res) => { return response.data;
resolve(res.data); } catch(error) {
}) console.warn("Failed to delete fragment", error);
.catch((error) => { bugsnag.notify(error)
bugsnag.notify(error); throw error;
reject(error); }
}); }
});