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,12 +74,10 @@ 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());
|
||||
|
||||
@@ -91,13 +89,13 @@ export const getPreferredTimeFormat = () => async(dispatch: Dispatch<GetPreferre
|
||||
preferredTimeFormat = await userStore.get('timeFormat');
|
||||
}
|
||||
|
||||
dispatch(receive({data: preferredTimeFormat}));
|
||||
dispatch(receive({data: preferredTimeFormat as TimeFormats}));
|
||||
} 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,12 +109,10 @@ 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());
|
||||
|
||||
@@ -129,4 +125,4 @@ export const setPreferredTimeFormat = (format: TimeFormats) => async(dispatch: D
|
||||
|
||||
dispatch(failed(message || 'An error occurred while setting the preferred time format'));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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;
|
||||
@@ -11,7 +11,7 @@ export const initialPreferredTimeFormatState: IPreferredTimeFormatState = {
|
||||
isLoading: false,
|
||||
error: null,
|
||||
timeFormat: TimeFormats.Utc
|
||||
}
|
||||
};
|
||||
|
||||
export const preferredTimeFormatSlice = createSlice({
|
||||
name: 'preferredTimeFormat',
|
||||
@@ -21,7 +21,7 @@ export const preferredTimeFormatSlice = createSlice({
|
||||
state.timeFormat = action.payload;
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
export const { setPreferredTimeFormat } = preferredTimeFormatSlice.actions;
|
||||
export const {setPreferredTimeFormat} = preferredTimeFormatSlice.actions;
|
||||
export default preferredTimeFormatSlice.reducer;
|
||||
Reference in New Issue
Block a user