import { mapResourceResponse } from './ResourceClient'; import { mapFontResponse } from './FontClient'; import ApiClient from './ApiClient'; import * as T from './types/Theme'; import urls from './urls'; import { mapFileUploadRequest, mapFileUploadResponse, mapNewFileUploadsResponse, } from './FileUploadUtils'; export const mapThemeResponse = (t: T.ThemeResponse): T.Theme => ({ id: t.id, presentationId: t.presentation_id, name: t.name, backgroundColor: t.background_color, backgroundImage: t.background_image, backgroundImagePortrait: t.background_image_portrait, backgroundImageFileUpload: t.background_image_file_upload ? mapFileUploadResponse(t.background_image_file_upload) : null, backgroundScale: t.background_scale, headingTextColor: t.heading_text_color, headingFont: t.heading_font, heading2TextColor: t.heading_2_text_color, heading2Font: t.heading_2_font, bodyTextColor: t.body_text_color, bodyFont: t.body_font, borderColor: t.border_color, resource: mapResourceResponse(t.resource), headingFontNew: t.heading_font_new ? mapFontResponse(t.heading_font_new) : null, heading2FontNew: t.heading_2_font_new ? mapFontResponse(t.heading_2_font_new) : null, bodyFontNew: t.body_font_new ? mapFontResponse(t.body_font_new) : null, }); export const mapThemeV2Response = (t: T.ThemeV2Response): T.ThemeV2 => ({ id: t.id, presentationId: t.presentation_id, name: t.name, backgroundColor: t.background_color, backgroundImage: t.background_image, backgroundImagePortrait: t.background_image_portrait, headingTextColor: t.heading_text_color, headingFont: t.heading_font, heading2TextColor: t.heading_2_text_color, heading2Font: t.heading_2_font, bodyTextColor: t.body_text_color, bodyFont: t.body_font, borderColor: t.border_color, headingFontNew: t.heading_font_new ? mapFontResponse(t.heading_font_new) : null, heading2FontNew: t.heading_2_font_new ? mapFontResponse(t.heading_2_font_new) : null, bodyFontNew: t.body_font_new ? mapFontResponse(t.body_font_new) : null, backgroundImageFileUpload: t.background_image_file_upload ? mapFileUploadResponse(t.background_image_file_upload) : null, backgroundScale: t.background_scale, }); export default class ThemeClient { async getThemes(this: ApiClient): Promise { const data = await this.requestProtected< T.GetThemesRequest, T.GetThemesResponse >({ method: 'GET', url: urls.themes(), }); return data.map(mapThemeResponse); } async updateTheme(this: ApiClient, id: string, params: Partial) { const { theme, background_image_file_uploads, } = await this.requestProtected< T.UpdateThemeRequest, T.UpdateThemeResponse >({ method: 'PATCH', url: urls.theme(id), body: { presentation_id: params.presentationId, name: params.name, heading_font: params.headingFont, heading_text_color: params.headingTextColor, heading_2_font: params.heading2Font, heading_2_text_color: params.heading2TextColor, body_font: params.bodyFont, body_text_color: params.bodyTextColor, background_color: params.backgroundColor, border_color: params.borderColor, heading_font_new_id: params.headingFontNew?.id, heading_2_font_new_id: params.heading2FontNew?.id, body_font_new_id: params.bodyFontNew?.id, background_image_file_upload: params.backgroundImageFileUpload ? mapFileUploadRequest(params.backgroundImageFileUpload) : null, background_scale: params.backgroundScale, }, }); return [ mapThemeResponse(theme), background_image_file_uploads ? mapNewFileUploadsResponse(background_image_file_uploads) : null, ]; } async createTheme(this: ApiClient, params: Partial) { const { theme, background_image_file_uploads, } = await this.requestProtected< T.UpdateThemeRequest, T.UpdateThemeResponse >({ method: 'POST', url: urls.themes(), body: { presentation_id: params.presentationId, name: params.name, heading_font: params.headingFont, heading_text_color: params.headingTextColor, heading_2_font: params.heading2Font, heading_2_text_color: params.heading2TextColor, body_font: params.bodyFont, body_text_color: params.bodyTextColor, background_color: params.backgroundColor, border_color: params.borderColor, heading_font_new_id: params.headingFontNew?.id, heading_2_font_new_id: params.heading2FontNew?.id, body_font_new_id: params.bodyFontNew?.id, background_image_file_upload: params.backgroundImageFileUpload ? mapFileUploadRequest(params.backgroundImageFileUpload) : null, background_scale: params.backgroundScale, }, }); return [ mapThemeResponse(theme), background_image_file_uploads ? mapNewFileUploadsResponse(background_image_file_uploads) : null, ]; } async deleteTheme(this: ApiClient, id: string): Promise { await this.requestProtected({ method: 'DELETE', url: urls.theme(id), }); } }