From 5cc1c4325710a461b4d511d641be6e15ab0b6aac Mon Sep 17 00:00:00 2001 From: Rishabh Date: Tue, 13 Jun 2023 13:24:47 +0530 Subject: [PATCH] Added theme management apis in adminX refs https://github.com/TryGhost/Team/issues/3432 --- ghost/admin-x-settings/src/types/api.ts | 11 +++++++ ghost/admin-x-settings/src/utils/api.ts | 44 ++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/ghost/admin-x-settings/src/types/api.ts b/ghost/admin-x-settings/src/types/api.ts index 1f011a1482..2b6e099a13 100644 --- a/ghost/admin-x-settings/src/types/api.ts +++ b/ghost/admin-x-settings/src/types/api.ts @@ -126,3 +126,14 @@ export type CustomThemeSetting = CustomThemeSettingData & { // homepage and post are the only two groups we handle, but technically theme authors can put other things in package.json group?: 'homepage' | 'post' | string } + +export type Theme = { + active: boolean; + name: string; + package: { + name?: string; + description?: string; + version?: string; + }; + templates?: string[]; +} diff --git a/ghost/admin-x-settings/src/utils/api.ts b/ghost/admin-x-settings/src/utils/api.ts index b7189e6a07..e31ef43d60 100644 --- a/ghost/admin-x-settings/src/utils/api.ts +++ b/ghost/admin-x-settings/src/utils/api.ts @@ -1,4 +1,4 @@ -import {CustomThemeSetting, Label, Offer, Post, Setting, SiteData, Tier, User, UserRole} from '../types/api'; +import {CustomThemeSetting, Label, Offer, Post, Setting, SiteData, Theme, Tier, User, UserRole} from '../types/api'; import {getGhostPaths} from './helpers'; interface Meta { @@ -90,6 +90,10 @@ export interface PasswordUpdateResponseType { }]; } +export interface ThemesResponseType { + themes: Theme[]; +} + interface RequestOptions { method?: string; body?: string | FormData; @@ -160,6 +164,12 @@ interface API { offers: { browse: () => Promise }; + themes: { + browse: () => Promise; + activate: (themeName: string) => Promise; + delete: (themeName: string) => Promise; + upload: ({file}: {file: File}) => Promise; + }; } interface GhostApiOptions { @@ -386,6 +396,38 @@ function setupGhostApi({ghostVersion}: GhostApiOptions): API { const data: OffersResponseType = await response.json(); return data; } + }, + themes: { + browse: async () => { + const response = await fetcher('/themes/'); + const data: ThemesResponseType = await response.json(); + return data; + }, + activate: async (themeName: string) => { + const response = await fetcher(`/themes/${themeName}/activate/`, { + method: 'PUT' + }); + const data: ThemesResponseType = await response.json(); + return data; + }, + delete: async (themeName: string) => { + await fetcher(`/themes/${themeName}/`, { + method: 'DELETE' + }); + return; + }, + upload: async ({file}: {file: File}) => { + const formData = new FormData(); + formData.append('file', file); + + const response = await fetcher(`/themes/upload/`, { + method: 'POST', + body: formData, + headers: {} + }); + const data: ThemesResponseType = await response.json(); + return data; + } } };