Refactor preferred time format actions and slice for improved type safety and consistency; update action payloads to include data structure.
This commit is contained in:
@@ -49,12 +49,12 @@ export type SetPreferredTimeFormatActionType = IRequestSetPreferredTimeFormat |
|
||||
|
||||
interface IGetPreferredTimeFormatActions {
|
||||
request: () => GetPreferredTimeFormatActionType;
|
||||
receive: (payload) => GetPreferredTimeFormatActionType;
|
||||
receive: (payload: {data: TimeFormats}) => GetPreferredTimeFormatActionType;
|
||||
failed: (error: null | string) => GetPreferredTimeFormatActionType;
|
||||
}
|
||||
interface ISetPreferredTimeFormatActions {
|
||||
request: () => SetPreferredTimeFormatActionType;
|
||||
receive: (payload) => SetPreferredTimeFormatActionType;
|
||||
receive: (payload: {data: TimeFormats}) => SetPreferredTimeFormatActionType;
|
||||
failed: (error: null | string) => SetPreferredTimeFormatActionType;
|
||||
}
|
||||
|
||||
@@ -74,30 +74,28 @@ const getPreferredTimeFormatActions: IGetPreferredTimeFormatActions = {
|
||||
})
|
||||
};
|
||||
|
||||
export const getPreferredTimeFormat = () => async(dispatch: Dispatch<GetPreferredTimeFormatActionType>): Promise<void> => {
|
||||
const {
|
||||
request,
|
||||
receive,
|
||||
failed
|
||||
} = getPreferredTimeFormatActions;
|
||||
export const getPreferredTimeFormat =
|
||||
() =>
|
||||
async (dispatch: Dispatch<GetPreferredTimeFormatActionType>): Promise<void> => {
|
||||
const {request, receive, failed} = getPreferredTimeFormatActions;
|
||||
|
||||
dispatch(request());
|
||||
dispatch(request());
|
||||
|
||||
try {
|
||||
let preferredTimeFormat = await userStore.get('timeFormat');
|
||||
try {
|
||||
let preferredTimeFormat = await userStore.get('timeFormat');
|
||||
|
||||
if (!preferredTimeFormat) {
|
||||
await userStore.set('timeFormat', TimeFormats.Utc);
|
||||
preferredTimeFormat = await userStore.get('timeFormat');
|
||||
if (!preferredTimeFormat) {
|
||||
await userStore.set('timeFormat', TimeFormats.Utc);
|
||||
preferredTimeFormat = await userStore.get('timeFormat');
|
||||
}
|
||||
|
||||
dispatch(receive({data: preferredTimeFormat as TimeFormats}));
|
||||
} catch (e) {
|
||||
const {message} = transformToPortalError(e);
|
||||
|
||||
dispatch(failed(message || 'An error occurred while getting the preferred time format'));
|
||||
}
|
||||
|
||||
dispatch(receive({data: preferredTimeFormat}));
|
||||
} catch (e) {
|
||||
const {message} = transformToPortalError(e);
|
||||
|
||||
dispatch(failed(message || 'An error occurred while getting the preferred time format'));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const setPreferredTimeFormatActions: ISetPreferredTimeFormatActions = {
|
||||
request: () => ({type: SET_PREFERRED_TIME_FORMAT}),
|
||||
@@ -111,22 +109,20 @@ const setPreferredTimeFormatActions: ISetPreferredTimeFormatActions = {
|
||||
})
|
||||
};
|
||||
|
||||
export const setPreferredTimeFormat = (format: TimeFormats) => async(dispatch: Dispatch<SetPreferredTimeFormatActionType>): Promise<void> => {
|
||||
const {
|
||||
request,
|
||||
receive,
|
||||
failed
|
||||
} = setPreferredTimeFormatActions;
|
||||
export const setPreferredTimeFormat =
|
||||
(format: TimeFormats) =>
|
||||
async (dispatch: Dispatch<SetPreferredTimeFormatActionType>): Promise<void> => {
|
||||
const {request, receive, failed} = setPreferredTimeFormatActions;
|
||||
|
||||
dispatch(request());
|
||||
dispatch(request());
|
||||
|
||||
try {
|
||||
await userStore.set('timeFormat', format);
|
||||
try {
|
||||
await userStore.set('timeFormat', format);
|
||||
|
||||
dispatch(receive({data: format}));
|
||||
} catch (e) {
|
||||
const {message} = transformToPortalError(e);
|
||||
dispatch(receive({data: format}));
|
||||
} catch (e) {
|
||||
const {message} = transformToPortalError(e);
|
||||
|
||||
dispatch(failed(message || 'An error occurred while setting the preferred time format'));
|
||||
}
|
||||
};
|
||||
dispatch(failed(message || 'An error occurred while setting the preferred time format'));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -96,7 +96,7 @@ const authenticationSlice = createSlice({
|
||||
},
|
||||
setCredentials: (state, action: PayloadAction<{applicationId: string; secret: string}>) => {
|
||||
const {applicationId, secret} = action.payload;
|
||||
|
||||
|
||||
if (applicationId) {
|
||||
state.applicationId = applicationId;
|
||||
}
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||
import { TimeFormats } from "utility";
|
||||
import {createSlice, PayloadAction} from '@reduxjs/toolkit';
|
||||
import {TimeFormats} from 'utility';
|
||||
|
||||
export interface IPreferredTimeFormatState {
|
||||
isLoading: boolean;
|
||||
error: null | string;
|
||||
timeFormat: TimeFormats;
|
||||
isLoading: boolean;
|
||||
error: null | string;
|
||||
timeFormat: TimeFormats;
|
||||
}
|
||||
|
||||
export const initialPreferredTimeFormatState: IPreferredTimeFormatState = {
|
||||
isLoading: false,
|
||||
error: null,
|
||||
timeFormat: TimeFormats.Utc
|
||||
}
|
||||
isLoading: false,
|
||||
error: null,
|
||||
timeFormat: TimeFormats.Utc
|
||||
};
|
||||
|
||||
export const preferredTimeFormatSlice = createSlice({
|
||||
name: 'preferredTimeFormat',
|
||||
initialState: initialPreferredTimeFormatState,
|
||||
reducers: {
|
||||
setPreferredTimeFormat: (state: IPreferredTimeFormatState, action: PayloadAction<TimeFormats>) => {
|
||||
state.timeFormat = action.payload;
|
||||
}
|
||||
name: 'preferredTimeFormat',
|
||||
initialState: initialPreferredTimeFormatState,
|
||||
reducers: {
|
||||
setPreferredTimeFormat: (state: IPreferredTimeFormatState, action: PayloadAction<TimeFormats>) => {
|
||||
state.timeFormat = action.payload;
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
export const { setPreferredTimeFormat } = preferredTimeFormatSlice.actions;
|
||||
export default preferredTimeFormatSlice.reducer;
|
||||
export const {setPreferredTimeFormat} = preferredTimeFormatSlice.actions;
|
||||
export default preferredTimeFormatSlice.reducer;
|
||||
|
||||
Reference in New Issue
Block a user