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

Refactor data syncing #101

Merged
merged 1 commit into from
Feb 24, 2024
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
5 changes: 5 additions & 0 deletions .changeset/perfect-mails-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gov4git-desktop-app': patch
---

Refactor data sync
34 changes: 18 additions & 16 deletions src/electron/services/CommunityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,14 @@ ${user.memberPublicBranch}`
}
}

public insertCommunity = async (projectUrl: string): Promise<string[]> => {
public insertCommunity = async (
projectUrl: string,
): Promise<[string, string[]]> => {
const user = await this.userService.getUser()
if (user == null)
return ['Unauthorized. Please log in to join a new community']
return ['', ['Unauthorized. Please log in to join a new community']]
if (projectUrl === '') {
return [`Community URL is a required field.`]
return ['', [`Community URL is a required field.`]]
}

const projectRepoUrl = projectUrl
Expand All @@ -279,13 +281,13 @@ ${user.memberPublicBranch}`
projectRepoUrl,
)
if (projectRepoError != null) {
return [projectRepoError]
return ['', [projectRepoError]]
}

const [communityMainBranch, communityRepoError] =
await this.getDefaultBranchFromUrl(user, communityUrl)
if (communityRepoError != null) {
return [communityRepoError]
return ['', [communityRepoError]]
}

const configPath = resolve(
Expand All @@ -312,25 +314,17 @@ ${user.memberPublicBranch}`
})
.returning()
)[0]!
const communityCount = await this.db
.select({
count: sql<number>`count(*)`,
})
.from(communities)
if (communityCount.length === 0 || communityCount[0]?.count === 1) {
await this.selectCommunity(community.url)
}

const initializationResults = await this.govService.initId()
if (!initializationResults.ok) {
return [initializationResults.error]
return ['', [initializationResults.error]]
}

const syncedCommunity = await this.syncCommunity(user, currentCommunity)

await this.requestToJoin(user, syncedCommunity)

return []
return [syncedCommunity.url, []]
}

public deployCommunity = async ({
Expand Down Expand Up @@ -358,7 +352,15 @@ ${user.memberPublicBranch}`

const projectUrl = `https://github.com/${org}/${repo}`

const errors = await this.insertCommunity(projectUrl)
const [communityUrl, errors] = await this.insertCommunity(projectUrl)
const communityCount = await this.db
.select({
count: sql<number>`count(*)`,
})
.from(communities)
if (communityCount.length === 0 || communityCount[0]?.count === 1) {
await this.selectCommunity(communityUrl)
}

if (errors.length) {
throw new Error(JSON.stringify(errors, undefined, 2))
Expand Down
7 changes: 5 additions & 2 deletions src/electron/services/MotionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class MotionService extends AbstractMotionService {
return null
}

private loadMotions = serialAsync(async () => {
private loadMotions = async () => {
const [user, community] = await Promise.all([
this.userService.getUser(),
this.communityService.getCommunity(),
Expand Down Expand Up @@ -229,7 +229,7 @@ export class MotionService extends AbstractMotionService {
set: motion,
})
}
})
}

public getMotions = async (
options?: MotionSearch,
Expand Down Expand Up @@ -264,6 +264,9 @@ export class MotionService extends AbstractMotionService {
)[0]

if (motionsCounts == null || motionsCounts.count === 0 || skipCache) {
await this.db
.delete(motions)
.where(eq(motions.communityUrl, community.url))
await this.loadMotions()
}

Expand Down
8 changes: 0 additions & 8 deletions src/renderer/src/pages/layout/DataLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { serialAsync } from '~/shared'

import { useDataStore } from '../../store/store.js'

let firstMount = true

export const DataLoader: FC = function DataLoader() {
const getUser = useDataStore((s) => s.userInfo.fetchUser)
const getCommunities = useDataStore((s) => s.communityInfo.fetchCommunities)
Expand All @@ -20,12 +18,6 @@ export const DataLoader: FC = function DataLoader() {
await fetchMotions(motionSearchArgs, false, false, () => shouldUpdate)
}
void run()
if (firstMount) {
setTimeout(async () => {
await fetchMotions(motionSearchArgs, true, true, () => shouldUpdate)
firstMount = false
}, 1000)
}

return () => {
shouldUpdate = false
Expand Down
8 changes: 1 addition & 7 deletions src/renderer/src/pages/polls/motions/Motions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,8 @@ export const Motions: FC<MotionsProps> = function Motions({
community?.name ?? '',
)
const motionsLoading = useDataStore((s) => s.motionInfo.loading)
const userLoading = useDataStore((s) => s.userInfo.loading)
const communityLoading = useDataStore((s) => s.communityInfo.loading)
const navigate = useNavigate()

const loading = useMemo(() => {
return motionsLoading || userLoading || communityLoading
}, [motionsLoading, userLoading, communityLoading])

useEffect(() => {
setInternalCommunityName(community?.name ?? '')
}, [community, setInternalCommunityName])
Expand Down Expand Up @@ -138,7 +132,7 @@ export const Motions: FC<MotionsProps> = function Motions({
)}
</MotionsControls>

<Loader isLoading={loading}>
<Loader isLoading={motionsLoading}>
{(motions == null || motions.length === 0) && (
<StyledCard>
<p>No matching ballots to display at this time.</p>
Expand Down
18 changes: 16 additions & 2 deletions src/renderer/src/store/communityJoinStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@ export const createCommunityJoinStore: StateCreator<
> = (set, get) => ({
joinCommunity: serialAsync(async (projectUrl: string) => {
try {
const insertErrors = await communityService.insertCommunity(projectUrl)
set((s) => {
s.communityInfo.loading = true
s.motionInfo.loading = true
})
const communities = get().communityInfo.communities
const [communityUrl, insertErrors] =
await communityService.insertCommunity(projectUrl)
if (insertErrors.length === 0) {
await get().refreshCache(false)
if (communities.length === 0) {
await get().communityInfo.selectCommunity(communityUrl)
} else {
await get().communityInfo.fetchCommunities(false)
}
}
set((s) => {
s.communityInfo.loading = false
s.motionInfo.loading = false
})
return insertErrors
} catch (ex) {
get().setException(`Failed to join community. ${ex}`)
Expand Down
11 changes: 6 additions & 5 deletions src/renderer/src/store/communityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ export const createCommunityStore: StateCreator<
}),
selectCommunity: serialAsync(async (url: string) => {
await get().tryRun(async () => {
set((s) => {
s.motionInfo.loading = true
s.communityInfo.loading = true
})
await communityService.selectCommunity(url)
const searchArgs = get().motionInfo.searchArgs
await Promise.all([
get().motionInfo.fetchMotions(searchArgs, false, false),
get().communityInfo.fetchCommunities(false),
])
await get().refreshCache(true)
await get().motionInfo.fetchMotions(searchArgs, false, false)
await get().communityInfo.fetchCommunities(false)
}, `Failed to select community.`)
}),
},
Expand Down
4 changes: 3 additions & 1 deletion src/shared/services/CommunityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import type {
} from '../../electron/services/index.js'

export abstract class AbstractCommunityService {
public abstract insertCommunity(projectUrl: string): Promise<string[]>
public abstract insertCommunity(
projectUrl: string,
): Promise<[string, string[]]>
public abstract selectCommunity(communityUrl: string): Promise<void>
public abstract getCommunities(): Promise<Community[]>
public abstract deployCommunity(
Expand Down
Loading