~breatheoutbreathein/u4u

66f746d14cf99db29506e272b01cb1b6fdd94ecf — Joseph Turner 2 years ago a2580ef swarm
more testing to fixup
M src/components/SwarmSubjects.tsx => src/components/SwarmSubjects.tsx +4 -1
@@ 33,6 33,7 @@ export const SwarmSubjects = (): ReactElement => {
  const dispatch = useAppDispatch()

  const { currentSubject, subjects, bySubject } = useAppSelector(({ swarm }) => swarm)
  const { currentIdentity } = useAppSelector(({ authors }) => authors.currentIdentity)

  const [newSubject, setNewSubject] = useState('')



@@ 41,7 42,9 @@ export const SwarmSubjects = (): ReactElement => {
  }

  const handleSubmit = (e: React.FormEvent): void => {
    dispatch(addSwarmSubjects({ subjects: [newSubject] }))
    dispatch(addSwarmSubjects({
      subjectsWithAuthorURLs: [{ subject: newSubject, authorURLs: [currentIdentity] }]
    }))
    e.preventDefault()
  }


M src/middleware/dbMiddleware/dbHelpers.ts => src/middleware/dbMiddleware/dbHelpers.ts +20 -6
@@ 389,18 389,32 @@ export const unpinMessageMiddlewareHelper: MiddlewareHelper = async (action, { d
}

export const addSwarmSubjectsMiddlewareHelper: MiddlewareHelper = async (action, { db }, storeAPI) => {
  const { writableIdentities } = storeAPI.getState().authors
  // TODO: Add this one to check currentIdentity middleware
  const { currentIdentity, writableIdentities } = storeAPI.getState().authors

  const { subjects } = action.payload
  const { subjectsWithAuthorURLs } = action.payload

  // Placeholder logic to be replaced with ushin-db method calls
  const swarmSubjectsToLoad = {
    [subjects]: Object.keys(writableIdentities)
  }
  // const swarmSubjectsToLoad = {
  //   [subjects]: Object.keys(writableIdentities)
  // }
  // End placeholder logic

  // const swarmSubjectsToLoad = await db.addSwarmSubject(currentIdentity, messageURL)
  // TODO: Type interface from ushin-db?
  const swarmSubjectsToLoad = {}

  for (const { subject, authorURLs } of subjectsWithAuthorURLs) {
    const topic = await db.getTopic(subject)

    await topic.addURL(currentIdentity)
    
    for await (const authorURLs of topic) {
      // console.log(authorURL)
      swarmSubjectsToLoad[subject] = authorURLs
      break;
    }
  }
  
  storeAPI.dispatch(addSwarmSubjectsSuccess({ swarmSubjectsToLoad }))
}


M src/slices/swarm.ts => src/slices/swarm.ts +5 -1
@@ 40,7 40,10 @@ const swarmSlice = createSlice({
  initialState: initialSwarmState,
  reducers: {
    addSwarmSubjects: (state, action: PayloadAction<{
      subjects: string[]
      subjectsWithAuthorURLs: Array<{
        subject: string
        authorURLs: string[]
      }>
    }>) => state,

    // TODO: get swarmSubjectsToLoad interface from ushin-db


@@ 50,6 53,7 @@ const swarmSlice = createSlice({
      }
    }>) => {
      const { swarmSubjectsToLoad } = action.payload
      console.log(swarmSubjectsToLoad)

      Object.assign(state.bySubject, swarmSubjectsToLoad)
      state.subjects = [...Object.keys(swarmSubjectsToLoad), ...state.subjects]