diff options
| author | Ashm Walia <40498934+ashmgarv@users.noreply.github.com> | 2020-12-22 08:50:27 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-22 11:50:27 -0500 |
| commit | a954d6b6b88485dddc0ccfda634ffd102cb34ccd (patch) | |
| tree | 560f152dd92ccb482a2bbf6b094060525373322c /src/services | |
| parent | 49ed044f5103cf6288fcf5b3ff6d3d720795860c (diff) | |
[TMA 446] Create category (#144)
* Added welcome page
* Working code
* Small fix
* Some more cleanup
* Fixes
* Cleanup
* Fix again
* Use gradient for white bg as well
* Fixed type
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/MomentCategoryService.ts | 88 | ||||
| -rw-r--r-- | src/services/index.ts | 1 |
2 files changed, 89 insertions, 0 deletions
diff --git a/src/services/MomentCategoryService.ts b/src/services/MomentCategoryService.ts new file mode 100644 index 00000000..8bdb70d2 --- /dev/null +++ b/src/services/MomentCategoryService.ts @@ -0,0 +1,88 @@ +import {Alert} from 'react-native'; +import {MomentCategoryType} from './../types/types'; +import {MOMENT_CATEGORY_ENDPOINT} from '../constants'; + +export const loadMomentCategories: ( + userId: string, + token: string, +) => Promise<MomentCategoryType[]> = async (userId, token) => { + let categories: MomentCategoryType[] = []; + try { + const response = await fetch(MOMENT_CATEGORY_ENDPOINT + `${userId}/`, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (status === 200) { + const data = await response.json(); + categories = data['categories']; + } else { + console.log('Could not load categories!'); + return []; + } + } catch (err) { + console.log(err); + return []; + } + return categories; +}; + +export const postMomentCategories: ( + categories: Array<MomentCategoryType>, + token: string, +) => Promise<boolean> = async (categories, token) => { + let success = false; + try { + const response = await fetch(MOMENT_CATEGORY_ENDPOINT, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Token ' + token, + }, + body: JSON.stringify({categories}), + }); + const status = response.status; + if (status === 200) { + success = true; + } else { + Alert.alert('There was a problem creating categories!'); + console.log('Could not post categories!'); + } + } catch (err) { + console.log(err); + return success; + } + return success; +}; + +export const deleteMomentCategories: ( + categories: Array<MomentCategoryType>, + userId: string, + token: string, +) => Promise<boolean> = async (categories, userId, token) => { + let success = false; + try { + const response = await fetch(MOMENT_CATEGORY_ENDPOINT + `${userId}/`, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Token ' + token, + }, + body: JSON.stringify({categories}), + }); + const status = response.status; + if (status === 200) { + Alert.alert(`The category was successfully deleted!`); + success = true; + } else { + Alert.alert('There was a problem while deleteing category!'); + console.log('Could not delete category!'); + } + } catch (err) { + console.log(err); + return success; + } + return success; +}; diff --git a/src/services/index.ts b/src/services/index.ts index bce3a75a..d98996ba 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -5,3 +5,4 @@ export * from './ExploreServices'; export * from './UserFollowServices'; export * from './ReportingService'; export * from './BlockUserService'; +export * from './MomentCategoryService'; |
