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 {
|
interface IGetPreferredTimeFormatActions {
|
||||||
request: () => GetPreferredTimeFormatActionType;
|
request: () => GetPreferredTimeFormatActionType;
|
||||||
receive: (payload) => GetPreferredTimeFormatActionType;
|
receive: (payload: {data: TimeFormats}) => GetPreferredTimeFormatActionType;
|
||||||
failed: (error: null | string) => GetPreferredTimeFormatActionType;
|
failed: (error: null | string) => GetPreferredTimeFormatActionType;
|
||||||
}
|
}
|
||||||
interface ISetPreferredTimeFormatActions {
|
interface ISetPreferredTimeFormatActions {
|
||||||
request: () => SetPreferredTimeFormatActionType;
|
request: () => SetPreferredTimeFormatActionType;
|
||||||
receive: (payload) => SetPreferredTimeFormatActionType;
|
receive: (payload: {data: TimeFormats}) => SetPreferredTimeFormatActionType;
|
||||||
failed: (error: null | string) => SetPreferredTimeFormatActionType;
|
failed: (error: null | string) => SetPreferredTimeFormatActionType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,30 +74,28 @@ const getPreferredTimeFormatActions: IGetPreferredTimeFormatActions = {
|
|||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getPreferredTimeFormat = () => async(dispatch: Dispatch<GetPreferredTimeFormatActionType>): Promise<void> => {
|
export const getPreferredTimeFormat =
|
||||||
const {
|
() =>
|
||||||
request,
|
async (dispatch: Dispatch<GetPreferredTimeFormatActionType>): Promise<void> => {
|
||||||
receive,
|
const {request, receive, failed} = getPreferredTimeFormatActions;
|
||||||
failed
|
|
||||||
} = getPreferredTimeFormatActions;
|
|
||||||
|
|
||||||
dispatch(request());
|
dispatch(request());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let preferredTimeFormat = await userStore.get('timeFormat');
|
let preferredTimeFormat = await userStore.get('timeFormat');
|
||||||
|
|
||||||
if (!preferredTimeFormat) {
|
if (!preferredTimeFormat) {
|
||||||
await userStore.set('timeFormat', TimeFormats.Utc);
|
await userStore.set('timeFormat', TimeFormats.Utc);
|
||||||
preferredTimeFormat = await userStore.get('timeFormat');
|
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 = {
|
const setPreferredTimeFormatActions: ISetPreferredTimeFormatActions = {
|
||||||
request: () => ({type: SET_PREFERRED_TIME_FORMAT}),
|
request: () => ({type: SET_PREFERRED_TIME_FORMAT}),
|
||||||
@@ -111,22 +109,20 @@ const setPreferredTimeFormatActions: ISetPreferredTimeFormatActions = {
|
|||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
export const setPreferredTimeFormat = (format: TimeFormats) => async(dispatch: Dispatch<SetPreferredTimeFormatActionType>): Promise<void> => {
|
export const setPreferredTimeFormat =
|
||||||
const {
|
(format: TimeFormats) =>
|
||||||
request,
|
async (dispatch: Dispatch<SetPreferredTimeFormatActionType>): Promise<void> => {
|
||||||
receive,
|
const {request, receive, failed} = setPreferredTimeFormatActions;
|
||||||
failed
|
|
||||||
} = setPreferredTimeFormatActions;
|
|
||||||
|
|
||||||
dispatch(request());
|
dispatch(request());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await userStore.set('timeFormat', format);
|
await userStore.set('timeFormat', format);
|
||||||
|
|
||||||
dispatch(receive({data: format}));
|
dispatch(receive({data: format}));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const {message} = transformToPortalError(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}>) => {
|
setCredentials: (state, action: PayloadAction<{applicationId: string; secret: string}>) => {
|
||||||
const {applicationId, secret} = action.payload;
|
const {applicationId, secret} = action.payload;
|
||||||
|
|
||||||
if (applicationId) {
|
if (applicationId) {
|
||||||
state.applicationId = applicationId;
|
state.applicationId = applicationId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,27 @@
|
|||||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
import {createSlice, PayloadAction} from '@reduxjs/toolkit';
|
||||||
import { TimeFormats } from "utility";
|
import {TimeFormats} from 'utility';
|
||||||
|
|
||||||
export interface IPreferredTimeFormatState {
|
export interface IPreferredTimeFormatState {
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
error: null | string;
|
error: null | string;
|
||||||
timeFormat: TimeFormats;
|
timeFormat: TimeFormats;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const initialPreferredTimeFormatState: IPreferredTimeFormatState = {
|
export const initialPreferredTimeFormatState: IPreferredTimeFormatState = {
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
error: null,
|
error: null,
|
||||||
timeFormat: TimeFormats.Utc
|
timeFormat: TimeFormats.Utc
|
||||||
}
|
};
|
||||||
|
|
||||||
export const preferredTimeFormatSlice = createSlice({
|
export const preferredTimeFormatSlice = createSlice({
|
||||||
name: 'preferredTimeFormat',
|
name: 'preferredTimeFormat',
|
||||||
initialState: initialPreferredTimeFormatState,
|
initialState: initialPreferredTimeFormatState,
|
||||||
reducers: {
|
reducers: {
|
||||||
setPreferredTimeFormat: (state: IPreferredTimeFormatState, action: PayloadAction<TimeFormats>) => {
|
setPreferredTimeFormat: (state: IPreferredTimeFormatState, action: PayloadAction<TimeFormats>) => {
|
||||||
state.timeFormat = action.payload;
|
state.timeFormat = action.payload;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export const { setPreferredTimeFormat } = preferredTimeFormatSlice.actions;
|
export const {setPreferredTimeFormat} = preferredTimeFormatSlice.actions;
|
||||||
export default preferredTimeFormatSlice.reducer;
|
export default preferredTimeFormatSlice.reducer;
|
||||||
|
|||||||
Reference in New Issue
Block a user