Newer
Older
import axios, { AxiosRequestConfig } from 'axios'
import { IMailSubject } from '../models/mailSubject.model'
import { IMonthlyInfo } from '../models/monthlyInfo.model'
export class NewsletterService {
/**
* Saves a mail subject for selected month
* @param date
* @param subject
*/
public saveMailSubject = async (
date: Date,
subject: string,
): Promise<void> => {
try {
await axios.put(
`/api/admin/mailSubject`,
{
month: date.getMonth() + 1,
year: date.getFullYear(),
subject: subject,
},
)
toast.success('Mail subject succesfully saved !')
} catch (e: any) {
if (e.response.status === 403) {
toast.error(
"Unauthorized : You don't have the rights to do this operation"
)
} else {
toast.error('Failed to create mail subject')
}
console.error(e)
}
}
/**
* Gets the mail subject for selected month
* @param date
*/
public getSingleMailSubject = async (
date: Date,
): Promise<IMailSubject | null> => {
try {
const { data } = await axios.get(
`/api/admin/mailSubject/${date.getFullYear()}/${date.getMonth() + 1}`,
)
return data as IMailSubject
} catch (e: any) {
console.error('error', e)
return null
}
}
/**
* Deletes the mail subject for selected month
* @param date
*/
public deleteMailSubject = async (
date: Date,
): Promise<void> => {
try {
await axios.delete(
`/api/admin/mailSubject/${date.getFullYear()}/${date.getMonth() + 1}`,
)
toast.success('Mail subject succesfully deleted !')
} catch (e: any) {
if (e.response.status === 403) {
toast.error(
"Unauthorized : You don't have the rights to do this operation"
)
} else {
toast.error('Failed to delete mail subject')
}
console.error(e)
}
}
* Creates a monthlyInfo for selected month
public saveMonthlyInfo = async (
`/api/admin/monthlyInfo`,
month: date.getMonth() + 1,
toast.success('Monthly info succesfully saved !')
} catch (e: any) {
if (e.response.status === 403) {
toast.error(
"Unauthorized : You don't have the rights to do this operation"
)
} else {
toast.error('Failed to create monthly info')
}
* Gets the information for selected month
public getSingleMonthlyInfo = async (
): Promise<IMonthlyInfo | null> => {
`/api/admin/monthlyInfo/${date.getFullYear()}/${date.getMonth() + 1}`,
return data as IMonthlyInfo
console.error('error', e)
* Deletes a Monthly Info for selected month
public deleteMonthlyInfo = async (
): Promise<void> => {
try {
await axios.delete(
`/api/admin/monthlyInfo/${date.getFullYear()}/${date.getMonth() + 1}`,
toast.success('Monthly info succesfully deleted !')
} catch (e: any) {
if (e.response.status === 403) {
toast.error(
"Unauthorized : You don't have the rights to do this operation"
)
} else {
toast.error('Failed to delete monthly info')
}
console.error(e)
}
}
/**
* Creates a monthlyNews for selected month
* @param date
* @param title
* @param content
*/
public saveMonthlyNews = async (
date: Date,
title: string,
content: string,
): Promise<void> => {
try {
await axios.put(
`/api/admin/monthlyNews`,
{
month: date.getMonth() + 1,
year: date.getFullYear(),
title: title,
content: content,
},
)
toast.success('Monthly news succesfully saved !')
} catch (e: any) {
if (e.response.status === 403) {
toast.error(
"Unauthorized : You don't have the rights to do this operation"
)
} else {
toast.error('Failed to save monthly news')
}
console.error(e)
}
}
/**
* Gets a news title and content for selected month
*/
public getSingleMonthlyNews = async (
): Promise<IMonthlyNews | null> => {
`/api/admin/monthlyNews/${date.getFullYear()}/${date.getMonth() + 1}`,
return data as IMonthlyNews
console.error('error', e)
/**
* Deletes a Monthly News for selected month
*/
public deleteMonthlyNews = async (
): Promise<void> => {
try {
await axios.delete(
`/api/admin/monthlyNews/${date.getFullYear()}/${date.getMonth() + 1}`,
toast.success('Monthly news succesfully deleted !')
} catch (e: any) {
if (e.response.status === 403) {
toast.error(
"Unauthorized : You don't have the rights to do this operation"
)
} else {
toast.error('Failed to delete monthly news')
}
console.error(e)
}
}
/**
* Creates a poll with question and link for selected month
* @param date
* @param question
* @param link
*/
public savePoll = async (
month: date.getMonth() + 1,
year: date.getFullYear(),
link: link,
question: question,
},
toast.success('Poll successfully saved !')
} catch (e: any) {
if (e.response.status === 403) {
toast.error(
"Unauthorized : You don't have the rights to do this operation"
)
} else {
toast.error('Failed to create poll')
}
* Gets a poll with question and link for selected month
public getSinglePoll = async (
): Promise<IPoll | null> => {
const { data } = await axios.get(
`/api/admin/poll/${date.getFullYear()}/${date.getMonth() + 1}`,
console.error('error', e)
return null
* Deletes a poll for selected month
public deletePoll = async (
date: Date,
axiosHeaders: AxiosRequestConfig
): Promise<void> => {
await axios.delete(
`/api/admin/poll/${date.getFullYear()}/${date.getMonth() + 1}`,
toast.success('Poll succesfully deleted !')
} catch (e: any) {
if (e.response.status === 403) {
toast.error(
"Unauthorized : You don't have the rights to do this operation"
)
} else {
toast.error('Failed to delete poll')
}
/**
* Gets the ecogesture images URLs
*/
public getEcogestureImages = async (
axiosHeaders: AxiosRequestConfig
): Promise<string[]> => {
const { data: imageNames } = await axios.get(
`/api/admin/imageNames`,
axiosHeaders
)
if (imageNames && imageNames !== null) {
return imageNames.map((image: string) => `/assets/ecogesture/${image}`)
}
return []
} catch (e) {
console.error('error', e)
return []
}
}