add fetching a poll with a pass hash

This commit is contained in:
tykayn 2021-04-21 12:17:05 +02:00
parent 2562b21d2f
commit 3850f3811c
2 changed files with 25 additions and 2 deletions

View File

@ -40,7 +40,6 @@ export class ApiService {
this.axiosInstance.defaults.headers.post['Charset'] = 'UTF-8';
this.axiosInstance.defaults.headers.post['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS';
this.axiosInstance.defaults.headers.post['Access-Control-Allow-Origin'] = '*';
console.log('this.axiosInstance.defaults.headers', this.axiosInstance.defaults.headers);
}
//////////////////////
@ -117,7 +116,6 @@ export class ApiService {
}
public async getPollBySlug(slug: string): Promise<Poll | undefined> {
// TODO: identifier should be decided according to backend : Id || Slug ?
try {
// TODO: this interceptor should be avoided if backends returns the good object
const adapterInterceptor: number = this.axiosInstance.interceptors.response.use(
@ -148,6 +146,23 @@ export class ApiService {
}
}
public async getPollBySlugWithHash(slug: string, hash: string): Promise<Poll | undefined> {
try {
const response: AxiosResponse<Poll> = await this.axiosInstance.get<Poll>(
`${this.pollsEndpoint}/${slug}/pass/${hash}`
);
console.log('fetch API : asking for poll with slug=' + slug, { response });
return response && response.data && !Array.isArray(response.data) ? response.data : undefined;
} catch (error) {
if (error.response?.status === 404) {
return undefined;
} else {
ApiService.handleError(error);
}
}
}
public async getSlug(slug: string): Promise<boolean> {
try {
// TODO: scenario should be : if we can get this slug, it exists. if not, it doesn't. It's just a GET.

View File

@ -76,6 +76,14 @@ export class PollService implements Resolve<Poll> {
this.updateCurrentPoll(poll);
}
}
public async loadPollBySlugWithPasswordHash(slug: string, hash: string): Promise<void> {
console.log('slug', slug);
if (slug) {
const poll: Poll | undefined = await this.apiService.getPollBySlugWithHash(slug, hash);
console.log({ loadPollBySlugResponse: poll });
this.updateCurrentPoll(poll);
}
}
public updateCurrentPoll(poll: Poll): void {
this._poll.next(poll);