Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scenario locks #854

Merged
merged 7 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/components/scenarios/item/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface ItemProps {
lastUpdate: string;
jobs?: Record<string, any>[];
runStatus: 'created' | 'running' | 'done' | 'failure',
lock?: Record<string, any>;
lastUpdateDistance: string;
className?: string;
onEdit: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
Expand Down
67 changes: 67 additions & 0 deletions app/hoc/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,73 @@ import PROJECTS from 'services/projects';

import { mergeDehydratedState } from './utils';

const fetchProject = (session, queryClient, { pid }) => {
return queryClient.prefetchQuery(['projects', pid], () => PROJECTS.request({
method: 'GET',
url: `/${pid}`,
headers: {
Authorization: `Bearer ${session.accessToken}`,
},
}).then((response) => {
return response.data;
}));
};

export function withProject(getServerSidePropsFunc?: Function) {
return async (context: any) => {
const session = await getSession(context);

if (!session) {
if (getServerSidePropsFunc) {
const SSPF = await getServerSidePropsFunc(context) || {};

return {
props: {
...SSPF.props,
},
};
}

return {
props: {},
};
}

const { params } = context;

const { pid } = params;

const queryClient = new QueryClient();

await fetchProject(session, queryClient, { pid });

if (getServerSidePropsFunc) {
const SSPF = await getServerSidePropsFunc(context) || {};

const { dehydratedState: prevDehydratedState } = SSPF.props;
const currentDehydratedState = JSON.parse(JSON.stringify(dehydrate(queryClient)));

const newDehydratedState = mergeDehydratedState(prevDehydratedState, currentDehydratedState);

return {
...SSPF,
props: {
session,
...SSPF.props,
dehydratedState: newDehydratedState,
},
};
}

return {
props: {
session,
dehydratedState: JSON.parse(JSON.stringify(dehydrate(queryClient))),
},
};
};
}

export function withPublishedProject(getServerSidePropsFunc?: Function) {
return async (context: any) => {
const session = await getSession(context);
Expand Down
103 changes: 95 additions & 8 deletions app/hoc/scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,31 @@ import SCENARIOS from 'services/scenarios';

import { mergeDehydratedState } from './utils';

const fetchScenario = (session, queryClient, { sid }) => {
return queryClient.prefetchQuery(['scenarios', sid], () => SCENARIOS.request({
method: 'GET',
url: `/${sid}`,
headers: {
Authorization: `Bearer ${session.accessToken}`,
},
}).then((response) => {
return response.data;
}));
};

const fetchScenarioLock = (session, queryClient, { sid }) => {
return queryClient.prefetchQuery(['scenario-lock', sid], () => SCENARIOS.request({
method: 'GET',
url: `/${sid}/editing-locks`,
headers: {
Authorization: `Bearer ${session.accessToken}`,
},
transformResponse: (data) => JSON.parse(data),
}).then((response) => {
return response.data;
}));
};

export function withScenario(getServerSidePropsFunc?: Function) {
return async (context: any) => {
const session = await getSession(context);
Expand All @@ -33,15 +58,77 @@ export function withScenario(getServerSidePropsFunc?: Function) {

const queryClient = new QueryClient();

await queryClient.prefetchQuery(['scenarios', sid], () => SCENARIOS.request({
method: 'GET',
url: `/${sid}`,
headers: {
Authorization: `Bearer ${session.accessToken}`,
await fetchScenario(session, queryClient, { sid });

if (getServerSidePropsFunc) {
const SSPF = await getServerSidePropsFunc(context) || {};

const { dehydratedState: prevDehydratedState } = SSPF.props;
const currentDehydratedState = JSON.parse(JSON.stringify(dehydrate(queryClient)));

const newDehydratedState = mergeDehydratedState(prevDehydratedState, currentDehydratedState);

return {
...SSPF,
props: {
session,
...SSPF.props,
dehydratedState: newDehydratedState,
},
};
}

return {
props: {
session,
dehydratedState: JSON.parse(JSON.stringify(dehydrate(queryClient))),
},
}).then((response) => {
return response.data;
}));
};
};
}

export function withScenarioLock(getServerSidePropsFunc?: Function) {
return async (context: any) => {
const session = await getSession(context);

if (!session) {
if (getServerSidePropsFunc) {
const SSPF = await getServerSidePropsFunc(context) || {};

return {
props: {
...SSPF.props,
},
};
}

return {
props: {},
};
}

const { params } = context;

const { sid } = params;

const queryClient = new QueryClient();

await fetchScenarioLock(session, queryClient, { sid });

const { data: scenarioLockData } = queryClient.getQueryData<any>(['scenario-lock', sid]);

if (!scenarioLockData) {
await SCENARIOS.request({
method: 'POST',
url: `/${sid}/lock`,
headers: {
Authorization: `Bearer ${session.accessToken}`,
},
transformResponse: (data) => JSON.parse(data),
});

await fetchScenarioLock(session, queryClient, { sid });
}

if (getServerSidePropsFunc) {
const SSPF = await getServerSidePropsFunc(context) || {};
Expand Down
Loading