Skip to content

Commit

Permalink
feat(dailyclean): remove useless hour converters
Browse files Browse the repository at this point in the history
  • Loading branch information
clement-mesureux committed Nov 27, 2024
1 parent 1d717d1 commit 3609821
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 54 deletions.
44 changes: 22 additions & 22 deletions api/web/src/FormConfigurationContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function extractEndHour(cron_stop) {
return "18";
}

const getTimeRangesAsync = (fetch) => (getLocalHour) => async (setState, state) => {
const getTimeRangesAsync = (fetch) => async (setState, state) => {
const response = await getAsync(fetch)(urls.timeranges);
if (response.status >= 300) {
setState({
Expand All @@ -112,11 +112,11 @@ const getTimeRangesAsync = (fetch) => (getLocalHour) => async (setState, state)
const cron_start = timeranges.cron_start;
let startHour = extractStartHour(cron_start);
const startWeekMode = extractStartWeek(cron_start);
let startHourLocal = getLocalHour(parseInt(startHour, 10));
let startHourLocal = Number.parseInt(startHour);


const endHour = extractEndHour(timeranges.cron_stop);
let endHourLocal = getLocalHour(parseInt(endHour, 10));
let endHourLocal = Number.parseInt(endHour);
const endWeekMode = timeranges.cron_stop ? endWeekModeEnum.enabled : endWeekModeEnum.disabled;
const form = state.form;
const newState = {
Expand All @@ -133,8 +133,8 @@ const getTimeRangesAsync = (fetch) => (getLocalHour) => async (setState, state)
return newState;
};

const extractTimeRanges=getUTCHour =>(form) => {
const extractTimeRanges=(form) => {

let startWeekMode;
switch (form.startWeekMode.value){
case startWeekModeEnum.workedDays:
Expand All @@ -147,7 +147,7 @@ const extractTimeRanges=getUTCHour =>(form) => {
startWeekMode=null;
break;
}

let endWeekMode;
switch (form.endWeekMode.value){
case endWeekModeEnum.enabled:
Expand All @@ -157,24 +157,24 @@ const extractTimeRanges=getUTCHour =>(form) => {
endWeekMode=null;
break;
}

const timeranges = {};
if(startWeekMode){
timeranges.cron_start = `0 ${getUTCHour(form.startHour.value).toString()} * * ${startWeekMode}`;
timeranges.cron_start = `0 ${form.startHour.value} * * ${startWeekMode}`;
}
if(endWeekMode){
timeranges.cron_stop = `0 ${getUTCHour(form.endHour.value).toString()} * * *`;
timeranges.cron_stop = `0 ${form.endHour.value} * * *`;
}

return timeranges;
}

const onSubmitAsync = fetch => getUTCHour => async (setState, state) => {
const onSubmitAsync = fetch => async (setState, state) => {
if (state.submit.disabled) {
return null;
}
const form = state.form;
const timeranges = extractTimeRanges(getUTCHour)(form);
const timeranges = extractTimeRanges(form);
setState({...state, status: resilienceStatus.POST});
const response = await postAsync(fetch)(urls.timeranges, timeranges);
const error = response.status >= 300;
Expand All @@ -184,8 +184,8 @@ const onSubmitAsync = fetch => getUTCHour => async (setState, state) => {
status: resilienceStatus.ERROR,
});
return null
}
}

const newState = {
...state,
status: resilienceStatus.SUCCESS,
Expand Down Expand Up @@ -216,14 +216,14 @@ function validateField(e, form, updatedRules, fieldName, disabledValue, fieldNam
if (e.name === fieldName || e.name === fieldName1) {
if (e.value === disabledValue) {
form[fieldName1] = {...form[fieldName1], message: ""}

if(form[fieldName2].message){
form = genericHandleChange(updatedRules, form, {name: fieldName2, ...form[fieldName2]});
if(form[fieldName2].message) {
form[fieldName2].forceDisplayMessage = true;
}
}

} else {
form = genericHandleChange(updatedRules, form, {name: fieldName1, ...form[fieldName1]});
form = genericHandleChange(updatedRules, form, {name: fieldName2, ...form[fieldName2]});
Expand All @@ -245,16 +245,16 @@ const doChange = (state, e, setState) => {
endHour: [...rules.endHour, customEndHour(state.form)]
};
let form = genericHandleChange(newRules, state.form, e);

const isKo = Object.values(form).find(field => field.message);
const submit = {disabled: isKo};
setState({...state, form, submit, status: resilienceStatus.EMPTY});
};

const FormConfigurationContainer = ({fetch, getLocalHour, getUTCHour, setConfigurationState}) => {
const FormConfigurationContainer = ({fetch, setConfigurationState}) => {
const [state, setState] = useState(initialState);
useEffect(() => {
getTimeRangesAsync(fetch)(getLocalHour)(setState, state).then(newState => {
getTimeRangesAsync(fetch)(setState, state).then(newState => {
if(newState) {
const form = newState.form;
setConfigurationState({
Expand All @@ -268,7 +268,7 @@ const FormConfigurationContainer = ({fetch, getLocalHour, getUTCHour, setConfigu
}, []);
const onChange= (e) => doChange(state, e, setState);
const onSubmit= () => {
onSubmitAsync(fetch)(getUTCHour)(setState, state).then(newState => {
onSubmitAsync(fetch)(setState, state).then(newState => {
if(newState) {
const form = newState.form;
setConfigurationState({
Expand Down
7 changes: 2 additions & 5 deletions api/web/src/FormConfigurationContainer.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ const fetch = (status =200, getCallback, postCallback) => async (url, config) =>
};
};

const getUTCHour = (hour) => hour;
const getLocalHour = (hour) => hour;

describe(`FormConfigurationContainer`, () => {

it(`submit should save data with success`, async () => {
const postCallback = vi.fn().mockImplementation(x => x);
const getCallback = vi.fn().mockImplementation(x => { return {"cron_start":"0 7 * * 1-5","cron_stop":"0 17 * * *"}});
const setConfigurationState = () => console.log("setConfigurationState");
const utils = render(<FormConfigurationContainer fetch={fetch(200, getCallback, postCallback)} getUTCHour={getUTCHour} getLocalHour={getLocalHour} setConfigurationState={setConfigurationState}/>);
const utils = render(<FormConfigurationContainer fetch={fetch(200, getCallback, postCallback)} setConfigurationState={setConfigurationState}/>);

await waitFor(() => expect(getCallback).toHaveBeenCalledTimes(1));

Expand Down Expand Up @@ -81,7 +78,7 @@ describe(`FormConfigurationContainer`, () => {
const postCallback = vi.fn().mockImplementation(x => x);
const getCallback = vi.fn().mockImplementation(x => { return {"cron_start":"0 7 * * 1-5","cron_stop":"0 17 * * *"}});
const setConfigurationState = () => console.log("setConfigurationState");
const utils = render(<FormConfigurationContainer fetch={fetch(200, getCallback, postCallback)} getUTCHour={getUTCHour} getLocalHour={getLocalHour} setConfigurationState={setConfigurationState}/>);
const utils = render(<FormConfigurationContainer fetch={fetch(200, getCallback, postCallback)} setConfigurationState={setConfigurationState}/>);

await waitFor(() => expect(getCallback).toHaveBeenCalledTimes(1));

Expand Down
5 changes: 0 additions & 5 deletions api/web/src/FormConfigurationContainer.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export default {
component: Header,
};

const getUTCHour = (hour) => hour;
const getLocalHour = (hour) => hour;

const fetch = (status =200) => async (url, config) => {
if(config.method === "POST") {
return {
Expand All @@ -42,8 +39,6 @@ const Template = (args) => <FormConfigurationContainer {...args} />;

export const FormConfiguration = Template.bind({});
FormConfiguration.args = {
getUTCHour,
getLocalHour,
fetch: fetch(200),
setConfigurationState
};
5 changes: 0 additions & 5 deletions api/web/src/FormStateContainer.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ export default {
component: Header,
};

const getUTCHour = (hour) => hour;
const getLocalHour = (hour) => hour;

const fetch = (status =200) => async (url, config) => {
if(config.method === "POST") {
return {
Expand All @@ -43,8 +40,6 @@ const Template = (args) => <FormStateContainer {...args} />;

export const FormState = Template.bind({});
FormState.args = {
getUTCHour,
getLocalHour,
fetch: fetch(200),
apiState:{data:mock}
};
14 changes: 0 additions & 14 deletions api/web/src/date.js

This file was deleted.

5 changes: 2 additions & 3 deletions api/web/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import React , { useState } from "react";
import ReactDOM from 'react-dom';
import App from './App';

import './scss/grid.css';
import './scss/grid.css';
import './scss/reboot.css';
import '@axa-fr/react-toolkit-core/dist/assets/fonts/icons/af-icons.css';
import '@axa-fr/react-toolkit-core/src/common/scss/core.scss';
import FormStateContainer from "./FormStateContainer";
import ListStateContainer from "./ListStateContainer";
import FormConfigurationContainer from "./FormConfigurationContainer";

import {getLocalHour, getUTCHour} from "./date";
import './index.scss';
import ApiStateProvider, { withApiState } from './ApiStateProvider';
import { withApiConfiguration, ApiConfigurationContext, apiConfigurationInit } from './ApiConfigurationProvider';
Expand Down Expand Up @@ -42,7 +41,7 @@ const ConfigurationStateProvider = ({apiState}) => {
<div className="container">
<div className="row">
<div className="col col-sm-12">
<FormConfigurationContainer fetch={fetch} getLocalHour={getLocalHour} getUTCHour={getUTCHour} setConfigurationState={setConfigurationState} apiState={apiState} />
<FormConfigurationContainer fetch={fetch} setConfigurationState={setConfigurationState} apiState={apiState} />
</div>
</div>
</div>
Expand Down

0 comments on commit 3609821

Please sign in to comment.