Skip to content

Media/refactor services #743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/contexts/ServicesContext.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { createContext } from "react"

import {
PageService,
DirectoryService,
MoverService,
SettingsService,
MediaService,
} from "services"

const ServicesContext = createContext({}) // holds all services we need
Expand All @@ -13,12 +15,14 @@ const ServicesProvider = ({ client, children }) => {
const directoryService = new DirectoryService({ apiClient: client })
const moverService = new MoverService({ apiClient: client })
const settingsService = new SettingsService({ apiClient: client })
const mediaService = new MediaService({ apiClient: client })

const services = {
pageService,
directoryService,
moverService,
settingsService,
mediaService,
}

return (
Expand Down
29 changes: 28 additions & 1 deletion src/services/DirectoryService.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLastItemType } from "../utils"
import { getLastItemType, getMediaDirectoryName } from "../utils"

export class DirectoryService {
constructor({ apiClient }) {
Expand All @@ -11,11 +11,32 @@ export class DirectoryService {
subCollectionName,
resourceRoomName,
resourceCategoryName,
mediaDirectoryName,
isCreate,
isReorder,
isUnlinked,
isResource,
}) {
// R media room (images)
// GET /sites/a-test-v4/media/images
// R media room (images folder)
// GET /sites/a-test-v4/media/images%2F:directoryName
// C media folder
// POST /sites/a-test-v4/media
// Rename media folder
// POST /sites/a-test-v4/media/images%2F:directoryName
// D media folder
// DELETE /sites/a-test-v4/media/images%2F:directoryName

if (mediaDirectoryName) {
let endpoint = `/sites/${siteName}/media`
if (isCreate) return endpoint
if (mediaDirectoryName) {
endpoint += `/${mediaDirectoryName}`
}
return endpoint
}

if (isUnlinked) {
// R Unlinked pages
// /sites/a-test-v4/pages
Expand Down Expand Up @@ -83,6 +104,12 @@ export class DirectoryService {
}

async update(apiParams, { newDirectoryName }) {
if (
apiParams.mediaDirectoryName &&
apiParams.mediaDirectoryName ===
getMediaDirectoryName(newDirectoryName, { splitOn: "/", joinOn: "%2F" })
)
return
if (apiParams[getLastItemType(apiParams)] === newDirectoryName) return
const body = {
newDirectoryName,
Expand Down
48 changes: 48 additions & 0 deletions src/services/MediaService.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export class MediaService {
constructor({ apiClient }) {
this.apiClient = apiClient
}

getMediaEndpoint({ siteName, mediaDirectoryName, fileName }) {
let endpoint = `/sites/${siteName}/media`
if (mediaDirectoryName) {
endpoint += `/${mediaDirectoryName}`
}
endpoint += `/pages`
if (fileName) {
endpoint += `/${fileName}`
}
return endpoint
}

async create(apiParams, { newFileName, content }) {
const body = {
content,
newFileName,
}
return await this.apiClient.post(this.getMediaEndpoint(apiParams), body)
}

async update(apiParams, { newFileName, sha }) {
const body = {
newFileName,
sha,
}
return await this.apiClient.post(this.getMediaEndpoint(apiParams), body)
}

async get(apiParams) {
return await this.apiClient
.get(this.getMediaEndpoint(apiParams))
.then((res) => res.data)
}

async delete(apiParams, { sha }) {
const body = {
sha,
}
return await this.apiClient
.delete(this.getMediaEndpoint(apiParams), { data: body })
.then((res) => res.data)
}
}
9 changes: 8 additions & 1 deletion src/services/MoverService.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ export class MoverService {
resourceCategoryName,
collectionName,
subCollectionName,
mediaRoom,
mediaDirectoryName,
}) {
let endpoint = `/sites/${siteName}`
if (mediaDirectoryName) {
endpoint += `/media/${mediaDirectoryName}`
}
if (resourceRoomName) {
endpoint += `/resourceRoom/${resourceRoomName}`
}
Expand All @@ -27,7 +32,9 @@ export class MoverService {
!collectionName &&
!subCollectionName &&
!resourceCategoryName &&
!resourceRoomName
!resourceRoomName &&
!mediaRoom &&
!mediaDirectoryName
) {
endpoint += `/pages`
}
Expand Down
1 change: 1 addition & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { PageService } from "./PageService"
export { MediaService } from "./MediaService"
export { DirectoryService } from "./DirectoryService"
export { MoverService } from "./MoverService"
export { SettingsService } from "./SettingsService"