Skip to content

Commit

Permalink
Create boilerplate for versions
Browse files Browse the repository at this point in the history
- Later on improve the logic, but now start with the boilerplate
  • Loading branch information
rottabonus committed Sep 29, 2024
1 parent 41e0515 commit 5740430
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/Screens/VersionCheck/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import RN from 'react-native';
import * as RD from '@devexperts/remote-data-ts';

import { StackScreenProps } from '@react-navigation/stack';
import { StackRoutes } from '..';

import * as redux from 'redux';
import { useDispatch, useSelector } from 'react-redux';
import { selectClientVersions } from 'src/state/reducers/minimumVersion';

import * as actions from '../../state/actions';

import colors from '../components/colors';

export type VersionCheckRoute = {
VersionCheck: {};
};

type Props = StackScreenProps<StackRoutes, 'VersionCheck'>;

const VersionCheck = ({ navigation }: Props) => {
const dispatch = useDispatch<redux.Dispatch<actions.Action>>();
React.useEffect(() => {
dispatch({ type: 'minimumVersion/get/start', payload: undefined });
}, []);

const versionState = useSelector(selectClientVersions);

React.useEffect(() => {
if (RD.isSuccess(versionState)) {
navigation.replace('Splash', {});
}
}, [versionState]);

return <RN.View style={styles.background} />;
};

const styles = RN.StyleSheet.create({
background: {
backgroundColor: colors.purplePale,
flex: 1,
},
});

export default VersionCheck;
4 changes: 3 additions & 1 deletion src/Screens/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import DeleteAccount, {
import PasswordChange, { PasswordChangeRoute } from './Main/Settings/Password';
import EmailChange, { EmailChangeRoute } from './Main/Settings/Email/Email';
import UserReport, { UserReportRoute } from './Main/UserReport';
import { VersionCheckRoute } from './VersionCheck';

export type StackRoutes = SplashRoute &
VersionCheckRoute &
WelcomeRoute &
MentorListRoute &
SignRoute &
Expand All @@ -54,7 +56,7 @@ const Stack = reactNavigationStack.createStackNavigator<StackRoutes>();
export default () => (
<NavigationContainer>
<Stack.Navigator
initialRouteName="Splash"
initialRouteName="VersionCheck"
screenOptions={{ headerShown: false }}
>
<Stack.Screen name="Splash" component={Splash} />
Expand Down
24 changes: 24 additions & 0 deletions src/api/minimumVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as t from 'io-ts';
import * as TE from 'fp-ts/lib/TaskEither';

import * as http from '../lib/http';

import * as config from './config';

const clientVersions = t.strict({
ylitse_ios: t.string,
ylitse_android: t.string,
});

const versionsResponse = t.strict({
resources: clientVersions,
});

export type ClientVersions = t.TypeOf<typeof clientVersions>;

export const fetchVersions: () => TE.TaskEither<string, ClientVersions> = () =>
http.validateResponse(
http.get(`${config.baseUrl}/version/clients`),
versionsResponse,
response => response.resources,
);
4 changes: 4 additions & 0 deletions src/state/actions/regular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as messageApi from '../../api/messages';
import * as statApi from '../../api/stat';
import * as userReportApi from '../../api/userReport';
import * as feedbackApi from '../../api/feedback';
import * as minimumVersionApi from '../../api/minimumVersion';
import * as messages from '../reducers/messages';
import * as updateMentorData from '../reducers/updateMentorData';

Expand Down Expand Up @@ -126,6 +127,9 @@ type RegularActions = {
'feedback/sendAnswer/start': feedbackApi.Answer;
'feedback/sendAnswer/end': Result<ReturnType<typeof feedbackApi.sendAnswer>>;
'feedback/reset/': undefined;

'minimumVersion/get/start': undefined;
'minimumVersion/get/end': Result<typeof minimumVersionApi.fetchVersions>;
};

// TODO name plz.
Expand Down
3 changes: 3 additions & 0 deletions src/state/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as userReport from './userReport';
import * as updateMentorData from './updateMentorData';
import * as questions from './questions';
import * as filterMentors from './filterMentors';
import * as minimumVersion from './minimumVersion';

import * as actions from '../actions';

Expand Down Expand Up @@ -72,6 +73,7 @@ export const rootReducer: automaton.Reducer<AppState, actions.Action> =
markMessageSeen: markSeen.reducer,
feedbackQuestions: questions.reducer,
filterMentors: filterMentors.filterMentorsReducer,
minimumVersion: minimumVersion.reducer,
}),
);

Expand All @@ -96,4 +98,5 @@ export const initialState: AppState = {
markMessageSeen: {},
feedbackQuestions: questions.initialState,
filterMentors: filterMentors.initialState,
minimumVersion: minimumVersion.initialState,
};
49 changes: 49 additions & 0 deletions src/state/reducers/minimumVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Reducer } from 'redux-automaton';
import * as RD from '@devexperts/remote-data-ts';
import * as automaton from 'redux-automaton';
import * as actions from '../actions';
import * as T from 'fp-ts/lib/Task';

import * as minimumVersionApi from '../../api/minimumVersion';
import { cmd } from '../middleware';

import { Action } from '../actions';
import { AppState } from '../types';

import { pipe } from 'fp-ts/lib/function';

export const initialState = RD.initial;

export const reducer: Reducer<AppState['minimumVersion'], Action> = (
state = initialState,
action,
) => {
switch (action.type) {
case 'minimumVersion/get/start': {
if (RD.isPending(state)) {
return state;
}

return automaton.loop(
RD.pending,
cmd(
pipe(
minimumVersionApi.fetchVersions(),
T.map(actions.make('minimumVersion/get/end')),
),
),
);
}

case 'minimumVersion/get/end': {
return pipe(action.payload, RD.fromEither);
}

default: {
return state;
}
}
};

export const selectClientVersions = ({ minimumVersion }: AppState) =>
minimumVersion;
2 changes: 2 additions & 0 deletions src/state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as buddyApi from '../api/buddies';
import * as mentorsApi from '../api/mentors';
import * as messageApi from '../api/messages';
import * as feedbackApi from '../api/feedback';
import * as minimumVersionApi from '../api/minimumVersion';
import { PollingParams } from './reducers/messages';
import { UpdateKey } from './reducers/updateMentorData';

Expand Down Expand Up @@ -75,4 +76,5 @@ export type AppState = {
sendDeviceToken: RemoteAction;
};
feedbackQuestions: RemoteData<Array<feedbackApi.Question>>;
minimumVersion: RemoteData<minimumVersionApi.ClientVersions>;
};

0 comments on commit 5740430

Please sign in to comment.