* Add api methods for subscription * Small changes * little changes in remark.tsx * disallow pass className to Button and Input * Subscribe block * add RSS and Email subscription drobdowns * add hook useTheme * unify dropdown import/export * Change API * rename subscribe methods * add unsubscribe method * Add email subscription to settings and user * Refactor and add new steps * render by single component * add final step * add unsubscribe step it user is subscribed * Add tests * Update subscription logic * test without mocking redux methods but with mocking store * update user in store after subscribe and unsubscribe * little changes in subscription flow * fix drobdown size * Fix RSS subscription link for site * fix link * add test for RSS subscription * Fix showing email subscription * it don't show to unauth users * it don't show to anonymous users * it tested * isUserAnonymous is a bit rewrited * isUserAnonymous is tested * Make Email button visible for anon users * React X supporst Fragments thats why .babelrc changed * disabled button is more visible * fix hovering on disabled buttons * create mocks for tests * move email dropdown to __subscribe-by-email
74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
/**
|
|
* map of codes that server returns in its response in case of error
|
|
* to client readable version
|
|
*/
|
|
const errorMessageForCodes = new Map([
|
|
[0, 'Something went wrong. Please try again a bit later.'],
|
|
[1, 'Comment cannot be found. Please refresh the page and try again.'],
|
|
[2, 'Failed to unmarshal incoming request.'],
|
|
[3, "You don't have permission for this operaton."],
|
|
[4, 'Invalid comment data.'],
|
|
[5, 'Comment cannot be found. Please refresh the page and try again.'],
|
|
[6, 'Site cannot be found. Please refresh the page and try again.'],
|
|
[7, 'User has been blocked.'],
|
|
[8, 'This post is read only.'],
|
|
[9, 'Comment changing failed. Please try again a bit later.'],
|
|
[10, 'It is too late to edit the comment.'],
|
|
[11, 'Comment already has reply, editing is not possible.'],
|
|
[12, 'Cannot save voting result. Please try again a bit later.'],
|
|
[13, 'You cannot vote for your own comment.'],
|
|
[14, 'You have already voted for the comment.'],
|
|
[15, 'Too many votes for the comment.'],
|
|
[16, 'Min score reached for the comment.'],
|
|
[17, 'Action rejected. Please try again a bit later.'],
|
|
[18, 'Requested file cannot be found.'],
|
|
]);
|
|
|
|
/**
|
|
* map of http rest codes to ui label, used by fetcher to generate error with `-1` code
|
|
*/
|
|
export const httpErrorMap = new Map([
|
|
[401, 'Not authorized.'],
|
|
[403, 'Forbidden.'],
|
|
[429, 'You have reached maximum request limit.'],
|
|
]);
|
|
|
|
export type FetcherError =
|
|
| string
|
|
| {
|
|
/**
|
|
* Error code, that is part of server error response.
|
|
*
|
|
* Note that -1 is reserved for error where `error` field shall be used directly
|
|
*/
|
|
code?: number;
|
|
details?: string;
|
|
error: string;
|
|
};
|
|
|
|
export function extractErrorMessageFromResponse(response: FetcherError): string {
|
|
const defaultErrorMessage = errorMessageForCodes.get(0) as string;
|
|
|
|
if (!response) {
|
|
return defaultErrorMessage;
|
|
}
|
|
|
|
if (typeof response === 'string') {
|
|
return response;
|
|
}
|
|
|
|
if (response.code === -1) {
|
|
return response.error;
|
|
}
|
|
|
|
if (typeof response.details === 'string') {
|
|
return response.details.charAt(0).toUpperCase() + response.details.substring(1);
|
|
}
|
|
|
|
if (typeof response.code === 'number' && errorMessageForCodes.has(response.code)) {
|
|
return errorMessageForCodes.get(response.code)!;
|
|
}
|
|
|
|
return defaultErrorMessage;
|
|
}
|